Re: [PATCH v3 01/15] hw/block/nvme: Define 64 bit cqe.result

2020-09-15 Thread Klaus Jensen
On Sep 15 20:44, Dmitry Fomichev wrote:
> > -Original Message-
> > From: Klaus Jensen 
> > Sent: Tuesday, September 15, 2020 3:56 PM
> > To: Dmitry Fomichev 
> > Cc: Fam Zheng ; Kevin Wolf ;
> > Damien Le Moal ; qemu-block@nongnu.org;
> > Niklas Cassel ; Klaus Jensen
> > ; qemu-de...@nongnu.org; Alistair Francis
> > ; Keith Busch ; Philippe
> > Mathieu-Daudé ; Matias Bjorling
> > 
> > Subject: Re: [PATCH v3 01/15] hw/block/nvme: Define 64 bit cqe.result
> > 
> > On Sep 15 18:56, Dmitry Fomichev wrote:
> > > > -Original Message-
> > > > From: Klaus Jensen 
> > > > Sent: Tuesday, September 15, 2020 3:37 AM
> > > > To: Dmitry Fomichev 
> > > > Cc: Keith Busch ; Klaus Jensen
> > > > ; Kevin Wolf ; Philippe
> > > > Mathieu-Daudé ; Maxim Levitsky
> > > > ; Fam Zheng ; Niklas Cassel
> > > > ; Damien Le Moal
> > ;
> > > > qemu-block@nongnu.org; qemu-de...@nongnu.org; Alistair Francis
> > > > ; Matias Bjorling 
> > > > Subject: Re: [PATCH v3 01/15] hw/block/nvme: Define 64 bit cqe.result
> > > >
> > > > On Sep 14 07:14, Dmitry Fomichev wrote:
> > > > > From: Ajay Joshi 
> > > > >
> > > > > A new write command, Zone Append, is added as a part of Zoned
> > > > > Namespace Command Set. Upon successful completion of this
> > command,
> > > > > the controller returns the start LBA of the performed write operation
> > > > > in cqe.result field. Therefore, the maximum size of this variable
> > > > > needs to be changed from 32 to 64 bit, consuming the reserved 32 bit
> > > > > field that follows the result in CQE struct. Since the existing
> > > > > commands are expected to return a 32 bit LE value, two separate
> > > > > variables, result32 and result64, are now kept in a union.
> > > > >
> > > > > Signed-off-by: Ajay Joshi 
> > > > > Signed-off-by: Dmitry Fomichev 
> > > > > Reviewed-by: Klaus Jensen 
> > > >
> > > > I know that I R-b'ed this, but can this be moved to the namespace types
> > > > patch, since that is the TP that changes this.
> > >
> > > You probably meant the ZNS patch since result64 is first used there to
> > return
> > > ZA starting data LBA. Sure, I can move this stuff to that patch.
> > >
> > 
> > No, I actually did mean the NST patch since TP 4056 is the TP that
> > "unreserves" dw1 in the CQE.
> 
> It is not necessary to change it in NST patch since result64 field is not used
> in that patch. But if you insist, I can move it to NST patch :)

You are right of course, but since it is a change to the "spec" related
data structures that go into include/block/nvme.h, I think it belongs in
"hw/block/nvme: Introduce the Namespace Types definitions".


signature.asc
Description: PGP signature


Re: [PATCH v3 14/15] hw/block/nvme: Use zone metadata file for persistence

2020-09-15 Thread Klaus Jensen
On Sep 15 20:44, Dmitry Fomichev wrote:
> > -Original Message-
> > From: Klaus Jensen 
> > Sent: Tuesday, September 15, 2020 3:10 PM
> > To: Dmitry Fomichev 
> > Cc: Keith Busch ; Klaus Jensen
> > ; Kevin Wolf ; Philippe
> > Mathieu-Daudé ; Maxim Levitsky
> > ; Fam Zheng ; Niklas Cassel
> > ; Damien Le Moal ;
> > qemu-block@nongnu.org; qemu-de...@nongnu.org; Alistair Francis
> > ; Matias Bjorling 
> > Subject: Re: [PATCH v3 14/15] hw/block/nvme: Use zone metadata file for
> > persistence
> > 
> > On Sep 14 07:14, Dmitry Fomichev wrote:
> > > A ZNS drive that is emulated by this module is currently initialized
> > > with all zones Empty upon startup. However, actual ZNS SSDs save the
> > > state and condition of all zones in their internal NVRAM in the event
> > > of power loss. When such a drive is powered up again, it closes or
> > > finishes all zones that were open at the moment of shutdown. Besides
> > > that, the write pointer position as well as the state and condition
> > > of all zones is preserved across power-downs.
> > >
> > > This commit adds the capability to have a persistent zone metadata
> > > to the device. The new optional module property, "zone_file",
> > > is introduced. If added to the command line, this property specifies
> > > the name of the file that stores the zone metadata. If "zone_file" is
> > > omitted, the device will be initialized with all zones empty, the same
> > > as before.
> > >
> > > If zone metadata is configured to be persistent, then zone descriptor
> > > extensions also persist across controller shutdowns.
> > >
> > > Signed-off-by: Dmitry Fomichev 
> > 
> > This doesn't build on mingw.
> 
> Thanks for letting me know. I'll try to look into this. Do you cross-compile
> with mingw64?

Yup,

make docker-test-mingw@fedora



signature.asc
Description: PGP signature


RE: [PATCH v3 01/15] hw/block/nvme: Define 64 bit cqe.result

2020-09-15 Thread Dmitry Fomichev
> -Original Message-
> From: Klaus Jensen 
> Sent: Tuesday, September 15, 2020 3:56 PM
> To: Dmitry Fomichev 
> Cc: Fam Zheng ; Kevin Wolf ;
> Damien Le Moal ; qemu-block@nongnu.org;
> Niklas Cassel ; Klaus Jensen
> ; qemu-de...@nongnu.org; Alistair Francis
> ; Keith Busch ; Philippe
> Mathieu-Daudé ; Matias Bjorling
> 
> Subject: Re: [PATCH v3 01/15] hw/block/nvme: Define 64 bit cqe.result
> 
> On Sep 15 18:56, Dmitry Fomichev wrote:
> > > -Original Message-
> > > From: Klaus Jensen 
> > > Sent: Tuesday, September 15, 2020 3:37 AM
> > > To: Dmitry Fomichev 
> > > Cc: Keith Busch ; Klaus Jensen
> > > ; Kevin Wolf ; Philippe
> > > Mathieu-Daudé ; Maxim Levitsky
> > > ; Fam Zheng ; Niklas Cassel
> > > ; Damien Le Moal
> ;
> > > qemu-block@nongnu.org; qemu-de...@nongnu.org; Alistair Francis
> > > ; Matias Bjorling 
> > > Subject: Re: [PATCH v3 01/15] hw/block/nvme: Define 64 bit cqe.result
> > >
> > > On Sep 14 07:14, Dmitry Fomichev wrote:
> > > > From: Ajay Joshi 
> > > >
> > > > A new write command, Zone Append, is added as a part of Zoned
> > > > Namespace Command Set. Upon successful completion of this
> command,
> > > > the controller returns the start LBA of the performed write operation
> > > > in cqe.result field. Therefore, the maximum size of this variable
> > > > needs to be changed from 32 to 64 bit, consuming the reserved 32 bit
> > > > field that follows the result in CQE struct. Since the existing
> > > > commands are expected to return a 32 bit LE value, two separate
> > > > variables, result32 and result64, are now kept in a union.
> > > >
> > > > Signed-off-by: Ajay Joshi 
> > > > Signed-off-by: Dmitry Fomichev 
> > > > Reviewed-by: Klaus Jensen 
> > >
> > > I know that I R-b'ed this, but can this be moved to the namespace types
> > > patch, since that is the TP that changes this.
> >
> > You probably meant the ZNS patch since result64 is first used there to
> return
> > ZA starting data LBA. Sure, I can move this stuff to that patch.
> >
> 
> No, I actually did mean the NST patch since TP 4056 is the TP that
> "unreserves" dw1 in the CQE.

It is not necessary to change it in NST patch since result64 field is not used
in that patch. But if you insist, I can move it to NST patch :)


RE: [PATCH v3 14/15] hw/block/nvme: Use zone metadata file for persistence

2020-09-15 Thread Dmitry Fomichev
> -Original Message-
> From: Klaus Jensen 
> Sent: Tuesday, September 15, 2020 3:10 PM
> To: Dmitry Fomichev 
> Cc: Keith Busch ; Klaus Jensen
> ; Kevin Wolf ; Philippe
> Mathieu-Daudé ; Maxim Levitsky
> ; Fam Zheng ; Niklas Cassel
> ; Damien Le Moal ;
> qemu-block@nongnu.org; qemu-de...@nongnu.org; Alistair Francis
> ; Matias Bjorling 
> Subject: Re: [PATCH v3 14/15] hw/block/nvme: Use zone metadata file for
> persistence
> 
> On Sep 14 07:14, Dmitry Fomichev wrote:
> > A ZNS drive that is emulated by this module is currently initialized
> > with all zones Empty upon startup. However, actual ZNS SSDs save the
> > state and condition of all zones in their internal NVRAM in the event
> > of power loss. When such a drive is powered up again, it closes or
> > finishes all zones that were open at the moment of shutdown. Besides
> > that, the write pointer position as well as the state and condition
> > of all zones is preserved across power-downs.
> >
> > This commit adds the capability to have a persistent zone metadata
> > to the device. The new optional module property, "zone_file",
> > is introduced. If added to the command line, this property specifies
> > the name of the file that stores the zone metadata. If "zone_file" is
> > omitted, the device will be initialized with all zones empty, the same
> > as before.
> >
> > If zone metadata is configured to be persistent, then zone descriptor
> > extensions also persist across controller shutdowns.
> >
> > Signed-off-by: Dmitry Fomichev 
> 
> This doesn't build on mingw.

Thanks for letting me know. I'll try to look into this. Do you cross-compile
with mingw64?

> 
> > ---
> >  hw/block/nvme.c | 370
> +---
> >  hw/block/nvme.h |  37 +
> >  2 files changed, 386 insertions(+), 21 deletions(-)
> >
> > diff --git a/hw/block/nvme.c b/hw/block/nvme.c
> > index b49ae83dd5..41f4c0dacd 100644
> > --- a/hw/block/nvme.c
> > +++ b/hw/block/nvme.c
> > @@ -3429,7 +3557,188 @@ static int nvme_init_zone_meta(NvmeCtrl *n,
> NvmeNamespace *ns,
> >  return 0;
> >  }
> >
> > -static void nvme_zoned_init_ctrl(NvmeCtrl *n, Error **errp)
> > +static int nvme_open_zone_file(NvmeCtrl *n, bool *init_meta)
> > +{
> > +struct stat statbuf;
> > +size_t fsize;
> > +int ret;
> > +
> > +ret = stat(n->params.zone_file, );
> > +if (ret && errno == ENOENT) {
> > +*init_meta = true;
> > +} else if (!S_ISREG(statbuf.st_mode)) {
> > +fprintf(stderr, "%s is not a regular file\n", strerror(errno));
> > +return -1;
> > +}
> > +
> > +n->zone_file_fd = open(n->params.zone_file,
> > +   O_RDWR | O_LARGEFILE | O_BINARY | O_CREAT, 644);
> 
> mode is wrong - I think you meant for it to be octal.

Nice catch, needs to be 0644...


Re: [PATCH v8 4/7] scripts: add block-coroutine-wrapper.py

2020-09-15 Thread Vladimir Sementsov-Ogievskiy

15.09.2020 19:44, Vladimir Sementsov-Ogievskiy wrote:

We have a very frequent pattern of creating coroutine from function
with several arguments:

   - create structure to pack parameters
   - create _entry function to call original function taking parameters
 from struct
   - do different magic to handle completion: set ret to NOT_DONE or
 EINPROGRESS or use separate bool field
   - fill the struct and create coroutine from _entry function and this
 struct as a parameter
   - do coroutine enter and BDRV_POLL_WHILE loop

Let's reduce code duplication by generating coroutine wrappers.

This patch adds scripts/block-coroutine-wrapper.py together with some
friends, which will generate functions with declared prototypes marked
by 'generated_co_wrapper' specifier.

The usage of new code generation is as follows:

 1. define somewhere

 int coroutine_fn bdrv_co_NAME(...) {...}

function

 2. declare in some header file

 int generated_co_wrapper bdrv_NAME(...);

function with same list of parameters. (you'll need to include
"block/generated-co-wrapper.h" to get the specifier)

 3. both declarations should be available through block/coroutines.h
header.

 4. add header with generated_co_wrapper declaration into
COROUTINE_HEADERS list in Makefile

Still, no function is now marked, this work is for the following
commit.

Signed-off-by: Vladimir Sementsov-Ogievskiy
---
  docs/devel/block-coroutine-wrapper.rst |  54 +++
  block/block-gen.h  |  49 +++
  include/block/block.h  |  10 ++
  block/meson.build  |   8 ++
  scripts/block-coroutine-wrapper.py | 187 +
  5 files changed, 308 insertions(+)
  create mode 100644 docs/devel/block-coroutine-wrapper.rst
  create mode 100644 block/block-gen.h
  create mode 100755 scripts/block-coroutine-wrapper.py



Also needed:

diff --git a/docs/devel/index.rst b/docs/devel/index.rst
index 04773ce076..cb0abe1e69 100644
--- a/docs/devel/index.rst
+++ b/docs/devel/index.rst
@@ -31,3 +31,4 @@ Contents:
reset
s390-dasd-ipl
clocks
+   block-coroutine-wrapper

--
Best regards,
Vladimir



Re: [PATCH v3 01/15] hw/block/nvme: Define 64 bit cqe.result

2020-09-15 Thread Klaus Jensen
On Sep 15 18:56, Dmitry Fomichev wrote:
> > -Original Message-
> > From: Klaus Jensen 
> > Sent: Tuesday, September 15, 2020 3:37 AM
> > To: Dmitry Fomichev 
> > Cc: Keith Busch ; Klaus Jensen
> > ; Kevin Wolf ; Philippe
> > Mathieu-Daudé ; Maxim Levitsky
> > ; Fam Zheng ; Niklas Cassel
> > ; Damien Le Moal ;
> > qemu-block@nongnu.org; qemu-de...@nongnu.org; Alistair Francis
> > ; Matias Bjorling 
> > Subject: Re: [PATCH v3 01/15] hw/block/nvme: Define 64 bit cqe.result
> > 
> > On Sep 14 07:14, Dmitry Fomichev wrote:
> > > From: Ajay Joshi 
> > >
> > > A new write command, Zone Append, is added as a part of Zoned
> > > Namespace Command Set. Upon successful completion of this command,
> > > the controller returns the start LBA of the performed write operation
> > > in cqe.result field. Therefore, the maximum size of this variable
> > > needs to be changed from 32 to 64 bit, consuming the reserved 32 bit
> > > field that follows the result in CQE struct. Since the existing
> > > commands are expected to return a 32 bit LE value, two separate
> > > variables, result32 and result64, are now kept in a union.
> > >
> > > Signed-off-by: Ajay Joshi 
> > > Signed-off-by: Dmitry Fomichev 
> > > Reviewed-by: Klaus Jensen 
> > 
> > I know that I R-b'ed this, but can this be moved to the namespace types
> > patch, since that is the TP that changes this.
> 
> You probably meant the ZNS patch since result64 is first used there to return
> ZA starting data LBA. Sure, I can move this stuff to that patch.
> 

No, I actually did mean the NST patch since TP 4056 is the TP that
"unreserves" dw1 in the CQE.


signature.asc
Description: PGP signature


Re: [PATCH v3 14/15] hw/block/nvme: Use zone metadata file for persistence

2020-09-15 Thread Klaus Jensen
On Sep 14 07:14, Dmitry Fomichev wrote:
> A ZNS drive that is emulated by this module is currently initialized
> with all zones Empty upon startup. However, actual ZNS SSDs save the
> state and condition of all zones in their internal NVRAM in the event
> of power loss. When such a drive is powered up again, it closes or
> finishes all zones that were open at the moment of shutdown. Besides
> that, the write pointer position as well as the state and condition
> of all zones is preserved across power-downs.
> 
> This commit adds the capability to have a persistent zone metadata
> to the device. The new optional module property, "zone_file",
> is introduced. If added to the command line, this property specifies
> the name of the file that stores the zone metadata. If "zone_file" is
> omitted, the device will be initialized with all zones empty, the same
> as before.
> 
> If zone metadata is configured to be persistent, then zone descriptor
> extensions also persist across controller shutdowns.
> 
> Signed-off-by: Dmitry Fomichev 

This doesn't build on mingw.

> ---
>  hw/block/nvme.c | 370 +---
>  hw/block/nvme.h |  37 +
>  2 files changed, 386 insertions(+), 21 deletions(-)
> 
> diff --git a/hw/block/nvme.c b/hw/block/nvme.c
> index b49ae83dd5..41f4c0dacd 100644
> --- a/hw/block/nvme.c
> +++ b/hw/block/nvme.c
> @@ -3429,7 +3557,188 @@ static int nvme_init_zone_meta(NvmeCtrl *n, 
> NvmeNamespace *ns,
>  return 0;
>  }
>  
> -static void nvme_zoned_init_ctrl(NvmeCtrl *n, Error **errp)
> +static int nvme_open_zone_file(NvmeCtrl *n, bool *init_meta)
> +{
> +struct stat statbuf;
> +size_t fsize;
> +int ret;
> +
> +ret = stat(n->params.zone_file, );
> +if (ret && errno == ENOENT) {
> +*init_meta = true;
> +} else if (!S_ISREG(statbuf.st_mode)) {
> +fprintf(stderr, "%s is not a regular file\n", strerror(errno));
> +return -1;
> +}
> +
> +n->zone_file_fd = open(n->params.zone_file,
> +   O_RDWR | O_LARGEFILE | O_BINARY | O_CREAT, 644);

mode is wrong - I think you meant for it to be octal.


signature.asc
Description: PGP signature


RE: [PATCH v3 02/15] hw/block/nvme: Report actual LBA data shift in LBAF

2020-09-15 Thread Dmitry Fomichev
> -Original Message-
> From: Klaus Jensen 
> Sent: Tuesday, September 15, 2020 3:34 AM
> To: Dmitry Fomichev 
> Cc: Keith Busch ; Klaus Jensen
> ; Kevin Wolf ; Philippe
> Mathieu-Daudé ; Maxim Levitsky
> ; Fam Zheng ; Niklas Cassel
> ; Damien Le Moal ;
> qemu-block@nongnu.org; qemu-de...@nongnu.org; Alistair Francis
> ; Matias Bjorling 
> Subject: Re: [PATCH v3 02/15] hw/block/nvme: Report actual LBA data shift in
> LBAF
> 
> On Sep 14 07:14, Dmitry Fomichev wrote:
> > Calculate the data shift value to report based on the set value of
> > logical_block_size device property.
> >
> > In the process, use a local variable to calculate the LBA format
> > index instead of the hardcoded value 0. This makes the code more
> > readable and it will make it easier to add support for multiple LBA
> > formats in the future.
> >
> > Signed-off-by: Dmitry Fomichev 
> > ---
> >  hw/block/nvme.c |  4 +++-
> >  hw/block/nvme.h | 11 +++
> >  2 files changed, 14 insertions(+), 1 deletion(-)
> >
> > diff --git a/hw/block/nvme.c b/hw/block/nvme.c
> > index 3a90d80694..1cfc136042 100644
> > --- a/hw/block/nvme.c
> > +++ b/hw/block/nvme.c
> > @@ -2203,6 +2203,7 @@ static void nvme_init_namespace(NvmeCtrl *n,
> NvmeNamespace *ns, Error **errp)
> >  {
> >  int64_t bs_size;
> >  NvmeIdNs *id_ns = >id_ns;
> > +int lba_index;
> >
> >  bs_size = blk_getlength(n->conf.blk);
> >  if (bs_size < 0) {
> > @@ -2212,7 +2213,8 @@ static void nvme_init_namespace(NvmeCtrl *n,
> NvmeNamespace *ns, Error **errp)
> >
> >  n->ns_size = bs_size;
> >
> > -id_ns->lbaf[0].ds = BDRV_SECTOR_BITS;
> > +lba_index = NVME_ID_NS_FLBAS_INDEX(ns->id_ns.flbas);
> > +id_ns->lbaf[lba_index].ds = nvme_ilog2(n->conf.logical_block_size);
> 
> Instead of defining a new function, we can directly use clz32().
> 
>   31 - clz32(n->conf.logical_block_size)

Ok nice! I looked up what QEMU uses for binary log, but couldn't find it quickly
so I decided to define a function for that :) Will switch to clzXX in the patch 
set -
I believe there are three occurrences where ilog2 is used in the added code.



RE: [PATCH v3 01/15] hw/block/nvme: Define 64 bit cqe.result

2020-09-15 Thread Dmitry Fomichev
> -Original Message-
> From: Klaus Jensen 
> Sent: Tuesday, September 15, 2020 3:37 AM
> To: Dmitry Fomichev 
> Cc: Keith Busch ; Klaus Jensen
> ; Kevin Wolf ; Philippe
> Mathieu-Daudé ; Maxim Levitsky
> ; Fam Zheng ; Niklas Cassel
> ; Damien Le Moal ;
> qemu-block@nongnu.org; qemu-de...@nongnu.org; Alistair Francis
> ; Matias Bjorling 
> Subject: Re: [PATCH v3 01/15] hw/block/nvme: Define 64 bit cqe.result
> 
> On Sep 14 07:14, Dmitry Fomichev wrote:
> > From: Ajay Joshi 
> >
> > A new write command, Zone Append, is added as a part of Zoned
> > Namespace Command Set. Upon successful completion of this command,
> > the controller returns the start LBA of the performed write operation
> > in cqe.result field. Therefore, the maximum size of this variable
> > needs to be changed from 32 to 64 bit, consuming the reserved 32 bit
> > field that follows the result in CQE struct. Since the existing
> > commands are expected to return a 32 bit LE value, two separate
> > variables, result32 and result64, are now kept in a union.
> >
> > Signed-off-by: Ajay Joshi 
> > Signed-off-by: Dmitry Fomichev 
> > Reviewed-by: Klaus Jensen 
> 
> I know that I R-b'ed this, but can this be moved to the namespace types
> patch, since that is the TP that changes this.

You probably meant the ZNS patch since result64 is first used there to return
ZA starting data LBA. Sure, I can move this stuff to that patch.

> 
> Also, I don't think we should touch the tracing in the block driver
> since it is not aware of namespace types.

Ok


RE: [PATCH v2 00/15] hw/block/nvme: Support Namespace Types and Zoned Namespace Command Set

2020-09-15 Thread Dmitry Fomichev
> -Original Message-
> From: Klaus Jensen 
> Sent: Tuesday, September 15, 2020 3:44 AM
> To: Dmitry Fomichev 
> Cc: Keith Busch ; Klaus Jensen
> ; Kevin Wolf ; Philippe
> Mathieu-Daudé ; Maxim Levitsky
> ; Fam Zheng ; Niklas Cassel
> ; Damien Le Moal ;
> qemu-block@nongnu.org; qemu-de...@nongnu.org; Alistair Francis
> ; Matias Bjorling 
> Subject: Re: [PATCH v2 00/15] hw/block/nvme: Support Namespace Types
> and Zoned Namespace Command Set
> 
> On Sep 13 07:54, Dmitry Fomichev wrote:
> > v1 -> v2:
> >
> >  - Incorporated feedback from Klaus and Alistair.
> 
> Since it's been a while since I reviewed this, it would have been nice
> if you had listed what feedback you incorporated ;)

I'll try to make the list and include it with v4...


Re: [PATCH 00/13] dma: Let the DMA API take MemTxAttrs argument and propagate MemTxResult

2020-09-15 Thread Philippe Mathieu-Daudé
This series is fully review.

Paolo, does it belong to your tree?

On 9/4/20 5:44 PM, Philippe Mathieu-Daudé wrote:
> Salvaging cleanups patches from the RFC series "Forbid DMA write
> accesses to MMIO regions" [*], propagating MemTxResult and
> adding documentation.
> 
> [*] https://www.mail-archive.com/qemu-block@nongnu.org/msg72924.html
> 
> Klaus Jensen (1):
>   pci: pass along the return value of dma_memory_rw
> 
> Philippe Mathieu-Daudé (12):
>   docs/devel/loads-stores: Add regexp for DMA functions
>   dma: Document address_space_map/address_space_unmap() prototypes
>   dma: Let dma_memory_set() propagate MemTxResult
>   dma: Let dma_memory_rw() propagate MemTxResult
>   dma: Let dma_memory_read() propagate MemTxResult
>   dma: Let dma_memory_write() propagate MemTxResult
>   dma: Let dma_memory_valid() take MemTxAttrs argument
>   dma: Let dma_memory_set() take MemTxAttrs argument
>   dma: Let dma_memory_rw_relaxed() take MemTxAttrs argument
>   dma: Let dma_memory_rw() take MemTxAttrs argument
>   dma: Let dma_memory_read/write() take MemTxAttrs argument
>   dma: Let dma_memory_map() take MemTxAttrs argument
> 
>  docs/devel/loads-stores.rst   |   2 +
>  include/hw/pci/pci.h  |   7 +-
>  include/hw/ppc/spapr_vio.h|  11 ++-
>  include/sysemu/dma.h  | 156 +++---
>  dma-helpers.c |  16 ++--
>  hw/arm/musicpal.c |  13 +--
>  hw/arm/smmu-common.c  |   3 +-
>  hw/arm/smmuv3.c   |  14 +--
>  hw/core/generic-loader.c  |   3 +-
>  hw/display/virtio-gpu.c   |   8 +-
>  hw/dma/pl330.c|  12 ++-
>  hw/dma/sparc32_dma.c  |  16 ++--
>  hw/dma/xlnx-zynq-devcfg.c |   6 +-
>  hw/dma/xlnx_dpdma.c   |  10 ++-
>  hw/hyperv/vmbus.c |   8 +-
>  hw/i386/amd_iommu.c   |  16 ++--
>  hw/i386/intel_iommu.c |  28 +++---
>  hw/ide/ahci.c |   9 +-
>  hw/ide/macio.c|   2 +-
>  hw/intc/spapr_xive.c  |   3 +-
>  hw/intc/xive.c|   7 +-
>  hw/misc/bcm2835_property.c|   3 +-
>  hw/misc/macio/mac_dbdma.c |  10 ++-
>  hw/net/allwinner-sun8i-emac.c |  21 +++--
>  hw/net/ftgmac100.c|  25 --
>  hw/net/imx_fec.c  |  32 ---
>  hw/nvram/fw_cfg.c |  12 ++-
>  hw/pci-host/pnv_phb3.c|   5 +-
>  hw/pci-host/pnv_phb3_msi.c|   9 +-
>  hw/pci-host/pnv_phb4.c|   7 +-
>  hw/sd/allwinner-sdhost.c  |  14 +--
>  hw/sd/sdhci.c |  35 +---
>  hw/usb/hcd-dwc2.c |   8 +-
>  hw/usb/hcd-ehci.c |   6 +-
>  hw/usb/hcd-ohci.c |  28 +++---
>  hw/usb/libhw.c|   3 +-
>  hw/virtio/virtio.c|   6 +-
>  37 files changed, 385 insertions(+), 189 deletions(-)
> 




Re: [PATCH v10 15/26] cirrus: Building freebsd in a single short

2020-09-15 Thread Philippe Mathieu-Daudé
Typo "single shot" in subject?

On 9/15/20 7:12 PM, Yonggang Luo wrote:
> This reverts commit 45f7b7b9f38f5c4d1529a37c93dedfc26a231bba
> ("cirrus.yml: Split FreeBSD job into two parts").
> 
> freebsd 1 hour limit not hit anymore
> 
> I think we going to a wrong direction, I think there is some tests a stall 
> the test runner,
> please look at
> https://cirrus-ci.com/task/5110577531977728
> When its running properly, the consumed time are little, but when tests 
> running too long,
> look at the cpu usage, the cpu usage are nearly zero. doesn't consuming time.
> 
> And look at
> https://cirrus-ci.com/task/6119341601062912
> 
> If the tests running properly, the time consuming are little
> We should not hide the error by split them
> 
> Signed-off-by: Yonggang Luo 
> Reviewed-by: Daniel P. Berrangé 
> Reviewed-by: Ed Maste 
> ---
>  .cirrus.yml | 35 ---
>  1 file changed, 8 insertions(+), 27 deletions(-)
> 
> diff --git a/.cirrus.yml b/.cirrus.yml
> index 3dd9fcff7f..25fb4add9b 100644
> --- a/.cirrus.yml
> +++ b/.cirrus.yml
> @@ -1,38 +1,19 @@
>  env:
>CIRRUS_CLONE_DEPTH: 1
>  
> -freebsd_1st_task:
> +freebsd_12_task:
>freebsd_instance:
>  image_family: freebsd-12-1
> -cpu: 4
> -memory: 4G
> -  install_script: ASSUME_ALWAYS_YES=yes pkg bootstrap -f ; pkg install -y
> -bash curl cyrus-sasl git glib gmake gnutls gsed
> -nettle perl5 pixman pkgconf png usbredir
> +cpu: 8
> +memory: 8G
> +  install_script:
> +- ASSUME_ALWAYS_YES=yes pkg bootstrap -f ;
> +- pkg install -y bash curl cyrus-sasl git glib gmake gnutls gsed
> +  nettle perl5 pixman pkgconf png usbredir
>script:
>  - mkdir build
>  - cd build
> -- ../configure --disable-user --target-list-exclude='alpha-softmmu
> -ppc64-softmmu ppc-softmmu riscv32-softmmu riscv64-softmmu 
> s390x-softmmu
> -sparc64-softmmu sparc-softmmu x86_64-softmmu i386-softmmu'
> ---enable-werror || { cat config.log; exit 1; }
> -- gmake -j$(sysctl -n hw.ncpu)
> -- gmake -j$(sysctl -n hw.ncpu) check
> -
> -freebsd_2nd_task:
> -  freebsd_instance:
> -image_family: freebsd-12-1
> -cpu: 4
> -memory: 4G
> -  install_script: ASSUME_ALWAYS_YES=yes pkg bootstrap -f ; pkg install -y
> -bash curl cyrus-sasl git glib gmake gnutls gtk3 gsed libepoxy mesa-libs
> -nettle perl5 pixman pkgconf png SDL2 usbredir
> -  script:
> -- ./configure --enable-werror --target-list='alpha-softmmu ppc64-softmmu
> -ppc-softmmu riscv32-softmmu riscv64-softmmu s390x-softmmu
> -sparc64-softmmu sparc-softmmu x86_64-softmmu i386-softmmu
> -sparc-bsd-user sparc64-bsd-user x86_64-bsd-user i386-bsd-user'
> -|| { cat config.log; exit 1; }
> +- ../configure --enable-werror || { cat config.log; exit 1; }
>  - gmake -j$(sysctl -n hw.ncpu)
>  - gmake -j$(sysctl -n hw.ncpu) check
>  
> 




Re: [PATCH v10 15/26] cirrus: Building freebsd in a single short

2020-09-15 Thread Yonggang Luo
On Wed, Sep 16, 2020 at 1:48 AM Philippe Mathieu-Daudé 
wrote:
>
> Typo "single shot" in subject?

Yeap, single shot, hope the maintainer fix it
>
>
> On 9/15/20 7:12 PM, Yonggang Luo wrote:
> > This reverts commit 45f7b7b9f38f5c4d1529a37c93dedfc26a231bba
> > ("cirrus.yml: Split FreeBSD job into two parts").
> >
> > freebsd 1 hour limit not hit anymore
> >
> > I think we going to a wrong direction, I think there is some tests a
stall the test runner,
> > please look at
> > https://cirrus-ci.com/task/5110577531977728
> > When its running properly, the consumed time are little, but when tests
running too long,
> > look at the cpu usage, the cpu usage are nearly zero. doesn't consuming
time.
> >
> > And look at
> > https://cirrus-ci.com/task/6119341601062912
> >
> > If the tests running properly, the time consuming are little
> > We should not hide the error by split them
> >
> > Signed-off-by: Yonggang Luo 
> > Reviewed-by: Daniel P. Berrangé 
> > Reviewed-by: Ed Maste 
> > ---
> >  .cirrus.yml | 35 ---
> >  1 file changed, 8 insertions(+), 27 deletions(-)
> >
> > diff --git a/.cirrus.yml b/.cirrus.yml
> > index 3dd9fcff7f..25fb4add9b 100644
> > --- a/.cirrus.yml
> > +++ b/.cirrus.yml
> > @@ -1,38 +1,19 @@
> >  env:
> >CIRRUS_CLONE_DEPTH: 1
> >
> > -freebsd_1st_task:
> > +freebsd_12_task:
> >freebsd_instance:
> >  image_family: freebsd-12-1
> > -cpu: 4
> > -memory: 4G
> > -  install_script: ASSUME_ALWAYS_YES=yes pkg bootstrap -f ; pkg install
-y
> > -bash curl cyrus-sasl git glib gmake gnutls gsed
> > -nettle perl5 pixman pkgconf png usbredir
> > +cpu: 8
> > +memory: 8G
> > +  install_script:
> > +- ASSUME_ALWAYS_YES=yes pkg bootstrap -f ;
> > +- pkg install -y bash curl cyrus-sasl git glib gmake gnutls gsed
> > +  nettle perl5 pixman pkgconf png usbredir
> >script:
> >  - mkdir build
> >  - cd build
> > -- ../configure --disable-user --target-list-exclude='alpha-softmmu
> > -ppc64-softmmu ppc-softmmu riscv32-softmmu riscv64-softmmu
s390x-softmmu
> > -sparc64-softmmu sparc-softmmu x86_64-softmmu i386-softmmu'
> > ---enable-werror || { cat config.log; exit 1; }
> > -- gmake -j$(sysctl -n hw.ncpu)
> > -- gmake -j$(sysctl -n hw.ncpu) check
> > -
> > -freebsd_2nd_task:
> > -  freebsd_instance:
> > -image_family: freebsd-12-1
> > -cpu: 4
> > -memory: 4G
> > -  install_script: ASSUME_ALWAYS_YES=yes pkg bootstrap -f ; pkg install
-y
> > -bash curl cyrus-sasl git glib gmake gnutls gtk3 gsed libepoxy
mesa-libs
> > -nettle perl5 pixman pkgconf png SDL2 usbredir
> > -  script:
> > -- ./configure --enable-werror --target-list='alpha-softmmu
ppc64-softmmu
> > -ppc-softmmu riscv32-softmmu riscv64-softmmu s390x-softmmu
> > -sparc64-softmmu sparc-softmmu x86_64-softmmu i386-softmmu
> > -sparc-bsd-user sparc64-bsd-user x86_64-bsd-user i386-bsd-user'
> > -|| { cat config.log; exit 1; }
> > +- ../configure --enable-werror || { cat config.log; exit 1; }
> >  - gmake -j$(sysctl -n hw.ncpu)
> >  - gmake -j$(sysctl -n hw.ncpu) check
> >
> >
>


--
 此致
礼
罗勇刚
Yours
sincerely,
Yonggang Luo


[PATCH v10 24/26] ci: Enable msys2 ci in cirrus

2020-09-15 Thread Yonggang Luo
Install msys2 in a proper way refer to 
https://github.com/cirruslabs/cirrus-ci-docs/issues/699
The https://wiki.qemu.org/Hosts/W32#Native_builds_with_MSYS2 need to be updated.
There is no need of --cross-prefix, open mingw64.exe instead of msys2.exe then 
we don't
need the --cross-prefix, besides we using environment variable settings:
MSYS: winsymlinks:nativestrict
MSYSTEM: MINGW64
CHERE_INVOKING: 1
to opening mingw64 native shell.
Now all enabled msys2/tests are passed

Signed-off-by: Yonggang Luo 
Reviewed-by: Daniel P. Berrangé 
---
 .cirrus.yml | 61 +
 1 file changed, 61 insertions(+)

diff --git a/.cirrus.yml b/.cirrus.yml
index 25fb4add9b..90ed891865 100644
--- a/.cirrus.yml
+++ b/.cirrus.yml
@@ -44,3 +44,64 @@ macos_xcode_task:
--enable-werror --cc=clang || { cat config.log; exit 1; }
 - gmake -j$(sysctl -n hw.ncpu)
 - gmake check
+
+windows_msys2_task:
+  windows_container:
+image: cirrusci/windowsservercore:cmake
+os_version: 2019
+cpu: 8
+memory: 8G
+  env:
+MSYS: winsymlinks:nativestrict
+MSYSTEM: MINGW64
+CHERE_INVOKING: 1
+  printenv_script:
+- C:\tools\msys64\usr\bin\bash.exe -lc 'printenv'
+  install_script:
+- C:\tools\msys64\usr\bin\bash.exe -lc "cd /c/tools &&
+curl -O 
http://repo.msys2.org/msys/x86_64/msys2-keyring-r21.b39fb11-1-any.pkg.tar.xz;
+- C:\tools\msys64\usr\bin\bash.exe -lc "cd /c/tools &&
+curl -O 
http://repo.msys2.org/msys/x86_64/msys2-keyring-r21.b39fb11-1-any.pkg.tar.xz.sig;
+- C:\tools\msys64\usr\bin\bash.exe -lc "cd /c/tools &&
+pacman -U --noconfirm msys2-keyring-r21.b39fb11-1-any.pkg.tar.xz"
+- C:\tools\msys64\usr\bin\bash.exe -lc "pacman --noconfirm -Sy"
+- C:\tools\msys64\usr\bin\bash.exe -lc "pacman --noconfirm -S --needed
+bash pacman pacman-mirrors msys2-runtime"
+- taskkill /F /IM gpg-agent.exe
+- C:\tools\msys64\usr\bin\bash.exe -lc "pacman --noconfirm -Su"
+- C:\tools\msys64\usr\bin\bash.exe -lc "pacman --noconfirm -S --needed
+base-devel
+git
+mingw-w64-x86_64-python
+mingw-w64-x86_64-python-setuptools
+mingw-w64-x86_64-toolchain
+mingw-w64-x86_64-SDL2
+mingw-w64-x86_64-SDL2_image
+mingw-w64-x86_64-gtk3
+mingw-w64-x86_64-glib2
+mingw-w64-x86_64-ninja
+mingw-w64-x86_64-make
+mingw-w64-x86_64-lzo2
+mingw-w64-x86_64-zstd
+mingw-w64-x86_64-libjpeg-turbo
+mingw-w64-x86_64-pixman
+mingw-w64-x86_64-libgcrypt
+mingw-w64-x86_64-libpng
+mingw-w64-x86_64-libssh
+mingw-w64-x86_64-libxml2
+mingw-w64-x86_64-snappy
+mingw-w64-x86_64-libusb
+mingw-w64-x86_64-usbredir
+mingw-w64-x86_64-libtasn1
+mingw-w64-x86_64-nettle
+mingw-w64-x86_64-cyrus-sasl
+mingw-w64-x86_64-curl
+mingw-w64-x86_64-gnutls
+mingw-w64-x86_64-zstd"
+  script:
+- C:\tools\msys64\usr\bin\bash.exe -lc "mkdir build"
+- C:\tools\msys64\usr\bin\bash.exe -lc "cd build && ../configure 
--python=python3"
+- C:\tools\msys64\usr\bin\bash.exe -lc "cd build && make 
-j$NUMBER_OF_PROCESSORS"
+  test_script:
+- C:\tools\msys64\usr\bin\bash.exe -lc "cd build && make V=1 check"
+
-- 
2.28.0.windows.1




[PATCH v10 20/26] tests: fix test-util-sockets.c

2020-09-15 Thread Yonggang Luo
Fixes following errors:
Running test test-util-sockets
ERROR test-util-sockets - missing test plan

# Start of name tests
**
ERROR:../tests/test-util-sockets.c:93:test_socket_fd_pass_name_good: assertion 
failed (fd != -1): (-1 != -1)
Bail out! ERROR:../tests/test-util-sockets.c:93:test_socket_fd_pass_name_good: 
assertion failed (fd != -1): (-1 != -1)

First should call to qemu_init_main_loop before socket_init,
then on win32 doesn't support for SOCKET_ADDRESS_TYPE_FD socket type

Signed-off-by: Yonggang Luo 
Reviewed-by: Daniel P. Berrangé 
---
 tests/test-util-sockets.c | 6 +-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/tests/test-util-sockets.c b/tests/test-util-sockets.c
index af9f5c0c70..1bbb16d9b1 100644
--- a/tests/test-util-sockets.c
+++ b/tests/test-util-sockets.c
@@ -75,7 +75,7 @@ int monitor_vprintf(Monitor *mon, const char *fmt, va_list 
ap) { abort(); }
 void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp) {}
 void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp) {}
 
-
+#ifndef _WIN32
 static void test_socket_fd_pass_name_good(void)
 {
 SocketAddress addr;
@@ -227,6 +227,7 @@ static void test_socket_fd_pass_num_nocli(void)
 
 g_free(addr.u.fd.str);
 }
+#endif
 
 #ifdef __linux__
 static gchar *abstract_sock_name;
@@ -321,6 +322,7 @@ int main(int argc, char **argv)
 {
 bool has_ipv4, has_ipv6;
 
+qemu_init_main_loop(_abort);
 socket_init();
 
 g_test_init(, , NULL);
@@ -340,6 +342,7 @@ int main(int argc, char **argv)
 test_fd_is_socket_bad);
 g_test_add_func("/util/socket/is-socket/good",
 test_fd_is_socket_good);
+#ifndef _WIN32
 g_test_add_func("/socket/fd-pass/name/good",
 test_socket_fd_pass_name_good);
 g_test_add_func("/socket/fd-pass/name/bad",
@@ -352,6 +355,7 @@ int main(int argc, char **argv)
 test_socket_fd_pass_num_bad);
 g_test_add_func("/socket/fd-pass/num/nocli",
 test_socket_fd_pass_num_nocli);
+#endif
 }
 
 #ifdef __linux__
-- 
2.28.0.windows.1




[PATCH v10 26/26] block: enable libnfs on msys2/mingw in cirrus.yml

2020-09-15 Thread Yonggang Luo
At the begging libnfs are not enabled because of compiling error,
now it's fixed so enable it

Signed-off-by: Yonggang Luo 
---
 .cirrus.yml | 1 +
 1 file changed, 1 insertion(+)

diff --git a/.cirrus.yml b/.cirrus.yml
index 90ed891865..ca4d7db64f 100644
--- a/.cirrus.yml
+++ b/.cirrus.yml
@@ -93,6 +93,7 @@ windows_msys2_task:
 mingw-w64-x86_64-libusb
 mingw-w64-x86_64-usbredir
 mingw-w64-x86_64-libtasn1
+mingw-w64-x86_64-libnfs
 mingw-w64-x86_64-nettle
 mingw-w64-x86_64-cyrus-sasl
 mingw-w64-x86_64-curl
-- 
2.28.0.windows.1




[PATCH v10 25/26] block: Fixes nfs compiling error on msys2/mingw

2020-09-15 Thread Yonggang Luo
These compiling errors are fixed:
../block/nfs.c:27:10: fatal error: poll.h: No such file or directory
   27 | #include 
  |  ^~~~
compilation terminated.

../block/nfs.c:63:5: error: unknown type name 'blkcnt_t'
   63 | blkcnt_t st_blocks;
  | ^~~~
../block/nfs.c: In function 'nfs_client_open':
../block/nfs.c:550:27: error: 'struct _stat64' has no member named 'st_blocks'
  550 | client->st_blocks = st.st_blocks;
  |   ^
../block/nfs.c: In function 'nfs_get_allocated_file_size':
../block/nfs.c:751:41: error: 'struct _stat64' has no member named 'st_blocks'
  751 | return (task.ret < 0 ? task.ret : st.st_blocks * 512);
  | ^
../block/nfs.c: In function 'nfs_reopen_prepare':
../block/nfs.c:805:31: error: 'struct _stat64' has no member named 'st_blocks'
  805 | client->st_blocks = st.st_blocks;
  |   ^
../block/nfs.c: In function 'nfs_get_allocated_file_size':
../block/nfs.c:752:1: error: control reaches end of non-void function 
[-Werror=return-type]
  752 | }
  | ^

On msys2/mingw, there is no st_blocks in struct _stat64 yet, we disable the 
usage of it
on msys2/mingw, and create a typedef long long blkcnt_t; for further 
implementation

Signed-off-by: Yonggang Luo 
---
 block/nfs.c | 37 ++---
 1 file changed, 30 insertions(+), 7 deletions(-)

diff --git a/block/nfs.c b/block/nfs.c
index 61a249a9fc..5f83dbe407 100644
--- a/block/nfs.c
+++ b/block/nfs.c
@@ -24,7 +24,9 @@
 
 #include "qemu/osdep.h"
 
+#if !defined(_WIN32)
 #include 
+#endif
 #include "qemu/config-file.h"
 #include "qemu/error-report.h"
 #include "qapi/error.h"
@@ -51,6 +53,13 @@
 #define QEMU_NFS_MAX_PAGECACHE_SIZE (8388608 / NFS_BLKSIZE)
 #define QEMU_NFS_MAX_DEBUG_LEVEL 2
 
+#if defined(_WIN32)
+#define nfs_stat __stat64
+typedef long long blkcnt_t;
+#else
+#define nfs_stat stat
+#endif
+
 typedef struct NFSClient {
 struct nfs_context *context;
 struct nfsfh *fh;
@@ -70,7 +79,7 @@ typedef struct NFSRPC {
 int ret;
 int complete;
 QEMUIOVector *iov;
-struct stat *st;
+struct nfs_stat *st;
 Coroutine *co;
 NFSClient *client;
 } NFSRPC;
@@ -415,11 +424,21 @@ static void nfs_file_close(BlockDriverState *bs)
 nfs_client_close(client);
 }
 
+static blkcnt_t nfs_get_st_blocks(const struct nfs_stat *st)
+{
+#if defined(_WIN32)
+/* TODO: Not be possible implement on win32 yet */
+return 0;
+#else
+return st->st_blocks;
+#endif
+}
+
 static int64_t nfs_client_open(NFSClient *client, BlockdevOptionsNfs *opts,
int flags, int open_flags, Error **errp)
 {
 int64_t ret = -EINVAL;
-struct stat st;
+struct nfs_stat st;
 char *file = NULL, *strp = NULL;
 
 qemu_mutex_init(>mutex);
@@ -545,7 +564,7 @@ static int64_t nfs_client_open(NFSClient *client, 
BlockdevOptionsNfs *opts,
 }
 
 ret = DIV_ROUND_UP(st.st_size, BDRV_SECTOR_SIZE);
-client->st_blocks = st.st_blocks;
+client->st_blocks = nfs_get_st_blocks();
 client->has_zero_init = S_ISREG(st.st_mode);
 *strp = '/';
 goto out;
@@ -706,6 +725,7 @@ static int nfs_has_zero_init(BlockDriverState *bs)
 return client->has_zero_init;
 }
 
+#if !defined(_WIN32)
 /* Called (via nfs_service) with QemuMutex held.  */
 static void
 nfs_get_allocated_file_size_cb(int ret, struct nfs_context *nfs, void *data,
@@ -729,7 +749,7 @@ static int64_t nfs_get_allocated_file_size(BlockDriverState 
*bs)
 {
 NFSClient *client = bs->opaque;
 NFSRPC task = {0};
-struct stat st;
+struct nfs_stat st;
 
 if (bdrv_is_read_only(bs) &&
 !(bs->open_flags & BDRV_O_NOCACHE)) {
@@ -746,8 +766,9 @@ static int64_t nfs_get_allocated_file_size(BlockDriverState 
*bs)
 nfs_set_events(client);
 BDRV_POLL_WHILE(bs, !task.complete);
 
-return (task.ret < 0 ? task.ret : st.st_blocks * 512);
+return (task.ret < 0 ? task.ret : nfs_get_st_blocks() * 512);
 }
+#endif
 
 static int coroutine_fn
 nfs_file_co_truncate(BlockDriverState *bs, int64_t offset, bool exact,
@@ -778,7 +799,7 @@ static int nfs_reopen_prepare(BDRVReopenState *state,
   BlockReopenQueue *queue, Error **errp)
 {
 NFSClient *client = state->bs->opaque;
-struct stat st;
+struct nfs_stat st;
 int ret = 0;
 
 if (state->flags & BDRV_O_RDWR && bdrv_is_read_only(state->bs)) {
@@ -800,7 +821,7 @@ static int nfs_reopen_prepare(BDRVReopenState *state,
nfs_get_error(client->context));
 return ret;
 }
-client->st_blocks = st.st_blocks;
+client->st_blocks = nfs_get_st_blocks();
 }
 
 return 0;
@@ -869,7 +890,9 @@ static BlockDriver bdrv_nfs = {
 .create_opts= _create_opts,
 
 .bdrv_has_zero_init = nfs_has_zero_init,
+#if !defined(_WIN32)
 .bdrv_get_allocated_file_size   = 

[PATCH v10 22/26] rcu: fixes test-logging.c by call drain_call_rcu before rmdir_full

2020-09-15 Thread Yonggang Luo
drain_call_rcu is necessary on win32, because under win32, if you
don't close the file before remove it, the remove would be fail.

Signed-off-by: Yonggang Luo 
---
 tests/test-logging.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/tests/test-logging.c b/tests/test-logging.c
index cec18b31b4..a7e36dbfe8 100644
--- a/tests/test-logging.c
+++ b/tests/test-logging.c
@@ -211,6 +211,8 @@ int main(int argc, char **argv)
  tmp_path, test_logfile_lock);
 
 rc = g_test_run();
+qemu_log_close();
+drain_call_rcu();
 
 rmdir_full(tmp_path);
 return rc;
-- 
2.28.0.windows.1




[PATCH v10 23/26] meson: upgrade meson for execute custom ninjatool under msys2 properly

2020-09-15 Thread Yonggang Luo
* Bump versions to 0.55.2 for release

* Tag Info:
object 008d13038f95e7c7d8ad553f14e408da5b94c360
type commit
tag 0.55.2
tagger Jussi Pakkanen  2020/9/11 1:24:47

Signed-off-by: Yonggang Luo 
---
 meson | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/meson b/meson
index 68ed748f84..008d13038f 16
--- a/meson
+++ b/meson
@@ -1 +1 @@
-Subproject commit 68ed748f84f14c2d4e62dcbd123816e5898eb04c
+Subproject commit 008d13038f95e7c7d8ad553f14e408da5b94c360
-- 
2.28.0.windows.1




Re: [PATCH v2 2/8] iotests: Drop readlink -f

2020-09-15 Thread Richard Henderson
On 9/15/20 6:43 AM, Alex Bennée wrote:
> From: Max Reitz 
> 
> On macOS, (out of the box) readlink does not have -f.  We do not really
> need readlink here, though, it was just a replacement for realpath
> (which is not available on our BSD test systems), which we needed to
> make the $(dirname) into an absolute path.
> 
> Instead of using either, just use "cd; pwd" like is done for
> $source_iotests.
> 
>("iotests: Allow running from different directory")
> 
> Fixes: b1cbc33a3971b6bb005d5ac3569feae35a71de0f
> Reported-by: Claudio Fontana 
> Reported-by: Thomas Huth 
> Suggested-by: Peter Maydell 
> Signed-off-by: Max Reitz 
> Message-Id: <20200914145606.94620-1-mre...@redhat.com>
> Signed-off-by: Alex Bennée 
> ---
>  tests/qemu-iotests/check | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

Reviewed-by: Richard Henderson 

r~




[PATCH v2] hw/block/nand: Decommission the NAND museum

2020-09-15 Thread Philippe Mathieu-Daudé
This is the QEMU equivalent of this Linux commit (but 7 years later):
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=f7025a43a9da2

The MTD subsystem has its own small museum of ancient NANDs
in a form of the CONFIG_MTD_NAND_MUSEUM_IDS configuration option.
The museum contains stone age NANDs with 256 bytes pages, as well
as iron age NANDs with 512 bytes per page and up to 8MiB page size.

It is with great sorrow that I inform you that the museum is being
decommissioned. The MTD subsystem is out of budget for Kconfig
options and already has too many of them, and there is a general
kernel trend to simplify the configuration menu.

We remove the stone age exhibits along with closing the museum,
but some of the iron age ones are transferred to the regular NAND
depot. Namely, only those which have unique device IDs are
transferred, and the ones which have conflicting device IDs are
removed.

The machine using this device are:
- axis-dev88
- tosa (via tc6393xb_init)
- spitz based (akita, borzoi, terrier)

Reviewed-by: Richard Henderson 
Signed-off-by: Philippe Mathieu-Daudé 
---
Peter, as 4 of the 5 machines are ARM-based, can this go via your tree?
---
 hw/block/nand.c | 13 ++---
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/hw/block/nand.c b/hw/block/nand.c
index 5c8112ed5a4..5f01ba2bc44 100644
--- a/hw/block/nand.c
+++ b/hw/block/nand.c
@@ -138,7 +138,7 @@ static void mem_and(uint8_t *dest, const uint8_t *src, 
size_t n)
 # define ADDR_SHIFT16
 # include "nand.c"
 
-/* Information based on Linux drivers/mtd/nand/nand_ids.c */
+/* Information based on Linux drivers/mtd/nand/raw/nand_ids.c */
 static const struct {
 int size;
 int width;
@@ -154,15 +154,14 @@ static const struct {
 [0xe8] = { 1,  8,  8, 4, 0 },
 [0xec] = { 1,  8,  8, 4, 0 },
 [0xea] = { 2,  8,  8, 4, 0 },
-[0xd5] = { 4,  8,  9, 4, 0 },
 [0xe3] = { 4,  8,  9, 4, 0 },
 [0xe5] = { 4,  8,  9, 4, 0 },
-[0xd6] = { 8,  8,  9, 4, 0 },
 
-[0x39] = { 8,  8,  9, 4, 0 },
-[0xe6] = { 8,  8,  9, 4, 0 },
-[0x49] = { 8,  16, 9, 4, NAND_BUSWIDTH_16 },
-[0x59] = { 8,  16, 9, 4, NAND_BUSWIDTH_16 },
+[0x6b] = { 4,8,9, 4, 0 },
+[0xe3] = { 4,8,9, 4, 0 },
+[0xe5] = { 4,8,9, 4, 0 },
+[0xd6] = { 8,8,9, 4, 0 },
+[0xe6] = { 8,8,9, 4, 0 },
 
 [0x33] = { 16, 8,  9, 5, 0 },
 [0x73] = { 16, 8,  9, 5, 0 },
-- 
2.26.2




[PATCH v10 00/26] W32, W64 msys2/mingw patches

2020-09-15 Thread Yonggang Luo
V9-V10
* gcrypt: test_tls_psk_init should write binary file instead text file.
  Split #include  out, that line not belong this patch

* tests: Enable crypto tests under msys2/mingw
  move qemu_socketpair into oslib-win32.c and rename to socketpair
  move qemu_link into osdep.c
  Fixes code style warning

* tests: Fixes test-io-channel-file by mask only owner file state mask bits
  Update comment place

* ci: Enable msys2 ci in cirrus
  Fixes misleading error message

Fixes all checkpatch error messages

V8-V9

* ci: Enable msys2 ci in cirrus
  do not install libnfs, libcapstone and jemalloc
  ident lines properly
  Do not install libnfs when the add the msys2 ci,

* Revert "configure: add --ninja option"
  Skip this revision

* block: Fixes nfs compiling error on msys2/mingw
  Use typedef long long blkcnt_t; for libnfs on msys2/mingw
  for futher implemenation, still disable nfs_get_allocated_file_size
  on win32 as it not working yet, but preserve the possibility implemenation
  it in futher

* gcrypt: test_tls_psk_init should write binary file instead text file.
  only fixes the file open mode parameter

* osdep: file locking functions are not available on Win32:
  Reword with "Do not declare the following locking functions on Win32:"

* meson: Use -b to ignore CR vs. CR-LF issues on Windows
  Reword of commit message

* tests: Enable crypto tests under msys2/mingw
  Reimplement qemu_socketpair in a simpler way. without thirdparty code

* block: enable libnfs on msys2/mingw in cirrus.yml
  New commit

* tests: disable /char/stdio/* tests in test-char.c on win32
  Needs review

* tests: fixes aio-win32 about aio_remove_fd_handler, get it consistence with=
 aio-posix.c
  Needs review

* rcu: fixes test-logging.c by call drain_call_rcu before rmdir_full
  Needs review

It first introduce msys2 CI on cirrus by fixes nfs, capstone, curses and
disable partial test-char tests.
And then fixes all unit tests failure on msys2/mingw
This fixes the reviews suggested in the mailling list
All cirrus CI are passed

Maxim Levitsky (1):
  rcu: Implement drain_call_rcu

Yonggang Luo (25):
  ci: fixes msys2 build by upgrading capstone to 4.0.2
  configure: Fixes ncursesw detection under msys2/mingw and enable
curses
  win32: Simplify gmtime_r detection direct base on
_POSIX_THREAD_SAFE_FUNCTIONS.
  curses: Fixes curses compiling errors.
  tests: disable /char/stdio/* tests in test-char.c on win32
  tests: Fixes test-replication.c on msys2/mingw.
  tests: test-replication disable /replication/secondary/* on
msys2/mingw.
  osdep: file locking functions are not available on Win32
  meson: Use -b to ignore CR vs. CR-LF issues on Windows
  gcrypt: test_tls_psk_init should write binary file instead text file.
  tests: Enable crypto tests under msys2/mingw
  meson: remove empty else and duplicated gio deps
  vmstate: Fixes test-vmstate.c on msys2/mingw
  cirrus: Building freebsd in a single short
  tests: Convert g_free to g_autofree macro in test-logging.c
  tests: Fixes test-io-channel-socket.c tests under msys2/mingw
  tests: fixes aio-win32 about aio_remove_fd_handler, get it consistence
with aio-posix.c
  tests: Fixes test-io-channel-file by mask only owner file state mask
bits
  tests: fix test-util-sockets.c
  tests: Fixes test-qdev-global-props.c
  rcu: fixes test-logging.c by call drain_call_rcu before rmdir_full
  meson: upgrade meson for execute custom ninjatool under msys2 properly
  ci: Enable msys2 ci in cirrus
  block: Fixes nfs compiling error on msys2/mingw
  block: enable libnfs on msys2/mingw in cirrus.yml

 .cirrus.yml  | 97 +++-
 block/nfs.c  | 37 +---
 capstone |  2 +-
 configure| 61 ++--
 include/qemu/osdep.h |  3 +-
 include/qemu/rcu.h   |  1 +
 include/sysemu/os-win32.h|  9 ++-
 meson|  2 +-
 meson.build  |  6 --
 tests/crypto-tls-psk-helpers.c   |  6 +-
 tests/crypto-tls-x509-helpers.c  |  6 +-
 tests/crypto-tls-x509-helpers.h  |  5 +-
 tests/qapi-schema/meson.build|  2 +-
 tests/test-char.c| 27 +
 tests/test-crypto-tlscredsx509.c | 47 
 tests/test-crypto-tlssession.c   | 64 -
 tests/test-io-channel-file.c | 12 +++-
 tests/test-io-channel-socket.c   |  2 +
 tests/test-io-channel-tls.c  | 49 +---
 tests/test-logging.c |  6 +-
 tests/test-qdev-global-props.c   |  9 ++-
 tests/test-replication.c | 22 ++--
 tests/test-util-sockets.c|  6 +-
 tests/test-vmstate.c |  3 +-
 ui/curses.c  | 14 ++---
 util/aio-win32.c | 12 +++-
 util/osdep.c | 16 ++
 util/oslib-win32.c   | 78 -
 util/rcu.c   | 55 ++
 29 files changed, 459 

[PATCH v10 21/26] tests: Fixes test-qdev-global-props.c

2020-09-15 Thread Yonggang Luo
On win32 the line ending are \r\n, so we skip the \n in function 
test_dynamic_globalprop

Signed-off-by: Yonggang Luo 
Reviewed-by: Daniel P. Berrangé 
---
 tests/test-qdev-global-props.c | 9 ++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/tests/test-qdev-global-props.c b/tests/test-qdev-global-props.c
index 8a3c14d92c..c8862cac5f 100644
--- a/tests/test-qdev-global-props.c
+++ b/tests/test-qdev-global-props.c
@@ -251,10 +251,13 @@ static void test_dynamic_globalprop(void)
 g_test_trap_assert_passed();
 g_test_trap_assert_stderr_unmatched("*prop1*");
 g_test_trap_assert_stderr_unmatched("*prop2*");
-g_test_trap_assert_stderr("*warning: global dynamic-prop-type-bad.prop3 
has invalid class name\n*");
+g_test_trap_assert_stderr(
+"*warning: global dynamic-prop-type-bad.prop3 has invalid class 
name*");
 g_test_trap_assert_stderr_unmatched("*prop4*");
-g_test_trap_assert_stderr("*warning: global nohotplug-type.prop5=105 not 
used\n*");
-g_test_trap_assert_stderr("*warning: global nondevice-type.prop6 has 
invalid class name\n*");
+g_test_trap_assert_stderr(
+"*warning: global nohotplug-type.prop5=105 not used*");
+g_test_trap_assert_stderr(
+"*warning: global nondevice-type.prop6 has invalid class name*");
 g_test_trap_assert_stdout("");
 }
 
-- 
2.28.0.windows.1




[PATCH v10 18/26] tests: fixes aio-win32 about aio_remove_fd_handler, get it consistence with aio-posix.c

2020-09-15 Thread Yonggang Luo
This is a fixes for
(C:\work\xemu\qemu\build\tests\test-aio-multithread.exe:19100): GLib-CRITICAL 
**: 23:03:24.965: g_source_remove_poll: assertion '!SOURCE_DESTROYED (source)' 
failed
ERROR test-aio-multithread - Bail out! GLib-FATAL-CRITICAL: 
g_source_remove_poll: assertion '!SOURCE_DESTROYED (source)' failed

(C:\work\xemu\qemu\build\tests\test-bdrv-drain.exe:21036): GLib-CRITICAL **: 
23:03:29.861: g_source_remove_poll: assertion '!SOURCE_DESTROYED (source)' 
failed
ERROR test-bdrv-drain - Bail out! GLib-FATAL-CRITICAL: g_source_remove_poll: 
assertion '!SOURCE_DESTROYED (source)' failed

And the idea comes from https://patchwork.kernel.org/patch/9975239/

Signed-off-by: Yonggang Luo 
---
 util/aio-win32.c | 12 ++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/util/aio-win32.c b/util/aio-win32.c
index 953c56ab48..49bd90e62e 100644
--- a/util/aio-win32.c
+++ b/util/aio-win32.c
@@ -37,6 +37,16 @@ struct AioHandler {
 
 static void aio_remove_fd_handler(AioContext *ctx, AioHandler *node)
 {
+/*
+ * If the GSource is in the process of being destroyed then
+ * g_source_remove_poll() causes an assertion failure.  Skip
+ * removal in that case, because glib cleans up its state during
+ * destruction anyway.
+ */
+if (!g_source_is_destroyed(>source)) {
+g_source_remove_poll(>source, >pfd);
+}
+
 /* If aio_poll is in progress, just mark the node as deleted */
 if (qemu_lockcnt_count(>list_lock)) {
 node->deleted = 1;
@@ -139,8 +149,6 @@ void aio_set_event_notifier(AioContext *ctx,
 /* Are we deleting the fd handler? */
 if (!io_notify) {
 if (node) {
-g_source_remove_poll(>source, >pfd);
-
 aio_remove_fd_handler(ctx, node);
 }
 } else {
-- 
2.28.0.windows.1




[PATCH v10 19/26] tests: Fixes test-io-channel-file by mask only owner file state mask bits

2020-09-15 Thread Yonggang Luo
This is the error on msys2/mingw
Running test test-io-channel-file
**
ERROR:../tests/test-io-channel-file.c:59:test_io_channel_file_helper: assertion 
failed (TEST_MASK & ~mask == st.st_mode & 0777): (384 == 438)
ERROR test-io-channel-file - Bail out! 
ERROR:../tests/test-io-channel-file.c:59:test_io_channel_file_helper: assertion 
failed (TEST_MASK & ~mask == st.st_mode & 0777): (384 == 438)

Signed-off-by: Yonggang Luo 
Reviewed-by: Daniel P. Berrangé 
---
 tests/test-io-channel-file.c | 12 +++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/tests/test-io-channel-file.c b/tests/test-io-channel-file.c
index bac2b07562..0aa0477541 100644
--- a/tests/test-io-channel-file.c
+++ b/tests/test-io-channel-file.c
@@ -28,6 +28,16 @@
 #define TEST_FILE "tests/test-io-channel-file.txt"
 #define TEST_MASK 0600
 
+/*
+ * On Windows the stat() function in the C library checks only
+ * the FAT-style READONLY attribute and does not look at the ACL at all.
+ */
+#ifdef _WIN32
+#define TEST_MASK_EXPECT 0700
+#else
+#define TEST_MASK_EXPECT 0777
+#endif
+
 static void test_io_channel_file_helper(int flags)
 {
 QIOChannel *src, *dst;
@@ -56,7 +66,7 @@ static void test_io_channel_file_helper(int flags)
 umask(mask);
 ret = stat(TEST_FILE, );
 g_assert_cmpint(ret, >, -1);
-g_assert_cmpuint(TEST_MASK & ~mask, ==, st.st_mode & 0777);
+g_assert_cmpuint(TEST_MASK & ~mask, ==, st.st_mode & TEST_MASK_EXPECT);
 
 unlink(TEST_FILE);
 object_unref(OBJECT(src));
-- 
2.28.0.windows.1




[PATCH v10 16/26] tests: Convert g_free to g_autofree macro in test-logging.c

2020-09-15 Thread Yonggang Luo
g_autofree are prefer than g_free when possible.

Signed-off-by: Yonggang Luo 
Reviewed-by: Philippe Mathieu-Daudé 
Reviewed-by: Daniel P. Berrangé 
---
 tests/test-logging.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tests/test-logging.c b/tests/test-logging.c
index 8a1161de1d..cec18b31b4 100644
--- a/tests/test-logging.c
+++ b/tests/test-logging.c
@@ -196,7 +196,8 @@ static void rmdir_full(gchar const *root)
 
 int main(int argc, char **argv)
 {
-gchar *tmp_path = g_dir_make_tmp("qemu-test-logging.XX", NULL);
+g_autofree gchar *tmp_path = g_dir_make_tmp(
+"qemu-test-logging.XX", NULL);
 int rc;
 
 g_test_init(, , NULL);
@@ -212,6 +213,5 @@ int main(int argc, char **argv)
 rc = g_test_run();
 
 rmdir_full(tmp_path);
-g_free(tmp_path);
 return rc;
 }
-- 
2.28.0.windows.1




[PATCH v10 02/26] ci: fixes msys2 build by upgrading capstone to 4.0.2

2020-09-15 Thread Yonggang Luo
The currently random version capstone have the following compiling issue:
  CC  /c/work/xemu/qemu/build/slirp/src/arp_table.o
make[1]: *** No rule to make target 
“/c/work/xemu/qemu/build/capstone/capstone.lib”。 Stop.

Subproject commit 1d230532840a37ac032c6ab80128238fc930c6c1 are the tagged 
version 4.0.2
when upgrading to this version, the folder structure of include are changed to
qemu\capstone\include
│  platform.h
│
├─capstone
│  arm.h
│  arm64.h
│  capstone.h
│  evm.h
│  m680x.h
│  m68k.h
│  mips.h
│  platform.h
│  ppc.h
│  sparc.h
│  systemz.h
│  tms320c64x.h
│  x86.h
│  xcore.h
│
└─windowsce
intrin.h
stdint.h

in capstone. so we need add extra include path 
-I${source_path}/capstone/include/capstone
for directly #include , and the exist include path should preserve, 
because
in capstone code there something like #include "capstone/capstone.h"

If only using
capstone_cflags="-I${source_path}/capstone/include/capstone"
Then will cause the following compiling error:

  CC  cs.o
cs.c:17:10: fatal error: 'capstone/capstone.h' file not found
#include 
 ^
1 error generated.

Signed-off-by: Yonggang Luo 
---
 capstone  | 2 +-
 configure | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/capstone b/capstone
index 22ead3e0bf..1d23053284 16
--- a/capstone
+++ b/capstone
@@ -1 +1 @@
-Subproject commit 22ead3e0bfdb87516656453336160e0a37b066bf
+Subproject commit 1d230532840a37ac032c6ab80128238fc930c6c1
diff --git a/configure b/configure
index ce27eafb0a..f59080703e 100755
--- a/configure
+++ b/configure
@@ -5157,7 +5157,7 @@ case "$capstone" in
   LIBCAPSTONE=libcapstone.a
 fi
 capstone_libs="-Lcapstone -lcapstone"
-capstone_cflags="-I${source_path}/capstone/include"
+capstone_cflags="-I${source_path}/capstone/include 
-I${source_path}/capstone/include/capstone"
 ;;
 
   system)
-- 
2.28.0.windows.1




[PATCH v10 01/26] rcu: Implement drain_call_rcu

2020-09-15 Thread Yonggang Luo
From: Maxim Levitsky 

This will allow is to preserve the semantics of hmp_device_del,
that the device is deleted immediatly which was changed by previos
patch that delayed this to RCU callback

Signed-off-by: Maxim Levitsky 
Suggested-by: Stefan Hajnoczi 
Reviewed-by: Stefan Hajnoczi 
---
 include/qemu/rcu.h |  1 +
 util/rcu.c | 55 ++
 2 files changed, 56 insertions(+)

diff --git a/include/qemu/rcu.h b/include/qemu/rcu.h
index 570aa603eb..0e375ebe13 100644
--- a/include/qemu/rcu.h
+++ b/include/qemu/rcu.h
@@ -133,6 +133,7 @@ struct rcu_head {
 };
 
 extern void call_rcu1(struct rcu_head *head, RCUCBFunc *func);
+extern void drain_call_rcu(void);
 
 /* The operands of the minus operator must have the same type,
  * which must be the one that we specify in the cast.
diff --git a/util/rcu.c b/util/rcu.c
index 60a37f72c3..c4fefa9333 100644
--- a/util/rcu.c
+++ b/util/rcu.c
@@ -293,6 +293,61 @@ void call_rcu1(struct rcu_head *node, void (*func)(struct 
rcu_head *node))
 qemu_event_set(_call_ready_event);
 }
 
+
+struct rcu_drain {
+struct rcu_head rcu;
+QemuEvent drain_complete_event;
+};
+
+static void drain_rcu_callback(struct rcu_head *node)
+{
+struct rcu_drain *event = (struct rcu_drain *)node;
+qemu_event_set(>drain_complete_event);
+}
+
+/*
+ * This function ensures that all pending RCU callbacks
+ * on the current thread are done executing
+
+ * drops big qemu lock during the wait to allow RCU thread
+ * to process the callbacks
+ *
+ */
+
+void drain_call_rcu(void)
+{
+struct rcu_drain rcu_drain;
+bool locked = qemu_mutex_iothread_locked();
+
+memset(_drain, 0, sizeof(struct rcu_drain));
+qemu_event_init(_drain.drain_complete_event, false);
+
+if (locked) {
+qemu_mutex_unlock_iothread();
+}
+
+
+/*
+ * RCU callbacks are invoked in the same order as in which they
+ * are registered, thus we can be sure that when 'drain_rcu_callback'
+ * is called, all RCU callbacks that were registered on this thread
+ * prior to calling this function are completed.
+ *
+ * Note that since we have only one global queue of the RCU callbacks,
+ * we also end up waiting for most of RCU callbacks that were registered
+ * on the other threads, but this is a side effect that shoudn't be
+ * assumed.
+ */
+
+call_rcu1(_drain.rcu, drain_rcu_callback);
+qemu_event_wait(_drain.drain_complete_event);
+
+if (locked) {
+qemu_mutex_lock_iothread();
+}
+
+}
+
 void rcu_register_thread(void)
 {
 assert(rcu_reader.ctr == 0);
-- 
2.28.0.windows.1




[PATCH v10 17/26] tests: Fixes test-io-channel-socket.c tests under msys2/mingw

2020-09-15 Thread Yonggang Luo
Currently test-io-channel-socket doesn't init with
qemu_init_main_loop
and that's cause the qemu_aio_context not inited,
and the following is the stack when null pointer accessed:

qemu_fd_register (c:\work\xemu\qemu\util\main-loop.c:336)
qemu_try_set_nonblock (c:\work\xemu\qemu\util\oslib-win32.c:224)
qemu_set_nonblock (c:\work\xemu\qemu\util\oslib-win32.c:230)
socket_can_bind_connect (c:\work\xemu\qemu\tests\socket-helpers.c:93)
socket_check_protocol_support (c:\work\xemu\qemu\tests\socket-helpers.c:141)
main (c:\work\xemu\qemu\tests\test-io-channel-socket.c:568)
__tmainCRTStartup (@__tmainCRTStartup:142)
mainCRTStartup (@1400014f6..140001539:3)
BaseThreadInitThunk (@BaseThreadInitThunk:9)
RtlUserThreadStart (@RtlUserThreadStart:12)

Signed-off-by: Yonggang Luo 
Reviewed-by: Daniel P. Berrangé 
---
 tests/test-io-channel-socket.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/tests/test-io-channel-socket.c b/tests/test-io-channel-socket.c
index d43083a766..743577d744 100644
--- a/tests/test-io-channel-socket.c
+++ b/tests/test-io-channel-socket.c
@@ -25,6 +25,7 @@
 #include "socket-helpers.h"
 #include "qapi/error.h"
 #include "qemu/module.h"
+#include "qemu/main-loop.h"
 
 
 static void test_io_channel_set_socket_bufs(QIOChannel *src,
@@ -556,6 +557,7 @@ int main(int argc, char **argv)
 bool has_ipv4, has_ipv6;
 
 module_call_init(MODULE_INIT_QOM);
+qemu_init_main_loop(_abort);
 socket_init();
 
 g_test_init(, , NULL);
-- 
2.28.0.windows.1




[PATCH v10 06/26] tests: disable /char/stdio/* tests in test-char.c on win32

2020-09-15 Thread Yonggang Luo
These tests are blocking test-char to be finished.
Disable them by using variable is_win32, so we doesn't
need macro to open it. and easy recover those function
latter.

Signed-off-by: Yonggang Luo 
---
 tests/test-char.c | 27 +--
 1 file changed, 17 insertions(+), 10 deletions(-)

diff --git a/tests/test-char.c b/tests/test-char.c
index d35cc839bc..09e4069306 100644
--- a/tests/test-char.c
+++ b/tests/test-char.c
@@ -77,7 +77,6 @@ static void fe_event(void *opaque, QEMUChrEvent event)
 }
 }
 
-#ifdef _WIN32
 static void char_console_test_subprocess(void)
 {
 QemuOpts *opts;
@@ -102,7 +101,7 @@ static void char_console_test(void)
 g_test_trap_assert_passed();
 g_test_trap_assert_stdout("CONSOLE");
 }
-#endif
+
 static void char_stdio_test_subprocess(void)
 {
 Chardev *chr;
@@ -1448,7 +1447,11 @@ static SocketAddress unixaddr = {
 
 int main(int argc, char **argv)
 {
-bool has_ipv4, has_ipv6;
+bool has_ipv4, has_ipv6, is_win32 = false;
+
+#ifdef _WIN32
+is_win32 = true;
+#endif
 
 qemu_init_main_loop(_abort);
 socket_init();
@@ -1467,12 +1470,16 @@ int main(int argc, char **argv)
 g_test_add_func("/char/invalid", char_invalid_test);
 g_test_add_func("/char/ringbuf", char_ringbuf_test);
 g_test_add_func("/char/mux", char_mux_test);
-#ifdef _WIN32
-g_test_add_func("/char/console/subprocess", char_console_test_subprocess);
-g_test_add_func("/char/console", char_console_test);
-#endif
-g_test_add_func("/char/stdio/subprocess", char_stdio_test_subprocess);
-g_test_add_func("/char/stdio", char_stdio_test);
+if (0) {
+g_test_add_func("/char/console/subprocess",
+char_console_test_subprocess);
+g_test_add_func("/char/console", char_console_test);
+}
+
+if (!is_win32) {
+g_test_add_func("/char/stdio/subprocess", char_stdio_test_subprocess);
+g_test_add_func("/char/stdio", char_stdio_test);
+}
 #ifndef _WIN32
 g_test_add_func("/char/pipe", char_pipe_test);
 #endif
@@ -1534,7 +1541,7 @@ int main(int argc, char **argv)
 g_test_add_data_func("/char/socket/client/dupid-reconnect/" # name, \
   ##name, char_socket_client_dupid_test)
 
-if (has_ipv4) {
+if (has_ipv4 && !is_win32) {
 SOCKET_SERVER_TEST(tcp, );
 SOCKET_CLIENT_TEST(tcp, );
 g_test_add_data_func("/char/socket/server/two-clients/tcp", ,
-- 
2.28.0.windows.1




[PATCH v10 09/26] osdep: file locking functions are not available on Win32

2020-09-15 Thread Yonggang Luo
Do not declare the following locking functions on Win32:
int qemu_lock_fd(int fd, int64_t start, int64_t len, bool exclusive);
int qemu_unlock_fd(int fd, int64_t start, int64_t len);
int qemu_lock_fd_test(int fd, int64_t start, int64_t len, bool exclusive);
bool qemu_has_ofd_lock(void);

Signed-off-by: Yonggang Luo 
Reviewed-by: Daniel P. Berrangé 
---
 include/qemu/osdep.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
index 412962d91a..e80fddd1e8 100644
--- a/include/qemu/osdep.h
+++ b/include/qemu/osdep.h
@@ -502,11 +502,11 @@ int qemu_close(int fd);
 int qemu_unlink(const char *name);
 #ifndef _WIN32
 int qemu_dup(int fd);
-#endif
 int qemu_lock_fd(int fd, int64_t start, int64_t len, bool exclusive);
 int qemu_unlock_fd(int fd, int64_t start, int64_t len);
 int qemu_lock_fd_test(int fd, int64_t start, int64_t len, bool exclusive);
 bool qemu_has_ofd_lock(void);
+#endif
 
 #if defined(__HAIKU__) && defined(__i386__)
 #define FMT_pid "%ld"
-- 
2.28.0.windows.1




[PATCH v10 03/26] configure: Fixes ncursesw detection under msys2/mingw and enable curses

2020-09-15 Thread Yonggang Luo
The mingw pkg-config are showing following absolute path and contains : as the 
separator,
so we must not use : as path separator. and we know the command line parameter 
are not likely
contains newline, we could use newline as path command line parameter separator

-D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=199506L 
-IC:/CI-Tools/msys64/mingw64/include/ncursesw:-I/usr/include/ncursesw:
-DNCURSES_WIDECHAR -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=199506L -IC -pipe 
-lncursesw -lgnurx -ltre -lintl -liconv
-DNCURSES_WIDECHAR -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=199506L -IC -lncursesw
-DNCURSES_WIDECHAR -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=199506L -IC -lcursesw
-DNCURSES_WIDECHAR /CI-Tools/msys64/mingw64/include/ncursesw -pipe -lncursesw 
-lgnurx -ltre -lintl -liconv
-DNCURSES_WIDECHAR /CI-Tools/msys64/mingw64/include/ncursesw -lncursesw
-DNCURSES_WIDECHAR /CI-Tools/msys64/mingw64/include/ncursesw -lcursesw
-DNCURSES_WIDECHAR -I/usr/include/ncursesw -pipe -lncursesw -lgnurx -ltre 
-lintl -liconv
-DNCURSES_WIDECHAR -I/usr/include/ncursesw -lncursesw
-DNCURSES_WIDECHAR -I/usr/include/ncursesw -lcursesw

Refer to https://unix.stackexchange.com/a/103011/218958

If your file names are guaranteed not to contain newlines, you can use newlines 
as the separator. W
hen you expand the variable, first turn off globbing with set -f and set the 
list of field splitting characters
IFS to contain only a newline.

msys2/mingw lacks the POSIX-required langinfo.h.

gcc test.c -DNCURSES_WIDECHAR -I/mingw64/include/ncursesw -pipe -lncursesw 
-lgnurx -ltre -lintl -liconv
test.c:4:10: fatal error: langinfo.h: No such file or directory
4 | #include 
  |  ^~~~
compilation terminated.

So we using g_get_codeset instead of nl_langinfo(CODESET)

Signed-off-by: Yonggang Luo 
Reviewed-by: Gerd Hoffmann 
---
 configure   | 25 +++--
 ui/curses.c | 10 +-
 2 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/configure b/configure
index f59080703e..dc4b7a2e55 100755
--- a/configure
+++ b/configure
@@ -3654,35 +3654,40 @@ if test "$iconv" = "no" ; then
 fi
 if test "$curses" != "no" ; then
   if test "$mingw32" = "yes" ; then
-curses_inc_list="$($pkg_config --cflags ncurses 2>/dev/null):"
-curses_lib_list="$($pkg_config --libs ncurses 2>/dev/null):-lpdcurses"
+curses_inc_list="$($pkg_config --cflags ncurses 2>/dev/null)
+  $($pkg_config --cflags ncursesw 2>/dev/null)"
+curses_lib_list="$($pkg_config --libs ncurses 2>/dev/null)
+  $($pkg_config --libs ncursesw 2>/dev/null)
+  -lpdcurses"
   else
-curses_inc_list="$($pkg_config --cflags ncursesw 
2>/dev/null):-I/usr/include/ncursesw:"
-curses_lib_list="$($pkg_config --libs ncursesw 
2>/dev/null):-lncursesw:-lcursesw"
+curses_inc_list="$($pkg_config --cflags ncursesw 2>/dev/null)
+  -I/usr/include/ncursesw:"
+curses_lib_list="$($pkg_config --libs ncursesw 2>/dev/null)
+  -lncursesw
+  -lcursesw"
   fi
   curses_found=no
   cat > $TMPC << EOF
 #include 
 #include 
 #include 
-#include 
 int main(void) {
-  const char *codeset;
   wchar_t wch = L'w';
   setlocale(LC_ALL, "");
   resize_term(0, 0);
   addwstr(L"wide chars\n");
   addnwstr(, 1);
   add_wch(WACS_DEGREE);
-  codeset = nl_langinfo(CODESET);
-  return codeset != 0;
+  return 0;
 }
 EOF
-  IFS=:
+  IFS='
+'   # turn off variable value expansion except for 
splitting at newlines
   for curses_inc in $curses_inc_list; do
 # Make sure we get the wide character prototypes
 curses_inc="-DNCURSES_WIDECHAR $curses_inc"
-IFS=:
+IFS='
+'   # turn off variable value expansion except for 
splitting at newlines
 for curses_lib in $curses_lib_list; do
   unset IFS
   if compile_prog "$curses_inc" "$curses_lib" ; then
diff --git a/ui/curses.c b/ui/curses.c
index a59b23a9cf..12bc682cf9 100644
--- a/ui/curses.c
+++ b/ui/curses.c
@@ -30,7 +30,6 @@
 #endif
 #include 
 #include 
-#include 
 #include 
 
 #include "qapi/error.h"
@@ -526,6 +525,7 @@ static void font_setup(void)
 iconv_t nativecharset_to_ucs2;
 iconv_t font_conv;
 int i;
+g_autofree gchar *local_codeset = g_get_codeset();
 
 /*
  * Control characters are normally non-printable, but VGA does have
@@ -566,14 +566,14 @@ static void font_setup(void)
   0x25bc
 };
 
-ucs2_to_nativecharset = iconv_open(nl_langinfo(CODESET), "UCS-2");
+ucs2_to_nativecharset = iconv_open(local_codeset, "UCS-2");
 if (ucs2_to_nativecharset == (iconv_t) -1) {
 fprintf(stderr, "Could not convert font glyphs from UCS-2: '%s'\n",
 strerror(errno));
 exit(1);
 }
 
-nativecharset_to_ucs2 = iconv_open("UCS-2", nl_langinfo(CODESET));
+nativecharset_to_ucs2 = iconv_open("UCS-2", local_codeset);
 if (nativecharset_to_ucs2 == (iconv_t) -1) {
 iconv_close(ucs2_to_nativecharset);
 fprintf(stderr, "Could not convert font glyphs to 

[PATCH v10 04/26] win32: Simplify gmtime_r detection direct base on _POSIX_THREAD_SAFE_FUNCTIONS.

2020-09-15 Thread Yonggang Luo
First, this reduce the size of configure, configure are tending to removal in 
future,
and this didn't introduce any new feature or remove any exist feature.
Second, the current localtime_r detection are conflict with ncursesw detection 
in
mingw, when ncursesw detected, it will provide the following compile flags
pkg-config --cflags ncursesw
-D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=199506L 
-IC:/CI-Tools/msys64/mingw64/include/ncursesw
And the compile flag _POSIX_C_SOURCE will always cause 
_POSIX_THREAD_SAFE_FUNCTIONS to
be defined, in new version of mingw, that's will cause localtime_r to be 
defined.
But the configure script didn't provide _POSIX_C_SOURCE macro, and that's will 
result
localtime_r not detected because localtime_r are defined in forceinline manner.

And finally cause conflict between QEMU defined localtime_r
struct tm *localtime_r(const time_t *timep, struct tm *result);
with mingw defined localtime_r

```
#if defined(_POSIX_C_SOURCE) && !defined(_POSIX_THREAD_SAFE_FUNCTIONS)
#define _POSIX_THREAD_SAFE_FUNCTIONS 200112L
#endif

#ifdef _POSIX_THREAD_SAFE_FUNCTIONS
__forceinline struct tm *__CRTDECL localtime_r(const time_t *_Time, struct tm 
*_Tm) {
  return localtime_s(_Tm, _Time) ? NULL : _Tm;
}
__forceinline struct tm *__CRTDECL gmtime_r(const time_t *_Time, struct tm 
*_Tm) {
  return gmtime_s(_Tm, _Time) ? NULL : _Tm;
}
__forceinline char *__CRTDECL ctime_r(const time_t *_Time, char *_Str) {
  return ctime_s(_Str, 0x7fff, _Time) ? NULL : _Str;
}
__forceinline char *__CRTDECL asctime_r(const struct tm *_Tm, char * _Str) {
  return asctime_s(_Str, 0x7fff, _Tm) ? NULL : _Str;
}
#endif
```

So I suggest remove this configure script, and restrict msys2/mingw version to 
easy to maintain.
And use _POSIX_THREAD_SAFE_FUNCTIONS to guard the localtime_r and counterpart 
functions

Signed-off-by: Yonggang Luo 
---
 configure | 34 --
 include/sysemu/os-win32.h |  4 ++--
 util/oslib-win32.c|  2 +-
 3 files changed, 3 insertions(+), 37 deletions(-)

diff --git a/configure b/configure
index dc4b7a2e55..bac48b5b49 100755
--- a/configure
+++ b/configure
@@ -2496,37 +2496,6 @@ if test "$vhost_net" = ""; then
   test "$vhost_kernel" = "yes" && vhost_net=yes
 fi
 
-##
-# MinGW / Mingw-w64 localtime_r/gmtime_r check
-
-if test "$mingw32" = "yes"; then
-# Some versions of MinGW / Mingw-w64 lack localtime_r
-# and gmtime_r entirely.
-#
-# Some versions of Mingw-w64 define a macro for
-# localtime_r/gmtime_r.
-#
-# Some versions of Mingw-w64 will define functions
-# for localtime_r/gmtime_r, but only if you have
-# _POSIX_THREAD_SAFE_FUNCTIONS defined. For fun
-# though, unistd.h and pthread.h both define
-# that for you.
-#
-# So this #undef localtime_r and #include 
-# are not in fact redundant.
-cat > $TMPC << EOF
-#include 
-#include 
-#undef localtime_r
-int main(void) { localtime_r(NULL, NULL); return 0; }
-EOF
-if compile_prog "" "" ; then
-localtime_r="yes"
-else
-localtime_r="no"
-fi
-fi
-
 ##
 # pkg-config probe
 
@@ -7088,9 +7057,6 @@ if [ "$bsd" = "yes" ] ; then
   echo "CONFIG_BSD=y" >> $config_host_mak
 fi
 
-if test "$localtime_r" = "yes" ; then
-  echo "CONFIG_LOCALTIME_R=y" >> $config_host_mak
-fi
 if test "$qom_cast_debug" = "yes" ; then
   echo "CONFIG_QOM_CAST_DEBUG=y" >> $config_host_mak
 fi
diff --git a/include/sysemu/os-win32.h b/include/sysemu/os-win32.h
index d8978e28c0..3ac8a53bac 100644
--- a/include/sysemu/os-win32.h
+++ b/include/sysemu/os-win32.h
@@ -48,12 +48,12 @@
 #define siglongjmp(env, val) longjmp(env, val)
 
 /* Missing POSIX functions. Don't use MinGW-w64 macros. */
-#ifndef CONFIG_LOCALTIME_R
+#ifndef _POSIX_THREAD_SAFE_FUNCTIONS
 #undef gmtime_r
 struct tm *gmtime_r(const time_t *timep, struct tm *result);
 #undef localtime_r
 struct tm *localtime_r(const time_t *timep, struct tm *result);
-#endif /* CONFIG_LOCALTIME_R */
+#endif
 
 static inline void os_setup_signal_handling(void) {}
 static inline void os_daemonize(void) {}
diff --git a/util/oslib-win32.c b/util/oslib-win32.c
index c654dafd93..f2fa9a3549 100644
--- a/util/oslib-win32.c
+++ b/util/oslib-win32.c
@@ -106,7 +106,7 @@ void qemu_anon_ram_free(void *ptr, size_t size)
 }
 }
 
-#ifndef CONFIG_LOCALTIME_R
+#ifndef _POSIX_THREAD_SAFE_FUNCTIONS
 /* FIXME: add proper locking */
 struct tm *gmtime_r(const time_t *timep, struct tm *result)
 {
-- 
2.28.0.windows.1




[PATCH v10 05/26] curses: Fixes curses compiling errors.

2020-09-15 Thread Yonggang Luo
This is the compiling error:
../ui/curses.c: In function 'curses_refresh':
../ui/curses.c:256:5: error: 'next_maybe_keycode' may be used uninitialized in 
this function [-Werror=maybe-uninitialized]
  256 | curses2foo(_curses2keycode, _curseskey2keycode, chr, maybe_keycode)
  | ^~
../ui/curses.c:302:32: note: 'next_maybe_keycode' was declared here
  302 | enum maybe_keycode next_maybe_keycode;
  |^~
../ui/curses.c:256:5: error: 'maybe_keycode' may be used uninitialized in this 
function [-Werror=maybe-uninitialized]
  256 | curses2foo(_curses2keycode, _curseskey2keycode, chr, maybe_keycode)
  | ^~
../ui/curses.c:265:24: note: 'maybe_keycode' was declared here
  265 | enum maybe_keycode maybe_keycode;
  |^
cc1.exe: all warnings being treated as errors

gcc version 10.2.0 (Rev1, Built by MSYS2 project)

Signed-off-by: Yonggang Luo 
Reviewed-by: Gerd Hoffmann 
Reviewed-by: Daniel P. Berrangé 
---
 ui/curses.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/ui/curses.c b/ui/curses.c
index 12bc682cf9..e4f9588c3e 100644
--- a/ui/curses.c
+++ b/ui/curses.c
@@ -262,7 +262,7 @@ static int curses2foo(const int _curses2foo[], const int 
_curseskey2foo[],
 static void curses_refresh(DisplayChangeListener *dcl)
 {
 int chr, keysym, keycode, keycode_alt;
-enum maybe_keycode maybe_keycode;
+enum maybe_keycode maybe_keycode = CURSES_KEYCODE;
 
 curses_winch_check();
 
@@ -299,7 +299,7 @@ static void curses_refresh(DisplayChangeListener *dcl)
 
 /* alt or esc key */
 if (keycode == 1) {
-enum maybe_keycode next_maybe_keycode;
+enum maybe_keycode next_maybe_keycode = CURSES_KEYCODE;
 int nextchr = console_getch(_maybe_keycode);
 
 if (nextchr != -1) {
-- 
2.28.0.windows.1




[PATCH v10 12/26] tests: Enable crypto tests under msys2/mingw

2020-09-15 Thread Yonggang Luo
Fixes following tests on msys2/mingw
'test-crypto-tlscredsx509'
test-crypto-tlssession'
'test-io-channel-tls'

These tests are failure with:
ERROR test-crypto-tlscredsx509 - missing test plan
ERROR test-crypto-tlssession - missing test plan
ERROR test-io-channel-tls - missing test plan

Because on win32 those test case are all disabled in the header

Add qemu_socket_pair for cross platform support
Convert file system handling functions to glib
Add qemu_link function instead posix only link function.
Use send ad recv from qemu that convert Windows Socks error to errno properly.
Use g_remove instead unlink
Use g_mkdir instead mkdir

Signed-off-by: Yonggang Luo 
---
 include/qemu/osdep.h |  1 +
 include/sysemu/os-win32.h|  5 +++
 tests/crypto-tls-psk-helpers.c   |  4 +-
 tests/crypto-tls-x509-helpers.c  |  6 ++-
 tests/crypto-tls-x509-helpers.h  |  5 ++-
 tests/test-crypto-tlscredsx509.c | 47 +++-
 tests/test-crypto-tlssession.c   | 64 +++
 tests/test-io-channel-tls.c  | 49 +++-
 util/osdep.c | 16 +++
 util/oslib-win32.c   | 76 
 10 files changed, 197 insertions(+), 76 deletions(-)

diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
index e80fddd1e8..dd4c63e4b6 100644
--- a/include/qemu/osdep.h
+++ b/include/qemu/osdep.h
@@ -499,6 +499,7 @@ int qemu_mprotect_none(void *addr, size_t size);
 
 int qemu_open(const char *name, int flags, ...);
 int qemu_close(int fd);
+int qemu_link(const char *exist_path1, const char *new_path2);
 int qemu_unlink(const char *name);
 #ifndef _WIN32
 int qemu_dup(int fd);
diff --git a/include/sysemu/os-win32.h b/include/sysemu/os-win32.h
index 3ac8a53bac..8863833f67 100644
--- a/include/sysemu/os-win32.h
+++ b/include/sysemu/os-win32.h
@@ -194,4 +194,9 @@ ssize_t qemu_recv_wrap(int sockfd, void *buf, size_t len, 
int flags);
 ssize_t qemu_recvfrom_wrap(int sockfd, void *buf, size_t len, int flags,
struct sockaddr *addr, socklen_t *addrlen);
 
+#undef socketpair
+#define socketpair qemu_socketpair
+int qemu_socketpair(int family, int type, int protocol, int channel[2]);
+
+
 #endif
diff --git a/tests/crypto-tls-psk-helpers.c b/tests/crypto-tls-psk-helpers.c
index 11ae26368c..6f82bfceb2 100644
--- a/tests/crypto-tls-psk-helpers.c
+++ b/tests/crypto-tls-psk-helpers.c
@@ -26,6 +26,8 @@
 #include "crypto-tls-psk-helpers.h"
 #include "qemu/sockets.h"
 
+#include 
+
 #ifdef QCRYPTO_HAVE_TLS_TEST_SUPPORT
 
 void test_tls_psk_init(const char *pskfile)
@@ -44,7 +46,7 @@ void test_tls_psk_init(const char *pskfile)
 
 void test_tls_psk_cleanup(const char *pskfile)
 {
-unlink(pskfile);
+qemu_unlink(pskfile);
 }
 
 #endif /* QCRYPTO_HAVE_TLS_TEST_SUPPORT */
diff --git a/tests/crypto-tls-x509-helpers.c b/tests/crypto-tls-x509-helpers.c
index 01b3daf358..051f045657 100644
--- a/tests/crypto-tls-x509-helpers.c
+++ b/tests/crypto-tls-x509-helpers.c
@@ -23,6 +23,8 @@
 #include "crypto-tls-x509-helpers.h"
 #include "crypto/init.h"
 #include "qemu/sockets.h"
+#include 
+#include 
 
 #ifdef QCRYPTO_HAVE_TLS_TEST_SUPPORT
 
@@ -133,7 +135,7 @@ void test_tls_init(const char *keyfile)
 void test_tls_cleanup(const char *keyfile)
 {
 asn1_delete_structure(_asn1);
-unlink(keyfile);
+qemu_unlink(keyfile);
 }
 
 /*
@@ -501,7 +503,7 @@ void test_tls_discard_cert(QCryptoTLSTestCertReq *req)
 req->crt = NULL;
 
 if (getenv("QEMU_TEST_DEBUG_CERTS") == NULL) {
-unlink(req->filename);
+qemu_unlink(req->filename);
 }
 }
 
diff --git a/tests/crypto-tls-x509-helpers.h b/tests/crypto-tls-x509-helpers.h
index 08efba4e19..0856934a70 100644
--- a/tests/crypto-tls-x509-helpers.h
+++ b/tests/crypto-tls-x509-helpers.h
@@ -24,8 +24,9 @@
 #include 
 #include 
 
-#if !(defined WIN32) && \
-defined(CONFIG_TASN1)
+#include "qemu/osdep.h"
+
+#if defined(CONFIG_TASN1)
 # define QCRYPTO_HAVE_TLS_TEST_SUPPORT
 #endif
 
diff --git a/tests/test-crypto-tlscredsx509.c b/tests/test-crypto-tlscredsx509.c
index f487349c32..62d5d2defd 100644
--- a/tests/test-crypto-tlscredsx509.c
+++ b/tests/test-crypto-tlscredsx509.c
@@ -25,6 +25,9 @@
 #include "qapi/error.h"
 #include "qemu/module.h"
 
+#include 
+#include 
+
 #ifdef QCRYPTO_HAVE_TLS_TEST_SUPPORT
 
 #define WORKDIR "tests/test-crypto-tlscredsx509-work/"
@@ -77,34 +80,34 @@ static void test_tls_creds(const void *opaque)
 QCryptoTLSCreds *creds;
 
 #define CERT_DIR "tests/test-crypto-tlscredsx509-certs/"
-mkdir(CERT_DIR, 0700);
+g_mkdir_with_parents(CERT_DIR, 0700);
 
-unlink(CERT_DIR QCRYPTO_TLS_CREDS_X509_CA_CERT);
+qemu_unlink(CERT_DIR QCRYPTO_TLS_CREDS_X509_CA_CERT);
 if (data->isServer) {
-unlink(CERT_DIR QCRYPTO_TLS_CREDS_X509_SERVER_CERT);
-unlink(CERT_DIR QCRYPTO_TLS_CREDS_X509_SERVER_KEY);
+qemu_unlink(CERT_DIR QCRYPTO_TLS_CREDS_X509_SERVER_CERT);
+qemu_unlink(CERT_DIR 

[PATCH v10 08/26] tests: test-replication disable /replication/secondary/* on msys2/mingw.

2020-09-15 Thread Yonggang Luo
They caused failure on msys2/mingw, that's because file-win32.c not implement
.bdrv_reopen_prepare/commit/abort yet.

This is the error message:
> $ ./tests/test-replication.exe
> # random seed: R02S3f4d1c01af2b0a046990e0235c481faf
> 1..13
> # Start of replication tests
> # Start of primary tests
> ok 1 /replication/primary/read
> ok 2 /replication/primary/write
> ok 3 /replication/primary/start
> ok 4 /replication/primary/stop
> ok 5 /replication/primary/do_checkpoint
> ok 6 /replication/primary/get_error_all
> # End of primary tests
> # Start of secondary tests
> ok 7 /replication/secondary/read
> ok 8 /replication/secondary/write
> Unexpected error in bdrv_reopen_prepare() at ../block.c:4191:
> Block format 'file' used by node '#block4287' does not support reopening
> files

Signed-off-by: Yonggang Luo 
Reviewed-by: Daniel P. Berrangé 
---
 tests/test-replication.c | 4 
 1 file changed, 4 insertions(+)

diff --git a/tests/test-replication.c b/tests/test-replication.c
index e7cbd6b144..b067240add 100644
--- a/tests/test-replication.c
+++ b/tests/test-replication.c
@@ -392,6 +392,7 @@ static void test_secondary_write(void)
 teardown_secondary();
 }
 
+#ifndef _WIN32
 static void test_secondary_start(void)
 {
 BlockBackend *top_blk, *local_blk;
@@ -546,6 +547,7 @@ static void test_secondary_get_error_all(void)
 
 teardown_secondary();
 }
+#endif
 
 static void sigabrt_handler(int signo)
 {
@@ -597,6 +599,7 @@ int main(int argc, char **argv)
 /* Secondary */
 g_test_add_func("/replication/secondary/read",  test_secondary_read);
 g_test_add_func("/replication/secondary/write", test_secondary_write);
+#ifndef _WIN32
 g_test_add_func("/replication/secondary/start", test_secondary_start);
 g_test_add_func("/replication/secondary/stop",  test_secondary_stop);
 g_test_add_func("/replication/secondary/continuous_replication",
@@ -605,6 +608,7 @@ int main(int argc, char **argv)
 test_secondary_do_checkpoint);
 g_test_add_func("/replication/secondary/get_error_all",
 test_secondary_get_error_all);
+#endif
 
 ret = g_test_run();
 
-- 
2.28.0.windows.1




[PATCH v10 10/26] meson: Use -b to ignore CR vs. CR-LF issues on Windows

2020-09-15 Thread Yonggang Luo
Ideally we would use the '--strip-trailing-cr' option, but not
being POSIX is a portability problem (i.e. BSDs and Solaris
based OSes). Instead use the '-b' option which, although doing
slightly more, produce the expected result on Windows."

Signed-off-by: Yonggang Luo 
Reviewed-by: Eric Blake 
Reviewed-by: Daniel P. Berrangé 
---
 tests/qapi-schema/meson.build | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/qapi-schema/meson.build b/tests/qapi-schema/meson.build
index c87d141417..f1449298b0 100644
--- a/tests/qapi-schema/meson.build
+++ b/tests/qapi-schema/meson.build
@@ -220,6 +220,6 @@ qapi_doc = custom_target('QAPI doc',
 
 # "full_path()" needed here to work around
 # https://github.com/mesonbuild/meson/issues/7585
-test('QAPI doc', diff, args: ['-u', files('doc-good.texi'), 
qapi_doc[0].full_path()],
+test('QAPI doc', diff, args: ['-b', '-u', files('doc-good.texi'), 
qapi_doc[0].full_path()],
  depends: qapi_doc,
  suite: ['qapi-schema', 'qapi-doc'])
-- 
2.28.0.windows.1




[PATCH v10 07/26] tests: Fixes test-replication.c on msys2/mingw.

2020-09-15 Thread Yonggang Luo
On Windows there is no path like /tmp/s_local_disk.XX
Use g_get_tmp_dir instead of /tmp.

Signed-off-by: Yonggang Luo 
Reviewed-by: Daniel P. Berrangé 
---
 tests/test-replication.c | 18 ++
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/tests/test-replication.c b/tests/test-replication.c
index 9ab3666a90..e7cbd6b144 100644
--- a/tests/test-replication.c
+++ b/tests/test-replication.c
@@ -23,14 +23,14 @@
 
 /* primary */
 #define P_ID "primary-id"
-static char p_local_disk[] = "/tmp/p_local_disk.XX";
+static char *p_local_disk;
 
 /* secondary */
 #define S_ID "secondary-id"
 #define S_LOCAL_DISK_ID "secondary-local-disk-id"
-static char s_local_disk[] = "/tmp/s_local_disk.XX";
-static char s_active_disk[] = "/tmp/s_active_disk.XX";
-static char s_hidden_disk[] = "/tmp/s_hidden_disk.XX";
+static char *s_local_disk;
+static char *s_active_disk;
+static char *s_hidden_disk;
 
 /* FIXME: steal from blockdev.c */
 QemuOptsList qemu_drive_opts = {
@@ -571,6 +571,11 @@ static void setup_sigabrt_handler(void)
 int main(int argc, char **argv)
 {
 int ret;
+const char *tmpdir = g_get_tmp_dir();
+p_local_disk = g_strdup_printf("%s/p_local_disk.XX", tmpdir);
+s_local_disk = g_strdup_printf("%s/s_local_disk.XX", tmpdir);
+s_active_disk = g_strdup_printf("%s/s_active_disk.XX", tmpdir);
+s_hidden_disk = g_strdup_printf("%s/s_hidden_disk.XX", tmpdir);
 qemu_init_main_loop(_fatal);
 bdrv_init();
 
@@ -605,5 +610,10 @@ int main(int argc, char **argv)
 
 cleanup_imgs();
 
+g_free(p_local_disk);
+g_free(s_local_disk);
+g_free(s_active_disk);
+g_free(s_hidden_disk);
+
 return ret;
 }
-- 
2.28.0.windows.1




[PATCH v10 11/26] gcrypt: test_tls_psk_init should write binary file instead text file.

2020-09-15 Thread Yonggang Luo
On windows, if open file with "w", it's will automatically convert
"\n" to "\r\n" when writing to file.

Signed-off-by: Yonggang Luo 
---
 tests/crypto-tls-psk-helpers.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/crypto-tls-psk-helpers.c b/tests/crypto-tls-psk-helpers.c
index a8395477c3..11ae26368c 100644
--- a/tests/crypto-tls-psk-helpers.c
+++ b/tests/crypto-tls-psk-helpers.c
@@ -32,7 +32,7 @@ void test_tls_psk_init(const char *pskfile)
 {
 FILE *fp;
 
-fp = fopen(pskfile, "w");
+fp = fopen(pskfile, "wb");
 if (fp == NULL) {
 g_critical("Failed to create pskfile %s", pskfile);
 abort();
-- 
2.28.0.windows.1




[PATCH v10 14/26] vmstate: Fixes test-vmstate.c on msys2/mingw

2020-09-15 Thread Yonggang Luo
The vmstate are valid on win32, just need generate tmp path properly

Signed-off-by: Yonggang Luo 
Reviewed-by: Philippe Mathieu-Daudé 
Reviewed-by: Thomas Huth 
Reviewed-by: Daniel P. Berrangé 
---
 tests/test-vmstate.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tests/test-vmstate.c b/tests/test-vmstate.c
index 1c763015d0..ac38bfcfe8 100644
--- a/tests/test-vmstate.c
+++ b/tests/test-vmstate.c
@@ -34,7 +34,6 @@
 #include "qemu/module.h"
 #include "io/channel-file.h"
 
-static char temp_file[] = "/tmp/vmst.test.XX";
 static int temp_fd;
 
 
@@ -1484,6 +1483,8 @@ static void test_tmp_struct(void)
 
 int main(int argc, char **argv)
 {
+g_autofree char *temp_file = g_strdup_printf(
+"%s/vmst.test.XX", g_get_tmp_dir());
 temp_fd = mkstemp(temp_file);
 
 module_call_init(MODULE_INIT_QOM);
-- 
2.28.0.windows.1




[PATCH v10 15/26] cirrus: Building freebsd in a single short

2020-09-15 Thread Yonggang Luo
This reverts commit 45f7b7b9f38f5c4d1529a37c93dedfc26a231bba
("cirrus.yml: Split FreeBSD job into two parts").

freebsd 1 hour limit not hit anymore

I think we going to a wrong direction, I think there is some tests a stall the 
test runner,
please look at
https://cirrus-ci.com/task/5110577531977728
When its running properly, the consumed time are little, but when tests running 
too long,
look at the cpu usage, the cpu usage are nearly zero. doesn't consuming time.

And look at
https://cirrus-ci.com/task/6119341601062912

If the tests running properly, the time consuming are little
We should not hide the error by split them

Signed-off-by: Yonggang Luo 
Reviewed-by: Daniel P. Berrangé 
Reviewed-by: Ed Maste 
---
 .cirrus.yml | 35 ---
 1 file changed, 8 insertions(+), 27 deletions(-)

diff --git a/.cirrus.yml b/.cirrus.yml
index 3dd9fcff7f..25fb4add9b 100644
--- a/.cirrus.yml
+++ b/.cirrus.yml
@@ -1,38 +1,19 @@
 env:
   CIRRUS_CLONE_DEPTH: 1
 
-freebsd_1st_task:
+freebsd_12_task:
   freebsd_instance:
 image_family: freebsd-12-1
-cpu: 4
-memory: 4G
-  install_script: ASSUME_ALWAYS_YES=yes pkg bootstrap -f ; pkg install -y
-bash curl cyrus-sasl git glib gmake gnutls gsed
-nettle perl5 pixman pkgconf png usbredir
+cpu: 8
+memory: 8G
+  install_script:
+- ASSUME_ALWAYS_YES=yes pkg bootstrap -f ;
+- pkg install -y bash curl cyrus-sasl git glib gmake gnutls gsed
+  nettle perl5 pixman pkgconf png usbredir
   script:
 - mkdir build
 - cd build
-- ../configure --disable-user --target-list-exclude='alpha-softmmu
-ppc64-softmmu ppc-softmmu riscv32-softmmu riscv64-softmmu s390x-softmmu
-sparc64-softmmu sparc-softmmu x86_64-softmmu i386-softmmu'
---enable-werror || { cat config.log; exit 1; }
-- gmake -j$(sysctl -n hw.ncpu)
-- gmake -j$(sysctl -n hw.ncpu) check
-
-freebsd_2nd_task:
-  freebsd_instance:
-image_family: freebsd-12-1
-cpu: 4
-memory: 4G
-  install_script: ASSUME_ALWAYS_YES=yes pkg bootstrap -f ; pkg install -y
-bash curl cyrus-sasl git glib gmake gnutls gtk3 gsed libepoxy mesa-libs
-nettle perl5 pixman pkgconf png SDL2 usbredir
-  script:
-- ./configure --enable-werror --target-list='alpha-softmmu ppc64-softmmu
-ppc-softmmu riscv32-softmmu riscv64-softmmu s390x-softmmu
-sparc64-softmmu sparc-softmmu x86_64-softmmu i386-softmmu
-sparc-bsd-user sparc64-bsd-user x86_64-bsd-user i386-bsd-user'
-|| { cat config.log; exit 1; }
+- ../configure --enable-werror || { cat config.log; exit 1; }
 - gmake -j$(sysctl -n hw.ncpu)
 - gmake -j$(sysctl -n hw.ncpu) check
 
-- 
2.28.0.windows.1




[PATCH v10 13/26] meson: remove empty else and duplicated gio deps

2020-09-15 Thread Yonggang Luo
Signed-off-by: Yonggang Luo 
Reviewed-by: Daniel P. Berrangé 
---
 meson.build | 6 --
 1 file changed, 6 deletions(-)

diff --git a/meson.build b/meson.build
index 690723b470..23cb1b8742 100644
--- a/meson.build
+++ b/meson.build
@@ -317,7 +317,6 @@ opengl = not_found
 if 'CONFIG_OPENGL' in config_host
   opengl = declare_dependency(compile_args: 
config_host['OPENGL_CFLAGS'].split(),
   link_args: config_host['OPENGL_LIBS'].split())
-else
 endif
 gtk = not_found
 if 'CONFIG_GTK' in config_host
@@ -344,11 +343,6 @@ if 'CONFIG_ICONV' in config_host
   iconv = declare_dependency(compile_args: config_host['ICONV_CFLAGS'].split(),
  link_args: config_host['ICONV_LIBS'].split())
 endif
-gio = not_found
-if 'CONFIG_GIO' in config_host
-  gio = declare_dependency(compile_args: config_host['GIO_CFLAGS'].split(),
-   link_args: config_host['GIO_LIBS'].split())
-endif
 vnc = not_found
 png = not_found
 jpeg = not_found
-- 
2.28.0.windows.1




[PATCH v8 1/7] block: return error-code from bdrv_invalidate_cache

2020-09-15 Thread Vladimir Sementsov-Ogievskiy
This is the only coroutine wrapper from block.c and block/io.c which
doesn't return a value, so let's convert it to the common behavior, to
simplify moving to generated coroutine wrappers in a further commit.

Also, bdrv_invalidate_cache is a void function, returning error only
through **errp parameter, which is considered to be bad practice, as
it forces callers to define and propagate local_err variable, so
conversion is good anyway.

This patch leaves the conversion of .bdrv_co_invalidate_cache() driver
callbacks and bdrv_invalidate_cache_all() for another day.

Signed-off-by: Vladimir Sementsov-Ogievskiy 
Reviewed-by: Eric Blake 
---
 include/block/block.h |  2 +-
 block.c   | 32 ++--
 2 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/include/block/block.h b/include/block/block.h
index 6e36154061..8aef849a75 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -460,7 +460,7 @@ void bdrv_aio_cancel_async(BlockAIOCB *acb);
 int bdrv_co_ioctl(BlockDriverState *bs, int req, void *buf);
 
 /* Invalidate any cached metadata used by image formats */
-void bdrv_invalidate_cache(BlockDriverState *bs, Error **errp);
+int bdrv_invalidate_cache(BlockDriverState *bs, Error **errp);
 void bdrv_invalidate_cache_all(Error **errp);
 int bdrv_inactivate_all(void);
 
diff --git a/block.c b/block.c
index 2ba76b2c36..ccfe1d851b 100644
--- a/block.c
+++ b/block.c
@@ -5649,8 +5649,8 @@ void bdrv_init_with_whitelist(void)
 bdrv_init();
 }
 
-static void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs,
-  Error **errp)
+static int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs,
+ Error **errp)
 {
 BdrvChild *child, *parent;
 uint64_t perm, shared_perm;
@@ -5659,14 +5659,14 @@ static void coroutine_fn 
bdrv_co_invalidate_cache(BlockDriverState *bs,
 BdrvDirtyBitmap *bm;
 
 if (!bs->drv)  {
-return;
+return -ENOMEDIUM;
 }
 
 QLIST_FOREACH(child, >children, next) {
 bdrv_co_invalidate_cache(child->bs, _err);
 if (local_err) {
 error_propagate(errp, local_err);
-return;
+return -EINVAL;
 }
 }
 
@@ -5689,7 +5689,7 @@ static void coroutine_fn 
bdrv_co_invalidate_cache(BlockDriverState *bs,
 ret = bdrv_check_perm(bs, NULL, perm, shared_perm, NULL, NULL, errp);
 if (ret < 0) {
 bs->open_flags |= BDRV_O_INACTIVE;
-return;
+return ret;
 }
 bdrv_set_perm(bs, perm, shared_perm);
 
@@ -5698,7 +5698,7 @@ static void coroutine_fn 
bdrv_co_invalidate_cache(BlockDriverState *bs,
 if (local_err) {
 bs->open_flags |= BDRV_O_INACTIVE;
 error_propagate(errp, local_err);
-return;
+return -EINVAL;
 }
 }
 
@@ -5710,7 +5710,7 @@ static void coroutine_fn 
bdrv_co_invalidate_cache(BlockDriverState *bs,
 if (ret < 0) {
 bs->open_flags |= BDRV_O_INACTIVE;
 error_setg_errno(errp, -ret, "Could not refresh total sector 
count");
-return;
+return ret;
 }
 }
 
@@ -5720,27 +5720,30 @@ static void coroutine_fn 
bdrv_co_invalidate_cache(BlockDriverState *bs,
 if (local_err) {
 bs->open_flags |= BDRV_O_INACTIVE;
 error_propagate(errp, local_err);
-return;
+return -EINVAL;
 }
 }
 }
+
+return 0;
 }
 
 typedef struct InvalidateCacheCo {
 BlockDriverState *bs;
 Error **errp;
 bool done;
+int ret;
 } InvalidateCacheCo;
 
 static void coroutine_fn bdrv_invalidate_cache_co_entry(void *opaque)
 {
 InvalidateCacheCo *ico = opaque;
-bdrv_co_invalidate_cache(ico->bs, ico->errp);
+ico->ret = bdrv_co_invalidate_cache(ico->bs, ico->errp);
 ico->done = true;
 aio_wait_kick();
 }
 
-void bdrv_invalidate_cache(BlockDriverState *bs, Error **errp)
+int bdrv_invalidate_cache(BlockDriverState *bs, Error **errp)
 {
 Coroutine *co;
 InvalidateCacheCo ico = {
@@ -5757,22 +5760,23 @@ void bdrv_invalidate_cache(BlockDriverState *bs, Error 
**errp)
 bdrv_coroutine_enter(bs, co);
 BDRV_POLL_WHILE(bs, !ico.done);
 }
+
+return ico.ret;
 }
 
 void bdrv_invalidate_cache_all(Error **errp)
 {
 BlockDriverState *bs;
-Error *local_err = NULL;
 BdrvNextIterator it;
 
 for (bs = bdrv_first(); bs; bs = bdrv_next()) {
 AioContext *aio_context = bdrv_get_aio_context(bs);
+int ret;
 
 aio_context_acquire(aio_context);
-bdrv_invalidate_cache(bs, _err);
+ret = bdrv_invalidate_cache(bs, errp);
 aio_context_release(aio_context);
-if (local_err) {
-error_propagate(errp, local_err);
+if (ret < 0) {
 bdrv_next_cleanup();
   

[PATCH v8 7/7] block/io: refactor save/load vmstate

2020-09-15 Thread Vladimir Sementsov-Ogievskiy
Like for read/write in a previous commit, drop extra indirection layer,
generate directly bdrv_readv_vmstate() and bdrv_writev_vmstate().

Signed-off-by: Vladimir Sementsov-Ogievskiy 
Reviewed-by: Eric Blake 
---
 block/coroutines.h| 10 +++
 include/block/block.h |  6 ++--
 block/io.c| 67 ++-
 3 files changed, 42 insertions(+), 41 deletions(-)

diff --git a/block/coroutines.h b/block/coroutines.h
index 6c63a819c9..f69179f5ef 100644
--- a/block/coroutines.h
+++ b/block/coroutines.h
@@ -57,11 +57,9 @@ bdrv_common_block_status_above(BlockDriverState *bs,
int64_t *map,
BlockDriverState **file);
 
-int coroutine_fn
-bdrv_co_rw_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos,
-   bool is_read);
-int generated_co_wrapper
-bdrv_rw_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos,
-bool is_read);
+int coroutine_fn bdrv_co_readv_vmstate(BlockDriverState *bs,
+   QEMUIOVector *qiov, int64_t pos);
+int coroutine_fn bdrv_co_writev_vmstate(BlockDriverState *bs,
+QEMUIOVector *qiov, int64_t pos);
 
 #endif /* BLOCK_COROUTINES_INT_H */
diff --git a/include/block/block.h b/include/block/block.h
index b8b4c177de..6cd789724b 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -572,8 +572,10 @@ int path_has_protocol(const char *path);
 int path_is_absolute(const char *path);
 char *path_combine(const char *base_path, const char *filename);
 
-int bdrv_readv_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos);
-int bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos);
+int generated_co_wrapper
+bdrv_readv_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos);
+int generated_co_wrapper
+bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos);
 int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
   int64_t pos, int size);
 
diff --git a/block/io.c b/block/io.c
index 68d7d9cf80..84f82bc069 100644
--- a/block/io.c
+++ b/block/io.c
@@ -2491,66 +2491,67 @@ int bdrv_is_allocated_above(BlockDriverState *top,
 }
 
 int coroutine_fn
-bdrv_co_rw_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos,
-   bool is_read)
+bdrv_co_readv_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos)
 {
 BlockDriver *drv = bs->drv;
 int ret = -ENOTSUP;
 
+if (!drv) {
+return -ENOMEDIUM;
+}
+
 bdrv_inc_in_flight(bs);
 
-if (!drv) {
-ret = -ENOMEDIUM;
-} else if (drv->bdrv_load_vmstate) {
-if (is_read) {
-ret = drv->bdrv_load_vmstate(bs, qiov, pos);
-} else {
-ret = drv->bdrv_save_vmstate(bs, qiov, pos);
-}
+if (drv->bdrv_load_vmstate) {
+ret = drv->bdrv_load_vmstate(bs, qiov, pos);
 } else if (bs->file) {
-ret = bdrv_co_rw_vmstate(bs->file->bs, qiov, pos, is_read);
+ret = bdrv_co_readv_vmstate(bs->file->bs, qiov, pos);
 }
 
 bdrv_dec_in_flight(bs);
+
 return ret;
 }
 
-int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
-  int64_t pos, int size)
+int coroutine_fn
+bdrv_co_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos)
 {
-QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, size);
-int ret;
+BlockDriver *drv = bs->drv;
+int ret = -ENOTSUP;
 
-ret = bdrv_writev_vmstate(bs, , pos);
-if (ret < 0) {
-return ret;
+if (!drv) {
+return -ENOMEDIUM;
 }
 
-return size;
-}
+bdrv_inc_in_flight(bs);
 
-int bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos)
-{
-return bdrv_rw_vmstate(bs, qiov, pos, false);
+if (drv->bdrv_load_vmstate) {
+ret = drv->bdrv_save_vmstate(bs, qiov, pos);
+} else if (bs->file) {
+ret = bdrv_co_writev_vmstate(bs->file->bs, qiov, pos);
+}
+
+bdrv_dec_in_flight(bs);
+
+return ret;
 }
 
-int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
+int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
   int64_t pos, int size)
 {
 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, size);
-int ret;
-
-ret = bdrv_readv_vmstate(bs, , pos);
-if (ret < 0) {
-return ret;
-}
+int ret = bdrv_writev_vmstate(bs, , pos);
 
-return size;
+return ret < 0 ? ret : size;
 }
 
-int bdrv_readv_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos)
+int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
+  int64_t pos, int size)
 {
-return bdrv_rw_vmstate(bs, qiov, pos, true);
+QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, size);
+int ret = bdrv_readv_vmstate(bs, , pos);
+
+return ret < 0 ? ret : size;
 }
 
 

[PATCH v8 5/7] block: generate coroutine-wrapper code

2020-09-15 Thread Vladimir Sementsov-Ogievskiy
Use code generation implemented in previous commit to generated
coroutine wrappers in block.c and block/io.c

Signed-off-by: Vladimir Sementsov-Ogievskiy 
Reviewed-by: Eric Blake 
---
 block/coroutines.h|   6 +-
 include/block/block.h |  16 ++--
 block.c   |  73 ---
 block/io.c| 212 --
 4 files changed, 13 insertions(+), 294 deletions(-)

diff --git a/block/coroutines.h b/block/coroutines.h
index 9ce1730a09..c62b3a2697 100644
--- a/block/coroutines.h
+++ b/block/coroutines.h
@@ -34,7 +34,7 @@ int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState 
*bs, Error **errp);
 int coroutine_fn
 bdrv_co_prwv(BdrvChild *child, int64_t offset, QEMUIOVector *qiov,
  bool is_write, BdrvRequestFlags flags);
-int
+int generated_co_wrapper
 bdrv_prwv(BdrvChild *child, int64_t offset, QEMUIOVector *qiov,
   bool is_write, BdrvRequestFlags flags);
 
@@ -47,7 +47,7 @@ bdrv_co_common_block_status_above(BlockDriverState *bs,
   int64_t *pnum,
   int64_t *map,
   BlockDriverState **file);
-int
+int generated_co_wrapper
 bdrv_common_block_status_above(BlockDriverState *bs,
BlockDriverState *base,
bool want_zero,
@@ -60,7 +60,7 @@ bdrv_common_block_status_above(BlockDriverState *bs,
 int coroutine_fn
 bdrv_co_rw_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos,
bool is_read);
-int
+int generated_co_wrapper
 bdrv_rw_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos,
 bool is_read);
 
diff --git a/include/block/block.h b/include/block/block.h
index a0655b84d6..d8fb02fa2a 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -403,8 +403,9 @@ void bdrv_refresh_filename(BlockDriverState *bs);
 int coroutine_fn bdrv_co_truncate(BdrvChild *child, int64_t offset, bool exact,
   PreallocMode prealloc, BdrvRequestFlags 
flags,
   Error **errp);
-int bdrv_truncate(BdrvChild *child, int64_t offset, bool exact,
-  PreallocMode prealloc, BdrvRequestFlags flags, Error **errp);
+int generated_co_wrapper
+bdrv_truncate(BdrvChild *child, int64_t offset, bool exact,
+  PreallocMode prealloc, BdrvRequestFlags flags, Error **errp);
 
 int64_t bdrv_nb_sectors(BlockDriverState *bs);
 int64_t bdrv_getlength(BlockDriverState *bs);
@@ -446,7 +447,8 @@ typedef enum {
 BDRV_FIX_ERRORS   = 2,
 } BdrvCheckMode;
 
-int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix);
+int generated_co_wrapper bdrv_check(BlockDriverState *bs, BdrvCheckResult *res,
+BdrvCheckMode fix);
 
 /* The units of offset and total_work_size may be chosen arbitrarily by the
  * block driver; total_work_size may change during the course of the amendment
@@ -470,12 +472,13 @@ void bdrv_aio_cancel_async(BlockAIOCB *acb);
 int bdrv_co_ioctl(BlockDriverState *bs, int req, void *buf);
 
 /* Invalidate any cached metadata used by image formats */
-int bdrv_invalidate_cache(BlockDriverState *bs, Error **errp);
+int generated_co_wrapper bdrv_invalidate_cache(BlockDriverState *bs,
+   Error **errp);
 void bdrv_invalidate_cache_all(Error **errp);
 int bdrv_inactivate_all(void);
 
 /* Ensure contents are flushed to disk.  */
-int bdrv_flush(BlockDriverState *bs);
+int generated_co_wrapper bdrv_flush(BlockDriverState *bs);
 int coroutine_fn bdrv_co_flush(BlockDriverState *bs);
 int bdrv_flush_all(void);
 void bdrv_close_all(void);
@@ -490,7 +493,8 @@ void bdrv_drain_all(void);
 AIO_WAIT_WHILE(bdrv_get_aio_context(bs_),  \
cond); })
 
-int bdrv_pdiscard(BdrvChild *child, int64_t offset, int64_t bytes);
+int generated_co_wrapper bdrv_pdiscard(BdrvChild *child, int64_t offset,
+   int64_t bytes);
 int bdrv_co_pdiscard(BdrvChild *child, int64_t offset, int64_t bytes);
 int bdrv_has_zero_init_1(BlockDriverState *bs);
 int bdrv_has_zero_init(BlockDriverState *bs);
diff --git a/block.c b/block.c
index ec5a8cbd7b..d49d591917 100644
--- a/block.c
+++ b/block.c
@@ -4655,43 +4655,6 @@ int coroutine_fn bdrv_co_check(BlockDriverState *bs,
 return bs->drv->bdrv_co_check(bs, res, fix);
 }
 
-typedef struct CheckCo {
-BlockDriverState *bs;
-BdrvCheckResult *res;
-BdrvCheckMode fix;
-int ret;
-} CheckCo;
-
-static void coroutine_fn bdrv_check_co_entry(void *opaque)
-{
-CheckCo *cco = opaque;
-cco->ret = bdrv_co_check(cco->bs, cco->res, cco->fix);
-aio_wait_kick();
-}
-
-int bdrv_check(BlockDriverState *bs,
-   BdrvCheckResult *res, BdrvCheckMode fix)
-{
-Coroutine *co;
-CheckCo cco = {
-.bs = bs,
-.res = res,
-.ret = -EINPROGRESS,
-.fix 

[PATCH v8 4/7] scripts: add block-coroutine-wrapper.py

2020-09-15 Thread Vladimir Sementsov-Ogievskiy
We have a very frequent pattern of creating coroutine from function
with several arguments:

  - create structure to pack parameters
  - create _entry function to call original function taking parameters
from struct
  - do different magic to handle completion: set ret to NOT_DONE or
EINPROGRESS or use separate bool field
  - fill the struct and create coroutine from _entry function and this
struct as a parameter
  - do coroutine enter and BDRV_POLL_WHILE loop

Let's reduce code duplication by generating coroutine wrappers.

This patch adds scripts/block-coroutine-wrapper.py together with some
friends, which will generate functions with declared prototypes marked
by 'generated_co_wrapper' specifier.

The usage of new code generation is as follows:

1. define somewhere

int coroutine_fn bdrv_co_NAME(...) {...}

   function

2. declare in some header file

int generated_co_wrapper bdrv_NAME(...);

   function with same list of parameters. (you'll need to include
   "block/generated-co-wrapper.h" to get the specifier)

3. both declarations should be available through block/coroutines.h
   header.

4. add header with generated_co_wrapper declaration into
   COROUTINE_HEADERS list in Makefile

Still, no function is now marked, this work is for the following
commit.

Signed-off-by: Vladimir Sementsov-Ogievskiy 
---
 docs/devel/block-coroutine-wrapper.rst |  54 +++
 block/block-gen.h  |  49 +++
 include/block/block.h  |  10 ++
 block/meson.build  |   8 ++
 scripts/block-coroutine-wrapper.py | 187 +
 5 files changed, 308 insertions(+)
 create mode 100644 docs/devel/block-coroutine-wrapper.rst
 create mode 100644 block/block-gen.h
 create mode 100755 scripts/block-coroutine-wrapper.py

diff --git a/docs/devel/block-coroutine-wrapper.rst 
b/docs/devel/block-coroutine-wrapper.rst
new file mode 100644
index 00..f7050bbc8f
--- /dev/null
+++ b/docs/devel/block-coroutine-wrapper.rst
@@ -0,0 +1,54 @@
+===
+block-coroutine-wrapper
+===
+
+A lot of functions in QEMJ block layer (see ``block/*``) can by called
+only in coroutine context. Such functions are normally marked by
+coroutine_fn specifier. Still, sometimes we need to call them from
+non-coroutine context, for this we need to start a coroutine, run the
+needed function from it and wait for coroutine finish in
+BDRV_POLL_WHILE() loop. To run a coroutine we need a function with one
+void* argument. So for each coroutine_fn function, which needs
+non-coroutine interface, we should define a structure to pack the
+parameters, define a separate function to unpack the parameters and
+call the original function and finally define a new interface function
+with same list of arguments as original one, which will pack the
+parameters into a struct, create a coroutine, run it and wait in
+BDRV_POLL_WHILE() loop. It's boring to create such wrappers by hand, so
+we have a script to generate them.
+
+Usage
+=
+
+Assume we have defined ``coroutine_fn`` function
+``bdrv_co_foo()`` and need a non-coroutine interface for it,
+called ``bdrv_foo()``. In this case the script can help. To
+trigger the generation:
+
+1. You need ``bdrv_foo`` declaration somewhere (for example in
+   ``block/coroutines.h`` with ``generated_co_wrapper`` mark,
+   like this:
+
+.. code-block:: c
+
+int generated_co_wrapper bdrv_foor();
+
+2. You need to feed this declaration to block-coroutine-wrapper script.
+   For this, add .h (or .c) file with the declaration to
+   ``input: files(...)`` list of ``block_gen_c`` target declaration in
+   ``block/meson.build``
+
+You are done. On build, coroutine wrappers will be generated in
+``/block/block-gen.c``.
+
+Links
+=
+
+1. The script location is ``scripts/block-coroutine-wrapper.py``.
+
+2. Generic place for private ``generated_co_wrapper`` declarations is
+   ``block/coroutines.h``, for public declarations:
+   ``include/block/block.h``
+
+3. The core API of generated coroutine wrappers is placed in
+   (not generated) ``block/block-gen.h``
diff --git a/block/block-gen.h b/block/block-gen.h
new file mode 100644
index 00..f80cf4897d
--- /dev/null
+++ b/block/block-gen.h
@@ -0,0 +1,49 @@
+/*
+ * Block coroutine wrapping core, used by auto-generated block/block-gen.c
+ *
+ * Copyright (c) 2003 Fabrice Bellard
+ * Copyright (c) 2020 Virtuozzo International GmbH
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to 
deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright 

[PATCH v8 6/7] block: drop bdrv_prwv

2020-09-15 Thread Vladimir Sementsov-Ogievskiy
Now that we are not maintaining boilerplate code for coroutine
wrappers, there is no more sense in keeping the extra indirection layer
of bdrv_prwv().  Let's drop it and instead generate pure bdrv_preadv()
and bdrv_pwritev().

Currently, bdrv_pwritev() and bdrv_preadv() are returning bytes on
success, auto generated functions will instead return zero, as their
_co_ prototype. Still, it's simple to make the conversion safe: the
only external user of bdrv_pwritev() is test-bdrv-drain, and it is
comfortable enough with bdrv_co_pwritev() instead. So prototypes are
moved to local block/coroutines.h. Next, the only internal use is
bdrv_pread() and bdrv_pwrite(), which are modified to return bytes on
success.

Of course, it would be great to convert bdrv_pread() and bdrv_pwrite()
to return 0 on success. But this requires audit (and probably
conversion) of all their users, let's leave it for another day
refactoring.

Signed-off-by: Vladimir Sementsov-Ogievskiy 
Reviewed-by: Eric Blake 
---
 block/coroutines.h  | 10 -
 include/block/block.h   |  2 --
 block/io.c  | 49 -
 tests/test-bdrv-drain.c |  2 +-
 4 files changed, 15 insertions(+), 48 deletions(-)

diff --git a/block/coroutines.h b/block/coroutines.h
index c62b3a2697..6c63a819c9 100644
--- a/block/coroutines.h
+++ b/block/coroutines.h
@@ -31,12 +31,12 @@ int coroutine_fn bdrv_co_check(BlockDriverState *bs,
BdrvCheckResult *res, BdrvCheckMode fix);
 int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp);
 
-int coroutine_fn
-bdrv_co_prwv(BdrvChild *child, int64_t offset, QEMUIOVector *qiov,
- bool is_write, BdrvRequestFlags flags);
 int generated_co_wrapper
-bdrv_prwv(BdrvChild *child, int64_t offset, QEMUIOVector *qiov,
-  bool is_write, BdrvRequestFlags flags);
+bdrv_preadv(BdrvChild *child, int64_t offset, unsigned int bytes,
+QEMUIOVector *qiov, BdrvRequestFlags flags);
+int generated_co_wrapper
+bdrv_pwritev(BdrvChild *child, int64_t offset, unsigned int bytes,
+ QEMUIOVector *qiov, BdrvRequestFlags flags);
 
 int coroutine_fn
 bdrv_co_common_block_status_above(BlockDriverState *bs,
diff --git a/include/block/block.h b/include/block/block.h
index d8fb02fa2a..b8b4c177de 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -383,9 +383,7 @@ int bdrv_pwrite_zeroes(BdrvChild *child, int64_t offset,
int bytes, BdrvRequestFlags flags);
 int bdrv_make_zero(BdrvChild *child, BdrvRequestFlags flags);
 int bdrv_pread(BdrvChild *child, int64_t offset, void *buf, int bytes);
-int bdrv_preadv(BdrvChild *child, int64_t offset, QEMUIOVector *qiov);
 int bdrv_pwrite(BdrvChild *child, int64_t offset, const void *buf, int bytes);
-int bdrv_pwritev(BdrvChild *child, int64_t offset, QEMUIOVector *qiov);
 int bdrv_pwrite_sync(BdrvChild *child, int64_t offset,
  const void *buf, int count);
 /*
diff --git a/block/io.c b/block/io.c
index 5270d68d72..68d7d9cf80 100644
--- a/block/io.c
+++ b/block/io.c
@@ -890,23 +890,11 @@ static int bdrv_check_byte_request(BlockDriverState *bs, 
int64_t offset,
 return 0;
 }
 
-int coroutine_fn bdrv_co_prwv(BdrvChild *child, int64_t offset,
-  QEMUIOVector *qiov, bool is_write,
-  BdrvRequestFlags flags)
-{
-if (is_write) {
-return bdrv_co_pwritev(child, offset, qiov->size, qiov, flags);
-} else {
-return bdrv_co_preadv(child, offset, qiov->size, qiov, flags);
-}
-}
-
 int bdrv_pwrite_zeroes(BdrvChild *child, int64_t offset,
int bytes, BdrvRequestFlags flags)
 {
-QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, bytes);
-
-return bdrv_prwv(child, offset, , true, BDRV_REQ_ZERO_WRITE | flags);
+return bdrv_pwritev(child, offset, bytes, NULL,
+BDRV_REQ_ZERO_WRITE | flags);
 }
 
 /*
@@ -950,41 +938,19 @@ int bdrv_make_zero(BdrvChild *child, BdrvRequestFlags 
flags)
 }
 }
 
-/* return < 0 if error. See bdrv_pwrite() for the return codes */
-int bdrv_preadv(BdrvChild *child, int64_t offset, QEMUIOVector *qiov)
-{
-int ret;
-
-ret = bdrv_prwv(child, offset, qiov, false, 0);
-if (ret < 0) {
-return ret;
-}
-
-return qiov->size;
-}
-
 /* See bdrv_pwrite() for the return codes */
 int bdrv_pread(BdrvChild *child, int64_t offset, void *buf, int bytes)
 {
+int ret;
 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, bytes);
 
 if (bytes < 0) {
 return -EINVAL;
 }
 
-return bdrv_preadv(child, offset, );
-}
-
-int bdrv_pwritev(BdrvChild *child, int64_t offset, QEMUIOVector *qiov)
-{
-int ret;
+ret = bdrv_preadv(child, offset, bytes, ,  0);
 
-ret = bdrv_prwv(child, offset, qiov, true, 0);
-if (ret < 0) {
-return ret;
-}
-
-return qiov->size;
+return ret < 0 ? ret : bytes;
 }
 
 /* Return no. of bytes 

[PATCH v8 3/7] block: declare some coroutine functions in block/coroutines.h

2020-09-15 Thread Vladimir Sementsov-Ogievskiy
We are going to keep coroutine-wrappers code (structure-packing
parameters, BDRV_POLL wrapper functions) in separate auto-generated
files. So, we'll need a header with declaration of original _co_
functions, for those which are static now. As well, we'll need
declarations for wrapper functions. Do these declarations now, as a
preparation step.

Signed-off-by: Vladimir Sementsov-Ogievskiy 
Reviewed-by: Eric Blake 
---
 block/coroutines.h | 67 ++
 block.c|  8 +++---
 block/io.c | 34 +++
 3 files changed, 88 insertions(+), 21 deletions(-)
 create mode 100644 block/coroutines.h

diff --git a/block/coroutines.h b/block/coroutines.h
new file mode 100644
index 00..9ce1730a09
--- /dev/null
+++ b/block/coroutines.h
@@ -0,0 +1,67 @@
+/*
+ * Block layer I/O functions
+ *
+ * Copyright (c) 2003 Fabrice Bellard
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to 
deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef BLOCK_COROUTINES_INT_H
+#define BLOCK_COROUTINES_INT_H
+
+#include "block/block_int.h"
+
+int coroutine_fn bdrv_co_check(BlockDriverState *bs,
+   BdrvCheckResult *res, BdrvCheckMode fix);
+int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp);
+
+int coroutine_fn
+bdrv_co_prwv(BdrvChild *child, int64_t offset, QEMUIOVector *qiov,
+ bool is_write, BdrvRequestFlags flags);
+int
+bdrv_prwv(BdrvChild *child, int64_t offset, QEMUIOVector *qiov,
+  bool is_write, BdrvRequestFlags flags);
+
+int coroutine_fn
+bdrv_co_common_block_status_above(BlockDriverState *bs,
+  BlockDriverState *base,
+  bool want_zero,
+  int64_t offset,
+  int64_t bytes,
+  int64_t *pnum,
+  int64_t *map,
+  BlockDriverState **file);
+int
+bdrv_common_block_status_above(BlockDriverState *bs,
+   BlockDriverState *base,
+   bool want_zero,
+   int64_t offset,
+   int64_t bytes,
+   int64_t *pnum,
+   int64_t *map,
+   BlockDriverState **file);
+
+int coroutine_fn
+bdrv_co_rw_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos,
+   bool is_read);
+int
+bdrv_rw_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos,
+bool is_read);
+
+#endif /* BLOCK_COROUTINES_INT_H */
diff --git a/block.c b/block.c
index ccfe1d851b..ec5a8cbd7b 100644
--- a/block.c
+++ b/block.c
@@ -48,6 +48,7 @@
 #include "qemu/timer.h"
 #include "qemu/cutils.h"
 #include "qemu/id.h"
+#include "block/coroutines.h"
 
 #ifdef CONFIG_BSD
 #include 
@@ -4640,8 +4641,8 @@ static void bdrv_delete(BlockDriverState *bs)
  * free of errors) or -errno when an internal error occurred. The results of 
the
  * check are stored in res.
  */
-static int coroutine_fn bdrv_co_check(BlockDriverState *bs,
-  BdrvCheckResult *res, BdrvCheckMode fix)
+int coroutine_fn bdrv_co_check(BlockDriverState *bs,
+   BdrvCheckResult *res, BdrvCheckMode fix)
 {
 if (bs->drv == NULL) {
 return -ENOMEDIUM;
@@ -5649,8 +5650,7 @@ void bdrv_init_with_whitelist(void)
 bdrv_init();
 }
 
-static int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs,
- Error **errp)
+int coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **errp)
 {
 BdrvChild *child, *parent;
 uint64_t perm, shared_perm;
diff --git a/block/io.c b/block/io.c
index 2e2c89ce31..676c932caf 100644
--- a/block/io.c
+++ b/block/io.c
@@ -29,6 +29,7 @@
 #include "block/blockjob.h"
 #include 

[PATCH v8 2/7] block/io: refactor coroutine wrappers

2020-09-15 Thread Vladimir Sementsov-Ogievskiy
Most of our coroutine wrappers already follow this convention:

We have 'coroutine_fn bdrv_co_()' as
the core function, and a wrapper 'bdrv_()' which does parameters packing and call bdrv_run_co().

The only outsiders are the bdrv_prwv_co and
bdrv_common_block_status_above wrappers. Let's refactor them to behave
as the others, it simplifies further conversion of coroutine wrappers.

This patch adds indirection layer, but it will be compensated by
further commit, which will drop bdrv_co_prwv together with is_write
logic, to keep read and write path separate.

Signed-off-by: Vladimir Sementsov-Ogievskiy 
Reviewed-by: Eric Blake 
---
 block/io.c | 60 +-
 1 file changed, 32 insertions(+), 28 deletions(-)

diff --git a/block/io.c b/block/io.c
index ad3a51ed53..2e2c89ce31 100644
--- a/block/io.c
+++ b/block/io.c
@@ -933,27 +933,31 @@ typedef struct RwCo {
 BdrvRequestFlags flags;
 } RwCo;
 
+static int coroutine_fn bdrv_co_prwv(BdrvChild *child, int64_t offset,
+ QEMUIOVector *qiov, bool is_write,
+ BdrvRequestFlags flags)
+{
+if (is_write) {
+return bdrv_co_pwritev(child, offset, qiov->size, qiov, flags);
+} else {
+return bdrv_co_preadv(child, offset, qiov->size, qiov, flags);
+}
+}
+
 static int coroutine_fn bdrv_rw_co_entry(void *opaque)
 {
 RwCo *rwco = opaque;
 
-if (!rwco->is_write) {
-return bdrv_co_preadv(rwco->child, rwco->offset,
-  rwco->qiov->size, rwco->qiov,
-  rwco->flags);
-} else {
-return bdrv_co_pwritev(rwco->child, rwco->offset,
-   rwco->qiov->size, rwco->qiov,
-   rwco->flags);
-}
+return bdrv_co_prwv(rwco->child, rwco->offset, rwco->qiov,
+rwco->is_write, rwco->flags);
 }
 
 /*
  * Process a vectored synchronous request using coroutines
  */
-static int bdrv_prwv_co(BdrvChild *child, int64_t offset,
-QEMUIOVector *qiov, bool is_write,
-BdrvRequestFlags flags)
+static int bdrv_prwv(BdrvChild *child, int64_t offset,
+ QEMUIOVector *qiov, bool is_write,
+ BdrvRequestFlags flags)
 {
 RwCo rwco = {
 .child = child,
@@ -971,8 +975,7 @@ int bdrv_pwrite_zeroes(BdrvChild *child, int64_t offset,
 {
 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, bytes);
 
-return bdrv_prwv_co(child, offset, , true,
-BDRV_REQ_ZERO_WRITE | flags);
+return bdrv_prwv(child, offset, , true, BDRV_REQ_ZERO_WRITE | flags);
 }
 
 /*
@@ -1021,7 +1024,7 @@ int bdrv_preadv(BdrvChild *child, int64_t offset, 
QEMUIOVector *qiov)
 {
 int ret;
 
-ret = bdrv_prwv_co(child, offset, qiov, false, 0);
+ret = bdrv_prwv(child, offset, qiov, false, 0);
 if (ret < 0) {
 return ret;
 }
@@ -1045,7 +1048,7 @@ int bdrv_pwritev(BdrvChild *child, int64_t offset, 
QEMUIOVector *qiov)
 {
 int ret;
 
-ret = bdrv_prwv_co(child, offset, qiov, true, 0);
+ret = bdrv_prwv(child, offset, qiov, true, 0);
 if (ret < 0) {
 return ret;
 }
@@ -2465,14 +2468,15 @@ early_out:
 return ret;
 }
 
-static int coroutine_fn bdrv_co_block_status_above(BlockDriverState *bs,
-   BlockDriverState *base,
-   bool want_zero,
-   int64_t offset,
-   int64_t bytes,
-   int64_t *pnum,
-   int64_t *map,
-   BlockDriverState **file)
+static int coroutine_fn
+bdrv_co_common_block_status_above(BlockDriverState *bs,
+  BlockDriverState *base,
+  bool want_zero,
+  int64_t offset,
+  int64_t bytes,
+  int64_t *pnum,
+  int64_t *map,
+  BlockDriverState **file)
 {
 BlockDriverState *p;
 int ret = 0;
@@ -2510,10 +2514,10 @@ static int coroutine_fn 
bdrv_block_status_above_co_entry(void *opaque)
 {
 BdrvCoBlockStatusData *data = opaque;
 
-return bdrv_co_block_status_above(data->bs, data->base,
-  data->want_zero,
-  data->offset, data->bytes,
-  data->pnum, data->map, data->file);
+return bdrv_co_common_block_status_above(data->bs, data->base,
+ data->want_zero,
+ data->offset, data->bytes,
+

[PATCH v8 0/7] coroutines: generate wrapper code

2020-09-15 Thread Vladimir Sementsov-Ogievskiy
Hi all!

The aim of the series is to reduce code-duplication and writing
parameters structure-packing by hand around coroutine function wrappers.

Benefits:
 - no code duplication
 - less indirection

v8:
04: - rebase on meson build
- script interface is changed to satisfy meson custom_target
- rename script s/coroutine-wrapper.py/block-coroutine-wrapper.py/
- add docs/devel/block-coroutine-wrapper.rst

Vladimir Sementsov-Ogievskiy (7):
  block: return error-code from bdrv_invalidate_cache
  block/io: refactor coroutine wrappers
  block: declare some coroutine functions in block/coroutines.h
  scripts: add block-coroutine-wrapper.py
  block: generate coroutine-wrapper code
  block: drop bdrv_prwv
  block/io: refactor save/load vmstate

 docs/devel/block-coroutine-wrapper.rst |  54 
 block/block-gen.h  |  49 
 block/coroutines.h |  65 +
 include/block/block.h  |  34 ++-
 block.c|  97 ++-
 block/io.c | 336 -
 tests/test-bdrv-drain.c|   2 +-
 block/meson.build  |   8 +
 scripts/block-coroutine-wrapper.py | 187 ++
 9 files changed, 451 insertions(+), 381 deletions(-)
 create mode 100644 docs/devel/block-coroutine-wrapper.rst
 create mode 100644 block/block-gen.h
 create mode 100644 block/coroutines.h
 create mode 100755 scripts/block-coroutine-wrapper.py

-- 
2.21.3




Re: [PATCH v7 11/13] util/async: Add aio_co_reschedule_self()

2020-09-15 Thread Stefan Hajnoczi
On Wed, Sep 09, 2020 at 05:11:47PM +0200, Kevin Wolf wrote:
> Add a function that can be used to move the currently running coroutine
> to a different AioContext (and therefore potentially a different
> thread).
> 
> Signed-off-by: Kevin Wolf 
> ---
>  include/block/aio.h | 10 ++
>  util/async.c| 30 ++
>  2 files changed, 40 insertions(+)

Reviewed-by: Stefan Hajnoczi 


signature.asc
Description: PGP signature


Re: [PATCH v7 13/13] block: Convert 'block_resize' to coroutine

2020-09-15 Thread Stefan Hajnoczi
On Wed, Sep 09, 2020 at 05:11:49PM +0200, Kevin Wolf wrote:
> @@ -2456,8 +2456,7 @@ void qmp_block_resize(bool has_device, const char 
> *device,
>  return;
>  }
>  
> -aio_context = bdrv_get_aio_context(bs);
> -aio_context_acquire(aio_context);
> +old_ctx = bdrv_co_move_to_aio_context(bs);
>  
>  if (size < 0) {
>  error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");

Is it safe to call blk_new() outside the BQL since it mutates global state?

In other words, could another thread race with us?

> @@ -2479,8 +2478,8 @@ void qmp_block_resize(bool has_device, const char 
> *device,
>  bdrv_drained_end(bs);
>  
>  out:
> +aio_co_reschedule_self(old_ctx);
>  blk_unref(blk);
> -aio_context_release(aio_context);

The following precondition is violated by the blk_unref -> bdrv_drain ->
AIO_WAIT_WHILE() call if blk->refcnt is 1 here:

 * The caller's thread must be the IOThread that owns @ctx or the main loop
 * thread (with @ctx acquired exactly once).

blk_unref() is called from the main loop thread without having acquired
blk's AioContext.

Normally blk->refcnt will be > 1 so bdrv_drain() won't be called, but
I'm not sure if that can be guaranteed.

The following seems safer although it's uglier:

  aio_context = bdrv_get_aio_context(bs);
  aio_context_acquire(aio_context);
  blk_unref(blk);
  aio_context_release(aio_context);


signature.asc
Description: PGP signature


Re: [PATCH v7 12/13] block: Add bdrv_co_move_to_aio_context()

2020-09-15 Thread Stefan Hajnoczi
On Wed, Sep 09, 2020 at 05:11:48PM +0200, Kevin Wolf wrote:
> Add a function to move the current coroutine to the AioContext of a
> given BlockDriverState.
> 
> Signed-off-by: Kevin Wolf 
> ---
>  include/block/block.h |  6 ++
>  block.c   | 10 ++
>  2 files changed, 16 insertions(+)
> 
> diff --git a/include/block/block.h b/include/block/block.h
> index 981ab5b314..80ab322f11 100644
> --- a/include/block/block.h
> +++ b/include/block/block.h
> @@ -626,6 +626,12 @@ bool bdrv_debug_is_suspended(BlockDriverState *bs, const 
> char *tag);
>   */
>  AioContext *bdrv_get_aio_context(BlockDriverState *bs);
>  
> +/**
> + * Move the current coroutine to the AioContext of @bs and return the old
> + * AioContext of the coroutine.
> + */
> +AioContext *coroutine_fn bdrv_co_move_to_aio_context(BlockDriverState *bs);

I'm not sure this function handles all cases:
1. Being called without the BQL (i.e. from an IOThread).
2. Being called while a device stops using its IOThread.

The races that come to mind are fetching the AioContext for bs and then
scheduling a BH. The BH is executed later on by the event loop. There
might be cases where the AioContext for bs is updated before the BH
runs.

I didn't investigate these cases but wanted to mention them in case you
want to add doc comments about when this function can be used or if
you'd like to verify them yourself.

Reviewed-by: Stefan Hajnoczi 


signature.asc
Description: PGP signature


Re: [PATCH v7 00/13] monitor: Optionally run handlers in coroutines

2020-09-15 Thread Stefan Hajnoczi
On Mon, Sep 14, 2020 at 05:09:49PM +0200, Markus Armbruster wrote:
> Stefan Hajnoczi  writes:
> 
> > On Wed, Sep 09, 2020 at 05:11:36PM +0200, Kevin Wolf wrote:
> >> Some QMP command handlers can block the main loop for a relatively long
> >> time, for example because they perform some I/O. This is quite nasty.
> >> Allowing such handlers to run in a coroutine where they can yield (and
> >> therefore release the BQL) while waiting for an event such as I/O
> >> completion solves the problem.
> >> 
> >> This series adds the infrastructure to allow this and switches
> >> block_resize to run in a coroutine as a first example.
> >> 
> >> This is an alternative solution to Marc-André's "monitor: add
> >> asynchronous command type" series.
> >
> > Please clarify the following in the QAPI documentation:
> >  * Is the QMP monitor suspended while the command is pending?
> >  * Are QMP events reported while the command is pending?
> 
> Good points.  Kevin, I'd be willing to take this as a follow-up patch,
> if that's more convenient for you.
> 
> > Acked-by: Stefan Hajnoczi 
> 
> Stefan, I could use your proper review of PATCH 11-13.  Pretty-please?

Sounds good. I have reviewed the patches 11-13 and left questions for
Kevin.


signature.asc
Description: PGP signature


[PATCH v2 2/8] iotests: Drop readlink -f

2020-09-15 Thread Alex Bennée
From: Max Reitz 

On macOS, (out of the box) readlink does not have -f.  We do not really
need readlink here, though, it was just a replacement for realpath
(which is not available on our BSD test systems), which we needed to
make the $(dirname) into an absolute path.

Instead of using either, just use "cd; pwd" like is done for
$source_iotests.

   ("iotests: Allow running from different directory")

Fixes: b1cbc33a3971b6bb005d5ac3569feae35a71de0f
Reported-by: Claudio Fontana 
Reported-by: Thomas Huth 
Suggested-by: Peter Maydell 
Signed-off-by: Max Reitz 
Message-Id: <20200914145606.94620-1-mre...@redhat.com>
Signed-off-by: Alex Bennée 
---
 tests/qemu-iotests/check | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/qemu-iotests/check b/tests/qemu-iotests/check
index e14a1f354dd9..678b6e49103a 100755
--- a/tests/qemu-iotests/check
+++ b/tests/qemu-iotests/check
@@ -44,7 +44,7 @@ then
 _init_error "failed to obtain source tree name from check symlink"
 fi
 source_iotests=$(cd "$source_iotests"; pwd) || _init_error "failed to 
enter source tree"
-build_iotests=$(readlink -f $(dirname "$0"))
+build_iotests=$(cd "$(dirname "$0")"; pwd)
 else
 # called from the source tree
 source_iotests=$PWD
-- 
2.20.1




Re: [PATCH] hw: ide: check the pointer before do dma memory unmap

2020-09-15 Thread Li Qiang
ping!!

Li Qiang  于2020年9月7日周一 上午9:39写道:
>
> Ping!
>
> Li Qiang  于2020年9月1日周二 下午6:34写道:
> >
> > Ping.
> >
> > Li Qiang  于2020年8月15日周六 下午3:21写道:
> > >
> > > In 'map_page' we need to check the return value of
> > > 'dma_memory_map' to ensure the we actully maped something.
> > > Otherwise, we will hit an assert in 'address_space_unmap'.
> > > This is because we can't find the MR with the NULL buffer.
> > > This is the LP#1884693:
> > >
> > > -->https://bugs.launchpad.net/qemu/+bug/1884693
> > >
> > > Reported-by: Alexander Bulekov 
> > > Signed-off-by: Li Qiang 
> > > ---
> > >  hw/ide/ahci.c | 5 +
> > >  1 file changed, 5 insertions(+)
> > >
> > > diff --git a/hw/ide/ahci.c b/hw/ide/ahci.c
> > > index 009120f88b..63e9fccdbe 100644
> > > --- a/hw/ide/ahci.c
> > > +++ b/hw/ide/ahci.c
> > > @@ -250,6 +250,11 @@ static void map_page(AddressSpace *as, uint8_t 
> > > **ptr, uint64_t addr,
> > >  }
> > >
> > >  *ptr = dma_memory_map(as, addr, , DMA_DIRECTION_FROM_DEVICE);
> > > +
> > > +if (!*ptr) {
> > > +return;
> > > +}
> > > +
> > >  if (len < wanted) {
> > >  dma_memory_unmap(as, *ptr, len, DMA_DIRECTION_FROM_DEVICE, len);
> > >  *ptr = NULL;
> > > --
> > > 2.17.1
> > >



Re: [PATCH v9 19/26] tests: Fixes test-io-channel-file by mask only owner file state mask bits

2020-09-15 Thread Daniel P . Berrangé
On Tue, Sep 15, 2020 at 08:13:11PM +0800, Yonggang Luo wrote:
> This is the error on msys2/mingw
> Running test test-io-channel-file
> **
> ERROR:../tests/test-io-channel-file.c:59:test_io_channel_file_helper: 
> assertion failed (TEST_MASK & ~mask == st.st_mode & 0777): (384 == 438)
> ERROR test-io-channel-file - Bail out! 
> ERROR:../tests/test-io-channel-file.c:59:test_io_channel_file_helper: 
> assertion failed (TEST_MASK & ~mask == st.st_mode & 0777): (384 == 438)
> 
> Signed-off-by: Yonggang Luo 
> ---
>  tests/test-io-channel-file.c | 10 +-
>  1 file changed, 9 insertions(+), 1 deletion(-)

Reviewed-by: Daniel P. Berrangé 


Regards,
Daniel
-- 
|: https://berrange.com  -o-https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org -o-https://fstop138.berrange.com :|
|: https://entangle-photo.org-o-https://www.instagram.com/dberrange :|




Re: [PATCH v9 04/26] win32: Simplify gmtime_r detection direct base on _POSIX_THREAD_SAFE_FUNCTIONS.

2020-09-15 Thread Yonggang Luo
On Tue, Sep 15, 2020 at 9:00 PM Daniel P. Berrangé 
wrote:
>
> On Tue, Sep 15, 2020 at 08:12:56PM +0800, Yonggang Luo wrote:
> > First, this reduce the size of configure, configure are tending to
removal in future,
> > and this didn't introduce any new feature or remove any exist feature.
> > Second, the current localtime_r detection are conflict with ncursesw
detection in
> > mingw, when ncursesw detected, it will provide the following compile
flags
> > pkg-config --cflags ncursesw
> > -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=199506L
-IC:/CI-Tools/msys64/mingw64/include/ncursesw
> > And the compile flag _POSIX_C_SOURCE will always cause
_POSIX_THREAD_SAFE_FUNCTIONS to
> > be defined, in new version of mingw, that's will cause localtime_r to
be defined.
> > But the configure script didn't provide _POSIX_C_SOURCE macro, and
that's will result
> > localtime_r not detected because localtime_r are defined in forceinline
manner.
>
> ncursesw is just one of the three curses impls we can select for
> building against, so it doesn't feel right to make an assumption
> that _POSIX_C_SOURCE is always defined.

That's what I am trying to do, not depends on if  _POSIX_C_SOURCE are
defined.
After this patch, whenever   ncursesw or other  curses lib trying define or
not define  _POSIX_C_SOURCE, the source will building properly
Because now, we don't make any assumption about  _POSIX_C_SOURCE, but
before this patch,
The configure always assume msys2/mingw `not define _POSIX_C_SOURCE ` at
all.
Now this restriction are removed, only depends on mingw related
_POSIX_THREAD_SAFE_FUNCTIONS
>
> >
> > And finally cause conflict between QEMU defined localtime_r
> > struct tm *localtime_r(const time_t *timep, struct tm *result);
> > with mingw defined localtime_r
> >
> > ```
> > #if defined(_POSIX_C_SOURCE) && !defined(_POSIX_THREAD_SAFE_FUNCTIONS)
> > #define _POSIX_THREAD_SAFE_FUNCTIONS 200112L
> > #endif
> >
> > #ifdef _POSIX_THREAD_SAFE_FUNCTIONS
> > __forceinline struct tm *__CRTDECL localtime_r(const time_t *_Time,
struct tm *_Tm) {
> >   return localtime_s(_Tm, _Time) ? NULL : _Tm;
> > }
> > __forceinline struct tm *__CRTDECL gmtime_r(const time_t *_Time, struct
tm *_Tm) {
> >   return gmtime_s(_Tm, _Time) ? NULL : _Tm;
> > }
> > __forceinline char *__CRTDECL ctime_r(const time_t *_Time, char *_Str) {
> >   return ctime_s(_Str, 0x7fff, _Time) ? NULL : _Str;
> > }
> > __forceinline char *__CRTDECL asctime_r(const struct tm *_Tm, char *
_Str) {
> >   return asctime_s(_Str, 0x7fff, _Tm) ? NULL : _Str;
> > }
> > #endif
> > ```
> >
> > So I suggest remove this configure script, and restrict msys2/mingw
version to easy to maintain.
> > And use _POSIX_THREAD_SAFE_FUNCTIONS to guard the localtime_r and
counterpart functions
> >
> > Signed-off-by: Yonggang Luo 
> > ---
> >  configure | 34 --
> >  include/sysemu/os-win32.h |  4 ++--
> >  util/oslib-win32.c|  2 +-
> >  3 files changed, 3 insertions(+), 37 deletions(-)
> >
> > diff --git a/configure b/configure
> > index dc4b7a2e55..bac48b5b49 100755
> > --- a/configure
> > +++ b/configure
> > @@ -2496,37 +2496,6 @@ if test "$vhost_net" = ""; then
> >test "$vhost_kernel" = "yes" && vhost_net=yes
> >  fi
> >
> > -##
> > -# MinGW / Mingw-w64 localtime_r/gmtime_r check
> > -
> > -if test "$mingw32" = "yes"; then
> > -# Some versions of MinGW / Mingw-w64 lack localtime_r
> > -# and gmtime_r entirely.
> > -#
> > -# Some versions of Mingw-w64 define a macro for
> > -# localtime_r/gmtime_r.
> > -#
> > -# Some versions of Mingw-w64 will define functions
> > -# for localtime_r/gmtime_r, but only if you have
> > -# _POSIX_THREAD_SAFE_FUNCTIONS defined. For fun
> > -# though, unistd.h and pthread.h both define
> > -# that for you.
> > -#
> > -# So this #undef localtime_r and #include 
> > -# are not in fact redundant.
> > -cat > $TMPC << EOF
> > -#include 
> > -#include 
> > -#undef localtime_r
> > -int main(void) { localtime_r(NULL, NULL); return 0; }
> > -EOF
> > -if compile_prog "" "" ; then
> > -localtime_r="yes"
> > -else
> > -localtime_r="no"
> > -fi
> > -fi
> > -
> >  ##
> >  # pkg-config probe
> >
> > @@ -7088,9 +7057,6 @@ if [ "$bsd" = "yes" ] ; then
> >echo "CONFIG_BSD=y" >> $config_host_mak
> >  fi
> >
> > -if test "$localtime_r" = "yes" ; then
> > -  echo "CONFIG_LOCALTIME_R=y" >> $config_host_mak
> > -fi
> >  if test "$qom_cast_debug" = "yes" ; then
> >echo "CONFIG_QOM_CAST_DEBUG=y" >> $config_host_mak
> >  fi
> > diff --git a/include/sysemu/os-win32.h b/include/sysemu/os-win32.h
> > index d8978e28c0..3ac8a53bac 100644
> > --- a/include/sysemu/os-win32.h
> > +++ b/include/sysemu/os-win32.h
> > @@ -48,12 +48,12 @@
> >  #define siglongjmp(env, val) longjmp(env, val)
> >
> >  /* Missing POSIX functions. Don't use MinGW-w64 macros. */
> > -#ifndef 

Re: [PATCH v9 12/26] tests: Enable crypto tests under msys2/mingw

2020-09-15 Thread Daniel P . Berrangé
On Tue, Sep 15, 2020 at 08:13:04PM +0800, Yonggang Luo wrote:
> Fixes following tests on msys2/mingw
> 'test-crypto-tlscredsx509'
> test-crypto-tlssession'
> 'test-io-channel-tls'
> 
> These tests are failure with:
> ERROR test-crypto-tlscredsx509 - missing test plan
> ERROR test-crypto-tlssession - missing test plan
> ERROR test-io-channel-tls - missing test plan
> 
> Because on win32 those test case are all disabled in the header
> 
> Add qemu_socket_pair for cross platform support
> Convert file system handling functions to glib
> Add qemu_link function instead posix only link function.
> Use send ad recv from qemu that convert Windows Socks error to errno properly.
> Use g_remove instead unlink
> Use g_mkdir instead mkdir
> 
> Signed-off-by: Yonggang Luo 
> ---
>  tests/crypto-tls-psk-helpers.c   |   2 +-
>  tests/crypto-tls-x509-helpers.c  | 106 ++-
>  tests/crypto-tls-x509-helpers.h  |   9 ++-
>  tests/test-crypto-tlscredsx509.c |  47 +++---
>  tests/test-crypto-tlssession.c   |  68 +++-
>  tests/test-io-channel-tls.c  |  51 ---
>  6 files changed, 204 insertions(+), 79 deletions(-)
> 
> diff --git a/tests/crypto-tls-psk-helpers.c b/tests/crypto-tls-psk-helpers.c
> index 4f3bd446ad..5d5537 100644
> --- a/tests/crypto-tls-psk-helpers.c
> +++ b/tests/crypto-tls-psk-helpers.c
> @@ -46,7 +46,7 @@ void test_tls_psk_init(const char *pskfile)
>  
>  void test_tls_psk_cleanup(const char *pskfile)
>  {
> -unlink(pskfile);
> +g_remove(pskfile);
>  }
>  
>  #endif /* QCRYPTO_HAVE_TLS_TEST_SUPPORT */
> diff --git a/tests/crypto-tls-x509-helpers.c b/tests/crypto-tls-x509-helpers.c
> index 01b3daf358..1238983755 100644
> --- a/tests/crypto-tls-x509-helpers.c
> +++ b/tests/crypto-tls-x509-helpers.c
> @@ -23,6 +23,8 @@
>  #include "crypto-tls-x509-helpers.h"
>  #include "crypto/init.h"
>  #include "qemu/sockets.h"
> +#include 
> +#include 
>  
>  #ifdef QCRYPTO_HAVE_TLS_TEST_SUPPORT
>  
> @@ -133,7 +135,7 @@ void test_tls_init(const char *keyfile)
>  void test_tls_cleanup(const char *keyfile)
>  {
>  asn1_delete_structure(_asn1);
> -unlink(keyfile);
> +g_remove(keyfile);
>  }
>  
>  /*
> @@ -501,8 +503,108 @@ void test_tls_discard_cert(QCryptoTLSTestCertReq *req)
>  req->crt = NULL;
>  
>  if (getenv("QEMU_TEST_DEBUG_CERTS") == NULL) {
> -unlink(req->filename);
> +g_remove(req->filename);
>  }
>  }
>  
> +int qemu_link(const char *exist_path1, const char *new_path2)
> +{
> +#if defined(_WIN32)
> +g_autofree gchar *current_dir = g_get_current_dir();
> +g_autofree gchar *full_path = g_build_filename(current_dir, exist_path1, 
> NULL);
> +return CreateSymbolicLinkA(
> +new_path2, full_path, 0 | 
> SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE) ? 0 : -1;
> +#else
> +return link(exist_path1, new_path2);
> +#endif
> +}
> +
> +#if defined(_WIN32)
> +
> +int qemu_socketpair(int family, int type, int protocol, int channel[2])
> +{
> +struct addrinfo addr_data;
> +struct addrinfo *addr = NULL;
> +int sock_listener = -1;
> +int sock_client = -1;
> +int sock_server = -1;
> +int one = 1;
> +
> +memset(_data, 0, sizeof(addr_data));
> +addr_data.ai_family = AF_INET;
> +addr_data.ai_socktype = type;
> +addr_data.ai_protocol = protocol;
> +if (0 != getaddrinfo("127.0.0.1", "0", _data, )) {


This test is backwards - QEMU style is "getaddrinfo(...) != 0"


> +goto error;
> +}
> +
> +if (NULL == addr) {
> +goto error;
> +}
> +
> +sock_listener = socket(addr->ai_family, addr->ai_socktype, 
> addr->ai_protocol);
> +if (-1 == sock_listener) {
> +goto error;
> +}

Normal code style is  "sock_listener < 0"

> +
> +if (-1 == setsockopt(sock_listener, SOL_SOCKET, SO_REUSEADDR, (const 
> char *), sizeof(one))) {
> +goto error;
> +}

SO_REUSEADDR is not desirable on Windows -  see os-win32.c comments about
it.

> +if (-1 == bind(sock_listener, addr->ai_addr, addr->ai_addrlen)) {
> +goto error;
> +}
> +if (-1 == getsockname(sock_listener, addr->ai_addr, (int 
> *)&(addr->ai_addrlen))) {
> +goto error;
> +}
> +if (-1 == listen(sock_listener, 1)) {
> +goto error;
> +}
> +
> +sock_client = socket(addr->ai_family, addr->ai_socktype, 
> addr->ai_protocol);
> +
> +if (-1 == sock_client) {
> +goto error;
> +}
> +
> +if (-1 == connect(sock_client, addr->ai_addr, addr->ai_addrlen)) {
> +goto error;
> +}
> +
> +sock_server = accept(sock_listener, 0, 0);
> +
> +if (-1 == sock_server) {
> +goto error;
> +}
> +
> +closesocket(sock_listener);
> +
> +channel[0] = sock_client;
> +channel[1] = sock_server;
> +return 0;
> +
> +error:
> +if (-1 != sock_server) {
> +closesocket(sock_server);
> +}
> +if (-1 != sock_client) {
> +closesocket(sock_client);
> +}
> +if 

Re: [PATCH v9 04/26] win32: Simplify gmtime_r detection direct base on _POSIX_THREAD_SAFE_FUNCTIONS.

2020-09-15 Thread Daniel P . Berrangé
On Tue, Sep 15, 2020 at 08:12:56PM +0800, Yonggang Luo wrote:
> First, this reduce the size of configure, configure are tending to removal in 
> future,
> and this didn't introduce any new feature or remove any exist feature.
> Second, the current localtime_r detection are conflict with ncursesw 
> detection in
> mingw, when ncursesw detected, it will provide the following compile flags
> pkg-config --cflags ncursesw
> -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=199506L 
> -IC:/CI-Tools/msys64/mingw64/include/ncursesw
> And the compile flag _POSIX_C_SOURCE will always cause 
> _POSIX_THREAD_SAFE_FUNCTIONS to
> be defined, in new version of mingw, that's will cause localtime_r to be 
> defined.
> But the configure script didn't provide _POSIX_C_SOURCE macro, and that's 
> will result
> localtime_r not detected because localtime_r are defined in forceinline 
> manner.

ncursesw is just one of the three curses impls we can select for
building against, so it doesn't feel right to make an assumption
that _POSIX_C_SOURCE is always defined.

> 
> And finally cause conflict between QEMU defined localtime_r
> struct tm *localtime_r(const time_t *timep, struct tm *result);
> with mingw defined localtime_r
> 
> ```
> #if defined(_POSIX_C_SOURCE) && !defined(_POSIX_THREAD_SAFE_FUNCTIONS)
> #define _POSIX_THREAD_SAFE_FUNCTIONS 200112L
> #endif
> 
> #ifdef _POSIX_THREAD_SAFE_FUNCTIONS
> __forceinline struct tm *__CRTDECL localtime_r(const time_t *_Time, struct tm 
> *_Tm) {
>   return localtime_s(_Tm, _Time) ? NULL : _Tm;
> }
> __forceinline struct tm *__CRTDECL gmtime_r(const time_t *_Time, struct tm 
> *_Tm) {
>   return gmtime_s(_Tm, _Time) ? NULL : _Tm;
> }
> __forceinline char *__CRTDECL ctime_r(const time_t *_Time, char *_Str) {
>   return ctime_s(_Str, 0x7fff, _Time) ? NULL : _Str;
> }
> __forceinline char *__CRTDECL asctime_r(const struct tm *_Tm, char * _Str) {
>   return asctime_s(_Str, 0x7fff, _Tm) ? NULL : _Str;
> }
> #endif
> ```
> 
> So I suggest remove this configure script, and restrict msys2/mingw version 
> to easy to maintain.
> And use _POSIX_THREAD_SAFE_FUNCTIONS to guard the localtime_r and counterpart 
> functions
> 
> Signed-off-by: Yonggang Luo 
> ---
>  configure | 34 --
>  include/sysemu/os-win32.h |  4 ++--
>  util/oslib-win32.c|  2 +-
>  3 files changed, 3 insertions(+), 37 deletions(-)
> 
> diff --git a/configure b/configure
> index dc4b7a2e55..bac48b5b49 100755
> --- a/configure
> +++ b/configure
> @@ -2496,37 +2496,6 @@ if test "$vhost_net" = ""; then
>test "$vhost_kernel" = "yes" && vhost_net=yes
>  fi
>  
> -##
> -# MinGW / Mingw-w64 localtime_r/gmtime_r check
> -
> -if test "$mingw32" = "yes"; then
> -# Some versions of MinGW / Mingw-w64 lack localtime_r
> -# and gmtime_r entirely.
> -#
> -# Some versions of Mingw-w64 define a macro for
> -# localtime_r/gmtime_r.
> -#
> -# Some versions of Mingw-w64 will define functions
> -# for localtime_r/gmtime_r, but only if you have
> -# _POSIX_THREAD_SAFE_FUNCTIONS defined. For fun
> -# though, unistd.h and pthread.h both define
> -# that for you.
> -#
> -# So this #undef localtime_r and #include 
> -# are not in fact redundant.
> -cat > $TMPC << EOF
> -#include 
> -#include 
> -#undef localtime_r
> -int main(void) { localtime_r(NULL, NULL); return 0; }
> -EOF
> -if compile_prog "" "" ; then
> -localtime_r="yes"
> -else
> -localtime_r="no"
> -fi
> -fi
> -
>  ##
>  # pkg-config probe
>  
> @@ -7088,9 +7057,6 @@ if [ "$bsd" = "yes" ] ; then
>echo "CONFIG_BSD=y" >> $config_host_mak
>  fi
>  
> -if test "$localtime_r" = "yes" ; then
> -  echo "CONFIG_LOCALTIME_R=y" >> $config_host_mak
> -fi
>  if test "$qom_cast_debug" = "yes" ; then
>echo "CONFIG_QOM_CAST_DEBUG=y" >> $config_host_mak
>  fi
> diff --git a/include/sysemu/os-win32.h b/include/sysemu/os-win32.h
> index d8978e28c0..3ac8a53bac 100644
> --- a/include/sysemu/os-win32.h
> +++ b/include/sysemu/os-win32.h
> @@ -48,12 +48,12 @@
>  #define siglongjmp(env, val) longjmp(env, val)
>  
>  /* Missing POSIX functions. Don't use MinGW-w64 macros. */
> -#ifndef CONFIG_LOCALTIME_R
> +#ifndef _POSIX_THREAD_SAFE_FUNCTIONS
>  #undef gmtime_r
>  struct tm *gmtime_r(const time_t *timep, struct tm *result);
>  #undef localtime_r
>  struct tm *localtime_r(const time_t *timep, struct tm *result);
> -#endif /* CONFIG_LOCALTIME_R */
> +#endif
>  
>  static inline void os_setup_signal_handling(void) {}
>  static inline void os_daemonize(void) {}
> diff --git a/util/oslib-win32.c b/util/oslib-win32.c
> index c654dafd93..f2fa9a3549 100644
> --- a/util/oslib-win32.c
> +++ b/util/oslib-win32.c
> @@ -106,7 +106,7 @@ void qemu_anon_ram_free(void *ptr, size_t size)
>  }
>  }
>  
> -#ifndef CONFIG_LOCALTIME_R
> +#ifndef _POSIX_THREAD_SAFE_FUNCTIONS
>  /* FIXME: add proper locking */
>  

Re: [PATCH v9 00/26] W32, W64 msys2/mingw patches

2020-09-15 Thread no-reply
Patchew URL: 
https://patchew.org/QEMU/20200915121318.247-1-luoyongg...@gmail.com/



Hi,

This series seems to have some coding style problems. See output below for
more information:

Type: series
Message-id: 20200915121318.247-1-luoyongg...@gmail.com
Subject: [PATCH v9 00/26] W32, W64 msys2/mingw patches

=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram
./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===

From https://github.com/patchew-project/qemu
 * [new tag] patchew/20200915121318.247-1-luoyongg...@gmail.com -> 
patchew/20200915121318.247-1-luoyongg...@gmail.com
Switched to a new branch 'test'
0b113c6 block: enable libnfs on msys2/mingw in cirrus.yml
948c78c block: Fixes nfs compiling error on msys2/mingw
083ce32 ci: Enable msys2 ci in cirrus
bc69278 meson: upgrade meson for execute custom ninjatool under msys2 properly
fef8a0c rcu: fixes test-logging.c by call drain_call_rcu before rmdir_full
58f2e78 tests: Fixes test-qdev-global-props.c
4a17a57 tests: fix test-util-sockets.c
6d2b926 tests: Fixes test-io-channel-file by mask only owner file state mask 
bits
e170301 tests: fixes aio-win32 about aio_remove_fd_handler, get it consistence 
with aio-posix.c
95d2579 tests: Fixes test-io-channel-socket.c tests under msys2/mingw
7e27f27 tests: Convert g_free to g_autofree macro in test-logging.c
07bddec cirrus: Building freebsd in a single short
ae61ebd vmstate: Fixes test-vmstate.c on msys2/mingw
787eeb4 meson: remove empty else and duplicated gio deps
ad29b42 tests: Enable crypto tests under msys2/mingw
eb97064 gcrypt: test_tls_psk_init should write binary file instead text file.
7ebfac6 meson: Use -b to ignore CR vs. CR-LF issues on Windows
6c6166c osdep: file locking functions are not available on Win32
5ae548a tests: test-replication disable /replication/secondary/* on msys2/mingw.
74a6589 tests: Fixes test-replication.c on msys2/mingw.
4a06ce7 tests: disable /char/stdio/* tests in test-char.c on win32
1c1fb53 curses: Fixes curses compiling errors.
bbfe966 win32: Simplify gmtime_r detection direct base on 
_POSIX_THREAD_SAFE_FUNCTIONS.
588c2f6 configure: Fixes ncursesw detection under msys2/mingw and enable curses
44ed485 ci: fixes msys2 build by upgrading capstone to 4.0.2
2f25baf rcu: Implement drain_call_rcu

=== OUTPUT BEGIN ===
1/26 Checking commit 2f25baf35413 (rcu: Implement drain_call_rcu)
2/26 Checking commit 44ed4856e1ef (ci: fixes msys2 build by upgrading capstone 
to 4.0.2)
3/26 Checking commit 588c2f6c1827 (configure: Fixes ncursesw detection under 
msys2/mingw and enable curses)
4/26 Checking commit bbfe966b472f (win32: Simplify gmtime_r detection direct 
base on _POSIX_THREAD_SAFE_FUNCTIONS.)
5/26 Checking commit 1c1fb5366733 (curses: Fixes curses compiling errors.)
6/26 Checking commit 4a06ce772c82 (tests: disable /char/stdio/* tests in 
test-char.c on win32)
WARNING: line over 80 characters
#60: FILE: tests/test-char.c:1474:
+g_test_add_func("/char/console/subprocess", 
char_console_test_subprocess);

total: 0 errors, 1 warnings, 56 lines checked

Patch 6/26 has style problems, please review.  If any of these errors
are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
7/26 Checking commit 74a6589a34e0 (tests: Fixes test-replication.c on 
msys2/mingw.)
8/26 Checking commit 5ae548ac2ff9 (tests: test-replication disable 
/replication/secondary/* on msys2/mingw.)
9/26 Checking commit 6c6166c50138 (osdep: file locking functions are not 
available on Win32)
10/26 Checking commit 7ebfac6615e0 (meson: Use -b to ignore CR vs. CR-LF issues 
on Windows)
11/26 Checking commit eb97064abc00 (gcrypt: test_tls_psk_init should write 
binary file instead text file.)
12/26 Checking commit ad29b4253ac2 (tests: Enable crypto tests under 
msys2/mingw)
WARNING: line over 80 characters
#77: FILE: tests/crypto-tls-x509-helpers.c:514:
+g_autofree gchar *full_path = g_build_filename(current_dir, exist_path1, 
NULL);

WARNING: line over 80 characters
#79: FILE: tests/crypto-tls-x509-helpers.c:516:
+new_path2, full_path, 0 | 
SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE) ? 0 : -1;

WARNING: line over 80 characters
#108: FILE: tests/crypto-tls-x509-helpers.c:545:
+sock_listener = socket(addr->ai_family, addr->ai_socktype, 
addr->ai_protocol);

ERROR: line over 90 characters
#113: FILE: tests/crypto-tls-x509-helpers.c:550:
+if (-1 == setsockopt(sock_listener, SOL_SOCKET, SO_REUSEADDR, (const char 
*), sizeof(one))) {

WARNING: line over 80 characters
#119: FILE: tests/crypto-tls-x509-helpers.c:556:
+if (-1 == getsockname(sock_listener, addr->ai_addr, (int 
*)&(addr->ai_addrlen))) {

ERROR: space prohibited between function name and open parenthesis '('
#223: FILE: tests/test-crypto-tlscredsx509.c:85:
+g_remove (CERT_DIR QCRYPTO_TLS_CREDS_X509_CA_CERT);

ERROR: space prohibited 

[PATCH v9 25/26] block: Fixes nfs compiling error on msys2/mingw

2020-09-15 Thread Yonggang Luo
These compiling errors are fixed:
../block/nfs.c:27:10: fatal error: poll.h: No such file or directory
   27 | #include 
  |  ^~~~
compilation terminated.

../block/nfs.c:63:5: error: unknown type name 'blkcnt_t'
   63 | blkcnt_t st_blocks;
  | ^~~~
../block/nfs.c: In function 'nfs_client_open':
../block/nfs.c:550:27: error: 'struct _stat64' has no member named 'st_blocks'
  550 | client->st_blocks = st.st_blocks;
  |   ^
../block/nfs.c: In function 'nfs_get_allocated_file_size':
../block/nfs.c:751:41: error: 'struct _stat64' has no member named 'st_blocks'
  751 | return (task.ret < 0 ? task.ret : st.st_blocks * 512);
  | ^
../block/nfs.c: In function 'nfs_reopen_prepare':
../block/nfs.c:805:31: error: 'struct _stat64' has no member named 'st_blocks'
  805 | client->st_blocks = st.st_blocks;
  |   ^
../block/nfs.c: In function 'nfs_get_allocated_file_size':
../block/nfs.c:752:1: error: control reaches end of non-void function 
[-Werror=return-type]
  752 | }
  | ^

On msys2/mingw, there is no st_blocks in struct _stat64 yet, we disable the 
usage of it
on msys2/mingw, and create a typedef long long blkcnt_t; for further 
implementation

Signed-off-by: Yonggang Luo 
---
 block/nfs.c | 37 ++---
 1 file changed, 30 insertions(+), 7 deletions(-)

diff --git a/block/nfs.c b/block/nfs.c
index 61a249a9fc..a8a8ef9e4f 100644
--- a/block/nfs.c
+++ b/block/nfs.c
@@ -24,7 +24,9 @@
 
 #include "qemu/osdep.h"
 
+#if !defined(_WIN32)
 #include 
+#endif
 #include "qemu/config-file.h"
 #include "qemu/error-report.h"
 #include "qapi/error.h"
@@ -51,6 +53,13 @@
 #define QEMU_NFS_MAX_PAGECACHE_SIZE (8388608 / NFS_BLKSIZE)
 #define QEMU_NFS_MAX_DEBUG_LEVEL 2
 
+#if defined (_WIN32)
+#define nfs_stat __stat64
+typedef long long blkcnt_t;
+#else
+#define nfs_stat stat
+#endif
+
 typedef struct NFSClient {
 struct nfs_context *context;
 struct nfsfh *fh;
@@ -70,7 +79,7 @@ typedef struct NFSRPC {
 int ret;
 int complete;
 QEMUIOVector *iov;
-struct stat *st;
+struct nfs_stat *st;
 Coroutine *co;
 NFSClient *client;
 } NFSRPC;
@@ -415,11 +424,21 @@ static void nfs_file_close(BlockDriverState *bs)
 nfs_client_close(client);
 }
 
+static blkcnt_t nfs_get_st_blocks(const struct nfs_stat *st)
+{
+#if defined(_WIN32)
+/* TODO: Not be possible implement on win32 yet, place here for futher 
implementation */
+return 0;
+#else
+return st->st_blocks;
+#endif
+}
+
 static int64_t nfs_client_open(NFSClient *client, BlockdevOptionsNfs *opts,
int flags, int open_flags, Error **errp)
 {
 int64_t ret = -EINVAL;
-struct stat st;
+struct nfs_stat st;
 char *file = NULL, *strp = NULL;
 
 qemu_mutex_init(>mutex);
@@ -545,7 +564,7 @@ static int64_t nfs_client_open(NFSClient *client, 
BlockdevOptionsNfs *opts,
 }
 
 ret = DIV_ROUND_UP(st.st_size, BDRV_SECTOR_SIZE);
-client->st_blocks = st.st_blocks;
+client->st_blocks = nfs_get_st_blocks();
 client->has_zero_init = S_ISREG(st.st_mode);
 *strp = '/';
 goto out;
@@ -706,6 +725,7 @@ static int nfs_has_zero_init(BlockDriverState *bs)
 return client->has_zero_init;
 }
 
+#if !defined (_WIN32)
 /* Called (via nfs_service) with QemuMutex held.  */
 static void
 nfs_get_allocated_file_size_cb(int ret, struct nfs_context *nfs, void *data,
@@ -729,7 +749,7 @@ static int64_t nfs_get_allocated_file_size(BlockDriverState 
*bs)
 {
 NFSClient *client = bs->opaque;
 NFSRPC task = {0};
-struct stat st;
+struct nfs_stat st;
 
 if (bdrv_is_read_only(bs) &&
 !(bs->open_flags & BDRV_O_NOCACHE)) {
@@ -746,8 +766,9 @@ static int64_t nfs_get_allocated_file_size(BlockDriverState 
*bs)
 nfs_set_events(client);
 BDRV_POLL_WHILE(bs, !task.complete);
 
-return (task.ret < 0 ? task.ret : st.st_blocks * 512);
+return (task.ret < 0 ? task.ret : nfs_get_st_blocks() * 512);
 }
+#endif
 
 static int coroutine_fn
 nfs_file_co_truncate(BlockDriverState *bs, int64_t offset, bool exact,
@@ -778,7 +799,7 @@ static int nfs_reopen_prepare(BDRVReopenState *state,
   BlockReopenQueue *queue, Error **errp)
 {
 NFSClient *client = state->bs->opaque;
-struct stat st;
+struct nfs_stat st;
 int ret = 0;
 
 if (state->flags & BDRV_O_RDWR && bdrv_is_read_only(state->bs)) {
@@ -800,7 +821,7 @@ static int nfs_reopen_prepare(BDRVReopenState *state,
nfs_get_error(client->context));
 return ret;
 }
-client->st_blocks = st.st_blocks;
+client->st_blocks = nfs_get_st_blocks();
 }
 
 return 0;
@@ -869,7 +890,9 @@ static BlockDriver bdrv_nfs = {
 .create_opts= _create_opts,
 
 .bdrv_has_zero_init = nfs_has_zero_init,
+#if !defined (_WIN32)
 

[PATCH v9 21/26] tests: Fixes test-qdev-global-props.c

2020-09-15 Thread Yonggang Luo
On win32 the line ending are \r\n, so we skip the \n in function 
test_dynamic_globalprop

Signed-off-by: Yonggang Luo 
Reviewed-by: Daniel P. Berrangé 
---
 tests/test-qdev-global-props.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/tests/test-qdev-global-props.c b/tests/test-qdev-global-props.c
index 8a3c14d92c..be6bcfc46b 100644
--- a/tests/test-qdev-global-props.c
+++ b/tests/test-qdev-global-props.c
@@ -251,10 +251,10 @@ static void test_dynamic_globalprop(void)
 g_test_trap_assert_passed();
 g_test_trap_assert_stderr_unmatched("*prop1*");
 g_test_trap_assert_stderr_unmatched("*prop2*");
-g_test_trap_assert_stderr("*warning: global dynamic-prop-type-bad.prop3 
has invalid class name\n*");
+g_test_trap_assert_stderr("*warning: global dynamic-prop-type-bad.prop3 
has invalid class name*");
 g_test_trap_assert_stderr_unmatched("*prop4*");
-g_test_trap_assert_stderr("*warning: global nohotplug-type.prop5=105 not 
used\n*");
-g_test_trap_assert_stderr("*warning: global nondevice-type.prop6 has 
invalid class name\n*");
+g_test_trap_assert_stderr("*warning: global nohotplug-type.prop5=105 not 
used*");
+g_test_trap_assert_stderr("*warning: global nondevice-type.prop6 has 
invalid class name*");
 g_test_trap_assert_stdout("");
 }
 
-- 
2.28.0.windows.1




[PATCH v9 20/26] tests: fix test-util-sockets.c

2020-09-15 Thread Yonggang Luo
Fixes following errors:
Running test test-util-sockets
ERROR test-util-sockets - missing test plan

# Start of name tests
**
ERROR:../tests/test-util-sockets.c:93:test_socket_fd_pass_name_good: assertion 
failed (fd != -1): (-1 != -1)
Bail out! ERROR:../tests/test-util-sockets.c:93:test_socket_fd_pass_name_good: 
assertion failed (fd != -1): (-1 != -1)

First should call to qemu_init_main_loop before socket_init,
then on win32 doesn't support for SOCKET_ADDRESS_TYPE_FD socket type

Signed-off-by: Yonggang Luo 
Reviewed-by: Daniel P. Berrangé 
---
 tests/test-util-sockets.c | 6 +-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/tests/test-util-sockets.c b/tests/test-util-sockets.c
index af9f5c0c70..1bbb16d9b1 100644
--- a/tests/test-util-sockets.c
+++ b/tests/test-util-sockets.c
@@ -75,7 +75,7 @@ int monitor_vprintf(Monitor *mon, const char *fmt, va_list 
ap) { abort(); }
 void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp) {}
 void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp) {}
 
-
+#ifndef _WIN32
 static void test_socket_fd_pass_name_good(void)
 {
 SocketAddress addr;
@@ -227,6 +227,7 @@ static void test_socket_fd_pass_num_nocli(void)
 
 g_free(addr.u.fd.str);
 }
+#endif
 
 #ifdef __linux__
 static gchar *abstract_sock_name;
@@ -321,6 +322,7 @@ int main(int argc, char **argv)
 {
 bool has_ipv4, has_ipv6;
 
+qemu_init_main_loop(_abort);
 socket_init();
 
 g_test_init(, , NULL);
@@ -340,6 +342,7 @@ int main(int argc, char **argv)
 test_fd_is_socket_bad);
 g_test_add_func("/util/socket/is-socket/good",
 test_fd_is_socket_good);
+#ifndef _WIN32
 g_test_add_func("/socket/fd-pass/name/good",
 test_socket_fd_pass_name_good);
 g_test_add_func("/socket/fd-pass/name/bad",
@@ -352,6 +355,7 @@ int main(int argc, char **argv)
 test_socket_fd_pass_num_bad);
 g_test_add_func("/socket/fd-pass/num/nocli",
 test_socket_fd_pass_num_nocli);
+#endif
 }
 
 #ifdef __linux__
-- 
2.28.0.windows.1




[PATCH v9 15/26] cirrus: Building freebsd in a single short

2020-09-15 Thread Yonggang Luo
This reverts commit 45f7b7b9f38f5c4d1529a37c93dedfc26a231bba
("cirrus.yml: Split FreeBSD job into two parts").

freebsd 1 hour limit not hit anymore

I think we going to a wrong direction, I think there is some tests a stall the 
test runner,
please look at
https://cirrus-ci.com/task/5110577531977728
When its running properly, the consumed time are little, but when tests running 
too long,
look at the cpu usage, the cpu usage are nearly zero. doesn't consuming time.

And look at
https://cirrus-ci.com/task/6119341601062912

If the tests running properly, the time consuming are little
We should not hide the error by split them

Signed-off-by: Yonggang Luo 
Reviewed-by: Daniel P. Berrangé 
Reviewed-by: Ed Maste 
---
 .cirrus.yml | 35 ---
 1 file changed, 8 insertions(+), 27 deletions(-)

diff --git a/.cirrus.yml b/.cirrus.yml
index 3dd9fcff7f..690c6882e8 100644
--- a/.cirrus.yml
+++ b/.cirrus.yml
@@ -1,38 +1,19 @@
 env:
   CIRRUS_CLONE_DEPTH: 1
 
-freebsd_1st_task:
+freebsd_12_task:
   freebsd_instance:
 image_family: freebsd-12-1
-cpu: 4
-memory: 4G
-  install_script: ASSUME_ALWAYS_YES=yes pkg bootstrap -f ; pkg install -y
-bash curl cyrus-sasl git glib gmake gnutls gsed
-nettle perl5 pixman pkgconf png usbredir
+cpu: 8
+memory: 8G
+  install_script:
+- ASSUME_ALWAYS_YES=yes pkg bootstrap -f ;
+- pkg install -y bash curl cyrus-sasl git glib gmake gnutls gsed 
+  nettle perl5 pixman pkgconf png usbredir
   script:
 - mkdir build
 - cd build
-- ../configure --disable-user --target-list-exclude='alpha-softmmu
-ppc64-softmmu ppc-softmmu riscv32-softmmu riscv64-softmmu s390x-softmmu
-sparc64-softmmu sparc-softmmu x86_64-softmmu i386-softmmu'
---enable-werror || { cat config.log; exit 1; }
-- gmake -j$(sysctl -n hw.ncpu)
-- gmake -j$(sysctl -n hw.ncpu) check
-
-freebsd_2nd_task:
-  freebsd_instance:
-image_family: freebsd-12-1
-cpu: 4
-memory: 4G
-  install_script: ASSUME_ALWAYS_YES=yes pkg bootstrap -f ; pkg install -y
-bash curl cyrus-sasl git glib gmake gnutls gtk3 gsed libepoxy mesa-libs
-nettle perl5 pixman pkgconf png SDL2 usbredir
-  script:
-- ./configure --enable-werror --target-list='alpha-softmmu ppc64-softmmu
-ppc-softmmu riscv32-softmmu riscv64-softmmu s390x-softmmu
-sparc64-softmmu sparc-softmmu x86_64-softmmu i386-softmmu
-sparc-bsd-user sparc64-bsd-user x86_64-bsd-user i386-bsd-user'
-|| { cat config.log; exit 1; }
+- ../configure --enable-werror || { cat config.log; exit 1; }
 - gmake -j$(sysctl -n hw.ncpu)
 - gmake -j$(sysctl -n hw.ncpu) check
 
-- 
2.28.0.windows.1




[PATCH v9 16/26] tests: Convert g_free to g_autofree macro in test-logging.c

2020-09-15 Thread Yonggang Luo
g_autofree are prefer than g_free when possible.

Signed-off-by: Yonggang Luo 
Reviewed-by: Philippe Mathieu-Daudé 
Reviewed-by: Daniel P. Berrangé 
---
 tests/test-logging.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/tests/test-logging.c b/tests/test-logging.c
index 8a1161de1d..783fe09a27 100644
--- a/tests/test-logging.c
+++ b/tests/test-logging.c
@@ -196,7 +196,7 @@ static void rmdir_full(gchar const *root)
 
 int main(int argc, char **argv)
 {
-gchar *tmp_path = g_dir_make_tmp("qemu-test-logging.XX", NULL);
+g_autofree gchar *tmp_path = g_dir_make_tmp("qemu-test-logging.XX", 
NULL);
 int rc;
 
 g_test_init(, , NULL);
@@ -212,6 +212,5 @@ int main(int argc, char **argv)
 rc = g_test_run();
 
 rmdir_full(tmp_path);
-g_free(tmp_path);
 return rc;
 }
-- 
2.28.0.windows.1




Re: [PATCH] fdc: check null block pointer before blk_pwrite

2020-09-15 Thread P J P
+-- On Thu, 27 Aug 2020, P J P wrote --+
| While transferring data via fdctrl_write_data(), check that
| current drive does not have a null block pointer. Avoid
| null pointer dereference.
| 
|  -> https://ruhr-uni-bochum.sciebo.de/s/NNWP2GfwzYKeKwE?path=%2Ffdc_nullptr1
| ==1658854==Hint: address points to the zero page.
| #0 blk_inc_in_flight block/block-backend.c:1327
| #1 blk_prw block/block-backend.c:1299
| #2 blk_pwrite block/block-backend.c:1464
| #3 fdctrl_write_data hw/block/fdc.c:2418
| #4 fdctrl_write hw/block/fdc.c:962
| #5 portio_write ioport.c:205
| #6 memory_region_write_accessor memory.c:483
| #7 access_with_adjusted_size memory.c:544
| #8 memory_region_dispatch_write memory.c:1476
| 
| Reported-by: Ruhr-University 
| Signed-off-by: Prasad J Pandit 
| ---
|  hw/block/fdc.c | 3 ++-
|  1 file changed, 2 insertions(+), 1 deletion(-)
| 
| diff --git a/hw/block/fdc.c b/hw/block/fdc.c
| index e9ed3eef45..dedadac68a 100644
| --- a/hw/block/fdc.c
| +++ b/hw/block/fdc.c
| @@ -2419,7 +2419,8 @@ static void fdctrl_write_data(FDCtrl *fdctrl, uint32_t 
value)
|  if (pos == FD_SECTOR_LEN - 1 ||
|  fdctrl->data_pos == fdctrl->data_len) {
|  cur_drv = get_cur_drv(fdctrl);
| -if (blk_pwrite(cur_drv->blk, fd_offset(cur_drv), fdctrl->fifo,
| +if (cur_drv->blk
| +&& blk_pwrite(cur_drv->blk, fd_offset(cur_drv), fdctrl->fifo,
| BDRV_SECTOR_SIZE, 0) < 0) {
|  FLOPPY_DPRINTF("error writing sector %d\n",
| fd_sector(cur_drv));
| 

Ping...!
--
Prasad J Pandit / Red Hat Product Security Team
8685 545E B54C 486B C6EB 271E E285 8B5A F050 DE8D




[PATCH v9 26/26] block: enable libnfs on msys2/mingw in cirrus.yml

2020-09-15 Thread Yonggang Luo
At the begging libnfs are not enabled because of compiling error,
now it's fixed so enable it

Signed-off-by: Yonggang Luo 
---
 .cirrus.yml | 1 +
 1 file changed, 1 insertion(+)

diff --git a/.cirrus.yml b/.cirrus.yml
index 176ccba76c..9566ec0439 100644
--- a/.cirrus.yml
+++ b/.cirrus.yml
@@ -93,6 +93,7 @@ windows_msys2_task:
 mingw-w64-x86_64-libusb
 mingw-w64-x86_64-usbredir
 mingw-w64-x86_64-libtasn1
+mingw-w64-x86_64-libnfs
 mingw-w64-x86_64-nettle
 mingw-w64-x86_64-cyrus-sasl
 mingw-w64-x86_64-curl
-- 
2.28.0.windows.1




[PATCH v9 23/26] meson: upgrade meson for execute custom ninjatool under msys2 properly

2020-09-15 Thread Yonggang Luo
* Bump versions to 0.55.2 for release

* Tag Info:
object 008d13038f95e7c7d8ad553f14e408da5b94c360
type commit
tag 0.55.2
tagger Jussi Pakkanen  2020/9/11 1:24:47

Signed-off-by: Yonggang Luo 
---
 meson | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/meson b/meson
index 68ed748f84..008d13038f 16
--- a/meson
+++ b/meson
@@ -1 +1 @@
-Subproject commit 68ed748f84f14c2d4e62dcbd123816e5898eb04c
+Subproject commit 008d13038f95e7c7d8ad553f14e408da5b94c360
-- 
2.28.0.windows.1




[PATCH v9 11/26] gcrypt: test_tls_psk_init should write binary file instead text file.

2020-09-15 Thread Yonggang Luo
On windows, if open file with "w", it's will automatically convert
"\n" to "\r\n" when writing to file.

Signed-off-by: Yonggang Luo 
---
 tests/crypto-tls-psk-helpers.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/tests/crypto-tls-psk-helpers.c b/tests/crypto-tls-psk-helpers.c
index a8395477c3..4f3bd446ad 100644
--- a/tests/crypto-tls-psk-helpers.c
+++ b/tests/crypto-tls-psk-helpers.c
@@ -26,13 +26,15 @@
 #include "crypto-tls-psk-helpers.h"
 #include "qemu/sockets.h"
 
+#include 
+
 #ifdef QCRYPTO_HAVE_TLS_TEST_SUPPORT
 
 void test_tls_psk_init(const char *pskfile)
 {
 FILE *fp;
 
-fp = fopen(pskfile, "w");
+fp = fopen(pskfile, "wb");
 if (fp == NULL) {
 g_critical("Failed to create pskfile %s", pskfile);
 abort();
-- 
2.28.0.windows.1




[PATCH v9 24/26] ci: Enable msys2 ci in cirrus

2020-09-15 Thread Yonggang Luo
Install msys2 in a proper way refer to 
https://github.com/cirruslabs/cirrus-ci-docs/issues/699
The https://wiki.qemu.org/Hosts/W32#Native_builds_with_MSYS2 need to be updated.
There is no need of --cross-prefix, open mingw64.exe instead of msys2.exe then 
we don't
need the --cross-prefix, besides we using environment variable settings:
MSYS: winsymlinks:nativestrict
MSYSTEM: MINGW64
CHERE_INVOKING: 1
to opening mingw64 native shell.
We now running tests with make -i check to skip tests errors.

Signed-off-by: Yonggang Luo 
Reviewed-by: Daniel P. Berrangé 
---
 .cirrus.yml | 61 +
 1 file changed, 61 insertions(+)

diff --git a/.cirrus.yml b/.cirrus.yml
index 690c6882e8..176ccba76c 100644
--- a/.cirrus.yml
+++ b/.cirrus.yml
@@ -44,3 +44,64 @@ macos_xcode_task:
--enable-werror --cc=clang || { cat config.log; exit 1; }
 - gmake -j$(sysctl -n hw.ncpu)
 - gmake check
+
+windows_msys2_task:
+  windows_container:
+image: cirrusci/windowsservercore:cmake
+os_version: 2019
+cpu: 8
+memory: 8G
+  env:
+MSYS: winsymlinks:nativestrict
+MSYSTEM: MINGW64
+CHERE_INVOKING: 1
+  printenv_script:
+- C:\tools\msys64\usr\bin\bash.exe -lc 'printenv'
+  install_script:
+- C:\tools\msys64\usr\bin\bash.exe -lc "cd /c/tools &&
+curl -O 
http://repo.msys2.org/msys/x86_64/msys2-keyring-r21.b39fb11-1-any.pkg.tar.xz;
+- C:\tools\msys64\usr\bin\bash.exe -lc "cd /c/tools &&
+curl -O 
http://repo.msys2.org/msys/x86_64/msys2-keyring-r21.b39fb11-1-any.pkg.tar.xz.sig;
+- C:\tools\msys64\usr\bin\bash.exe -lc "cd /c/tools &&
+pacman -U --noconfirm msys2-keyring-r21.b39fb11-1-any.pkg.tar.xz"
+- C:\tools\msys64\usr\bin\bash.exe -lc "pacman --noconfirm -Sy"
+- C:\tools\msys64\usr\bin\bash.exe -lc "pacman --noconfirm -S --needed
+bash pacman pacman-mirrors msys2-runtime"
+- taskkill /F /IM gpg-agent.exe
+- C:\tools\msys64\usr\bin\bash.exe -lc "pacman --noconfirm -Su"
+- C:\tools\msys64\usr\bin\bash.exe -lc "pacman --noconfirm -S --needed
+base-devel
+git
+mingw-w64-x86_64-python
+mingw-w64-x86_64-python-setuptools
+mingw-w64-x86_64-toolchain
+mingw-w64-x86_64-SDL2
+mingw-w64-x86_64-SDL2_image
+mingw-w64-x86_64-gtk3
+mingw-w64-x86_64-glib2
+mingw-w64-x86_64-ninja
+mingw-w64-x86_64-make
+mingw-w64-x86_64-lzo2
+mingw-w64-x86_64-zstd
+mingw-w64-x86_64-libjpeg-turbo
+mingw-w64-x86_64-pixman
+mingw-w64-x86_64-libgcrypt
+mingw-w64-x86_64-libpng
+mingw-w64-x86_64-libssh
+mingw-w64-x86_64-libxml2
+mingw-w64-x86_64-snappy
+mingw-w64-x86_64-libusb
+mingw-w64-x86_64-usbredir
+mingw-w64-x86_64-libtasn1
+mingw-w64-x86_64-nettle
+mingw-w64-x86_64-cyrus-sasl
+mingw-w64-x86_64-curl
+mingw-w64-x86_64-gnutls
+mingw-w64-x86_64-zstd"
+  script:
+- C:\tools\msys64\usr\bin\bash.exe -lc "mkdir build"
+- C:\tools\msys64\usr\bin\bash.exe -lc "cd build && ../configure 
--python=python3"
+- C:\tools\msys64\usr\bin\bash.exe -lc "cd build && make 
-j$NUMBER_OF_PROCESSORS"
+  test_script:
+- C:\tools\msys64\usr\bin\bash.exe -lc "cd build && make V=1 check"
+
-- 
2.28.0.windows.1




[PATCH v9 09/26] osdep: file locking functions are not available on Win32

2020-09-15 Thread Yonggang Luo
Do not declare the following locking functions on Win32:
int qemu_lock_fd(int fd, int64_t start, int64_t len, bool exclusive);
int qemu_unlock_fd(int fd, int64_t start, int64_t len);
int qemu_lock_fd_test(int fd, int64_t start, int64_t len, bool exclusive);
bool qemu_has_ofd_lock(void);

Signed-off-by: Yonggang Luo 
Reviewed-by: Daniel P. Berrangé 
---
 include/qemu/osdep.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
index 412962d91a..e80fddd1e8 100644
--- a/include/qemu/osdep.h
+++ b/include/qemu/osdep.h
@@ -502,11 +502,11 @@ int qemu_close(int fd);
 int qemu_unlink(const char *name);
 #ifndef _WIN32
 int qemu_dup(int fd);
-#endif
 int qemu_lock_fd(int fd, int64_t start, int64_t len, bool exclusive);
 int qemu_unlock_fd(int fd, int64_t start, int64_t len);
 int qemu_lock_fd_test(int fd, int64_t start, int64_t len, bool exclusive);
 bool qemu_has_ofd_lock(void);
+#endif
 
 #if defined(__HAIKU__) && defined(__i386__)
 #define FMT_pid "%ld"
-- 
2.28.0.windows.1




[PATCH v9 22/26] rcu: fixes test-logging.c by call drain_call_rcu before rmdir_full

2020-09-15 Thread Yonggang Luo
drain_call_rcu is necessary on win32, because under win32, if you
don't close the file before remove it, the remove would be fail.

Signed-off-by: Yonggang Luo 
---
 tests/test-logging.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/tests/test-logging.c b/tests/test-logging.c
index 783fe09a27..8b1522cfed 100644
--- a/tests/test-logging.c
+++ b/tests/test-logging.c
@@ -210,6 +210,8 @@ int main(int argc, char **argv)
  tmp_path, test_logfile_lock);
 
 rc = g_test_run();
+qemu_log_close();
+drain_call_rcu();
 
 rmdir_full(tmp_path);
 return rc;
-- 
2.28.0.windows.1




[PATCH v9 08/26] tests: test-replication disable /replication/secondary/* on msys2/mingw.

2020-09-15 Thread Yonggang Luo
They caused failure on msys2/mingw, that's because file-win32.c not implement
.bdrv_reopen_prepare/commit/abort yet.

This is the error message:
> $ ./tests/test-replication.exe
> # random seed: R02S3f4d1c01af2b0a046990e0235c481faf
> 1..13
> # Start of replication tests
> # Start of primary tests
> ok 1 /replication/primary/read
> ok 2 /replication/primary/write
> ok 3 /replication/primary/start
> ok 4 /replication/primary/stop
> ok 5 /replication/primary/do_checkpoint
> ok 6 /replication/primary/get_error_all
> # End of primary tests
> # Start of secondary tests
> ok 7 /replication/secondary/read
> ok 8 /replication/secondary/write
> Unexpected error in bdrv_reopen_prepare() at ../block.c:4191:
> Block format 'file' used by node '#block4287' does not support reopening
> files

Signed-off-by: Yonggang Luo 
Reviewed-by: Daniel P. Berrangé 
---
 tests/test-replication.c | 4 
 1 file changed, 4 insertions(+)

diff --git a/tests/test-replication.c b/tests/test-replication.c
index e7cbd6b144..b067240add 100644
--- a/tests/test-replication.c
+++ b/tests/test-replication.c
@@ -392,6 +392,7 @@ static void test_secondary_write(void)
 teardown_secondary();
 }
 
+#ifndef _WIN32
 static void test_secondary_start(void)
 {
 BlockBackend *top_blk, *local_blk;
@@ -546,6 +547,7 @@ static void test_secondary_get_error_all(void)
 
 teardown_secondary();
 }
+#endif
 
 static void sigabrt_handler(int signo)
 {
@@ -597,6 +599,7 @@ int main(int argc, char **argv)
 /* Secondary */
 g_test_add_func("/replication/secondary/read",  test_secondary_read);
 g_test_add_func("/replication/secondary/write", test_secondary_write);
+#ifndef _WIN32
 g_test_add_func("/replication/secondary/start", test_secondary_start);
 g_test_add_func("/replication/secondary/stop",  test_secondary_stop);
 g_test_add_func("/replication/secondary/continuous_replication",
@@ -605,6 +608,7 @@ int main(int argc, char **argv)
 test_secondary_do_checkpoint);
 g_test_add_func("/replication/secondary/get_error_all",
 test_secondary_get_error_all);
+#endif
 
 ret = g_test_run();
 
-- 
2.28.0.windows.1




[PATCH v9 19/26] tests: Fixes test-io-channel-file by mask only owner file state mask bits

2020-09-15 Thread Yonggang Luo
This is the error on msys2/mingw
Running test test-io-channel-file
**
ERROR:../tests/test-io-channel-file.c:59:test_io_channel_file_helper: assertion 
failed (TEST_MASK & ~mask == st.st_mode & 0777): (384 == 438)
ERROR test-io-channel-file - Bail out! 
ERROR:../tests/test-io-channel-file.c:59:test_io_channel_file_helper: assertion 
failed (TEST_MASK & ~mask == st.st_mode & 0777): (384 == 438)

Signed-off-by: Yonggang Luo 
---
 tests/test-io-channel-file.c | 10 +-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/tests/test-io-channel-file.c b/tests/test-io-channel-file.c
index bac2b07562..1b0e8d7c1b 100644
--- a/tests/test-io-channel-file.c
+++ b/tests/test-io-channel-file.c
@@ -28,6 +28,12 @@
 #define TEST_FILE "tests/test-io-channel-file.txt"
 #define TEST_MASK 0600
 
+#ifdef _WIN32
+#define TEST_MASK_EXPECT 0700
+#else
+#define TEST_MASK_EXPECT 0777
+#endif
+
 static void test_io_channel_file_helper(int flags)
 {
 QIOChannel *src, *dst;
@@ -56,7 +62,9 @@ static void test_io_channel_file_helper(int flags)
 umask(mask);
 ret = stat(TEST_FILE, );
 g_assert_cmpint(ret, >, -1);
-g_assert_cmpuint(TEST_MASK & ~mask, ==, st.st_mode & 0777);
+/* On Windows the stat() function in the C library checks only
+ the FAT-style READONLY attribute and does not look at the ACL at all. */
+g_assert_cmpuint(TEST_MASK & ~mask, ==, st.st_mode & TEST_MASK_EXPECT);
 
 unlink(TEST_FILE);
 object_unref(OBJECT(src));
-- 
2.28.0.windows.1




[PATCH v9 18/26] tests: fixes aio-win32 about aio_remove_fd_handler, get it consistence with aio-posix.c

2020-09-15 Thread Yonggang Luo
This is a fixes for
(C:\work\xemu\qemu\build\tests\test-aio-multithread.exe:19100): GLib-CRITICAL 
**: 23:03:24.965: g_source_remove_poll: assertion '!SOURCE_DESTROYED (source)' 
failed
ERROR test-aio-multithread - Bail out! GLib-FATAL-CRITICAL: 
g_source_remove_poll: assertion '!SOURCE_DESTROYED (source)' failed

(C:\work\xemu\qemu\build\tests\test-bdrv-drain.exe:21036): GLib-CRITICAL **: 
23:03:29.861: g_source_remove_poll: assertion '!SOURCE_DESTROYED (source)' 
failed
ERROR test-bdrv-drain - Bail out! GLib-FATAL-CRITICAL: g_source_remove_poll: 
assertion '!SOURCE_DESTROYED (source)' failed

And the idea comes from https://patchwork.kernel.org/patch/9975239/

Signed-off-by: Yonggang Luo 
---
 util/aio-win32.c | 11 +--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/util/aio-win32.c b/util/aio-win32.c
index 953c56ab48..9899546a8a 100644
--- a/util/aio-win32.c
+++ b/util/aio-win32.c
@@ -37,6 +37,15 @@ struct AioHandler {
 
 static void aio_remove_fd_handler(AioContext *ctx, AioHandler *node)
 {
+/* If the GSource is in the process of being destroyed then
+ * g_source_remove_poll() causes an assertion failure.  Skip
+ * removal in that case, because glib cleans up its state during
+ * destruction anyway.
+ */
+if (!g_source_is_destroyed(>source)) {
+g_source_remove_poll(>source, >pfd);
+}
+
 /* If aio_poll is in progress, just mark the node as deleted */
 if (qemu_lockcnt_count(>list_lock)) {
 node->deleted = 1;
@@ -139,8 +148,6 @@ void aio_set_event_notifier(AioContext *ctx,
 /* Are we deleting the fd handler? */
 if (!io_notify) {
 if (node) {
-g_source_remove_poll(>source, >pfd);
-
 aio_remove_fd_handler(ctx, node);
 }
 } else {
-- 
2.28.0.windows.1




[PATCH v9 14/26] vmstate: Fixes test-vmstate.c on msys2/mingw

2020-09-15 Thread Yonggang Luo
The vmstate are valid on win32, just need generate tmp path properly

Signed-off-by: Yonggang Luo 
Reviewed-by: Philippe Mathieu-Daudé 
Reviewed-by: Thomas Huth 
Reviewed-by: Daniel P. Berrangé 
---
 tests/test-vmstate.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tests/test-vmstate.c b/tests/test-vmstate.c
index 1c763015d0..ac38bfcfe8 100644
--- a/tests/test-vmstate.c
+++ b/tests/test-vmstate.c
@@ -34,7 +34,6 @@
 #include "qemu/module.h"
 #include "io/channel-file.h"
 
-static char temp_file[] = "/tmp/vmst.test.XX";
 static int temp_fd;
 
 
@@ -1484,6 +1483,8 @@ static void test_tmp_struct(void)
 
 int main(int argc, char **argv)
 {
+g_autofree char *temp_file = g_strdup_printf(
+"%s/vmst.test.XX", g_get_tmp_dir());
 temp_fd = mkstemp(temp_file);
 
 module_call_init(MODULE_INIT_QOM);
-- 
2.28.0.windows.1




[PATCH v9 07/26] tests: Fixes test-replication.c on msys2/mingw.

2020-09-15 Thread Yonggang Luo
On Windows there is no path like /tmp/s_local_disk.XX
Use g_get_tmp_dir instead of /tmp.

Signed-off-by: Yonggang Luo 
Reviewed-by: Daniel P. Berrangé 
---
 tests/test-replication.c | 18 ++
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/tests/test-replication.c b/tests/test-replication.c
index 9ab3666a90..e7cbd6b144 100644
--- a/tests/test-replication.c
+++ b/tests/test-replication.c
@@ -23,14 +23,14 @@
 
 /* primary */
 #define P_ID "primary-id"
-static char p_local_disk[] = "/tmp/p_local_disk.XX";
+static char *p_local_disk;
 
 /* secondary */
 #define S_ID "secondary-id"
 #define S_LOCAL_DISK_ID "secondary-local-disk-id"
-static char s_local_disk[] = "/tmp/s_local_disk.XX";
-static char s_active_disk[] = "/tmp/s_active_disk.XX";
-static char s_hidden_disk[] = "/tmp/s_hidden_disk.XX";
+static char *s_local_disk;
+static char *s_active_disk;
+static char *s_hidden_disk;
 
 /* FIXME: steal from blockdev.c */
 QemuOptsList qemu_drive_opts = {
@@ -571,6 +571,11 @@ static void setup_sigabrt_handler(void)
 int main(int argc, char **argv)
 {
 int ret;
+const char *tmpdir = g_get_tmp_dir();
+p_local_disk = g_strdup_printf("%s/p_local_disk.XX", tmpdir);
+s_local_disk = g_strdup_printf("%s/s_local_disk.XX", tmpdir);
+s_active_disk = g_strdup_printf("%s/s_active_disk.XX", tmpdir);
+s_hidden_disk = g_strdup_printf("%s/s_hidden_disk.XX", tmpdir);
 qemu_init_main_loop(_fatal);
 bdrv_init();
 
@@ -605,5 +610,10 @@ int main(int argc, char **argv)
 
 cleanup_imgs();
 
+g_free(p_local_disk);
+g_free(s_local_disk);
+g_free(s_active_disk);
+g_free(s_hidden_disk);
+
 return ret;
 }
-- 
2.28.0.windows.1




[PATCH v9 06/26] tests: disable /char/stdio/* tests in test-char.c on win32

2020-09-15 Thread Yonggang Luo
These tests are blocking test-char to be finished.
Disable them by using variable is_win32, so we doesn't
need macro to open it. and easy recover those function
latter.

Signed-off-by: Yonggang Luo 
---
 tests/test-char.c | 26 --
 1 file changed, 16 insertions(+), 10 deletions(-)

diff --git a/tests/test-char.c b/tests/test-char.c
index d35cc839bc..184ddceab8 100644
--- a/tests/test-char.c
+++ b/tests/test-char.c
@@ -77,7 +77,6 @@ static void fe_event(void *opaque, QEMUChrEvent event)
 }
 }
 
-#ifdef _WIN32
 static void char_console_test_subprocess(void)
 {
 QemuOpts *opts;
@@ -102,7 +101,7 @@ static void char_console_test(void)
 g_test_trap_assert_passed();
 g_test_trap_assert_stdout("CONSOLE");
 }
-#endif
+
 static void char_stdio_test_subprocess(void)
 {
 Chardev *chr;
@@ -1448,7 +1447,11 @@ static SocketAddress unixaddr = {
 
 int main(int argc, char **argv)
 {
-bool has_ipv4, has_ipv6;
+bool has_ipv4, has_ipv6, is_win32 = false;
+
+#ifdef _WIN32
+is_win32 = true;
+#endif
 
 qemu_init_main_loop(_abort);
 socket_init();
@@ -1467,12 +1470,15 @@ int main(int argc, char **argv)
 g_test_add_func("/char/invalid", char_invalid_test);
 g_test_add_func("/char/ringbuf", char_ringbuf_test);
 g_test_add_func("/char/mux", char_mux_test);
-#ifdef _WIN32
-g_test_add_func("/char/console/subprocess", char_console_test_subprocess);
-g_test_add_func("/char/console", char_console_test);
-#endif
-g_test_add_func("/char/stdio/subprocess", char_stdio_test_subprocess);
-g_test_add_func("/char/stdio", char_stdio_test);
+if (0) {
+g_test_add_func("/char/console/subprocess", 
char_console_test_subprocess);
+g_test_add_func("/char/console", char_console_test);
+}
+
+if (!is_win32) {
+g_test_add_func("/char/stdio/subprocess", char_stdio_test_subprocess);
+g_test_add_func("/char/stdio", char_stdio_test);
+}
 #ifndef _WIN32
 g_test_add_func("/char/pipe", char_pipe_test);
 #endif
@@ -1534,7 +1540,7 @@ int main(int argc, char **argv)
 g_test_add_data_func("/char/socket/client/dupid-reconnect/" # name, \
   ##name, char_socket_client_dupid_test)
 
-if (has_ipv4) {
+if (has_ipv4 && !is_win32) {
 SOCKET_SERVER_TEST(tcp, );
 SOCKET_CLIENT_TEST(tcp, );
 g_test_add_data_func("/char/socket/server/two-clients/tcp", ,
-- 
2.28.0.windows.1




[PATCH v9 05/26] curses: Fixes curses compiling errors.

2020-09-15 Thread Yonggang Luo
This is the compiling error:
../ui/curses.c: In function 'curses_refresh':
../ui/curses.c:256:5: error: 'next_maybe_keycode' may be used uninitialized in 
this function [-Werror=maybe-uninitialized]
  256 | curses2foo(_curses2keycode, _curseskey2keycode, chr, maybe_keycode)
  | ^~
../ui/curses.c:302:32: note: 'next_maybe_keycode' was declared here
  302 | enum maybe_keycode next_maybe_keycode;
  |^~
../ui/curses.c:256:5: error: 'maybe_keycode' may be used uninitialized in this 
function [-Werror=maybe-uninitialized]
  256 | curses2foo(_curses2keycode, _curseskey2keycode, chr, maybe_keycode)
  | ^~
../ui/curses.c:265:24: note: 'maybe_keycode' was declared here
  265 | enum maybe_keycode maybe_keycode;
  |^
cc1.exe: all warnings being treated as errors

gcc version 10.2.0 (Rev1, Built by MSYS2 project)

Signed-off-by: Yonggang Luo 
Reviewed-by: Gerd Hoffmann 
Reviewed-by: Daniel P. Berrangé 
---
 ui/curses.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/ui/curses.c b/ui/curses.c
index 12bc682cf9..e4f9588c3e 100644
--- a/ui/curses.c
+++ b/ui/curses.c
@@ -262,7 +262,7 @@ static int curses2foo(const int _curses2foo[], const int 
_curseskey2foo[],
 static void curses_refresh(DisplayChangeListener *dcl)
 {
 int chr, keysym, keycode, keycode_alt;
-enum maybe_keycode maybe_keycode;
+enum maybe_keycode maybe_keycode = CURSES_KEYCODE;
 
 curses_winch_check();
 
@@ -299,7 +299,7 @@ static void curses_refresh(DisplayChangeListener *dcl)
 
 /* alt or esc key */
 if (keycode == 1) {
-enum maybe_keycode next_maybe_keycode;
+enum maybe_keycode next_maybe_keycode = CURSES_KEYCODE;
 int nextchr = console_getch(_maybe_keycode);
 
 if (nextchr != -1) {
-- 
2.28.0.windows.1




[PATCH v9 12/26] tests: Enable crypto tests under msys2/mingw

2020-09-15 Thread Yonggang Luo
Fixes following tests on msys2/mingw
'test-crypto-tlscredsx509'
test-crypto-tlssession'
'test-io-channel-tls'

These tests are failure with:
ERROR test-crypto-tlscredsx509 - missing test plan
ERROR test-crypto-tlssession - missing test plan
ERROR test-io-channel-tls - missing test plan

Because on win32 those test case are all disabled in the header

Add qemu_socket_pair for cross platform support
Convert file system handling functions to glib
Add qemu_link function instead posix only link function.
Use send ad recv from qemu that convert Windows Socks error to errno properly.
Use g_remove instead unlink
Use g_mkdir instead mkdir

Signed-off-by: Yonggang Luo 
---
 tests/crypto-tls-psk-helpers.c   |   2 +-
 tests/crypto-tls-x509-helpers.c  | 106 ++-
 tests/crypto-tls-x509-helpers.h  |   9 ++-
 tests/test-crypto-tlscredsx509.c |  47 +++---
 tests/test-crypto-tlssession.c   |  68 +++-
 tests/test-io-channel-tls.c  |  51 ---
 6 files changed, 204 insertions(+), 79 deletions(-)

diff --git a/tests/crypto-tls-psk-helpers.c b/tests/crypto-tls-psk-helpers.c
index 4f3bd446ad..5d5537 100644
--- a/tests/crypto-tls-psk-helpers.c
+++ b/tests/crypto-tls-psk-helpers.c
@@ -46,7 +46,7 @@ void test_tls_psk_init(const char *pskfile)
 
 void test_tls_psk_cleanup(const char *pskfile)
 {
-unlink(pskfile);
+g_remove(pskfile);
 }
 
 #endif /* QCRYPTO_HAVE_TLS_TEST_SUPPORT */
diff --git a/tests/crypto-tls-x509-helpers.c b/tests/crypto-tls-x509-helpers.c
index 01b3daf358..1238983755 100644
--- a/tests/crypto-tls-x509-helpers.c
+++ b/tests/crypto-tls-x509-helpers.c
@@ -23,6 +23,8 @@
 #include "crypto-tls-x509-helpers.h"
 #include "crypto/init.h"
 #include "qemu/sockets.h"
+#include 
+#include 
 
 #ifdef QCRYPTO_HAVE_TLS_TEST_SUPPORT
 
@@ -133,7 +135,7 @@ void test_tls_init(const char *keyfile)
 void test_tls_cleanup(const char *keyfile)
 {
 asn1_delete_structure(_asn1);
-unlink(keyfile);
+g_remove(keyfile);
 }
 
 /*
@@ -501,8 +503,108 @@ void test_tls_discard_cert(QCryptoTLSTestCertReq *req)
 req->crt = NULL;
 
 if (getenv("QEMU_TEST_DEBUG_CERTS") == NULL) {
-unlink(req->filename);
+g_remove(req->filename);
 }
 }
 
+int qemu_link(const char *exist_path1, const char *new_path2)
+{
+#if defined(_WIN32)
+g_autofree gchar *current_dir = g_get_current_dir();
+g_autofree gchar *full_path = g_build_filename(current_dir, exist_path1, 
NULL);
+return CreateSymbolicLinkA(
+new_path2, full_path, 0 | 
SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE) ? 0 : -1;
+#else
+return link(exist_path1, new_path2);
+#endif
+}
+
+#if defined(_WIN32)
+
+int qemu_socketpair(int family, int type, int protocol, int channel[2])
+{
+struct addrinfo addr_data;
+struct addrinfo *addr = NULL;
+int sock_listener = -1;
+int sock_client = -1;
+int sock_server = -1;
+int one = 1;
+
+memset(_data, 0, sizeof(addr_data));
+addr_data.ai_family = AF_INET;
+addr_data.ai_socktype = type;
+addr_data.ai_protocol = protocol;
+if (0 != getaddrinfo("127.0.0.1", "0", _data, )) {
+goto error;
+}
+
+if (NULL == addr) {
+goto error;
+}
+
+sock_listener = socket(addr->ai_family, addr->ai_socktype, 
addr->ai_protocol);
+if (-1 == sock_listener) {
+goto error;
+}
+
+if (-1 == setsockopt(sock_listener, SOL_SOCKET, SO_REUSEADDR, (const char 
*), sizeof(one))) {
+goto error;
+}
+if (-1 == bind(sock_listener, addr->ai_addr, addr->ai_addrlen)) {
+goto error;
+}
+if (-1 == getsockname(sock_listener, addr->ai_addr, (int 
*)&(addr->ai_addrlen))) {
+goto error;
+}
+if (-1 == listen(sock_listener, 1)) {
+goto error;
+}
+
+sock_client = socket(addr->ai_family, addr->ai_socktype, 
addr->ai_protocol);
+
+if (-1 == sock_client) {
+goto error;
+}
+
+if (-1 == connect(sock_client, addr->ai_addr, addr->ai_addrlen)) {
+goto error;
+}
+
+sock_server = accept(sock_listener, 0, 0);
+
+if (-1 == sock_server) {
+goto error;
+}
+
+closesocket(sock_listener);
+
+channel[0] = sock_client;
+channel[1] = sock_server;
+return 0;
+
+error:
+if (-1 != sock_server) {
+closesocket(sock_server);
+}
+if (-1 != sock_client) {
+closesocket(sock_client);
+}
+if (-1 != sock_listener) {
+closesocket(sock_listener);
+}
+if (NULL != addr) {
+freeaddrinfo(addr);
+}
+return -1;
+}
+
+#else
+
+int qemu_socketpair(int family, int type, int protocol, int recv[2])
+{
+return socketpair(family, type, protocol, recv);
+}
+
+#endif
+
 #endif /* QCRYPTO_HAVE_TLS_TEST_SUPPORT */
diff --git a/tests/crypto-tls-x509-helpers.h b/tests/crypto-tls-x509-helpers.h
index 08efba4e19..6902d43af7 100644
--- a/tests/crypto-tls-x509-helpers.h
+++ b/tests/crypto-tls-x509-helpers.h
@@ -24,8 +24,9 @@
 #include 
 #include 
 

[PATCH v9 17/26] tests: Fixes test-io-channel-socket.c tests under msys2/mingw

2020-09-15 Thread Yonggang Luo
Currently test-io-channel-socket doesn't init with
qemu_init_main_loop
and that's cause the qemu_aio_context not inited,
and the following is the stack when null pointer accessed:

qemu_fd_register (c:\work\xemu\qemu\util\main-loop.c:336)
qemu_try_set_nonblock (c:\work\xemu\qemu\util\oslib-win32.c:224)
qemu_set_nonblock (c:\work\xemu\qemu\util\oslib-win32.c:230)
socket_can_bind_connect (c:\work\xemu\qemu\tests\socket-helpers.c:93)
socket_check_protocol_support (c:\work\xemu\qemu\tests\socket-helpers.c:141)
main (c:\work\xemu\qemu\tests\test-io-channel-socket.c:568)
__tmainCRTStartup (@__tmainCRTStartup:142)
mainCRTStartup (@1400014f6..140001539:3)
BaseThreadInitThunk (@BaseThreadInitThunk:9)
RtlUserThreadStart (@RtlUserThreadStart:12)

Signed-off-by: Yonggang Luo 
Reviewed-by: Daniel P. Berrangé 
---
 tests/test-io-channel-socket.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/tests/test-io-channel-socket.c b/tests/test-io-channel-socket.c
index d43083a766..743577d744 100644
--- a/tests/test-io-channel-socket.c
+++ b/tests/test-io-channel-socket.c
@@ -25,6 +25,7 @@
 #include "socket-helpers.h"
 #include "qapi/error.h"
 #include "qemu/module.h"
+#include "qemu/main-loop.h"
 
 
 static void test_io_channel_set_socket_bufs(QIOChannel *src,
@@ -556,6 +557,7 @@ int main(int argc, char **argv)
 bool has_ipv4, has_ipv6;
 
 module_call_init(MODULE_INIT_QOM);
+qemu_init_main_loop(_abort);
 socket_init();
 
 g_test_init(, , NULL);
-- 
2.28.0.windows.1




[PATCH v9 04/26] win32: Simplify gmtime_r detection direct base on _POSIX_THREAD_SAFE_FUNCTIONS.

2020-09-15 Thread Yonggang Luo
First, this reduce the size of configure, configure are tending to removal in 
future,
and this didn't introduce any new feature or remove any exist feature.
Second, the current localtime_r detection are conflict with ncursesw detection 
in
mingw, when ncursesw detected, it will provide the following compile flags
pkg-config --cflags ncursesw
-D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=199506L 
-IC:/CI-Tools/msys64/mingw64/include/ncursesw
And the compile flag _POSIX_C_SOURCE will always cause 
_POSIX_THREAD_SAFE_FUNCTIONS to
be defined, in new version of mingw, that's will cause localtime_r to be 
defined.
But the configure script didn't provide _POSIX_C_SOURCE macro, and that's will 
result
localtime_r not detected because localtime_r are defined in forceinline manner.

And finally cause conflict between QEMU defined localtime_r
struct tm *localtime_r(const time_t *timep, struct tm *result);
with mingw defined localtime_r

```
#if defined(_POSIX_C_SOURCE) && !defined(_POSIX_THREAD_SAFE_FUNCTIONS)
#define _POSIX_THREAD_SAFE_FUNCTIONS 200112L
#endif

#ifdef _POSIX_THREAD_SAFE_FUNCTIONS
__forceinline struct tm *__CRTDECL localtime_r(const time_t *_Time, struct tm 
*_Tm) {
  return localtime_s(_Tm, _Time) ? NULL : _Tm;
}
__forceinline struct tm *__CRTDECL gmtime_r(const time_t *_Time, struct tm 
*_Tm) {
  return gmtime_s(_Tm, _Time) ? NULL : _Tm;
}
__forceinline char *__CRTDECL ctime_r(const time_t *_Time, char *_Str) {
  return ctime_s(_Str, 0x7fff, _Time) ? NULL : _Str;
}
__forceinline char *__CRTDECL asctime_r(const struct tm *_Tm, char * _Str) {
  return asctime_s(_Str, 0x7fff, _Tm) ? NULL : _Str;
}
#endif
```

So I suggest remove this configure script, and restrict msys2/mingw version to 
easy to maintain.
And use _POSIX_THREAD_SAFE_FUNCTIONS to guard the localtime_r and counterpart 
functions

Signed-off-by: Yonggang Luo 
---
 configure | 34 --
 include/sysemu/os-win32.h |  4 ++--
 util/oslib-win32.c|  2 +-
 3 files changed, 3 insertions(+), 37 deletions(-)

diff --git a/configure b/configure
index dc4b7a2e55..bac48b5b49 100755
--- a/configure
+++ b/configure
@@ -2496,37 +2496,6 @@ if test "$vhost_net" = ""; then
   test "$vhost_kernel" = "yes" && vhost_net=yes
 fi
 
-##
-# MinGW / Mingw-w64 localtime_r/gmtime_r check
-
-if test "$mingw32" = "yes"; then
-# Some versions of MinGW / Mingw-w64 lack localtime_r
-# and gmtime_r entirely.
-#
-# Some versions of Mingw-w64 define a macro for
-# localtime_r/gmtime_r.
-#
-# Some versions of Mingw-w64 will define functions
-# for localtime_r/gmtime_r, but only if you have
-# _POSIX_THREAD_SAFE_FUNCTIONS defined. For fun
-# though, unistd.h and pthread.h both define
-# that for you.
-#
-# So this #undef localtime_r and #include 
-# are not in fact redundant.
-cat > $TMPC << EOF
-#include 
-#include 
-#undef localtime_r
-int main(void) { localtime_r(NULL, NULL); return 0; }
-EOF
-if compile_prog "" "" ; then
-localtime_r="yes"
-else
-localtime_r="no"
-fi
-fi
-
 ##
 # pkg-config probe
 
@@ -7088,9 +7057,6 @@ if [ "$bsd" = "yes" ] ; then
   echo "CONFIG_BSD=y" >> $config_host_mak
 fi
 
-if test "$localtime_r" = "yes" ; then
-  echo "CONFIG_LOCALTIME_R=y" >> $config_host_mak
-fi
 if test "$qom_cast_debug" = "yes" ; then
   echo "CONFIG_QOM_CAST_DEBUG=y" >> $config_host_mak
 fi
diff --git a/include/sysemu/os-win32.h b/include/sysemu/os-win32.h
index d8978e28c0..3ac8a53bac 100644
--- a/include/sysemu/os-win32.h
+++ b/include/sysemu/os-win32.h
@@ -48,12 +48,12 @@
 #define siglongjmp(env, val) longjmp(env, val)
 
 /* Missing POSIX functions. Don't use MinGW-w64 macros. */
-#ifndef CONFIG_LOCALTIME_R
+#ifndef _POSIX_THREAD_SAFE_FUNCTIONS
 #undef gmtime_r
 struct tm *gmtime_r(const time_t *timep, struct tm *result);
 #undef localtime_r
 struct tm *localtime_r(const time_t *timep, struct tm *result);
-#endif /* CONFIG_LOCALTIME_R */
+#endif
 
 static inline void os_setup_signal_handling(void) {}
 static inline void os_daemonize(void) {}
diff --git a/util/oslib-win32.c b/util/oslib-win32.c
index c654dafd93..f2fa9a3549 100644
--- a/util/oslib-win32.c
+++ b/util/oslib-win32.c
@@ -106,7 +106,7 @@ void qemu_anon_ram_free(void *ptr, size_t size)
 }
 }
 
-#ifndef CONFIG_LOCALTIME_R
+#ifndef _POSIX_THREAD_SAFE_FUNCTIONS
 /* FIXME: add proper locking */
 struct tm *gmtime_r(const time_t *timep, struct tm *result)
 {
-- 
2.28.0.windows.1




[PATCH v9 00/26] W32, W64 msys2/mingw patches

2020-09-15 Thread Yonggang Luo
V8-V9

* ci: Enable msys2 ci in cirrus
  do not install libnfs, libcapstone and jemalloc
  ident lines properly
  Do not install libnfs when the add the msys2 ci,

* Revert "configure: add --ninja option"
  Skip this revision

* block: Fixes nfs compiling error on msys2/mingw
  Use typedef long long blkcnt_t; for libnfs on msys2/mingw
  for futher implemenation, still disable nfs_get_allocated_file_size
  on win32 as it not working yet, but preserve the possibility implemenation
  it in futher

* gcrypt: test_tls_psk_init should write binary file instead text file.
  only fixes the file open mode parameter

* osdep: file locking functions are not available on Win32:
  Reword with "Do not declare the following locking functions on Win32:"

* meson: Use -b to ignore CR vs. CR-LF issues on Windows
  Reword of commit message

* tests: Enable crypto tests under msys2/mingw
  Reimplement qemu_socketpair in a simpler way. without thirdparty code

* block: enable libnfs on msys2/mingw in cirrus.yml
  New commit

* tests: disable /char/stdio/* tests in test-char.c on win32
  Needs review

* tests: fixes aio-win32 about aio_remove_fd_handler, get it consistence with=
 aio-posix.c
  Needs review

* rcu: fixes test-logging.c by call drain_call_rcu before rmdir_full
  Needs review

It first introduce msys2 CI on cirrus by fixes nfs, capstone, curses and
disable partial test-char tests.
And then fixes all unit tests failure on msys2/mingw
This fixes the reviews suggested in the mailling list
All cirrus CI are passed

Maxim Levitsky (1):
  rcu: Implement drain_call_rcu

Yonggang Luo (25):
  ci: fixes msys2 build by upgrading capstone to 4.0.2
  configure: Fixes ncursesw detection under msys2/mingw and enable
curses
  win32: Simplify gmtime_r detection direct base on
_POSIX_THREAD_SAFE_FUNCTIONS.
  curses: Fixes curses compiling errors.
  tests: disable /char/stdio/* tests in test-char.c on win32
  tests: Fixes test-replication.c on msys2/mingw.
  tests: test-replication disable /replication/secondary/* on
msys2/mingw.
  osdep: file locking functions are not available on Win32
  meson: Use -b to ignore CR vs. CR-LF issues on Windows
  gcrypt: test_tls_psk_init should write binary file instead text file.
  tests: Enable crypto tests under msys2/mingw
  meson: remove empty else and duplicated gio deps
  vmstate: Fixes test-vmstate.c on msys2/mingw
  cirrus: Building freebsd in a single short
  tests: Convert g_free to g_autofree macro in test-logging.c
  tests: Fixes test-io-channel-socket.c tests under msys2/mingw
  tests: fixes aio-win32 about aio_remove_fd_handler, get it consistence
with aio-posix.c
  tests: Fixes test-io-channel-file by mask only owner file state mask
bits
  tests: fix test-util-sockets.c
  tests: Fixes test-qdev-global-props.c
  rcu: fixes test-logging.c by call drain_call_rcu before rmdir_full
  meson: upgrade meson for execute custom ninjatool under msys2 properly
  ci: Enable msys2 ci in cirrus
  block: Fixes nfs compiling error on msys2/mingw
  block: enable libnfs on msys2/mingw in cirrus.yml

 .cirrus.yml  |  97 
 block/nfs.c  |  37 +--
 capstone |   2 +-
 configure|  61 +-
 include/qemu/osdep.h |   2 +-
 include/qemu/rcu.h   |   1 +
 include/sysemu/os-win32.h|   4 +-
 meson|   2 +-
 meson.build  |   6 --
 tests/crypto-tls-psk-helpers.c   |   6 +-
 tests/crypto-tls-x509-helpers.c  | 106 ++-
 tests/crypto-tls-x509-helpers.h  |   9 ++-
 tests/qapi-schema/meson.build|   2 +-
 tests/test-char.c|  26 +---
 tests/test-crypto-tlscredsx509.c |  47 +++---
 tests/test-crypto-tlssession.c   |  68 +++-
 tests/test-io-channel-file.c |  10 ++-
 tests/test-io-channel-socket.c   |   2 +
 tests/test-io-channel-tls.c  |  51 ---
 tests/test-logging.c |   5 +-
 tests/test-qdev-global-props.c   |   6 +-
 tests/test-replication.c |  22 +--
 tests/test-util-sockets.c|   6 +-
 tests/test-vmstate.c |   3 +-
 ui/curses.c  |  14 ++--
 util/aio-win32.c |  11 +++-
 util/oslib-win32.c   |   2 +-
 util/rcu.c   |  55 
 28 files changed, 460 insertions(+), 203 deletions(-)

--=20
2.28.0.windows.1




[PATCH v9 01/26] rcu: Implement drain_call_rcu

2020-09-15 Thread Yonggang Luo
From: Maxim Levitsky 

This will allow is to preserve the semantics of hmp_device_del,
that the device is deleted immediatly which was changed by previos
patch that delayed this to RCU callback

Signed-off-by: Maxim Levitsky 
Suggested-by: Stefan Hajnoczi 
Reviewed-by: Stefan Hajnoczi 
---
 include/qemu/rcu.h |  1 +
 util/rcu.c | 55 ++
 2 files changed, 56 insertions(+)

diff --git a/include/qemu/rcu.h b/include/qemu/rcu.h
index 570aa603eb..0e375ebe13 100644
--- a/include/qemu/rcu.h
+++ b/include/qemu/rcu.h
@@ -133,6 +133,7 @@ struct rcu_head {
 };
 
 extern void call_rcu1(struct rcu_head *head, RCUCBFunc *func);
+extern void drain_call_rcu(void);
 
 /* The operands of the minus operator must have the same type,
  * which must be the one that we specify in the cast.
diff --git a/util/rcu.c b/util/rcu.c
index 60a37f72c3..c4fefa9333 100644
--- a/util/rcu.c
+++ b/util/rcu.c
@@ -293,6 +293,61 @@ void call_rcu1(struct rcu_head *node, void (*func)(struct 
rcu_head *node))
 qemu_event_set(_call_ready_event);
 }
 
+
+struct rcu_drain {
+struct rcu_head rcu;
+QemuEvent drain_complete_event;
+};
+
+static void drain_rcu_callback(struct rcu_head *node)
+{
+struct rcu_drain *event = (struct rcu_drain *)node;
+qemu_event_set(>drain_complete_event);
+}
+
+/*
+ * This function ensures that all pending RCU callbacks
+ * on the current thread are done executing
+
+ * drops big qemu lock during the wait to allow RCU thread
+ * to process the callbacks
+ *
+ */
+
+void drain_call_rcu(void)
+{
+struct rcu_drain rcu_drain;
+bool locked = qemu_mutex_iothread_locked();
+
+memset(_drain, 0, sizeof(struct rcu_drain));
+qemu_event_init(_drain.drain_complete_event, false);
+
+if (locked) {
+qemu_mutex_unlock_iothread();
+}
+
+
+/*
+ * RCU callbacks are invoked in the same order as in which they
+ * are registered, thus we can be sure that when 'drain_rcu_callback'
+ * is called, all RCU callbacks that were registered on this thread
+ * prior to calling this function are completed.
+ *
+ * Note that since we have only one global queue of the RCU callbacks,
+ * we also end up waiting for most of RCU callbacks that were registered
+ * on the other threads, but this is a side effect that shoudn't be
+ * assumed.
+ */
+
+call_rcu1(_drain.rcu, drain_rcu_callback);
+qemu_event_wait(_drain.drain_complete_event);
+
+if (locked) {
+qemu_mutex_lock_iothread();
+}
+
+}
+
 void rcu_register_thread(void)
 {
 assert(rcu_reader.ctr == 0);
-- 
2.28.0.windows.1




[PATCH v9 03/26] configure: Fixes ncursesw detection under msys2/mingw and enable curses

2020-09-15 Thread Yonggang Luo
The mingw pkg-config are showing following absolute path and contains : as the 
separator,
so we must not use : as path separator. and we know the command line parameter 
are not likely
contains newline, we could use newline as path command line parameter separator

-D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=199506L 
-IC:/CI-Tools/msys64/mingw64/include/ncursesw:-I/usr/include/ncursesw:
-DNCURSES_WIDECHAR -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=199506L -IC -pipe 
-lncursesw -lgnurx -ltre -lintl -liconv
-DNCURSES_WIDECHAR -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=199506L -IC -lncursesw
-DNCURSES_WIDECHAR -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=199506L -IC -lcursesw
-DNCURSES_WIDECHAR /CI-Tools/msys64/mingw64/include/ncursesw -pipe -lncursesw 
-lgnurx -ltre -lintl -liconv
-DNCURSES_WIDECHAR /CI-Tools/msys64/mingw64/include/ncursesw -lncursesw
-DNCURSES_WIDECHAR /CI-Tools/msys64/mingw64/include/ncursesw -lcursesw
-DNCURSES_WIDECHAR -I/usr/include/ncursesw -pipe -lncursesw -lgnurx -ltre 
-lintl -liconv
-DNCURSES_WIDECHAR -I/usr/include/ncursesw -lncursesw
-DNCURSES_WIDECHAR -I/usr/include/ncursesw -lcursesw

Refer to https://unix.stackexchange.com/a/103011/218958

If your file names are guaranteed not to contain newlines, you can use newlines 
as the separator. W
hen you expand the variable, first turn off globbing with set -f and set the 
list of field splitting characters
IFS to contain only a newline.

msys2/mingw lacks the POSIX-required langinfo.h.

gcc test.c -DNCURSES_WIDECHAR -I/mingw64/include/ncursesw -pipe -lncursesw 
-lgnurx -ltre -lintl -liconv
test.c:4:10: fatal error: langinfo.h: No such file or directory
4 | #include 
  |  ^~~~
compilation terminated.

So we using g_get_codeset instead of nl_langinfo(CODESET)

Signed-off-by: Yonggang Luo 
Reviewed-by: Gerd Hoffmann 
---
 configure   | 25 +++--
 ui/curses.c | 10 +-
 2 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/configure b/configure
index f59080703e..dc4b7a2e55 100755
--- a/configure
+++ b/configure
@@ -3654,35 +3654,40 @@ if test "$iconv" = "no" ; then
 fi
 if test "$curses" != "no" ; then
   if test "$mingw32" = "yes" ; then
-curses_inc_list="$($pkg_config --cflags ncurses 2>/dev/null):"
-curses_lib_list="$($pkg_config --libs ncurses 2>/dev/null):-lpdcurses"
+curses_inc_list="$($pkg_config --cflags ncurses 2>/dev/null)
+  $($pkg_config --cflags ncursesw 2>/dev/null)"
+curses_lib_list="$($pkg_config --libs ncurses 2>/dev/null)
+  $($pkg_config --libs ncursesw 2>/dev/null)
+  -lpdcurses"
   else
-curses_inc_list="$($pkg_config --cflags ncursesw 
2>/dev/null):-I/usr/include/ncursesw:"
-curses_lib_list="$($pkg_config --libs ncursesw 
2>/dev/null):-lncursesw:-lcursesw"
+curses_inc_list="$($pkg_config --cflags ncursesw 2>/dev/null)
+  -I/usr/include/ncursesw:"
+curses_lib_list="$($pkg_config --libs ncursesw 2>/dev/null)
+  -lncursesw
+  -lcursesw"
   fi
   curses_found=no
   cat > $TMPC << EOF
 #include 
 #include 
 #include 
-#include 
 int main(void) {
-  const char *codeset;
   wchar_t wch = L'w';
   setlocale(LC_ALL, "");
   resize_term(0, 0);
   addwstr(L"wide chars\n");
   addnwstr(, 1);
   add_wch(WACS_DEGREE);
-  codeset = nl_langinfo(CODESET);
-  return codeset != 0;
+  return 0;
 }
 EOF
-  IFS=:
+  IFS='
+'   # turn off variable value expansion except for 
splitting at newlines
   for curses_inc in $curses_inc_list; do
 # Make sure we get the wide character prototypes
 curses_inc="-DNCURSES_WIDECHAR $curses_inc"
-IFS=:
+IFS='
+'   # turn off variable value expansion except for 
splitting at newlines
 for curses_lib in $curses_lib_list; do
   unset IFS
   if compile_prog "$curses_inc" "$curses_lib" ; then
diff --git a/ui/curses.c b/ui/curses.c
index a59b23a9cf..12bc682cf9 100644
--- a/ui/curses.c
+++ b/ui/curses.c
@@ -30,7 +30,6 @@
 #endif
 #include 
 #include 
-#include 
 #include 
 
 #include "qapi/error.h"
@@ -526,6 +525,7 @@ static void font_setup(void)
 iconv_t nativecharset_to_ucs2;
 iconv_t font_conv;
 int i;
+g_autofree gchar *local_codeset = g_get_codeset();
 
 /*
  * Control characters are normally non-printable, but VGA does have
@@ -566,14 +566,14 @@ static void font_setup(void)
   0x25bc
 };
 
-ucs2_to_nativecharset = iconv_open(nl_langinfo(CODESET), "UCS-2");
+ucs2_to_nativecharset = iconv_open(local_codeset, "UCS-2");
 if (ucs2_to_nativecharset == (iconv_t) -1) {
 fprintf(stderr, "Could not convert font glyphs from UCS-2: '%s'\n",
 strerror(errno));
 exit(1);
 }
 
-nativecharset_to_ucs2 = iconv_open("UCS-2", nl_langinfo(CODESET));
+nativecharset_to_ucs2 = iconv_open("UCS-2", local_codeset);
 if (nativecharset_to_ucs2 == (iconv_t) -1) {
 iconv_close(ucs2_to_nativecharset);
 fprintf(stderr, "Could not convert font glyphs to 

[PATCH v9 10/26] meson: Use -b to ignore CR vs. CR-LF issues on Windows

2020-09-15 Thread Yonggang Luo
Ideally we would use the '--strip-trailing-cr' option, but not
being POSIX is a portability problem (i.e. BSDs and Solaris
based OSes). Instead use the '-b' option which, although doing
slightly more, produce the expected result on Windows."

Signed-off-by: Yonggang Luo 
Reviewed-by: Eric Blake 
Reviewed-by: Daniel P. Berrangé 
---
 tests/qapi-schema/meson.build | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/qapi-schema/meson.build b/tests/qapi-schema/meson.build
index c87d141417..f1449298b0 100644
--- a/tests/qapi-schema/meson.build
+++ b/tests/qapi-schema/meson.build
@@ -220,6 +220,6 @@ qapi_doc = custom_target('QAPI doc',
 
 # "full_path()" needed here to work around
 # https://github.com/mesonbuild/meson/issues/7585
-test('QAPI doc', diff, args: ['-u', files('doc-good.texi'), 
qapi_doc[0].full_path()],
+test('QAPI doc', diff, args: ['-b', '-u', files('doc-good.texi'), 
qapi_doc[0].full_path()],
  depends: qapi_doc,
  suite: ['qapi-schema', 'qapi-doc'])
-- 
2.28.0.windows.1




[PATCH v9 13/26] meson: remove empty else and duplicated gio deps

2020-09-15 Thread Yonggang Luo
Signed-off-by: Yonggang Luo 
Reviewed-by: Daniel P. Berrangé 
---
 meson.build | 6 --
 1 file changed, 6 deletions(-)

diff --git a/meson.build b/meson.build
index 690723b470..23cb1b8742 100644
--- a/meson.build
+++ b/meson.build
@@ -317,7 +317,6 @@ opengl = not_found
 if 'CONFIG_OPENGL' in config_host
   opengl = declare_dependency(compile_args: 
config_host['OPENGL_CFLAGS'].split(),
   link_args: config_host['OPENGL_LIBS'].split())
-else
 endif
 gtk = not_found
 if 'CONFIG_GTK' in config_host
@@ -344,11 +343,6 @@ if 'CONFIG_ICONV' in config_host
   iconv = declare_dependency(compile_args: config_host['ICONV_CFLAGS'].split(),
  link_args: config_host['ICONV_LIBS'].split())
 endif
-gio = not_found
-if 'CONFIG_GIO' in config_host
-  gio = declare_dependency(compile_args: config_host['GIO_CFLAGS'].split(),
-   link_args: config_host['GIO_LIBS'].split())
-endif
 vnc = not_found
 png = not_found
 jpeg = not_found
-- 
2.28.0.windows.1




[PATCH v9 02/26] ci: fixes msys2 build by upgrading capstone to 4.0.2

2020-09-15 Thread Yonggang Luo
The currently random version capstone have the following compiling issue:
  CC  /c/work/xemu/qemu/build/slirp/src/arp_table.o
make[1]: *** No rule to make target 
“/c/work/xemu/qemu/build/capstone/capstone.lib”。 Stop.

Subproject commit 1d230532840a37ac032c6ab80128238fc930c6c1 are the tagged 
version 4.0.2
when upgrading to this version, the folder structure of include are changed to
qemu\capstone\include
│  platform.h
│
├─capstone
│  arm.h
│  arm64.h
│  capstone.h
│  evm.h
│  m680x.h
│  m68k.h
│  mips.h
│  platform.h
│  ppc.h
│  sparc.h
│  systemz.h
│  tms320c64x.h
│  x86.h
│  xcore.h
│
└─windowsce
intrin.h
stdint.h

in capstone. so we need add extra include path 
-I${source_path}/capstone/include/capstone
for directly #include , and the exist include path should preserve, 
because
in capstone code there something like #include "capstone/capstone.h"

If only using
capstone_cflags="-I${source_path}/capstone/include/capstone"
Then will cause the following compiling error:

  CC  cs.o
cs.c:17:10: fatal error: 'capstone/capstone.h' file not found
#include 
 ^
1 error generated.

Signed-off-by: Yonggang Luo 
---
 capstone  | 2 +-
 configure | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/capstone b/capstone
index 22ead3e0bf..1d23053284 16
--- a/capstone
+++ b/capstone
@@ -1 +1 @@
-Subproject commit 22ead3e0bfdb87516656453336160e0a37b066bf
+Subproject commit 1d230532840a37ac032c6ab80128238fc930c6c1
diff --git a/configure b/configure
index ce27eafb0a..f59080703e 100755
--- a/configure
+++ b/configure
@@ -5157,7 +5157,7 @@ case "$capstone" in
   LIBCAPSTONE=libcapstone.a
 fi
 capstone_libs="-Lcapstone -lcapstone"
-capstone_cflags="-I${source_path}/capstone/include"
+capstone_cflags="-I${source_path}/capstone/include 
-I${source_path}/capstone/include/capstone"
 ;;
 
   system)
-- 
2.28.0.windows.1




Re: [PULL 00/22] Block patches

2020-09-15 Thread Peter Maydell
On Tue, 15 Sep 2020 at 11:46, Max Reitz  wrote:
>
> The following changes since commit 2d2c73d0e3d504a61f868e46e6abd5643f38091b:
>
>   Merge remote-tracking branch 
> 'remotes/pmaydell/tags/pull-target-arm-20200914-1' into staging (2020-09-14 
> 16:03:08 +0100)
>
> are available in the Git repository at:
>
>   https://github.com/XanClic/qemu.git tags/pull-block-2020-09-15
>
> for you to fetch changes up to 7bae7c805d82675eb3a02c744093703d84ada2d6:
>
>   block/rbd: add 'namespace' to qemu_rbd_strong_runtime_opts[] (2020-09-15 
> 11:31:10 +0200)
>
> 
> Block patches:
> - Several qcow2 fixes and refactorings
> - Let qemu-img convert try to stay at cluster boundaries
> - Stable child names for quorum (with x-blockdev-change)
> - Explicitly drop vhdx 4k sector support, as it was never actually
>   working
> - rbd: Mark @namespace a strong runtime option
> - iotests.py improvements
> - Drop unused runtime_opts objects
> - Skip a test case in 030 when run through make check-block
>
> 


Applied, thanks.

Please update the changelog at https://wiki.qemu.org/ChangeLog/5.2
for any user-visible changes.

-- PMM



[PATCH v2 2/2] tests: add test-fdmon-epoll

2020-09-15 Thread Stefan Hajnoczi
Test aio_disable_external(), which switches from fdmon-epoll back to
fdmon-poll. This resulted in an assertion failure that was fixed in the
previous patch.

Signed-off-by: Stefan Hajnoczi 
---
 MAINTAINERS  |  1 +
 tests/test-fdmon-epoll.c | 73 
 tests/meson.build|  3 ++
 3 files changed, 77 insertions(+)
 create mode 100644 tests/test-fdmon-epoll.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 3d17cad19a..99e4010623 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2132,6 +2132,7 @@ F: migration/block*
 F: include/block/aio.h
 F: include/block/aio-wait.h
 F: scripts/qemugdb/aio.py
+F: tests/test-fdmon-epoll.c
 T: git https://github.com/stefanha/qemu.git block
 
 Block SCSI subsystem
diff --git a/tests/test-fdmon-epoll.c b/tests/test-fdmon-epoll.c
new file mode 100644
index 00..11fd8a2fa9
--- /dev/null
+++ b/tests/test-fdmon-epoll.c
@@ -0,0 +1,73 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * fdmon-epoll tests
+ *
+ * Copyright (c) 2020 Red Hat, Inc.
+ */
+
+#include "qemu/osdep.h"
+#include "block/aio.h"
+#include "qapi/error.h"
+#include "qemu/main-loop.h"
+
+static AioContext *ctx;
+
+static void dummy_fd_handler(EventNotifier *notifier)
+{
+event_notifier_test_and_clear(notifier);
+}
+
+static void add_event_notifiers(EventNotifier *notifiers, size_t n)
+{
+for (size_t i = 0; i < n; i++) {
+event_notifier_init([i], false);
+aio_set_event_notifier(ctx, [i], false,
+   dummy_fd_handler, NULL);
+}
+}
+
+static void remove_event_notifiers(EventNotifier *notifiers, size_t n)
+{
+for (size_t i = 0; i < n; i++) {
+aio_set_event_notifier(ctx, [i], false, NULL, NULL);
+event_notifier_cleanup([i]);
+}
+}
+
+/* Check that fd handlers work when external clients are disabled */
+static void test_external_disabled(void)
+{
+EventNotifier notifiers[100];
+
+/* fdmon-epoll is only enabled when many fd handlers are registered */
+add_event_notifiers(notifiers, G_N_ELEMENTS(notifiers));
+
+event_notifier_set([0]);
+assert(aio_poll(ctx, true));
+
+aio_disable_external(ctx);
+event_notifier_set([0]);
+assert(aio_poll(ctx, true));
+aio_enable_external(ctx);
+
+remove_event_notifiers(notifiers, G_N_ELEMENTS(notifiers));
+}
+
+int main(int argc, char **argv)
+{
+/*
+ * This code relies on the fact that fdmon-io_uring disables itself when
+ * the glib main loop is in use. The main loop uses fdmon-poll and upgrades
+ * to fdmon-epoll when the number of fds exceeds a threshold.
+ */
+qemu_init_main_loop(_fatal);
+ctx = qemu_get_aio_context();
+
+while (g_main_context_iteration(NULL, false)) {
+/* Do nothing */
+}
+
+g_test_init(, , NULL);
+g_test_add_func("/fdmon-epoll/external-disabled", test_external_disabled);
+return g_test_run();
+}
diff --git a/tests/meson.build b/tests/meson.build
index dae8a77df1..62277e6858 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -169,6 +169,9 @@ if have_block
   if 'CONFIG_NETTLE' in config_host or 'CONFIG_GCRYPT' in config_host
 tests += {'test-crypto-pbkdf': [io]}
   endif
+  if 'CONFIG_EPOLL_CREATE1' in config_host
+tests += {'test-fdmon-epoll': [testblock]}
+  endif
   benchs += {
  'benchmark-crypto-hash': [crypto],
  'benchmark-crypto-hmac': [crypto],
-- 
2.26.2



[PATCH v2 0/2] fdmon-poll: reset npfd when upgrading to fdmon-epoll

2020-09-15 Thread Stefan Hajnoczi
v2:
 * Rebased onto qemu.git/master for meson

Fix an assertion failure when aio_poll() is called in a aio_disable_external()
region. The failure happens when fdmon-poll is first upgraded to fdmon-epoll
and then downgraded again due to aio_disable_external().

Stefan Hajnoczi (2):
  fdmon-poll: reset npfd when upgrading to fdmon-epoll
  tests: add test-fdmon-epoll

 MAINTAINERS  |  1 +
 tests/test-fdmon-epoll.c | 73 
 util/fdmon-poll.c|  1 +
 tests/meson.build|  3 ++
 4 files changed, 78 insertions(+)
 create mode 100644 tests/test-fdmon-epoll.c

-- 
2.26.2



[PATCH v2 1/2] fdmon-poll: reset npfd when upgrading to fdmon-epoll

2020-09-15 Thread Stefan Hajnoczi
npfd keeps track of how many pollfds are currently being monitored. It
must be reset to 0 when fdmon_poll_wait() returns.

When npfd reaches a treshold we switch to fdmon-epoll because it scales
better.

This patch resets npfd in the case where we switch to fdmon-epoll.
Forgetting to do so results in the following assertion failure:

  util/fdmon-poll.c:65: fdmon_poll_wait: Assertion `npfd == 0' failed.

Buglink: https://bugzilla.redhat.com/show_bug.cgi?id=1869952
Fixes: 1f050a4690f62a1e7dabc4f44141e9f762c3769f ("aio-posix: extract ppoll(2) 
and epoll(7) fd monitoring")
Reviewed-by: Philippe Mathieu-Daudé 
Signed-off-by: Stefan Hajnoczi 
---
 util/fdmon-poll.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/util/fdmon-poll.c b/util/fdmon-poll.c
index 488067b679..5fe3b47865 100644
--- a/util/fdmon-poll.c
+++ b/util/fdmon-poll.c
@@ -73,6 +73,7 @@ static int fdmon_poll_wait(AioContext *ctx, AioHandlerList 
*ready_list,
 
 /* epoll(7) is faster above a certain number of fds */
 if (fdmon_epoll_try_upgrade(ctx, npfd)) {
+npfd = 0; /* we won't need pollfds[], reset npfd */
 return ctx->fdmon_ops->wait(ctx, ready_list, timeout);
 }
 
-- 
2.26.2



Re: [PATCH v8 27/27] Revert "configure: add --ninja option"

2020-09-15 Thread Yonggang Luo
On Tue, Sep 15, 2020 at 7:44 PM Thomas Huth  wrote:

> On 13/09/2020 16.08, Paolo Bonzini wrote:
> > On 13/09/20 00:44, Yonggang Luo wrote:
> >> This reverts commit 48328880fddf0145bdccc499160fb24dfabfbd41.
> >>
> >> The --ninja option doesn't need anymore because of upgrade meson to
> 0.55.2
> >> At that version we can use ninjatool
> >
> > We might actually get rid of ninjatool before QEMU 5.2 goes out, if we
> > decide to make Ninja a mandatory build dependency.  So we can hold on
> > patches 26 and 27.  Thanks for testing though!
> >
> > I'm also not sure about patch 16, since that's not my area, but Daniel
> > and Ed both reviewed it so that's okay.
> >
> > Finally, instead of checking !_WIN32 it's better to check CONFIG_POSIX
> > or CONFIG_WIN32.  That can be changed on commit though.
> >
> > Everything else seems okay.  I'll wait a couple days and queue the whole
> > bunch up to patch 25.
>
> Patch 13 definitely needs a replacement, and I think patch 2 should
> likely go through the block tree instead...
>
I am prepareing V9, and move nfs related things to the end  patch 13 are
re-implemented
please wait for some minutes

>
> But I recently started to experiment with these patches, too, and I
> think I got a reasonable subset now which should be OK for getting
> merged (e.g. disabling NFS and the crypto test in the CI for now). I'll
> take those through my testing tree. The remaining work can then be done
> on top later.
>
>  Thomas
>
>

-- 
 此致
礼
罗勇刚
Yours
sincerely,
Yonggang Luo


Re: Potential regression in 'qemu-img convert' to LVM

2020-09-15 Thread Stefan Reiter

On 9/15/20 11:08 AM, Nir Soffer wrote:

On Mon, Sep 14, 2020 at 3:25 PM Stefan Reiter  wrote:


Hi list,

following command fails since 5.1 (tested on kernel 5.4.60):

# qemu-img convert -p -f raw -O raw /dev/zvol/pool/disk-1 /dev/vg/disk-1
qemu-img: error while writing at byte 2157968896: Device or resource busy

(source is ZFS here, but doesn't matter in practice, it always fails the
same; offset changes slightly but consistently hovers around 2^31)

strace shows the following:
fallocate(13, FALLOC_FL_KEEP_SIZE|FALLOC_FL_PUNCH_HOLE, 2157968896,
4608) = -1 EBUSY (Device or resource busy)


What is the size of the LV?



Same as the source, 5GB in my test case. Created with:

# lvcreate -ay --size 5242880k --name disk-1 vg


Does it happen if you change sparse minimum size (-S)?

For example: -S 64k

 qemu-img convert -p -f raw -O raw -S 64k /dev/zvol/pool/disk-1
/dev/vg/disk-1



Tried a few different values, always the same result: EBUSY at byte 
2157968896.



Other fallocate calls leading up to this work fine.

This happens since commit edafc70c0c "qemu-img convert: Don't pre-zero
images", before that all fallocates happened at the start. Reverting the
commit and calling qemu-img exactly the same way on the same data works
fine.


But slowly, doing up to 100% more work for fully allocated images.



Of course, I'm not saying the patch is wrong, reverting it just avoids 
triggering the bug.



Simply retrying the syscall on EBUSY (like EINTR) does *not* work,
once it fails it keeps failing with the same error.

I couldn't find anything related to EBUSY on fallocate, and it only
happens on LVM targets... Any idea or pointers where to look?


Is this thin LV?



No, regular LV. See command above.


This works for us using regular LVs.

Which kernel? which distro?



Reproducible on:
* PVE w/ kernel 5.4.60 (Ubuntu based)
* Manjaro w/ kernel 5.8.6

I found that it does not happen with all images, I suppose there must be 
a certain number of smaller holes for it to happen. I am using a VM 
image with a bare-bones Alpine Linux installation, but it's not an 
isolated case, we've had two people report the issue on our bug tracker: 
https://bugzilla.proxmox.com/show_bug.cgi?id=3002


Thanks,
Stefan


Nir







Re: [PATCH v8 27/27] Revert "configure: add --ninja option"

2020-09-15 Thread Thomas Huth
On 13/09/2020 16.08, Paolo Bonzini wrote:
> On 13/09/20 00:44, Yonggang Luo wrote:
>> This reverts commit 48328880fddf0145bdccc499160fb24dfabfbd41.
>>
>> The --ninja option doesn't need anymore because of upgrade meson to 0.55.2
>> At that version we can use ninjatool
> 
> We might actually get rid of ninjatool before QEMU 5.2 goes out, if we
> decide to make Ninja a mandatory build dependency.  So we can hold on
> patches 26 and 27.  Thanks for testing though!
> 
> I'm also not sure about patch 16, since that's not my area, but Daniel
> and Ed both reviewed it so that's okay.
> 
> Finally, instead of checking !_WIN32 it's better to check CONFIG_POSIX
> or CONFIG_WIN32.  That can be changed on commit though.
> 
> Everything else seems okay.  I'll wait a couple days and queue the whole
> bunch up to patch 25.

Patch 13 definitely needs a replacement, and I think patch 2 should
likely go through the block tree instead...

But I recently started to experiment with these patches, too, and I
think I got a reasonable subset now which should be OK for getting
merged (e.g. disabling NFS and the crypto test in the CI for now). I'll
take those through my testing tree. The remaining work can then be done
on top later.

 Thomas




[PATCH v4 9/9] migration: introduce snapshot-{save, load, delete} QMP commands

2020-09-15 Thread Daniel P . Berrangé
savevm, loadvm and delvm are some of the few HMP commands that have never
been converted to use QMP. The reasons for the lack of conversion are
that they blocked execution of the event thread, and the semantics
around choice of disks were ill-defined.

Despite this downside, however, libvirt and applications using libvirt
have used these commands for as long as QMP has existed, via the
"human-monitor-command" passthrough command. IOW, while it is clearly
desirable to be able to fix the problems, they are not a blocker to
all real world usage.

Meanwhile there is a need for other features which involve adding new
parameters to the commands. This is possible with HMP passthrough, but
it provides no reliable way for apps to introspect features, so using
QAPI modelling is highly desirable.

This patch thus introduces new snapshot-{load,save,delete} commands to
QMP that are intended to replace the old HMP counterparts. The new
commands are given different names, because they will be using the new
QEMU job framework and thus will have diverging behaviour from the HMP
originals. It would thus be misleading to keep the same name.

While this design uses the generic job framework, the current impl is
still blocking. The intention that the blocking problem is fixed later.
None the less applications using these new commands should assume that
they are asynchronous and thus wait for the job status change event to
indicate completion.

In addition to using the job framework, the new commands require the
caller to be explicit about all the block device nodes used in the
snapshot operations, with no built-in default heuristics in use.

Signed-off-by: Daniel P. Berrangé 
---
 migration/savevm.c | 183 
 qapi/job.json  |   9 +-
 qapi/migration.json| 120 +++
 tests/qemu-iotests/310 | 338 ++
 tests/qemu-iotests/310.out | 412 +
 tests/qemu-iotests/group   |   1 +
 6 files changed, 1062 insertions(+), 1 deletion(-)
 create mode 100755 tests/qemu-iotests/310
 create mode 100644 tests/qemu-iotests/310.out

diff --git a/migration/savevm.c b/migration/savevm.c
index 56f85be250..85b50953ad 100644
--- a/migration/savevm.c
+++ b/migration/savevm.c
@@ -2980,3 +2980,186 @@ bool vmstate_check_only_migratable(const 
VMStateDescription *vmsd)
 
 return !(vmsd && vmsd->unmigratable);
 }
+
+typedef struct SnapshotJob {
+Job common;
+char *tag;
+char *vmstate;
+strList *devices;
+Coroutine *co;
+Error **errp;
+int ret;
+} SnapshotJob;
+
+static void qmp_snapshot_job_free(SnapshotJob *s)
+{
+g_free(s->tag);
+g_free(s->vmstate);
+qapi_free_strList(s->devices);
+}
+
+
+static void snapshot_load_job_bh(void *opaque)
+{
+Job *job = opaque;
+SnapshotJob *s = container_of(job, SnapshotJob, common);
+int saved_vm_running;
+
+job_progress_set_remaining(>common, 1);
+
+saved_vm_running = runstate_is_running();
+vm_stop(RUN_STATE_RESTORE_VM);
+
+s->ret = load_snapshot(s->tag, s->vmstate, true, s->devices, s->errp);
+if (s->ret == 0 && saved_vm_running) {
+vm_start();
+}
+
+job_progress_update(>common, 1);
+
+qmp_snapshot_job_free(s);
+aio_co_wake(s->co);
+}
+
+static void snapshot_save_job_bh(void *opaque)
+{
+Job *job = opaque;
+SnapshotJob *s = container_of(job, SnapshotJob, common);
+
+job_progress_set_remaining(>common, 1);
+s->ret = save_snapshot(s->tag, false, s->vmstate, true, s->devices, 
s->errp);
+job_progress_update(>common, 1);
+
+qmp_snapshot_job_free(s);
+aio_co_wake(s->co);
+}
+
+static void snapshot_delete_job_bh(void *opaque)
+{
+Job *job = opaque;
+SnapshotJob *s = container_of(job, SnapshotJob, common);
+
+job_progress_set_remaining(>common, 1);
+s->ret = delete_snapshot(s->tag, true, s->devices, s->errp);
+job_progress_update(>common, 1);
+
+qmp_snapshot_job_free(s);
+aio_co_wake(s->co);
+}
+
+static int coroutine_fn snapshot_save_job_run(Job *job, Error **errp)
+{
+SnapshotJob *s = container_of(job, SnapshotJob, common);
+s->errp = errp;
+s->co = qemu_coroutine_self();
+aio_bh_schedule_oneshot(qemu_get_aio_context(),
+snapshot_save_job_bh, job);
+qemu_coroutine_yield();
+return s->ret;
+}
+
+static int coroutine_fn snapshot_load_job_run(Job *job, Error **errp)
+{
+SnapshotJob *s = container_of(job, SnapshotJob, common);
+s->errp = errp;
+s->co = qemu_coroutine_self();
+aio_bh_schedule_oneshot(qemu_get_aio_context(),
+snapshot_load_job_bh, job);
+qemu_coroutine_yield();
+return s->ret;
+}
+
+static int coroutine_fn snapshot_delete_job_run(Job *job, Error **errp)
+{
+SnapshotJob *s = container_of(job, SnapshotJob, common);
+s->errp = errp;
+s->co = qemu_coroutine_self();
+aio_bh_schedule_oneshot(qemu_get_aio_context(),
+  

[PATCH v4 3/9] block: add ability to specify list of blockdevs during snapshot

2020-09-15 Thread Daniel P . Berrangé
When running snapshot operations, there are various rules for which
blockdevs are included/excluded. While this provides reasonable default
behaviour, there are scenarios that are not well handled by the default
logic. Some of the conditions do not have a single correct answer.

Thus there needs to be a way for the mgmt app to provide an explicit
list of blockdevs to perform snapshots across. This can be achieved
by passing a list of node names that should be used.

Signed-off-by: Daniel P. Berrangé 
---
 block/monitor/block-hmp-cmds.c |   4 +-
 block/snapshot.c   | 180 -
 include/block/snapshot.h   |  22 ++--
 migration/savevm.c |  16 +--
 monitor/hmp-cmds.c |   2 +-
 5 files changed, 160 insertions(+), 64 deletions(-)

diff --git a/block/monitor/block-hmp-cmds.c b/block/monitor/block-hmp-cmds.c
index bec794253d..30279bd15a 100644
--- a/block/monitor/block-hmp-cmds.c
+++ b/block/monitor/block-hmp-cmds.c
@@ -900,7 +900,7 @@ void hmp_info_snapshots(Monitor *mon, const QDict *qdict)
 SnapshotEntry *snapshot_entry;
 Error *err = NULL;
 
-bs = bdrv_all_find_vmstate_bs();
+bs = bdrv_all_find_vmstate_bs(false, NULL, );
 if (!bs) {
 error_report_err(err);
 return;
@@ -952,7 +952,7 @@ void hmp_info_snapshots(Monitor *mon, const QDict *qdict)
 total = 0;
 for (i = 0; i < nb_sns; i++) {
 SnapshotEntry *next_sn;
-if (bdrv_all_find_snapshot(sn_tab[i].name, NULL) == 0) {
+if (bdrv_all_find_snapshot(sn_tab[i].name, false, NULL, NULL) == 0) {
 global_snapshots[total] = i;
 total++;
 QTAILQ_FOREACH(image_entry, _list, next) {
diff --git a/block/snapshot.c b/block/snapshot.c
index 50e35bb9fa..155b8aad88 100644
--- a/block/snapshot.c
+++ b/block/snapshot.c
@@ -447,6 +447,41 @@ int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState 
*bs,
 return ret;
 }
 
+
+static int bdrv_all_get_snapshot_devices(bool has_devices, strList *devices,
+ GList **all_bdrvs,
+ Error **errp)
+{
+g_autoptr(GList) bdrvs = NULL;
+
+if (has_devices) {
+if (!devices) {
+error_setg(errp, "At least one device is required for snapshot");
+return -1;
+}
+
+while (devices) {
+BlockDriverState *bs = bdrv_find_node(devices->value);
+if (!bs) {
+error_setg(errp, "No block device node '%s'", devices->value);
+return -1;
+}
+bdrvs = g_list_append(bdrvs, bs);
+devices = devices->next;
+}
+} else {
+BlockDriverState *bs;
+BdrvNextIterator it;
+for (bs = bdrv_first(); bs; bs = bdrv_next()) {
+bdrvs = g_list_append(bdrvs, bs);
+}
+}
+
+*all_bdrvs = g_steal_pointer();
+return 0;
+}
+
+
 static bool bdrv_all_snapshots_includes_bs(BlockDriverState *bs)
 {
 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
@@ -462,43 +497,59 @@ static bool 
bdrv_all_snapshots_includes_bs(BlockDriverState *bs)
  * These functions will properly handle dataplane (take aio_context_acquire
  * when appropriate for appropriate block drivers) */
 
-bool bdrv_all_can_snapshot(Error **errp)
+bool bdrv_all_can_snapshot(bool has_devices, strList *devices,
+   Error **errp)
 {
-BlockDriverState *bs;
-BdrvNextIterator it;
+g_autoptr(GList) bdrvs = NULL;
+GList *iterbdrvs;
 
-for (bs = bdrv_first(); bs; bs = bdrv_next()) {
+if (bdrv_all_get_snapshot_devices(has_devices, devices, , errp) < 0) 
{
+return false;
+}
+
+iterbdrvs = bdrvs;
+while (iterbdrvs) {
+BlockDriverState *bs = iterbdrvs->data;
 AioContext *ctx = bdrv_get_aio_context(bs);
 bool ok;
 
 aio_context_acquire(ctx);
-if (bdrv_all_snapshots_includes_bs(bs)) {
+if (devices || bdrv_all_snapshots_includes_bs(bs)) {
 ok = bdrv_can_snapshot(bs);
 }
 aio_context_release(ctx);
 if (!ok) {
 error_setg(errp, "Device '%s' is writable but does not support "
"snapshots", bdrv_get_device_or_node_name(bs));
-bdrv_next_cleanup();
 return false;
 }
+
+iterbdrvs = iterbdrvs->next;
 }
 
 return true;
 }
 
-int bdrv_all_delete_snapshot(const char *name, Error **errp)
+int bdrv_all_delete_snapshot(const char *name,
+ bool has_devices, strList *devices,
+ Error **errp)
 {
-BlockDriverState *bs;
-BdrvNextIterator it;
-QEMUSnapshotInfo sn1, *snapshot = 
+g_autoptr(GList) bdrvs = NULL;
+GList *iterbdrvs;
+
+if (bdrv_all_get_snapshot_devices(has_devices, devices, , errp) < 0) 
{
+return -1;
+}
 
-for (bs = bdrv_first(); bs; bs = bdrv_next()) {
+iterbdrvs = 

[PATCH v4 8/9] iotests: add support for capturing and matching QMP events

2020-09-15 Thread Daniel P . Berrangé
When using the _launch_qemu and _send_qemu_cmd functions from
common.qemu, any QMP events get mixed in with the output from
the commands and responses.

This makes it difficult to write a test case as the ordering
of events in the output is not stable.

This introduces a variable 'capture_events' which can be set
to a list of event names. Any events listed in this variable
will not be printed, instead collected in the $QEMU_EVENTS
environment variable.

A new '_wait_event' function can be invoked to collect events
at a fixed point in time. The function will first pull events
cached in $QEMU_EVENTS variable, and if none are found, will
then read more from QMP.

Signed-off-by: Daniel P. Berrangé 
---
 tests/qemu-iotests/common.qemu | 107 -
 1 file changed, 106 insertions(+), 1 deletion(-)

diff --git a/tests/qemu-iotests/common.qemu b/tests/qemu-iotests/common.qemu
index de680cf1c7..87d7a54001 100644
--- a/tests/qemu-iotests/common.qemu
+++ b/tests/qemu-iotests/common.qemu
@@ -53,6 +53,15 @@ _in_fd=4
 # If $mismatch_only is set, only non-matching responses will
 # be echoed.
 #
+# If $capture_events is non-empty, then any QMP event names it lists
+# will not be echoed out, but instead collected in the $QEMU_EVENTS
+# variable. The _wait_event function can later be used to received
+# the cached events.
+#
+# If $only_capture_events is set to anything but an empty string,
+# when an error will be raised if a QMP message is seen which is
+# not an event listed in $capture_events.
+#
 # If $success_or_failure is set, the meaning of the arguments is
 # changed as follows:
 # $2: A string to search for in the response; if found, this indicates
@@ -78,6 +87,32 @@ _timed_wait_for()
 QEMU_STATUS[$h]=0
 while IFS= read -t ${QEMU_COMM_TIMEOUT} resp <&${QEMU_OUT[$h]}
 do
+if [ -n "$capture_events" ]; then
+capture=0
+local evname
+for evname in $capture_events
+do
+grep -q "\"event\": \"${evname}\"" < <(echo "${resp}")
+if [ $? -eq 0 ]; then
+capture=1
+fi
+done
+if [ $capture = 1 ];
+then
+ev=$(echo "${resp}" | tr -d '\r' | tr % .)
+QEMU_EVENTS="${QEMU_EVENTS:+${QEMU_EVENTS}%}${ev}"
+if [ -n "$only_capture_events" ]; then
+return
+else
+continue
+fi
+fi
+fi
+if [ -n "$only_capture_events" ]; then
+echo "Only expected $capture_events but got ${resp}"
+exit 1
+fi
+
 if [ -z "${silent}" ] && [ -z "${mismatch_only}" ]; then
 echo "${resp}" | _filter_testdir | _filter_qemu \
| _filter_qemu_io | _filter_qmp | _filter_hmp
@@ -177,12 +212,82 @@ _send_qemu_cmd()
 let count--;
 done
 if [ ${QEMU_STATUS[$h]} -ne 0 ] && [ -z "${qemu_error_no_exit}" ]; then
-echo "Timeout waiting for ${1} on handle ${h}"
+echo "Timeout waiting for command ${1} response on handle ${h}"
 exit 1 #Timeout means the test failed
 fi
 }
 
 
+# Check event cache for a named QMP event
+#
+# Input parameters:
+# $1:   Name of the QMP event to check for
+#
+# Checks if the named QMP event that was previously captured
+# into $QEMU_EVENTS. When matched, the QMP event will be echoed
+# and the $matched variable set to 1.
+#
+# _wait_event is more suitable for test usage in most cases
+_check_cached_events()
+{
+local evname=${1}
+
+local match="\"event\": \"$evname\""
+
+matched=0
+if [ -n "$QEMU_EVENTS" ]; then
+CURRENT_QEMU_EVENTS=$QEMU_EVENTS
+QEMU_EVENTS=
+old_IFS=$IFS
+IFS="%"
+for ev in $CURRENT_QEMU_EVENTS
+do
+grep -q "$match" < <(echo "${ev}")
+if [ $? -eq 0 -a $matched = 0 ]; then
+echo "${ev}" | _filter_testdir | _filter_qemu \
+   | _filter_qemu_io | _filter_qmp | _filter_hmp
+matched=1
+else
+QEMU_EVENTS="${QEMU_EVENTS:+${QEMU_EVENTS}%}${ev}"
+fi
+done
+IFS=$old_IFS
+fi
+}
+
+# Wait for a named QMP event
+#
+# Input parameters:
+# $1:   QEMU handle to use
+# $2:   Name of the QMP event to wait for
+#
+# Checks if the named QMP event that was previously captured
+# into $QEMU_EVENTS. If none are present, then waits for the
+# event to arrive on the QMP channel. When matched, the QMP
+# event will be echoed
+_wait_event()
+{
+local h=${1}
+local evname=${2}
+
+while true
+do
+_check_cached_events $evname
+
+if [ $matched = 1 ];
+then
+return
+fi
+
+only_capture_events=1 qemu_error_no_exit=1 _timed_wait_for ${h}
+
+if [ ${QEMU_STATUS[$h]} -ne 0 ] ; then
+echo "Timeout waiting for 

[PATCH v4 5/9] migration: control whether snapshots are ovewritten

2020-09-15 Thread Daniel P . Berrangé
The traditional HMP "savevm" command will overwrite an existing snapshot
if it already exists with the requested name. This new flag allows this
to be controlled allowing for safer behaviour with a future QMP command.

Signed-off-by: Daniel P. Berrangé 
---
 include/migration/snapshot.h | 2 +-
 migration/savevm.c   | 4 ++--
 monitor/hmp-cmds.c   | 2 +-
 replay/replay-snapshot.c | 2 +-
 4 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/include/migration/snapshot.h b/include/migration/snapshot.h
index c85b6ec75b..d7db1174ef 100644
--- a/include/migration/snapshot.h
+++ b/include/migration/snapshot.h
@@ -15,7 +15,7 @@
 #ifndef QEMU_MIGRATION_SNAPSHOT_H
 #define QEMU_MIGRATION_SNAPSHOT_H
 
-int save_snapshot(const char *name, Error **errp);
+int save_snapshot(const char *name, bool overwrite, Error **errp);
 int load_snapshot(const char *name, Error **errp);
 
 #endif
diff --git a/migration/savevm.c b/migration/savevm.c
index 76972d69b0..2025e3e579 100644
--- a/migration/savevm.c
+++ b/migration/savevm.c
@@ -2658,7 +2658,7 @@ int qemu_load_device_state(QEMUFile *f)
 return 0;
 }
 
-int save_snapshot(const char *name, Error **errp)
+int save_snapshot(const char *name, bool overwrite, Error **errp)
 {
 BlockDriverState *bs;
 QEMUSnapshotInfo sn1, *sn = , old_sn1, *old_sn = _sn1;
@@ -2685,7 +2685,7 @@ int save_snapshot(const char *name, Error **errp)
 }
 
 /* Delete old snapshots of the same name */
-if (name) {
+if (name && overwrite) {
 if (bdrv_all_delete_snapshot(name, false, NULL, errp) < 0) {
 return ret;
 }
diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
index 98c98ae182..c1b8533d0c 100644
--- a/monitor/hmp-cmds.c
+++ b/monitor/hmp-cmds.c
@@ -1131,7 +1131,7 @@ void hmp_savevm(Monitor *mon, const QDict *qdict)
 {
 Error *err = NULL;
 
-save_snapshot(qdict_get_try_str(qdict, "name"), );
+save_snapshot(qdict_get_try_str(qdict, "name"), true, );
 hmp_handle_error(mon, err);
 }
 
diff --git a/replay/replay-snapshot.c b/replay/replay-snapshot.c
index e26fa4c892..8e7ff97d11 100644
--- a/replay/replay-snapshot.c
+++ b/replay/replay-snapshot.c
@@ -77,7 +77,7 @@ void replay_vmstate_init(void)
 
 if (replay_snapshot) {
 if (replay_mode == REPLAY_MODE_RECORD) {
-if (save_snapshot(replay_snapshot, ) != 0) {
+if (save_snapshot(replay_snapshot, true, ) != 0) {
 error_report_err(err);
 error_report("Could not create snapshot for icount record");
 exit(1);
-- 
2.26.2




  1   2   >