Skip to contentSkip to conversation

'qemu-nbd' explicit flush

Qemu-devel Mark Trumpold 3 participants 5 messages May 23, 2013 – May 27, 2013

lore
Share & tools
lore
#0 lore raw

I have a working configuration using the signal approach suggested by Stefan.

'qemu-nbd.c' is patched as follows:

    do {
        main_loop_wait(false);
+       if (sighup_reported) {
+           sighup_reported = false;
+           bdrv_drain_all();
+           bdrv_flush_all();
        }
    } while (!sigterm_reported && (persistent || !nbd_started || nb_fds > 0));

The driving script was patched as follows:

mount -o remount,ro /dev/nbd0
blockdev --flushbufs /dev/nbd0
+ kill -HUP <qemu-nbd process id>

I needed to retain 'blockdev --flushbufs' for things to work. Seems the 'bdrv_flush_all' is flushing what is being missed by the blockdev flush. I did not go back an retest with 'fsync' or other approaches I had tried before.

Thanks again Paolo and Stefan for your help!!
Regards,
Mark T.

-----Original Message-----
From: Paolo Bonzini [mailto:[email protected]]
Sent: Wednesday, May 22, 2013 04:07 AM
To: 'Stefan Hajnoczi'
Cc: 'Mark Trumpold', [email protected], [email protected]
Subject: Re: 'qemu-nbd' explicit flush

Il 22/05/2013 11:47, Stefan Hajnoczi ha scritto:

Quoted 11 lines · Mark Trumpold
Quoted 2 lines

Linux kernel 3.3.1 with Qemu patch to enable kernel flushing:
http://thread.gmane.org/gmane.linux.drivers.nbd.general/1108

Did you check that the kernel is sending NBD_FLUSH commands? You can use tcpdump and then check the captured network traffic.

Quoted 4 lines

Usage example:
'qemu-nbd --cache=writeback -c /dev/nbd0 /images/my-qcow.img' 'mount /dev/nbd0 /my-mount-point'

Quoted 7 lines
Quoted 5 lines

Everything does flush correctly when I first unmount and then disconnect the device; however, in my case I am not able to unmount things before snapshotting.

I tried several approaches externally to flush the device. For example:
'mount -o remount,ro /dev/nbd0'
'blockdev --flushbufs /dev/nbd0'

Did you try plain old sync(1)?

This could also work:

dd if=/dev/zero of=dummy oflag=sync bs=512 count=1

Quoted 2 lines

1. Add a signal handler (like SIGHUP or SIGUSR1) to qemu-nbd which flushes all exports.

That would be a useful addition anyway.

Paolo

#1 lore raw
Quoted 13 lines · Mark Trumpold

I have a working configuration using the signal approach suggested by Stefan.

'qemu-nbd.c' is patched as follows:

    do {
        main_loop_wait(false);
(quoted diff) +4 -0
+       if (sighup_reported) {
+           sighup_reported = false;
+           bdrv_drain_all();
+           bdrv_flush_all();
        }
    } while (!sigterm_reported && (persistent || !nbd_started || nb_fds > 0));
Quoted 7 lines

The driving script was patched as follows:

mount -o remount,ro /dev/nbd0
blockdev --flushbufs /dev/nbd0
+ kill -HUP <qemu-nbd process id>

I needed to retain 'blockdev --flushbufs' for things to work. Seems the 'bdrv_flush_all' is flushing what is being missed by the blockdev flush. I did not go back an retest with 'fsync' or other approaches I had tried before.

Okay, that makes sense:

'blockdev --flushbufs' is writing dirty pages to the NBD device.

bdrv_drain_all() + bdrv_flush_all() ensures that image file writes reach the physical disk.

One thing to be careful of is whether these operations are asynchronous. The signal is asynchronous, you have no way of knowing when qemu-nbd is finished flushing to the physical disk.

I didn't check blockdev(8) but it could be the same there.

So watch out, otherwise your script is timing-dependent and may not actually have finished flushing when you take the snapshot.

Stefan

#4 lore raw
In reply to Mark Trumpold

Il 23/05/2013 23:58, Mark Trumpold ha scritto:

Quoted 13 lines

I have a working configuration using the signal approach suggested by Stefan.

'qemu-nbd.c' is patched as follows:

    do {
        main_loop_wait(false);
(quoted diff) +4 -0
+       if (sighup_reported) {
+           sighup_reported = false;
+           bdrv_drain_all();
+           bdrv_flush_all();
        }
    } while (!sigterm_reported && (persistent || !nbd_started || nb_fds > 0));
Quoted 1 line

The driving script was patched as follows:

Yes, a patch along these lines would be acceptable.

Quoted 8 lines

mount -o remount,ro /dev/nbd0
blockdev --flushbufs /dev/nbd0
+ kill -HUP <qemu-nbd process id>

I needed to retain 'blockdev --flushbufs' for things to work. Seems the 'bdrv_flush_all' is flushing what is being missed by the blockdev flush. I did not go back an retest with 'fsync' or other approaches I had tried before.

Right. That said, I think a newer kernel would do what you want. Perhaps you can look at the actual patches that went into 3.9 and backport them.

Paolo

#2 lore raw
In reply to Stefan Hajnoczi
Quoted 29 lines · Stefan Hajnoczi
Quoted 17 lines · Mark Trumpold

I have a working configuration using the signal approach suggested by Stefan.

'qemu-nbd.c' is patched as follows:

    do {
        main_loop_wait(false);
(quoted diff) +4 -0
+       if (sighup_reported) {
+           sighup_reported = false;
+           bdrv_drain_all();
+           bdrv_flush_all();
        }
    } while (!sigterm_reported && (persistent || !nbd_started || nb_fds
Quoted 1 line

0));

The driving script was patched as follows:

Quoted 8 lines

mount -o remount,ro /dev/nbd0
blockdev --flushbufs /dev/nbd0
+ kill -HUP <qemu-nbd process id>

I needed to retain 'blockdev --flushbufs' for things to work. Seems the 'bdrv_flush_all' is flushing what is being missed by the blockdev flush. I did not go back an retest with 'fsync' or other approaches I had tried before.

Okay, that makes sense:

Quoted 8 lines

'blockdev --flushbufs' is writing dirty pages to the NBD device.

bdrv_drain_all() + bdrv_flush_all() ensures that image file writes reach the physical disk.

One thing to be careful of is whether these operations are asynchronous. The signal is asynchronous, you have no way of knowing when qemu-nbd is finished flushing to the physical disk.

Right, of course. I missed the obvious.

Quoted 8 lines

I didn't check blockdev(8) but it could be the same there.

So watch out, otherwise your script is timing-dependent and may not actually have finished flushing when you take the snapshot.

Stefan

The race condition would not be acceptable. You had mentioned another approach using the socket interface:

Quoted 4 lines

2. Instantiate a block/nbd.c client that connects to the running qemu-nbd server (make sure format=raw). Then call bdrv_flush() on the NBD client. You must use the qemu-nbd --shared=2 option.

In my case I only have a 'qemu-nbd' process per loop device. Would the 'qemu-nbd' process act as the socket server, and I would then write a simple socket client to instruct him to do the flush? And, would the client block until the flush is complete?

Thank you,
Mark T.

#3 lore raw
Quoted 7 lines · Mark Trumpold
Quoted 4 lines · Stefan Hajnoczi

On Thu, May 23, 2013 at 09:58:31PM +0000, Mark Trumpold wrote:
One thing to be careful of is whether these operations are asynchronous. The signal is asynchronous, you have no way of knowing when qemu-nbd is finished flushing to the physical disk.

Right, of course. I missed the obvious.

I missed something too. Paolo may have already hinted at this when he posted a dd oflag=sync command-line option:

blockdev --flushbufs is the wrong tool because ioctl(BLKFLSBUF) only writes out dirty pages to the block device. It does *not* guarantee to send a flush request to the device.

Therefore, the underlying image file may not be put into an up-to-date state by qemu-nbd.

I suggest trying the following instead of blockdev --flushbufs:

python -c 'import os; os.fsync(open("/dev/loopX", "r+b"))'

This should do the same as blockdev --flushbufs *plus* it sends and waits for the NBD FLUSH command.

You may have to play with this command-line a little but the main idea is to open the block device and fsync it.

Stefan