Compare commits
	
		
			126 Commits
		
	
	
		
			pull-input
			...
			SLE12-SP1-
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					3456706297 | ||
| 
						 | 
					6bfc70462b | ||
| 
						 | 
					77bd1c30f4 | ||
| 
						 | 
					0d9bbf181e | ||
| 
						 | 
					c7f5d47e08 | ||
| 
						 | 
					aa7fb34246 | ||
| 
						 | 
					4c3849714f | ||
| 
						 | 
					8970c6a04e | ||
| 
						 | 
					0bcc356da2 | ||
| 
						 | 
					5cc51586d1 | ||
| 
						 | 
					7637d9fbfe | ||
| 
						 | 
					589b223ca5 | ||
| 
						 | 
					225bfdb2ed | ||
| 
						 | 
					fc1b871631 | ||
| 
						 | 
					a78f57a82b | ||
| 
						 | 
					457b3ba055 | ||
| 
						 | 
					bff3afb57d | ||
| 
						 | 
					eb70eb33cb | ||
| 
						 | 
					8526e2d444 | ||
| 
						 | 
					99bc193099 | ||
| 
						 | 
					5e454fe423 | ||
| 
						 | 
					41eb27692f | ||
| 
						 | 
					c251fa606e | ||
| 
						 | 
					c923a7775c | ||
| 
						 | 
					09d66eb5f5 | ||
| 
						 | 
					b313f6d23d | ||
| 
						 | 
					319e0e347f | ||
| 
						 | 
					daede1f0f2 | ||
| 
						 | 
					789adc278e | ||
| 
						 | 
					c4c86364b3 | ||
| 
						 | 
					b31a018f58 | ||
| 
						 | 
					588bf41e1f | ||
| 
						 | 
					babe800bcc | ||
| 
						 | 
					17e0a09e25 | ||
| 
						 | 
					ed87ee47e0 | ||
| 
						 | 
					621ddf509e | ||
| 
						 | 
					ac10d55b82 | ||
| 
						 | 
					8e9b8b2204 | ||
| 
						 | 
					2ac80007da | ||
| 
						 | 
					4168180338 | ||
| 
						 | 
					683051c844 | ||
| 
						 | 
					4a7c93f23e | ||
| 
						 | 
					66e090df95 | ||
| 
						 | 
					acc6309904 | ||
| 
						 | 
					9bb28247d7 | ||
| 
						 | 
					1a8b5a066a | ||
| 
						 | 
					5673fb20cd | ||
| 
						 | 
					f8898ec278 | ||
| 
						 | 
					efa9daceb1 | ||
| 
						 | 
					ed19da1195 | ||
| 
						 | 
					b8ddc9be4c | ||
| 
						 | 
					0228bc7ae0 | ||
| 
						 | 
					a5a901fc4c | ||
| 
						 | 
					ca05fad76e | ||
| 
						 | 
					6855fbd119 | ||
| 
						 | 
					02792b8b1d | ||
| 
						 | 
					f72dd33b9a | ||
| 
						 | 
					cc2fb7f00d | ||
| 
						 | 
					bd2ce7536f | ||
| 
						 | 
					f2b44e9116 | ||
| 
						 | 
					b5af05aeb7 | ||
| 
						 | 
					dfa83a6bae | ||
| 
						 | 
					35a616edef | ||
| 
						 | 
					35c30d3efd | ||
| 
						 | 
					f4c861fd68 | ||
| 
						 | 
					b7a197c39e | ||
| 
						 | 
					85611098ff | ||
| 
						 | 
					ce4f451bbb | ||
| 
						 | 
					6722c126f3 | ||
| 
						 | 
					8dd45dcd83 | ||
| 
						 | 
					e750591c8a | ||
| 
						 | 
					f9c0ae2723 | ||
| 
						 | 
					c8bd74d1d5 | ||
| 
						 | 
					d1557697fd | ||
| 
						 | 
					86d6fe4cb0 | ||
| 
						 | 
					9634e45e0b | ||
| 
						 | 
					0dc545e977 | ||
| 
						 | 
					358f0ee234 | ||
| 
						 | 
					961c74a841 | ||
| 
						 | 
					98fe91ed66 | ||
| 
						 | 
					46addaa0b5 | ||
| 
						 | 
					5a4568717c | ||
| 
						 | 
					87740cecc3 | ||
| 
						 | 
					8df2a9acd2 | ||
| 
						 | 
					c5c71e87aa | ||
| 
						 | 
					2060efae47 | ||
| 
						 | 
					8d64975c98 | ||
| 
						 | 
					9b4420ad62 | ||
| 
						 | 
					99c3468d8f | ||
| 
						 | 
					1c17e8c7d3 | ||
| 
						 | 
					ffd060d51f | ||
| 
						 | 
					e4fb4bea37 | ||
| 
						 | 
					edc0a65326 | ||
| 
						 | 
					c62f6c8f67 | ||
| 
						 | 
					3d8b7aed60 | ||
| 
						 | 
					27ed14c4d7 | ||
| 
						 | 
					6a45a1b8e4 | ||
| 
						 | 
					6cacd2651a | ||
| 
						 | 
					e8248a5af1 | ||
| 
						 | 
					81cb0a5657 | ||
| 
						 | 
					6130c46232 | ||
| 
						 | 
					49ef542e41 | ||
| 
						 | 
					c270245a53 | ||
| 
						 | 
					9272707a1f | ||
| 
						 | 
					c759f1a078 | ||
| 
						 | 
					714b54401c | ||
| 
						 | 
					e7e08380c3 | ||
| 
						 | 
					c631ee6520 | ||
| 
						 | 
					b153c8d3f3 | ||
| 
						 | 
					f45048225a | ||
| 
						 | 
					ae0fa48f51 | ||
| 
						 | 
					bb3a1da4d4 | ||
| 
						 | 
					b48a391cff | ||
| 
						 | 
					cc883fe42d | ||
| 
						 | 
					4072585ecf | ||
| 
						 | 
					959fad0ff1 | ||
| 
						 | 
					a4bb522ee5 | ||
| 
						 | 
					cf6c213981 | ||
| 
						 | 
					cf3297868c | ||
| 
						 | 
					ad9c167fd2 | ||
| 
						 | 
					d8e231fce2 | ||
| 
						 | 
					53cd79c117 | ||
| 
						 | 
					3dd15f3e58 | ||
| 
						 | 
					4c59860506 | ||
| 
						 | 
					b575af0730 | ||
| 
						 | 
					d3b59789e8 | 
							
								
								
									
										209
									
								
								MAINTAINERS
									
									
									
									
									
								
							
							
						
						
									
										209
									
								
								MAINTAINERS
									
									
									
									
									
								
							@@ -172,8 +172,7 @@ F: hw/unicore32/
 | 
			
		||||
X86
 | 
			
		||||
M: Paolo Bonzini <pbonzini@redhat.com>
 | 
			
		||||
M: Richard Henderson <rth@twiddle.net>
 | 
			
		||||
M: Eduardo Habkost <ehabkost@redhat.com>
 | 
			
		||||
S: Maintained
 | 
			
		||||
S: Odd Fixes
 | 
			
		||||
F: target-i386/
 | 
			
		||||
F: hw/i386/
 | 
			
		||||
 | 
			
		||||
@@ -356,13 +355,6 @@ F: hw/misc/zynq_slcr.c
 | 
			
		||||
F: hw/*/cadence_*
 | 
			
		||||
F: hw/ssi/xilinx_spips.c
 | 
			
		||||
 | 
			
		||||
ARM ACPI Subsystem
 | 
			
		||||
M: Shannon Zhao <zhaoshenglong@huawei.com>
 | 
			
		||||
M: Shannon Zhao <shannon.zhao@linaro.org>
 | 
			
		||||
S: Maintained
 | 
			
		||||
F: hw/arm/virt-acpi-build.c
 | 
			
		||||
F: include/hw/arm/virt-acpi-build.h
 | 
			
		||||
 | 
			
		||||
CRIS Machines
 | 
			
		||||
-------------
 | 
			
		||||
Axis Dev88
 | 
			
		||||
@@ -493,8 +485,7 @@ F: hw/ppc/prep.c
 | 
			
		||||
F: hw/pci-host/prep.[hc]
 | 
			
		||||
F: hw/isa/pc87312.[hc]
 | 
			
		||||
 | 
			
		||||
sPAPR (pseries)
 | 
			
		||||
M: David Gibson <david@gibson.dropbear.id.au>
 | 
			
		||||
sPAPR
 | 
			
		||||
M: Alexander Graf <agraf@suse.de>
 | 
			
		||||
L: qemu-ppc@nongnu.org
 | 
			
		||||
S: Supported
 | 
			
		||||
@@ -644,19 +635,7 @@ M: Michael S. Tsirkin <mst@redhat.com>
 | 
			
		||||
S: Supported
 | 
			
		||||
F: include/hw/pci/*
 | 
			
		||||
F: hw/pci/*
 | 
			
		||||
 | 
			
		||||
ACPI
 | 
			
		||||
M: Michael S. Tsirkin <mst@redhat.com>
 | 
			
		||||
M: Igor Mammedov <imammedo@redhat.com>
 | 
			
		||||
S: Supported
 | 
			
		||||
F: include/hw/acpi/*
 | 
			
		||||
F: hw/mem/*
 | 
			
		||||
F: hw/acpi/*
 | 
			
		||||
F: hw/i386/acpi-build.[hc]
 | 
			
		||||
F: hw/i386/*dsl
 | 
			
		||||
F: hw/arm/virt-acpi-build.c
 | 
			
		||||
F: include/hw/arm/virt-acpi-build.h
 | 
			
		||||
F: scripts/acpi*py
 | 
			
		||||
 | 
			
		||||
ppc4xx
 | 
			
		||||
M: Alexander Graf <agraf@suse.de>
 | 
			
		||||
@@ -714,7 +693,6 @@ virtio
 | 
			
		||||
M: Michael S. Tsirkin <mst@redhat.com>
 | 
			
		||||
S: Supported
 | 
			
		||||
F: hw/*/virtio*
 | 
			
		||||
F: net/vhost-user.c
 | 
			
		||||
 | 
			
		||||
virtio-9p
 | 
			
		||||
M: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
 | 
			
		||||
@@ -725,13 +703,10 @@ F: tests/virtio-9p-test.c
 | 
			
		||||
T: git git://github.com/kvaneesh/QEMU.git
 | 
			
		||||
 | 
			
		||||
virtio-blk
 | 
			
		||||
M: Kevin Wolf <kwolf@redhat.com>
 | 
			
		||||
M: Stefan Hajnoczi <stefanha@redhat.com>
 | 
			
		||||
L: qemu-block@nongnu.org
 | 
			
		||||
S: Supported
 | 
			
		||||
F: hw/block/virtio-blk.c
 | 
			
		||||
F: hw/block/dataplane/*
 | 
			
		||||
F: hw/virtio/dataplane/*
 | 
			
		||||
T: git git://github.com/stefanha/qemu.git block
 | 
			
		||||
 | 
			
		||||
virtio-ccw
 | 
			
		||||
M: Cornelia Huck <cornelia.huck@de.ibm.com>
 | 
			
		||||
@@ -740,12 +715,6 @@ S: Supported
 | 
			
		||||
F: hw/s390x/virtio-ccw.[hc]
 | 
			
		||||
T: git git://github.com/cohuck/qemu virtio-ccw-upstr
 | 
			
		||||
 | 
			
		||||
virtio-input
 | 
			
		||||
M: Gerd Hoffmann <kraxel@redhat.com>
 | 
			
		||||
S: Maintained
 | 
			
		||||
F: hw/input/virtio-input*.c
 | 
			
		||||
F: include/hw/virtio/virtio-input.h
 | 
			
		||||
 | 
			
		||||
virtio-serial
 | 
			
		||||
M: Amit Shah <amit.shah@redhat.com>
 | 
			
		||||
S: Supported
 | 
			
		||||
@@ -762,14 +731,12 @@ F: backends/rng*.c
 | 
			
		||||
 | 
			
		||||
nvme
 | 
			
		||||
M: Keith Busch <keith.busch@intel.com>
 | 
			
		||||
L: qemu-block@nongnu.org
 | 
			
		||||
S: Supported
 | 
			
		||||
F: hw/block/nvme*
 | 
			
		||||
F: tests/nvme-test.c
 | 
			
		||||
 | 
			
		||||
megasas
 | 
			
		||||
M: Hannes Reinecke <hare@suse.de>
 | 
			
		||||
L: qemu-block@nongnu.org
 | 
			
		||||
S: Supported
 | 
			
		||||
F: hw/scsi/megasas.c
 | 
			
		||||
F: hw/scsi/mfi.h
 | 
			
		||||
@@ -787,15 +754,10 @@ S: Maintained
 | 
			
		||||
F: hw/net/vmxnet*
 | 
			
		||||
F: hw/scsi/vmw_pvscsi*
 | 
			
		||||
 | 
			
		||||
Rocker
 | 
			
		||||
M: Scott Feldman <sfeldma@gmail.com>
 | 
			
		||||
M: Jiri Pirko <jiri@resnulli.us>
 | 
			
		||||
S: Maintained
 | 
			
		||||
F: hw/net/rocker/
 | 
			
		||||
 | 
			
		||||
Subsystems
 | 
			
		||||
----------
 | 
			
		||||
Audio
 | 
			
		||||
M: Vassili Karpov (malc) <av1474@comtv.ru>
 | 
			
		||||
M: Gerd Hoffmann <kraxel@redhat.com>
 | 
			
		||||
S: Maintained
 | 
			
		||||
F: audio/
 | 
			
		||||
@@ -804,27 +766,21 @@ F: tests/ac97-test.c
 | 
			
		||||
F: tests/es1370-test.c
 | 
			
		||||
F: tests/intel-hda-test.c
 | 
			
		||||
 | 
			
		||||
Block layer core
 | 
			
		||||
Block
 | 
			
		||||
M: Kevin Wolf <kwolf@redhat.com>
 | 
			
		||||
L: qemu-block@nongnu.org
 | 
			
		||||
S: Supported
 | 
			
		||||
F: block*
 | 
			
		||||
F: block/
 | 
			
		||||
F: hw/block/
 | 
			
		||||
F: include/block/
 | 
			
		||||
F: qemu-img*
 | 
			
		||||
F: qemu-io*
 | 
			
		||||
F: tests/qemu-iotests/
 | 
			
		||||
T: git git://repo.or.cz/qemu/kevin.git block
 | 
			
		||||
 | 
			
		||||
Block I/O path
 | 
			
		||||
M: Stefan Hajnoczi <stefanha@redhat.com>
 | 
			
		||||
L: qemu-block@nongnu.org
 | 
			
		||||
S: Supported
 | 
			
		||||
F: async.c
 | 
			
		||||
F: aio-*.c
 | 
			
		||||
F: block/io.c
 | 
			
		||||
F: block*
 | 
			
		||||
F: block/
 | 
			
		||||
F: hw/block/
 | 
			
		||||
F: migration/block*
 | 
			
		||||
F: qemu-img*
 | 
			
		||||
F: qemu-io*
 | 
			
		||||
F: tests/image-fuzzer/
 | 
			
		||||
F: tests/qemu-iotests/
 | 
			
		||||
T: git git://repo.or.cz/qemu/kevin.git block
 | 
			
		||||
T: git git://github.com/stefanha/qemu.git block
 | 
			
		||||
 | 
			
		||||
Block Jobs
 | 
			
		||||
@@ -839,14 +795,6 @@ F: block/stream.h
 | 
			
		||||
F: block/mirror.c
 | 
			
		||||
T: git git://github.com/codyprime/qemu-kvm-jtc.git block
 | 
			
		||||
 | 
			
		||||
Block QAPI, monitor, command line
 | 
			
		||||
M: Markus Armbruster <armbru@redhat.com>
 | 
			
		||||
S: Supported
 | 
			
		||||
F: blockdev.c
 | 
			
		||||
F: block/qapi.c
 | 
			
		||||
F: qapi/block*.json
 | 
			
		||||
T: git git://repo.or.cz/qemu/armbru.git block-next
 | 
			
		||||
 | 
			
		||||
Character Devices
 | 
			
		||||
M: Paolo Bonzini <pbonzini@redhat.com>
 | 
			
		||||
S: Maintained
 | 
			
		||||
@@ -957,33 +905,21 @@ F: nbd.*
 | 
			
		||||
F: qemu-nbd.c
 | 
			
		||||
T: git git://github.com/bonzini/qemu.git nbd-next
 | 
			
		||||
 | 
			
		||||
NUMA
 | 
			
		||||
M: Eduardo Habkost <ehabkost@redhat.com>
 | 
			
		||||
S: Maintained
 | 
			
		||||
F: numa.c
 | 
			
		||||
F: include/sysemu/numa.h
 | 
			
		||||
K: numa|NUMA
 | 
			
		||||
K: srat|SRAT
 | 
			
		||||
T: git git://github.com/ehabkost/qemu.git numa
 | 
			
		||||
 | 
			
		||||
QAPI
 | 
			
		||||
M: Markus Armbruster <armbru@redhat.com>
 | 
			
		||||
M: Luiz Capitulino <lcapitulino@redhat.com>
 | 
			
		||||
M: Michael Roth <mdroth@linux.vnet.ibm.com>
 | 
			
		||||
S: Supported
 | 
			
		||||
S: Maintained
 | 
			
		||||
F: qapi/
 | 
			
		||||
X: qapi/*.json
 | 
			
		||||
F: tests/qapi-schema/
 | 
			
		||||
F: scripts/qapi*
 | 
			
		||||
F: docs/qapi*
 | 
			
		||||
T: git git://repo.or.cz/qemu/armbru.git qapi-next
 | 
			
		||||
T: git git://repo.or.cz/qemu/qmp-unstable.git queue/qmp
 | 
			
		||||
 | 
			
		||||
QAPI Schema
 | 
			
		||||
M: Eric Blake <eblake@redhat.com>
 | 
			
		||||
M: Luiz Capitulino <lcapitulino@redhat.com>
 | 
			
		||||
M: Markus Armbruster <armbru@redhat.com>
 | 
			
		||||
S: Supported
 | 
			
		||||
F: qapi-schema.json
 | 
			
		||||
F: qapi/*.json
 | 
			
		||||
T: git git://repo.or.cz/qemu/armbru.git qapi-next
 | 
			
		||||
T: git git://repo.or.cz/qemu/qmp-unstable.git queue/qmp
 | 
			
		||||
 | 
			
		||||
QObject
 | 
			
		||||
M: Luiz Capitulino <lcapitulino@redhat.com>
 | 
			
		||||
@@ -1008,14 +944,13 @@ X: qom/cpu.c
 | 
			
		||||
F: tests/qom-test.c
 | 
			
		||||
 | 
			
		||||
QMP
 | 
			
		||||
M: Markus Armbruster <armbru@redhat.com>
 | 
			
		||||
S: Supported
 | 
			
		||||
M: Luiz Capitulino <lcapitulino@redhat.com>
 | 
			
		||||
S: Maintained
 | 
			
		||||
F: qmp.c
 | 
			
		||||
F: monitor.c
 | 
			
		||||
F: qmp-commands.hx
 | 
			
		||||
F: docs/qmp/
 | 
			
		||||
F: scripts/qmp/
 | 
			
		||||
T: git git://repo.or.cz/qemu/armbru.git qapi-next
 | 
			
		||||
F: QMP/
 | 
			
		||||
T: git git://repo.or.cz/qemu/qmp-unstable.git queue/qmp
 | 
			
		||||
 | 
			
		||||
SLIRP
 | 
			
		||||
M: Jan Kiszka <jan.kiszka@siemens.com>
 | 
			
		||||
@@ -1043,6 +978,8 @@ M: Amit Shah <amit.shah@redhat.com>
 | 
			
		||||
S: Maintained
 | 
			
		||||
F: include/migration/
 | 
			
		||||
F: migration/
 | 
			
		||||
F: savevm.c
 | 
			
		||||
F: arch_init.c
 | 
			
		||||
F: scripts/vmstate-static-checker.py
 | 
			
		||||
F: tests/vmstate-static-checker-data/
 | 
			
		||||
 | 
			
		||||
@@ -1157,7 +1094,6 @@ Block drivers
 | 
			
		||||
-------------
 | 
			
		||||
VMDK
 | 
			
		||||
M: Fam Zheng <famz@redhat.com>
 | 
			
		||||
L: qemu-block@nongnu.org
 | 
			
		||||
S: Supported
 | 
			
		||||
F: block/vmdk.c
 | 
			
		||||
 | 
			
		||||
@@ -1188,7 +1124,6 @@ T: git git://github.com/codyprime/qemu-kvm-jtc.git block
 | 
			
		||||
 | 
			
		||||
VDI
 | 
			
		||||
M: Stefan Weil <sw@weilnetz.de>
 | 
			
		||||
L: qemu-block@nongnu.org
 | 
			
		||||
S: Maintained
 | 
			
		||||
F: block/vdi.c
 | 
			
		||||
 | 
			
		||||
@@ -1196,7 +1131,6 @@ iSCSI
 | 
			
		||||
M: Ronnie Sahlberg <ronniesahlberg@gmail.com>
 | 
			
		||||
M: Paolo Bonzini <pbonzini@redhat.com>
 | 
			
		||||
M: Peter Lieven <pl@kamp.de>
 | 
			
		||||
L: qemu-block@nongnu.org
 | 
			
		||||
S: Supported
 | 
			
		||||
F: block/iscsi.c
 | 
			
		||||
 | 
			
		||||
@@ -1238,102 +1172,7 @@ S: Supported
 | 
			
		||||
F: block/gluster.c
 | 
			
		||||
T: git git://github.com/codyprime/qemu-kvm-jtc.git block
 | 
			
		||||
 | 
			
		||||
Null Block Driver
 | 
			
		||||
M: Fam Zheng <famz@redhat.com>
 | 
			
		||||
L: qemu-block@nongnu.org
 | 
			
		||||
S: Supported
 | 
			
		||||
F: block/null.c
 | 
			
		||||
 | 
			
		||||
Bootdevice
 | 
			
		||||
M: Gonglei <arei.gonglei@huawei.com>
 | 
			
		||||
S: Maintained
 | 
			
		||||
F: bootdevice.c
 | 
			
		||||
 | 
			
		||||
Quorum
 | 
			
		||||
M: Alberto Garcia <berto@igalia.com>
 | 
			
		||||
S: Supported
 | 
			
		||||
F: block/quorum.c
 | 
			
		||||
L: qemu-block@nongnu.org
 | 
			
		||||
 | 
			
		||||
blkverify
 | 
			
		||||
M: Stefan Hajnoczi <stefanha@redhat.com>
 | 
			
		||||
L: qemu-block@nongnu.org
 | 
			
		||||
S: Supported
 | 
			
		||||
F: block/blkverify.c
 | 
			
		||||
 | 
			
		||||
bochs
 | 
			
		||||
M: Stefan Hajnoczi <stefanha@redhat.com>
 | 
			
		||||
L: qemu-block@nongnu.org
 | 
			
		||||
S: Supported
 | 
			
		||||
F: block/bochs.c
 | 
			
		||||
 | 
			
		||||
cloop
 | 
			
		||||
M: Stefan Hajnoczi <stefanha@redhat.com>
 | 
			
		||||
L: qemu-block@nongnu.org
 | 
			
		||||
S: Supported
 | 
			
		||||
F: block/cloop.c
 | 
			
		||||
 | 
			
		||||
dmg
 | 
			
		||||
M: Stefan Hajnoczi <stefanha@redhat.com>
 | 
			
		||||
L: qemu-block@nongnu.org
 | 
			
		||||
S: Supported
 | 
			
		||||
F: block/dmg.c
 | 
			
		||||
 | 
			
		||||
parallels
 | 
			
		||||
M: Stefan Hajnoczi <stefanha@redhat.com>
 | 
			
		||||
L: qemu-block@nongnu.org
 | 
			
		||||
S: Supported
 | 
			
		||||
F: block/parallels.c
 | 
			
		||||
 | 
			
		||||
qed
 | 
			
		||||
M: Stefan Hajnoczi <stefanha@redhat.com>
 | 
			
		||||
L: qemu-block@nongnu.org
 | 
			
		||||
S: Supported
 | 
			
		||||
F: block/qed.c
 | 
			
		||||
 | 
			
		||||
raw
 | 
			
		||||
M: Kevin Wolf <kwolf@redhat.com>
 | 
			
		||||
L: qemu-block@nongnu.org
 | 
			
		||||
S: Supported
 | 
			
		||||
F: block/linux-aio.c
 | 
			
		||||
F: block/raw-aio.h
 | 
			
		||||
F: block/raw-posix.c
 | 
			
		||||
F: block/raw-win32.c
 | 
			
		||||
F: block/raw_bsd.c
 | 
			
		||||
F: block/win32-aio.c
 | 
			
		||||
 | 
			
		||||
qcow2
 | 
			
		||||
M: Kevin Wolf <kwolf@redhat.com>
 | 
			
		||||
L: qemu-block@nongnu.org
 | 
			
		||||
S: Supported
 | 
			
		||||
F: block/qcow2*
 | 
			
		||||
 | 
			
		||||
qcow
 | 
			
		||||
M: Kevin Wolf <kwolf@redhat.com>
 | 
			
		||||
L: qemu-block@nongnu.org
 | 
			
		||||
S: Supported
 | 
			
		||||
F: block/qcow.c
 | 
			
		||||
 | 
			
		||||
blkdebug
 | 
			
		||||
M: Kevin Wolf <kwolf@redhat.com>
 | 
			
		||||
L: qemu-block@nongnu.org
 | 
			
		||||
S: Supported
 | 
			
		||||
F: block/blkdebug.c
 | 
			
		||||
 | 
			
		||||
vpc
 | 
			
		||||
M: Kevin Wolf <kwolf@redhat.com>
 | 
			
		||||
L: qemu-block@nongnu.org
 | 
			
		||||
S: Supported
 | 
			
		||||
F: block/vpc.c
 | 
			
		||||
 | 
			
		||||
vvfat
 | 
			
		||||
M: Kevin Wolf <kwolf@redhat.com>
 | 
			
		||||
L: qemu-block@nongnu.org
 | 
			
		||||
S: Supported
 | 
			
		||||
F: block/vvfat.c
 | 
			
		||||
 | 
			
		||||
Image format fuzzer
 | 
			
		||||
M: Stefan Hajnoczi <stefanha@redhat.com>
 | 
			
		||||
L: qemu-block@nongnu.org
 | 
			
		||||
S: Supported
 | 
			
		||||
F: tests/image-fuzzer/
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										84
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										84
									
								
								Makefile
									
									
									
									
									
								
							@@ -3,11 +3,6 @@
 | 
			
		||||
# Always point to the root of the build tree (needs GNU make).
 | 
			
		||||
BUILD_DIR=$(CURDIR)
 | 
			
		||||
 | 
			
		||||
# Before including a proper config-host.mak, assume we are in the source tree
 | 
			
		||||
SRC_PATH=.
 | 
			
		||||
 | 
			
		||||
UNCHECKED_GOALS := %clean TAGS cscope ctags
 | 
			
		||||
 | 
			
		||||
# All following code might depend on configuration variables
 | 
			
		||||
ifneq ($(wildcard config-host.mak),)
 | 
			
		||||
# Put the all: rule here so that config-host.mak can contain dependencies.
 | 
			
		||||
@@ -43,7 +38,7 @@ config-host.mak: $(SRC_PATH)/configure
 | 
			
		||||
	fi
 | 
			
		||||
else
 | 
			
		||||
config-host.mak:
 | 
			
		||||
ifneq ($(filter-out $(UNCHECKED_GOALS),$(MAKECMDGOALS)),$(if $(MAKECMDGOALS),,fail))
 | 
			
		||||
ifneq ($(filter-out %clean,$(MAKECMDGOALS)),$(if $(MAKECMDGOALS),,fail))
 | 
			
		||||
	@echo "Please call configure before running make!"
 | 
			
		||||
	@exit 1
 | 
			
		||||
endif
 | 
			
		||||
@@ -79,7 +74,7 @@ Makefile: ;
 | 
			
		||||
configure: ;
 | 
			
		||||
 | 
			
		||||
.PHONY: all clean cscope distclean dvi html info install install-doc \
 | 
			
		||||
	pdf recurse-all speed test dist msi
 | 
			
		||||
	pdf recurse-all speed test dist
 | 
			
		||||
 | 
			
		||||
$(call set-vpath, $(SRC_PATH))
 | 
			
		||||
 | 
			
		||||
@@ -248,17 +243,17 @@ qapi-py = $(SRC_PATH)/scripts/qapi.py $(SRC_PATH)/scripts/ordereddict.py
 | 
			
		||||
qga/qapi-generated/qga-qapi-types.c qga/qapi-generated/qga-qapi-types.h :\
 | 
			
		||||
$(SRC_PATH)/qga/qapi-schema.json $(SRC_PATH)/scripts/qapi-types.py $(qapi-py)
 | 
			
		||||
	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-types.py \
 | 
			
		||||
		$(gen-out-type) -o qga/qapi-generated -p "qga-" $<, \
 | 
			
		||||
		$(gen-out-type) -o qga/qapi-generated -p "qga-" -i $<, \
 | 
			
		||||
		"  GEN   $@")
 | 
			
		||||
qga/qapi-generated/qga-qapi-visit.c qga/qapi-generated/qga-qapi-visit.h :\
 | 
			
		||||
$(SRC_PATH)/qga/qapi-schema.json $(SRC_PATH)/scripts/qapi-visit.py $(qapi-py)
 | 
			
		||||
	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-visit.py \
 | 
			
		||||
		$(gen-out-type) -o qga/qapi-generated -p "qga-" $<, \
 | 
			
		||||
		$(gen-out-type) -o qga/qapi-generated -p "qga-" -i $<, \
 | 
			
		||||
		"  GEN   $@")
 | 
			
		||||
qga/qapi-generated/qga-qmp-commands.h qga/qapi-generated/qga-qmp-marshal.c :\
 | 
			
		||||
$(SRC_PATH)/qga/qapi-schema.json $(SRC_PATH)/scripts/qapi-commands.py $(qapi-py)
 | 
			
		||||
	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-commands.py \
 | 
			
		||||
		$(gen-out-type) -o qga/qapi-generated -p "qga-" $<, \
 | 
			
		||||
		$(gen-out-type) -o qga/qapi-generated -p "qga-" -i $<, \
 | 
			
		||||
		"  GEN   $@")
 | 
			
		||||
 | 
			
		||||
qapi-modules = $(SRC_PATH)/qapi-schema.json $(SRC_PATH)/qapi/common.json \
 | 
			
		||||
@@ -268,22 +263,22 @@ qapi-modules = $(SRC_PATH)/qapi-schema.json $(SRC_PATH)/qapi/common.json \
 | 
			
		||||
qapi-types.c qapi-types.h :\
 | 
			
		||||
$(qapi-modules) $(SRC_PATH)/scripts/qapi-types.py $(qapi-py)
 | 
			
		||||
	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-types.py \
 | 
			
		||||
		$(gen-out-type) -o "." -b $<, \
 | 
			
		||||
		$(gen-out-type) -o "." -b -i $<, \
 | 
			
		||||
		"  GEN   $@")
 | 
			
		||||
qapi-visit.c qapi-visit.h :\
 | 
			
		||||
$(qapi-modules) $(SRC_PATH)/scripts/qapi-visit.py $(qapi-py)
 | 
			
		||||
	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-visit.py \
 | 
			
		||||
		$(gen-out-type) -o "." -b $<, \
 | 
			
		||||
		$(gen-out-type) -o "." -b -i $<, \
 | 
			
		||||
		"  GEN   $@")
 | 
			
		||||
qapi-event.c qapi-event.h :\
 | 
			
		||||
$(qapi-modules) $(SRC_PATH)/scripts/qapi-event.py $(qapi-py)
 | 
			
		||||
	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-event.py \
 | 
			
		||||
		$(gen-out-type) -o "." $<, \
 | 
			
		||||
		$(gen-out-type) -o "." -b -i $<, \
 | 
			
		||||
		"  GEN   $@")
 | 
			
		||||
qmp-commands.h qmp-marshal.c :\
 | 
			
		||||
$(qapi-modules) $(SRC_PATH)/scripts/qapi-commands.py $(qapi-py)
 | 
			
		||||
	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-commands.py \
 | 
			
		||||
		$(gen-out-type) -o "." -m $<, \
 | 
			
		||||
		$(gen-out-type) -o "." -m -i $<, \
 | 
			
		||||
		"  GEN   $@")
 | 
			
		||||
 | 
			
		||||
QGALIB_GEN=$(addprefix qga/qapi-generated/, qga-qapi-types.h qga-qapi-visit.h qga-qmp-commands.h)
 | 
			
		||||
@@ -292,38 +287,15 @@ $(qga-obj-y) qemu-ga.o: $(QGALIB_GEN)
 | 
			
		||||
qemu-ga$(EXESUF): $(qga-obj-y) libqemuutil.a libqemustub.a
 | 
			
		||||
	$(call LINK, $^)
 | 
			
		||||
 | 
			
		||||
ifdef QEMU_GA_MSI_ENABLED
 | 
			
		||||
QEMU_GA_MSI=qemu-ga-$(ARCH).msi
 | 
			
		||||
 | 
			
		||||
msi: ${QEMU_GA_MSI}
 | 
			
		||||
 | 
			
		||||
$(QEMU_GA_MSI): qemu-ga.exe
 | 
			
		||||
 | 
			
		||||
ifdef QEMU_GA_MSI_WITH_VSS
 | 
			
		||||
$(QEMU_GA_MSI): qga/vss-win32/qga-vss.dll
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
$(QEMU_GA_MSI): config-host.mak
 | 
			
		||||
 | 
			
		||||
$(QEMU_GA_MSI):  qga/installer/qemu-ga.wxs
 | 
			
		||||
	$(call quiet-command,QEMU_GA_VERSION="$(QEMU_GA_VERSION)" QEMU_GA_MANUFACTURER="$(QEMU_GA_MANUFACTURER)" QEMU_GA_DISTRO="$(QEMU_GA_DISTRO)" \
 | 
			
		||||
	wixl -o $@ $(QEMU_GA_MSI_ARCH) $(QEMU_GA_MSI_WITH_VSS) $(QEMU_GA_MSI_MINGW_DLL_PATH) $<, "  WIXL  $@")
 | 
			
		||||
else
 | 
			
		||||
msi:
 | 
			
		||||
	@echo MSI build not configured or dependency resolution failed (reconfigure with --enable-guest-agent-msi option)
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
clean:
 | 
			
		||||
# avoid old build problems by removing potentially incorrect old files
 | 
			
		||||
	rm -f config.mak op-i386.h opc-i386.h gen-op-i386.h op-arm.h opc-arm.h gen-op-arm.h
 | 
			
		||||
	rm -f qemu-options.def
 | 
			
		||||
	rm -f *.msi
 | 
			
		||||
	find . \( -name '*.l[oa]' -o -name '*.so' -o -name '*.dll' -o -name '*.mo' -o -name '*.[oda]' \) -type f -exec rm {} +
 | 
			
		||||
	rm -f $(filter-out %.tlb,$(TOOLS)) $(HELPERS-y) qemu-ga TAGS cscope.* *.pod *~ */*~
 | 
			
		||||
	rm -f fsdev/*.pod
 | 
			
		||||
	rm -rf .libs */.libs
 | 
			
		||||
	rm -f qemu-img-cmds.h
 | 
			
		||||
	rm -f ui/shader/*-vert.h ui/shader/*-frag.h
 | 
			
		||||
	@# May not be present in GENERATED_HEADERS
 | 
			
		||||
	rm -f trace/generated-tracers-dtrace.dtrace*
 | 
			
		||||
	rm -f trace/generated-tracers-dtrace.h*
 | 
			
		||||
@@ -369,7 +341,7 @@ bepo    cz
 | 
			
		||||
 | 
			
		||||
ifdef INSTALL_BLOBS
 | 
			
		||||
BLOBS=bios.bin bios-256k.bin sgabios.bin vgabios.bin vgabios-cirrus.bin \
 | 
			
		||||
vgabios-stdvga.bin vgabios-vmware.bin vgabios-qxl.bin vgabios-virtio.bin \
 | 
			
		||||
vgabios-stdvga.bin vgabios-vmware.bin vgabios-qxl.bin \
 | 
			
		||||
acpi-dsdt.aml q35-acpi-dsdt.aml \
 | 
			
		||||
ppc_rom.bin openbios-sparc32 openbios-sparc64 openbios-ppc QEMU,tcx.bin QEMU,cgthree.bin \
 | 
			
		||||
pxe-e1000.rom pxe-eepro100.rom pxe-ne2k_pci.rom \
 | 
			
		||||
@@ -416,8 +388,13 @@ ifneq (,$(findstring qemu-ga,$(TOOLS)))
 | 
			
		||||
endif
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
install-confdir:
 | 
			
		||||
	$(INSTALL_DIR) "$(DESTDIR)$(qemu_confdir)"
 | 
			
		||||
 | 
			
		||||
install: all $(if $(BUILD_DOCS),install-doc) \
 | 
			
		||||
install-sysconfig: install-datadir install-confdir
 | 
			
		||||
	$(INSTALL_DATA) $(SRC_PATH)/sysconfigs/target/target-x86_64.conf "$(DESTDIR)$(qemu_confdir)"
 | 
			
		||||
 | 
			
		||||
install: all $(if $(BUILD_DOCS),install-doc) install-sysconfig \
 | 
			
		||||
install-datadir install-localstatedir
 | 
			
		||||
ifneq ($(TOOLS),)
 | 
			
		||||
	$(call install-prog,$(TOOLS),$(DESTDIR)$(bindir))
 | 
			
		||||
@@ -454,36 +431,15 @@ endif
 | 
			
		||||
test speed: all
 | 
			
		||||
	$(MAKE) -C tests/tcg $@
 | 
			
		||||
 | 
			
		||||
.PHONY: ctags
 | 
			
		||||
ctags:
 | 
			
		||||
	rm -f $@
 | 
			
		||||
	find "$(SRC_PATH)" -name '*.[hc]' -exec ctags --append {} +
 | 
			
		||||
 | 
			
		||||
.PHONY: TAGS
 | 
			
		||||
TAGS:
 | 
			
		||||
	rm -f $@
 | 
			
		||||
	find "$(SRC_PATH)" -name '*.[hc]' -exec etags --append {} +
 | 
			
		||||
 | 
			
		||||
cscope:
 | 
			
		||||
	rm -f "$(SRC_PATH)"/cscope.*
 | 
			
		||||
	find "$(SRC_PATH)/" -name "*.[chsS]" -print | sed 's,^\./,,' > "$(SRC_PATH)/cscope.files"
 | 
			
		||||
	cscope -b -i"$(SRC_PATH)/cscope.files"
 | 
			
		||||
 | 
			
		||||
# opengl shader programs
 | 
			
		||||
ui/shader/%-vert.h: $(SRC_PATH)/ui/shader/%.vert $(SRC_PATH)/scripts/shaderinclude.pl
 | 
			
		||||
	@mkdir -p $(dir $@)
 | 
			
		||||
	$(call quiet-command,\
 | 
			
		||||
		perl $(SRC_PATH)/scripts/shaderinclude.pl $< > $@,\
 | 
			
		||||
		"  VERT  $@")
 | 
			
		||||
 | 
			
		||||
ui/shader/%-frag.h: $(SRC_PATH)/ui/shader/%.frag $(SRC_PATH)/scripts/shaderinclude.pl
 | 
			
		||||
	@mkdir -p $(dir $@)
 | 
			
		||||
	$(call quiet-command,\
 | 
			
		||||
		perl $(SRC_PATH)/scripts/shaderinclude.pl $< > $@,\
 | 
			
		||||
		"  FRAG  $@")
 | 
			
		||||
 | 
			
		||||
ui/console-gl.o: $(SRC_PATH)/ui/console-gl.c \
 | 
			
		||||
	ui/shader/texture-blit-vert.h ui/shader/texture-blit-frag.h
 | 
			
		||||
	rm -f ./cscope.*
 | 
			
		||||
	find "$(SRC_PATH)" -name "*.[chsS]" -print | sed 's,^\./,,' > ./cscope.files
 | 
			
		||||
	cscope -b
 | 
			
		||||
 | 
			
		||||
# documentation
 | 
			
		||||
MAKEINFO=makeinfo
 | 
			
		||||
@@ -610,7 +566,7 @@ endif # CONFIG_WIN
 | 
			
		||||
 | 
			
		||||
# Add a dependency on the generated files, so that they are always
 | 
			
		||||
# rebuilt before other object files
 | 
			
		||||
ifneq ($(filter-out $(UNCHECKED_GOALS),$(MAKECMDGOALS)),$(if $(MAKECMDGOALS),,fail))
 | 
			
		||||
ifneq ($(filter-out %clean,$(MAKECMDGOALS)),$(if $(MAKECMDGOALS),,fail))
 | 
			
		||||
Makefile: $(GENERATED_HEADERS)
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -76,8 +76,6 @@ common-obj-$(CONFIG_SECCOMP) += qemu-seccomp.o
 | 
			
		||||
 | 
			
		||||
common-obj-$(CONFIG_SMARTCARD_NSS) += $(libcacard-y)
 | 
			
		||||
 | 
			
		||||
common-obj-$(CONFIG_FDT) += device_tree.o
 | 
			
		||||
 | 
			
		||||
######################################################################
 | 
			
		||||
# qapi
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,5 @@
 | 
			
		||||
# -*- Mode: makefile -*-
 | 
			
		||||
 | 
			
		||||
BUILD_DIR?=$(CURDIR)/..
 | 
			
		||||
 | 
			
		||||
include ../config-host.mak
 | 
			
		||||
include config-target.mak
 | 
			
		||||
include config-devices.mak
 | 
			
		||||
@@ -36,6 +34,10 @@ endif
 | 
			
		||||
PROGS=$(QEMU_PROG) $(QEMU_PROGW)
 | 
			
		||||
STPFILES=
 | 
			
		||||
 | 
			
		||||
ifdef CONFIG_LINUX_USER
 | 
			
		||||
PROGS+=$(QEMU_PROG)-binfmt
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
config-target.h: config-target.h-timestamp
 | 
			
		||||
config-target.h-timestamp: config-target.mak
 | 
			
		||||
 | 
			
		||||
@@ -110,6 +112,8 @@ QEMU_CFLAGS+=-I$(SRC_PATH)/linux-user/$(TARGET_ABI_DIR) -I$(SRC_PATH)/linux-user
 | 
			
		||||
obj-y += linux-user/
 | 
			
		||||
obj-y += gdbstub.o thunk.o user-exec.o
 | 
			
		||||
 | 
			
		||||
obj-binfmt-y += linux-user/
 | 
			
		||||
 | 
			
		||||
endif #CONFIG_LINUX_USER
 | 
			
		||||
 | 
			
		||||
#########################################################
 | 
			
		||||
@@ -131,12 +135,12 @@ ifdef CONFIG_SOFTMMU
 | 
			
		||||
obj-y += arch_init.o cpus.o monitor.o gdbstub.o balloon.o ioport.o numa.o
 | 
			
		||||
obj-y += qtest.o bootdevice.o
 | 
			
		||||
obj-y += hw/
 | 
			
		||||
obj-$(CONFIG_FDT) += device_tree.o
 | 
			
		||||
obj-$(CONFIG_KVM) += kvm-all.o
 | 
			
		||||
obj-y += memory.o cputlb.o
 | 
			
		||||
obj-y += memory.o savevm.o cputlb.o
 | 
			
		||||
obj-y += memory_mapping.o
 | 
			
		||||
obj-y += dump.o
 | 
			
		||||
obj-y += migration/ram.o migration/savevm.o
 | 
			
		||||
LIBS := $(libs_softmmu) $(LIBS)
 | 
			
		||||
LIBS+=$(libs_softmmu)
 | 
			
		||||
 | 
			
		||||
# xen support
 | 
			
		||||
obj-$(CONFIG_XEN) += xen-common.o
 | 
			
		||||
@@ -158,7 +162,11 @@ endif # CONFIG_SOFTMMU
 | 
			
		||||
# Workaround for http://gcc.gnu.org/PR55489, see configure.
 | 
			
		||||
%/translate.o: QEMU_CFLAGS += $(TRANSLATE_OPT_CFLAGS)
 | 
			
		||||
 | 
			
		||||
ifdef CONFIG_LINUX_USER
 | 
			
		||||
dummy := $(call unnest-vars,,obj-y obj-binfmt-y)
 | 
			
		||||
else
 | 
			
		||||
dummy := $(call unnest-vars,,obj-y)
 | 
			
		||||
endif
 | 
			
		||||
all-obj-y := $(obj-y)
 | 
			
		||||
 | 
			
		||||
target-obj-y :=
 | 
			
		||||
@@ -182,10 +190,9 @@ $(QEMU_PROG_BUILD): config-devices.mak
 | 
			
		||||
# build either PROG or PROGW
 | 
			
		||||
$(QEMU_PROG_BUILD): $(all-obj-y) ../libqemuutil.a ../libqemustub.a
 | 
			
		||||
	$(call LINK, $(filter-out %.mak, $^))
 | 
			
		||||
ifdef CONFIG_DARWIN
 | 
			
		||||
	$(call quiet-command,Rez -append $(SRC_PATH)/pc-bios/qemu.rsrc -o $@,"  REZ   $(TARGET_DIR)$@")
 | 
			
		||||
	$(call quiet-command,SetFile -a C $@,"  SETFILE $(TARGET_DIR)$@")
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
$(QEMU_PROG)-binfmt: $(obj-binfmt-y)
 | 
			
		||||
	$(call LINK,$^)
 | 
			
		||||
 | 
			
		||||
gdbstub-xml.c: $(TARGET_XML_FILES) $(SRC_PATH)/scripts/feature_to_c.sh
 | 
			
		||||
	$(call quiet-command,rm -f $@ && $(SHELL) $(SRC_PATH)/scripts/feature_to_c.sh $@ $(TARGET_XML_FILES),"  GEN   $(TARGET_DIR)$@")
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										87
									
								
								aio-posix.c
									
									
									
									
									
								
							
							
						
						
									
										87
									
								
								aio-posix.c
									
									
									
									
									
								
							@@ -24,6 +24,7 @@ struct AioHandler
 | 
			
		||||
    IOHandler *io_read;
 | 
			
		||||
    IOHandler *io_write;
 | 
			
		||||
    int deleted;
 | 
			
		||||
    int pollfds_idx;
 | 
			
		||||
    void *opaque;
 | 
			
		||||
    QLIST_ENTRY(AioHandler) node;
 | 
			
		||||
};
 | 
			
		||||
@@ -82,6 +83,7 @@ void aio_set_fd_handler(AioContext *ctx,
 | 
			
		||||
        node->io_read = io_read;
 | 
			
		||||
        node->io_write = io_write;
 | 
			
		||||
        node->opaque = opaque;
 | 
			
		||||
        node->pollfds_idx = -1;
 | 
			
		||||
 | 
			
		||||
        node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP | G_IO_ERR : 0);
 | 
			
		||||
        node->pfd.events |= (io_write ? G_IO_OUT | G_IO_ERR : 0);
 | 
			
		||||
@@ -184,61 +186,13 @@ bool aio_dispatch(AioContext *ctx)
 | 
			
		||||
    return progress;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* These thread-local variables are used only in a small part of aio_poll
 | 
			
		||||
 * around the call to the poll() system call.  In particular they are not
 | 
			
		||||
 * used while aio_poll is performing callbacks, which makes it much easier
 | 
			
		||||
 * to think about reentrancy!
 | 
			
		||||
 *
 | 
			
		||||
 * Stack-allocated arrays would be perfect but they have size limitations;
 | 
			
		||||
 * heap allocation is expensive enough that we want to reuse arrays across
 | 
			
		||||
 * calls to aio_poll().  And because poll() has to be called without holding
 | 
			
		||||
 * any lock, the arrays cannot be stored in AioContext.  Thread-local data
 | 
			
		||||
 * has none of the disadvantages of these three options.
 | 
			
		||||
 */
 | 
			
		||||
static __thread GPollFD *pollfds;
 | 
			
		||||
static __thread AioHandler **nodes;
 | 
			
		||||
static __thread unsigned npfd, nalloc;
 | 
			
		||||
static __thread Notifier pollfds_cleanup_notifier;
 | 
			
		||||
 | 
			
		||||
static void pollfds_cleanup(Notifier *n, void *unused)
 | 
			
		||||
{
 | 
			
		||||
    g_assert(npfd == 0);
 | 
			
		||||
    g_free(pollfds);
 | 
			
		||||
    g_free(nodes);
 | 
			
		||||
    nalloc = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void add_pollfd(AioHandler *node)
 | 
			
		||||
{
 | 
			
		||||
    if (npfd == nalloc) {
 | 
			
		||||
        if (nalloc == 0) {
 | 
			
		||||
            pollfds_cleanup_notifier.notify = pollfds_cleanup;
 | 
			
		||||
            qemu_thread_atexit_add(&pollfds_cleanup_notifier);
 | 
			
		||||
            nalloc = 8;
 | 
			
		||||
        } else {
 | 
			
		||||
            g_assert(nalloc <= INT_MAX);
 | 
			
		||||
            nalloc *= 2;
 | 
			
		||||
        }
 | 
			
		||||
        pollfds = g_renew(GPollFD, pollfds, nalloc);
 | 
			
		||||
        nodes = g_renew(AioHandler *, nodes, nalloc);
 | 
			
		||||
    }
 | 
			
		||||
    nodes[npfd] = node;
 | 
			
		||||
    pollfds[npfd] = (GPollFD) {
 | 
			
		||||
        .fd = node->pfd.fd,
 | 
			
		||||
        .events = node->pfd.events,
 | 
			
		||||
    };
 | 
			
		||||
    npfd++;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool aio_poll(AioContext *ctx, bool blocking)
 | 
			
		||||
{
 | 
			
		||||
    AioHandler *node;
 | 
			
		||||
    bool was_dispatching;
 | 
			
		||||
    int i, ret;
 | 
			
		||||
    int ret;
 | 
			
		||||
    bool progress;
 | 
			
		||||
    int64_t timeout;
 | 
			
		||||
 | 
			
		||||
    aio_context_acquire(ctx);
 | 
			
		||||
    was_dispatching = ctx->dispatching;
 | 
			
		||||
    progress = false;
 | 
			
		||||
 | 
			
		||||
@@ -256,36 +210,39 @@ bool aio_poll(AioContext *ctx, bool blocking)
 | 
			
		||||
 | 
			
		||||
    ctx->walking_handlers++;
 | 
			
		||||
 | 
			
		||||
    assert(npfd == 0);
 | 
			
		||||
    g_array_set_size(ctx->pollfds, 0);
 | 
			
		||||
 | 
			
		||||
    /* fill pollfds */
 | 
			
		||||
    QLIST_FOREACH(node, &ctx->aio_handlers, node) {
 | 
			
		||||
        node->pollfds_idx = -1;
 | 
			
		||||
        if (!node->deleted && node->pfd.events) {
 | 
			
		||||
            add_pollfd(node);
 | 
			
		||||
            GPollFD pfd = {
 | 
			
		||||
                .fd = node->pfd.fd,
 | 
			
		||||
                .events = node->pfd.events,
 | 
			
		||||
            };
 | 
			
		||||
            node->pollfds_idx = ctx->pollfds->len;
 | 
			
		||||
            g_array_append_val(ctx->pollfds, pfd);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    timeout = blocking ? aio_compute_timeout(ctx) : 0;
 | 
			
		||||
    ctx->walking_handlers--;
 | 
			
		||||
 | 
			
		||||
    /* wait until next event */
 | 
			
		||||
    if (timeout) {
 | 
			
		||||
        aio_context_release(ctx);
 | 
			
		||||
    }
 | 
			
		||||
    ret = qemu_poll_ns((GPollFD *)pollfds, npfd, timeout);
 | 
			
		||||
    if (timeout) {
 | 
			
		||||
        aio_context_acquire(ctx);
 | 
			
		||||
    }
 | 
			
		||||
    ret = qemu_poll_ns((GPollFD *)ctx->pollfds->data,
 | 
			
		||||
                         ctx->pollfds->len,
 | 
			
		||||
                         blocking ? aio_compute_timeout(ctx) : 0);
 | 
			
		||||
 | 
			
		||||
    /* if we have any readable fds, dispatch event */
 | 
			
		||||
    if (ret > 0) {
 | 
			
		||||
        for (i = 0; i < npfd; i++) {
 | 
			
		||||
            nodes[i]->pfd.revents = pollfds[i].revents;
 | 
			
		||||
        QLIST_FOREACH(node, &ctx->aio_handlers, node) {
 | 
			
		||||
            if (node->pollfds_idx != -1) {
 | 
			
		||||
                GPollFD *pfd = &g_array_index(ctx->pollfds, GPollFD,
 | 
			
		||||
                                              node->pollfds_idx);
 | 
			
		||||
                node->pfd.revents = pfd->revents;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    npfd = 0;
 | 
			
		||||
    ctx->walking_handlers--;
 | 
			
		||||
 | 
			
		||||
    /* Run dispatch even if there were no readable fds to run timers */
 | 
			
		||||
    aio_set_dispatching(ctx, true);
 | 
			
		||||
    if (aio_dispatch(ctx)) {
 | 
			
		||||
@@ -293,7 +250,5 @@ bool aio_poll(AioContext *ctx, bool blocking)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    aio_set_dispatching(ctx, was_dispatching);
 | 
			
		||||
    aio_context_release(ctx);
 | 
			
		||||
 | 
			
		||||
    return progress;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -283,7 +283,6 @@ bool aio_poll(AioContext *ctx, bool blocking)
 | 
			
		||||
    int count;
 | 
			
		||||
    int timeout;
 | 
			
		||||
 | 
			
		||||
    aio_context_acquire(ctx);
 | 
			
		||||
    have_select_revents = aio_prepare(ctx);
 | 
			
		||||
    if (have_select_revents) {
 | 
			
		||||
        blocking = false;
 | 
			
		||||
@@ -324,13 +323,7 @@ bool aio_poll(AioContext *ctx, bool blocking)
 | 
			
		||||
 | 
			
		||||
        timeout = blocking
 | 
			
		||||
            ? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx)) : 0;
 | 
			
		||||
        if (timeout) {
 | 
			
		||||
            aio_context_release(ctx);
 | 
			
		||||
        }
 | 
			
		||||
        ret = WaitForMultipleObjects(count, events, FALSE, timeout);
 | 
			
		||||
        if (timeout) {
 | 
			
		||||
            aio_context_acquire(ctx);
 | 
			
		||||
        }
 | 
			
		||||
        aio_set_dispatching(ctx, true);
 | 
			
		||||
 | 
			
		||||
        if (first && aio_bh_poll(ctx)) {
 | 
			
		||||
@@ -356,6 +349,5 @@ bool aio_poll(AioContext *ctx, bool blocking)
 | 
			
		||||
    progress |= timerlistgroup_run_timers(&ctx->tlg);
 | 
			
		||||
 | 
			
		||||
    aio_set_dispatching(ctx, was_dispatching);
 | 
			
		||||
    aio_context_release(ctx);
 | 
			
		||||
    return progress;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										1189
									
								
								arch_init.c
									
									
									
									
									
								
							
							
						
						
									
										1189
									
								
								arch_init.c
									
									
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										2
									
								
								async.c
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								async.c
									
									
									
									
									
								
							@@ -230,6 +230,7 @@ aio_ctx_finalize(GSource     *source)
 | 
			
		||||
    event_notifier_cleanup(&ctx->notifier);
 | 
			
		||||
    rfifolock_destroy(&ctx->lock);
 | 
			
		||||
    qemu_mutex_destroy(&ctx->bh_lock);
 | 
			
		||||
    g_array_free(ctx->pollfds, TRUE);
 | 
			
		||||
    timerlistgroup_deinit(&ctx->tlg);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -301,6 +302,7 @@ AioContext *aio_context_new(Error **errp)
 | 
			
		||||
    aio_set_event_notifier(ctx, &ctx->notifier,
 | 
			
		||||
                           (EventNotifierHandler *)
 | 
			
		||||
                           event_notifier_test_and_clear);
 | 
			
		||||
    ctx->pollfds = g_array_new(FALSE, FALSE, sizeof(GPollFD));
 | 
			
		||||
    ctx->thread_pool = NULL;
 | 
			
		||||
    qemu_mutex_init(&ctx->bh_lock);
 | 
			
		||||
    rfifolock_init(&ctx->lock, aio_rfifolock_cb, ctx);
 | 
			
		||||
 
 | 
			
		||||
@@ -5,9 +5,13 @@ common-obj-$(CONFIG_SPICE) += spiceaudio.o
 | 
			
		||||
common-obj-$(CONFIG_COREAUDIO) += coreaudio.o
 | 
			
		||||
common-obj-$(CONFIG_ALSA) += alsaaudio.o
 | 
			
		||||
common-obj-$(CONFIG_DSOUND) += dsoundaudio.o
 | 
			
		||||
common-obj-$(CONFIG_FMOD) += fmodaudio.o
 | 
			
		||||
common-obj-$(CONFIG_ESD) += esdaudio.o
 | 
			
		||||
common-obj-$(CONFIG_PA) += paaudio.o
 | 
			
		||||
common-obj-$(CONFIG_WINWAVE) += winwaveaudio.o
 | 
			
		||||
common-obj-$(CONFIG_AUDIO_PT_INT) += audio_pt_int.o
 | 
			
		||||
common-obj-$(CONFIG_AUDIO_WIN_INT) += audio_win_int.o
 | 
			
		||||
common-obj-y += wavcapture.o
 | 
			
		||||
 | 
			
		||||
$(obj)/audio.o $(obj)/fmodaudio.o: QEMU_CFLAGS += $(FMOD_CFLAGS)
 | 
			
		||||
sdlaudio.o-cflags := $(SDL_CFLAGS)
 | 
			
		||||
 
 | 
			
		||||
@@ -25,7 +25,6 @@
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "qemu/main-loop.h"
 | 
			
		||||
#include "audio.h"
 | 
			
		||||
#include "trace.h"
 | 
			
		||||
 | 
			
		||||
#if QEMU_GNUC_PREREQ(4, 3)
 | 
			
		||||
#pragma GCC diagnostic ignored "-Waddress"
 | 
			
		||||
@@ -34,28 +33,9 @@
 | 
			
		||||
#define AUDIO_CAP "alsa"
 | 
			
		||||
#include "audio_int.h"
 | 
			
		||||
 | 
			
		||||
typedef struct ALSAConf {
 | 
			
		||||
    int size_in_usec_in;
 | 
			
		||||
    int size_in_usec_out;
 | 
			
		||||
    const char *pcm_name_in;
 | 
			
		||||
    const char *pcm_name_out;
 | 
			
		||||
    unsigned int buffer_size_in;
 | 
			
		||||
    unsigned int period_size_in;
 | 
			
		||||
    unsigned int buffer_size_out;
 | 
			
		||||
    unsigned int period_size_out;
 | 
			
		||||
    unsigned int threshold;
 | 
			
		||||
 | 
			
		||||
    int buffer_size_in_overridden;
 | 
			
		||||
    int period_size_in_overridden;
 | 
			
		||||
 | 
			
		||||
    int buffer_size_out_overridden;
 | 
			
		||||
    int period_size_out_overridden;
 | 
			
		||||
} ALSAConf;
 | 
			
		||||
 | 
			
		||||
struct pollhlp {
 | 
			
		||||
    snd_pcm_t *handle;
 | 
			
		||||
    struct pollfd *pfds;
 | 
			
		||||
    ALSAConf *conf;
 | 
			
		||||
    int count;
 | 
			
		||||
    int mask;
 | 
			
		||||
};
 | 
			
		||||
@@ -76,6 +56,30 @@ typedef struct ALSAVoiceIn {
 | 
			
		||||
    struct pollhlp pollhlp;
 | 
			
		||||
} ALSAVoiceIn;
 | 
			
		||||
 | 
			
		||||
static struct {
 | 
			
		||||
    int size_in_usec_in;
 | 
			
		||||
    int size_in_usec_out;
 | 
			
		||||
    const char *pcm_name_in;
 | 
			
		||||
    const char *pcm_name_out;
 | 
			
		||||
    unsigned int buffer_size_in;
 | 
			
		||||
    unsigned int period_size_in;
 | 
			
		||||
    unsigned int buffer_size_out;
 | 
			
		||||
    unsigned int period_size_out;
 | 
			
		||||
    unsigned int threshold;
 | 
			
		||||
 | 
			
		||||
    int buffer_size_in_overridden;
 | 
			
		||||
    int period_size_in_overridden;
 | 
			
		||||
 | 
			
		||||
    int buffer_size_out_overridden;
 | 
			
		||||
    int period_size_out_overridden;
 | 
			
		||||
    int verbose;
 | 
			
		||||
} conf = {
 | 
			
		||||
    .buffer_size_out = 4096,
 | 
			
		||||
    .period_size_out = 1024,
 | 
			
		||||
    .pcm_name_out = "default",
 | 
			
		||||
    .pcm_name_in = "default",
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct alsa_params_req {
 | 
			
		||||
    int freq;
 | 
			
		||||
    snd_pcm_format_t fmt;
 | 
			
		||||
@@ -201,7 +205,9 @@ static void alsa_poll_handler (void *opaque)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!(revents & hlp->mask)) {
 | 
			
		||||
        trace_alsa_revents(revents);
 | 
			
		||||
        if (conf.verbose) {
 | 
			
		||||
            dolog ("revents = %d\n", revents);
 | 
			
		||||
        }
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -260,14 +266,31 @@ static int alsa_poll_helper (snd_pcm_t *handle, struct pollhlp *hlp, int mask)
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < count; ++i) {
 | 
			
		||||
        if (pfds[i].events & POLLIN) {
 | 
			
		||||
            qemu_set_fd_handler (pfds[i].fd, alsa_poll_handler, NULL, hlp);
 | 
			
		||||
            err = qemu_set_fd_handler (pfds[i].fd, alsa_poll_handler,
 | 
			
		||||
                                       NULL, hlp);
 | 
			
		||||
        }
 | 
			
		||||
        if (pfds[i].events & POLLOUT) {
 | 
			
		||||
            trace_alsa_pollout(i, pfds[i].fd);
 | 
			
		||||
            qemu_set_fd_handler (pfds[i].fd, NULL, alsa_poll_handler, hlp);
 | 
			
		||||
            if (conf.verbose) {
 | 
			
		||||
                dolog ("POLLOUT %d %d\n", i, pfds[i].fd);
 | 
			
		||||
            }
 | 
			
		||||
            err = qemu_set_fd_handler (pfds[i].fd, NULL,
 | 
			
		||||
                                       alsa_poll_handler, hlp);
 | 
			
		||||
        }
 | 
			
		||||
        if (conf.verbose) {
 | 
			
		||||
            dolog ("Set handler events=%#x index=%d fd=%d err=%d\n",
 | 
			
		||||
                   pfds[i].events, i, pfds[i].fd, err);
 | 
			
		||||
        }
 | 
			
		||||
        trace_alsa_set_handler(pfds[i].events, i, pfds[i].fd, err);
 | 
			
		||||
 | 
			
		||||
        if (err) {
 | 
			
		||||
            dolog ("Failed to set handler events=%#x index=%d fd=%d err=%d\n",
 | 
			
		||||
                   pfds[i].events, i, pfds[i].fd, err);
 | 
			
		||||
 | 
			
		||||
            while (i--) {
 | 
			
		||||
                qemu_set_fd_handler (pfds[i].fd, NULL, NULL, NULL);
 | 
			
		||||
            }
 | 
			
		||||
            g_free (pfds);
 | 
			
		||||
            return -1;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    hlp->pfds = pfds;
 | 
			
		||||
    hlp->count = count;
 | 
			
		||||
@@ -453,15 +476,14 @@ static void alsa_set_threshold (snd_pcm_t *handle, snd_pcm_uframes_t threshold)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int alsa_open (int in, struct alsa_params_req *req,
 | 
			
		||||
                      struct alsa_params_obt *obt, snd_pcm_t **handlep,
 | 
			
		||||
                      ALSAConf *conf)
 | 
			
		||||
                      struct alsa_params_obt *obt, snd_pcm_t **handlep)
 | 
			
		||||
{
 | 
			
		||||
    snd_pcm_t *handle;
 | 
			
		||||
    snd_pcm_hw_params_t *hw_params;
 | 
			
		||||
    int err;
 | 
			
		||||
    int size_in_usec;
 | 
			
		||||
    unsigned int freq, nchannels;
 | 
			
		||||
    const char *pcm_name = in ? conf->pcm_name_in : conf->pcm_name_out;
 | 
			
		||||
    const char *pcm_name = in ? conf.pcm_name_in : conf.pcm_name_out;
 | 
			
		||||
    snd_pcm_uframes_t obt_buffer_size;
 | 
			
		||||
    const char *typ = in ? "ADC" : "DAC";
 | 
			
		||||
    snd_pcm_format_t obtfmt;
 | 
			
		||||
@@ -500,7 +522,7 @@ static int alsa_open (int in, struct alsa_params_req *req,
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    err = snd_pcm_hw_params_set_format (handle, hw_params, req->fmt);
 | 
			
		||||
    if (err < 0) {
 | 
			
		||||
    if (err < 0 && conf.verbose) {
 | 
			
		||||
        alsa_logerr2 (err, typ, "Failed to set format %d\n", req->fmt);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -632,7 +654,7 @@ static int alsa_open (int in, struct alsa_params_req *req,
 | 
			
		||||
        goto err;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!in && conf->threshold) {
 | 
			
		||||
    if (!in && conf.threshold) {
 | 
			
		||||
        snd_pcm_uframes_t threshold;
 | 
			
		||||
        int bytes_per_sec;
 | 
			
		||||
 | 
			
		||||
@@ -654,7 +676,7 @@ static int alsa_open (int in, struct alsa_params_req *req,
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        threshold = (conf->threshold * bytes_per_sec) / 1000;
 | 
			
		||||
        threshold = (conf.threshold * bytes_per_sec) / 1000;
 | 
			
		||||
        alsa_set_threshold (handle, threshold);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -664,9 +686,10 @@ static int alsa_open (int in, struct alsa_params_req *req,
 | 
			
		||||
 | 
			
		||||
    *handlep = handle;
 | 
			
		||||
 | 
			
		||||
    if (obtfmt != req->fmt ||
 | 
			
		||||
    if (conf.verbose &&
 | 
			
		||||
        (obtfmt != req->fmt ||
 | 
			
		||||
         obt->nchannels != req->nchannels ||
 | 
			
		||||
         obt->freq != req->freq) {
 | 
			
		||||
         obt->freq != req->freq)) {
 | 
			
		||||
        dolog ("Audio parameters for %s\n", typ);
 | 
			
		||||
        alsa_dump_info (req, obt, obtfmt);
 | 
			
		||||
    }
 | 
			
		||||
@@ -720,7 +743,9 @@ static void alsa_write_pending (ALSAVoiceOut *alsa)
 | 
			
		||||
            if (written <= 0) {
 | 
			
		||||
                switch (written) {
 | 
			
		||||
                case 0:
 | 
			
		||||
                    trace_alsa_wrote_zero(len);
 | 
			
		||||
                    if (conf.verbose) {
 | 
			
		||||
                        dolog ("Failed to write %d frames (wrote zero)\n", len);
 | 
			
		||||
                    }
 | 
			
		||||
                    return;
 | 
			
		||||
 | 
			
		||||
                case -EPIPE:
 | 
			
		||||
@@ -729,7 +754,9 @@ static void alsa_write_pending (ALSAVoiceOut *alsa)
 | 
			
		||||
                                     len);
 | 
			
		||||
                        return;
 | 
			
		||||
                    }
 | 
			
		||||
                    trace_alsa_xrun_out();
 | 
			
		||||
                    if (conf.verbose) {
 | 
			
		||||
                        dolog ("Recovering from playback xrun\n");
 | 
			
		||||
                    }
 | 
			
		||||
                    continue;
 | 
			
		||||
 | 
			
		||||
                case -ESTRPIPE:
 | 
			
		||||
@@ -740,7 +767,9 @@ static void alsa_write_pending (ALSAVoiceOut *alsa)
 | 
			
		||||
                                     len);
 | 
			
		||||
                        return;
 | 
			
		||||
                    }
 | 
			
		||||
                    trace_alsa_resume_out();
 | 
			
		||||
                    if (conf.verbose) {
 | 
			
		||||
                        dolog ("Resuming suspended output stream\n");
 | 
			
		||||
                    }
 | 
			
		||||
                    continue;
 | 
			
		||||
 | 
			
		||||
                case -EAGAIN:
 | 
			
		||||
@@ -790,27 +819,25 @@ static void alsa_fini_out (HWVoiceOut *hw)
 | 
			
		||||
    alsa->pcm_buf = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int alsa_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
                         void *drv_opaque)
 | 
			
		||||
static int alsa_init_out (HWVoiceOut *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
 | 
			
		||||
    struct alsa_params_req req;
 | 
			
		||||
    struct alsa_params_obt obt;
 | 
			
		||||
    snd_pcm_t *handle;
 | 
			
		||||
    struct audsettings obt_as;
 | 
			
		||||
    ALSAConf *conf = drv_opaque;
 | 
			
		||||
 | 
			
		||||
    req.fmt = aud_to_alsafmt (as->fmt, as->endianness);
 | 
			
		||||
    req.freq = as->freq;
 | 
			
		||||
    req.nchannels = as->nchannels;
 | 
			
		||||
    req.period_size = conf->period_size_out;
 | 
			
		||||
    req.buffer_size = conf->buffer_size_out;
 | 
			
		||||
    req.size_in_usec = conf->size_in_usec_out;
 | 
			
		||||
    req.period_size = conf.period_size_out;
 | 
			
		||||
    req.buffer_size = conf.buffer_size_out;
 | 
			
		||||
    req.size_in_usec = conf.size_in_usec_out;
 | 
			
		||||
    req.override_mask =
 | 
			
		||||
        (conf->period_size_out_overridden ? 1 : 0) |
 | 
			
		||||
        (conf->buffer_size_out_overridden ? 2 : 0);
 | 
			
		||||
        (conf.period_size_out_overridden ? 1 : 0) |
 | 
			
		||||
        (conf.buffer_size_out_overridden ? 2 : 0);
 | 
			
		||||
 | 
			
		||||
    if (alsa_open (0, &req, &obt, &handle, conf)) {
 | 
			
		||||
    if (alsa_open (0, &req, &obt, &handle)) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -831,7 +858,6 @@ static int alsa_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    alsa->handle = handle;
 | 
			
		||||
    alsa->pollhlp.conf = conf;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -902,26 +928,25 @@ static int alsa_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int alsa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
 | 
			
		||||
static int alsa_init_in (HWVoiceIn *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
 | 
			
		||||
    struct alsa_params_req req;
 | 
			
		||||
    struct alsa_params_obt obt;
 | 
			
		||||
    snd_pcm_t *handle;
 | 
			
		||||
    struct audsettings obt_as;
 | 
			
		||||
    ALSAConf *conf = drv_opaque;
 | 
			
		||||
 | 
			
		||||
    req.fmt = aud_to_alsafmt (as->fmt, as->endianness);
 | 
			
		||||
    req.freq = as->freq;
 | 
			
		||||
    req.nchannels = as->nchannels;
 | 
			
		||||
    req.period_size = conf->period_size_in;
 | 
			
		||||
    req.buffer_size = conf->buffer_size_in;
 | 
			
		||||
    req.size_in_usec = conf->size_in_usec_in;
 | 
			
		||||
    req.period_size = conf.period_size_in;
 | 
			
		||||
    req.buffer_size = conf.buffer_size_in;
 | 
			
		||||
    req.size_in_usec = conf.size_in_usec_in;
 | 
			
		||||
    req.override_mask =
 | 
			
		||||
        (conf->period_size_in_overridden ? 1 : 0) |
 | 
			
		||||
        (conf->buffer_size_in_overridden ? 2 : 0);
 | 
			
		||||
        (conf.period_size_in_overridden ? 1 : 0) |
 | 
			
		||||
        (conf.buffer_size_in_overridden ? 2 : 0);
 | 
			
		||||
 | 
			
		||||
    if (alsa_open (1, &req, &obt, &handle, conf)) {
 | 
			
		||||
    if (alsa_open (1, &req, &obt, &handle)) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -942,7 +967,6 @@ static int alsa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    alsa->handle = handle;
 | 
			
		||||
    alsa->pollhlp.conf = conf;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -998,10 +1022,14 @@ static int alsa_run_in (HWVoiceIn *hw)
 | 
			
		||||
                dolog ("Failed to resume suspended input stream\n");
 | 
			
		||||
                return 0;
 | 
			
		||||
            }
 | 
			
		||||
            trace_alsa_resume_in();
 | 
			
		||||
            if (conf.verbose) {
 | 
			
		||||
                dolog ("Resuming suspended input stream\n");
 | 
			
		||||
            }
 | 
			
		||||
            break;
 | 
			
		||||
        default:
 | 
			
		||||
            trace_alsa_no_frames(state);
 | 
			
		||||
            if (conf.verbose) {
 | 
			
		||||
                dolog ("No frames available and ALSA state is %d\n", state);
 | 
			
		||||
            }
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
@@ -1036,7 +1064,9 @@ static int alsa_run_in (HWVoiceIn *hw)
 | 
			
		||||
            if (nread <= 0) {
 | 
			
		||||
                switch (nread) {
 | 
			
		||||
                case 0:
 | 
			
		||||
                    trace_alsa_read_zero(len);
 | 
			
		||||
                    if (conf.verbose) {
 | 
			
		||||
                        dolog ("Failed to read %ld frames (read zero)\n", len);
 | 
			
		||||
                    }
 | 
			
		||||
                    goto exit;
 | 
			
		||||
 | 
			
		||||
                case -EPIPE:
 | 
			
		||||
@@ -1044,7 +1074,9 @@ static int alsa_run_in (HWVoiceIn *hw)
 | 
			
		||||
                        alsa_logerr (nread, "Failed to read %ld frames\n", len);
 | 
			
		||||
                        goto exit;
 | 
			
		||||
                    }
 | 
			
		||||
                    trace_alsa_xrun_in();
 | 
			
		||||
                    if (conf.verbose) {
 | 
			
		||||
                        dolog ("Recovering from capture xrun\n");
 | 
			
		||||
                    }
 | 
			
		||||
                    continue;
 | 
			
		||||
 | 
			
		||||
                case -EAGAIN:
 | 
			
		||||
@@ -1116,85 +1148,82 @@ static int alsa_ctl_in (HWVoiceIn *hw, int cmd, ...)
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static ALSAConf glob_conf = {
 | 
			
		||||
    .buffer_size_out = 4096,
 | 
			
		||||
    .period_size_out = 1024,
 | 
			
		||||
    .pcm_name_out = "default",
 | 
			
		||||
    .pcm_name_in = "default",
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void *alsa_audio_init (void)
 | 
			
		||||
{
 | 
			
		||||
    ALSAConf *conf = g_malloc(sizeof(ALSAConf));
 | 
			
		||||
    *conf = glob_conf;
 | 
			
		||||
    return conf;
 | 
			
		||||
    return &conf;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void alsa_audio_fini (void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    g_free(opaque);
 | 
			
		||||
    (void) opaque;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct audio_option alsa_options[] = {
 | 
			
		||||
    {
 | 
			
		||||
        .name        = "DAC_SIZE_IN_USEC",
 | 
			
		||||
        .tag         = AUD_OPT_BOOL,
 | 
			
		||||
        .valp        = &glob_conf.size_in_usec_out,
 | 
			
		||||
        .valp        = &conf.size_in_usec_out,
 | 
			
		||||
        .descr       = "DAC period/buffer size in microseconds (otherwise in frames)"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name        = "DAC_PERIOD_SIZE",
 | 
			
		||||
        .tag         = AUD_OPT_INT,
 | 
			
		||||
        .valp        = &glob_conf.period_size_out,
 | 
			
		||||
        .valp        = &conf.period_size_out,
 | 
			
		||||
        .descr       = "DAC period size (0 to go with system default)",
 | 
			
		||||
        .overriddenp = &glob_conf.period_size_out_overridden
 | 
			
		||||
        .overriddenp = &conf.period_size_out_overridden
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name        = "DAC_BUFFER_SIZE",
 | 
			
		||||
        .tag         = AUD_OPT_INT,
 | 
			
		||||
        .valp        = &glob_conf.buffer_size_out,
 | 
			
		||||
        .valp        = &conf.buffer_size_out,
 | 
			
		||||
        .descr       = "DAC buffer size (0 to go with system default)",
 | 
			
		||||
        .overriddenp = &glob_conf.buffer_size_out_overridden
 | 
			
		||||
        .overriddenp = &conf.buffer_size_out_overridden
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name        = "ADC_SIZE_IN_USEC",
 | 
			
		||||
        .tag         = AUD_OPT_BOOL,
 | 
			
		||||
        .valp        = &glob_conf.size_in_usec_in,
 | 
			
		||||
        .valp        = &conf.size_in_usec_in,
 | 
			
		||||
        .descr       =
 | 
			
		||||
        "ADC period/buffer size in microseconds (otherwise in frames)"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name        = "ADC_PERIOD_SIZE",
 | 
			
		||||
        .tag         = AUD_OPT_INT,
 | 
			
		||||
        .valp        = &glob_conf.period_size_in,
 | 
			
		||||
        .valp        = &conf.period_size_in,
 | 
			
		||||
        .descr       = "ADC period size (0 to go with system default)",
 | 
			
		||||
        .overriddenp = &glob_conf.period_size_in_overridden
 | 
			
		||||
        .overriddenp = &conf.period_size_in_overridden
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name        = "ADC_BUFFER_SIZE",
 | 
			
		||||
        .tag         = AUD_OPT_INT,
 | 
			
		||||
        .valp        = &glob_conf.buffer_size_in,
 | 
			
		||||
        .valp        = &conf.buffer_size_in,
 | 
			
		||||
        .descr       = "ADC buffer size (0 to go with system default)",
 | 
			
		||||
        .overriddenp = &glob_conf.buffer_size_in_overridden
 | 
			
		||||
        .overriddenp = &conf.buffer_size_in_overridden
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name        = "THRESHOLD",
 | 
			
		||||
        .tag         = AUD_OPT_INT,
 | 
			
		||||
        .valp        = &glob_conf.threshold,
 | 
			
		||||
        .valp        = &conf.threshold,
 | 
			
		||||
        .descr       = "(undocumented)"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name        = "DAC_DEV",
 | 
			
		||||
        .tag         = AUD_OPT_STR,
 | 
			
		||||
        .valp        = &glob_conf.pcm_name_out,
 | 
			
		||||
        .valp        = &conf.pcm_name_out,
 | 
			
		||||
        .descr       = "DAC device name (for instance dmix)"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name        = "ADC_DEV",
 | 
			
		||||
        .tag         = AUD_OPT_STR,
 | 
			
		||||
        .valp        = &glob_conf.pcm_name_in,
 | 
			
		||||
        .valp        = &conf.pcm_name_in,
 | 
			
		||||
        .descr       = "ADC device name"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name        = "VERBOSE",
 | 
			
		||||
        .tag         = AUD_OPT_BOOL,
 | 
			
		||||
        .valp        = &conf.verbose,
 | 
			
		||||
        .descr       = "Behave in a more verbose way"
 | 
			
		||||
    },
 | 
			
		||||
    { /* End of list */ }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -30,6 +30,7 @@
 | 
			
		||||
#define AUDIO_CAP "audio"
 | 
			
		||||
#include "audio_int.h"
 | 
			
		||||
 | 
			
		||||
/* #define DEBUG_PLIVE */
 | 
			
		||||
/* #define DEBUG_LIVE */
 | 
			
		||||
/* #define DEBUG_OUT */
 | 
			
		||||
/* #define DEBUG_CAPTURE */
 | 
			
		||||
@@ -65,6 +66,8 @@ static struct {
 | 
			
		||||
        int hertz;
 | 
			
		||||
        int64_t ticks;
 | 
			
		||||
    } period;
 | 
			
		||||
    int plive;
 | 
			
		||||
    int log_to_monitor;
 | 
			
		||||
    int try_poll_in;
 | 
			
		||||
    int try_poll_out;
 | 
			
		||||
} conf = {
 | 
			
		||||
@@ -93,6 +96,8 @@ static struct {
 | 
			
		||||
    },
 | 
			
		||||
 | 
			
		||||
    .period = { .hertz = 100 },
 | 
			
		||||
    .plive = 0,
 | 
			
		||||
    .log_to_monitor = 0,
 | 
			
		||||
    .try_poll_in = 1,
 | 
			
		||||
    .try_poll_out = 1,
 | 
			
		||||
};
 | 
			
		||||
@@ -326,11 +331,20 @@ static const char *audio_get_conf_str (const char *key,
 | 
			
		||||
 | 
			
		||||
void AUD_vlog (const char *cap, const char *fmt, va_list ap)
 | 
			
		||||
{
 | 
			
		||||
    if (cap) {
 | 
			
		||||
        fprintf(stderr, "%s: ", cap);
 | 
			
		||||
    }
 | 
			
		||||
    if (conf.log_to_monitor) {
 | 
			
		||||
        if (cap) {
 | 
			
		||||
            monitor_printf(default_mon, "%s: ", cap);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    vfprintf(stderr, fmt, ap);
 | 
			
		||||
        monitor_vprintf(default_mon, fmt, ap);
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
        if (cap) {
 | 
			
		||||
            fprintf (stderr, "%s: ", cap);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        vfprintf (stderr, fmt, ap);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void AUD_log (const char *cap, const char *fmt, ...)
 | 
			
		||||
@@ -1440,6 +1454,9 @@ static void audio_run_out (AudioState *s)
 | 
			
		||||
            while (sw) {
 | 
			
		||||
                sw1 = sw->entries.le_next;
 | 
			
		||||
                if (!sw->active && !sw->callback.fn) {
 | 
			
		||||
#ifdef DEBUG_PLIVE
 | 
			
		||||
                    dolog ("Finishing with old voice\n");
 | 
			
		||||
#endif
 | 
			
		||||
                    audio_close_out (sw);
 | 
			
		||||
                }
 | 
			
		||||
                sw = sw1;
 | 
			
		||||
@@ -1631,6 +1648,18 @@ static struct audio_option audio_options[] = {
 | 
			
		||||
        .valp  = &conf.period.hertz,
 | 
			
		||||
        .descr = "Timer period in HZ (0 - use lowest possible)"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "PLIVE",
 | 
			
		||||
        .tag   = AUD_OPT_BOOL,
 | 
			
		||||
        .valp  = &conf.plive,
 | 
			
		||||
        .descr = "(undocumented)"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "LOG_TO_MONITOR",
 | 
			
		||||
        .tag   = AUD_OPT_BOOL,
 | 
			
		||||
        .valp  = &conf.log_to_monitor,
 | 
			
		||||
        .descr = "Print logging messages to monitor instead of stderr"
 | 
			
		||||
    },
 | 
			
		||||
    { /* End of list */ }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -156,13 +156,13 @@ struct audio_driver {
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct audio_pcm_ops {
 | 
			
		||||
    int  (*init_out)(HWVoiceOut *hw, struct audsettings *as, void *drv_opaque);
 | 
			
		||||
    int  (*init_out)(HWVoiceOut *hw, struct audsettings *as);
 | 
			
		||||
    void (*fini_out)(HWVoiceOut *hw);
 | 
			
		||||
    int  (*run_out) (HWVoiceOut *hw, int live);
 | 
			
		||||
    int  (*write)   (SWVoiceOut *sw, void *buf, int size);
 | 
			
		||||
    int  (*ctl_out) (HWVoiceOut *hw, int cmd, ...);
 | 
			
		||||
 | 
			
		||||
    int  (*init_in) (HWVoiceIn *hw, struct audsettings *as, void *drv_opaque);
 | 
			
		||||
    int  (*init_in) (HWVoiceIn *hw, struct audsettings *as);
 | 
			
		||||
    void (*fini_in) (HWVoiceIn *hw);
 | 
			
		||||
    int  (*run_in)  (HWVoiceIn *hw);
 | 
			
		||||
    int  (*read)    (SWVoiceIn *sw, void *buf, int size);
 | 
			
		||||
@@ -206,11 +206,14 @@ extern struct audio_driver no_audio_driver;
 | 
			
		||||
extern struct audio_driver oss_audio_driver;
 | 
			
		||||
extern struct audio_driver sdl_audio_driver;
 | 
			
		||||
extern struct audio_driver wav_audio_driver;
 | 
			
		||||
extern struct audio_driver fmod_audio_driver;
 | 
			
		||||
extern struct audio_driver alsa_audio_driver;
 | 
			
		||||
extern struct audio_driver coreaudio_audio_driver;
 | 
			
		||||
extern struct audio_driver dsound_audio_driver;
 | 
			
		||||
extern struct audio_driver esd_audio_driver;
 | 
			
		||||
extern struct audio_driver pa_audio_driver;
 | 
			
		||||
extern struct audio_driver spice_audio_driver;
 | 
			
		||||
extern struct audio_driver winwave_audio_driver;
 | 
			
		||||
extern const struct mixeng_volume nominal_volume;
 | 
			
		||||
 | 
			
		||||
void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as);
 | 
			
		||||
 
 | 
			
		||||
@@ -262,7 +262,7 @@ static HW *glue (audio_pcm_hw_add_new_, TYPE) (struct audsettings *as)
 | 
			
		||||
#ifdef DAC
 | 
			
		||||
    QLIST_INIT (&hw->cap_head);
 | 
			
		||||
#endif
 | 
			
		||||
    if (glue (hw->pcm_ops->init_, TYPE) (hw, as, s->drv_opaque)) {
 | 
			
		||||
    if (glue (hw->pcm_ops->init_, TYPE) (hw, as)) {
 | 
			
		||||
        goto err0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -398,6 +398,10 @@ SW *glue (AUD_open_, TYPE) (
 | 
			
		||||
    )
 | 
			
		||||
{
 | 
			
		||||
    AudioState *s = &glob_audio_state;
 | 
			
		||||
#ifdef DAC
 | 
			
		||||
    int live = 0;
 | 
			
		||||
    SW *old_sw = NULL;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    if (audio_bug (AUDIO_FUNC, !card || !name || !callback_fn || !as)) {
 | 
			
		||||
        dolog ("card=%p name=%p callback_fn=%p as=%p\n",
 | 
			
		||||
@@ -422,6 +426,29 @@ SW *glue (AUD_open_, TYPE) (
 | 
			
		||||
        return sw;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
#ifdef DAC
 | 
			
		||||
    if (conf.plive && sw && (!sw->active && !sw->empty)) {
 | 
			
		||||
        live = sw->total_hw_samples_mixed;
 | 
			
		||||
 | 
			
		||||
#ifdef DEBUG_PLIVE
 | 
			
		||||
        dolog ("Replacing voice %s with %d live samples\n", SW_NAME (sw), live);
 | 
			
		||||
        dolog ("Old %s freq %d, bits %d, channels %d\n",
 | 
			
		||||
               SW_NAME (sw), sw->info.freq, sw->info.bits, sw->info.nchannels);
 | 
			
		||||
        dolog ("New %s freq %d, bits %d, channels %d\n",
 | 
			
		||||
               name,
 | 
			
		||||
               as->freq,
 | 
			
		||||
               (as->fmt == AUD_FMT_S16 || as->fmt == AUD_FMT_U16) ? 16 : 8,
 | 
			
		||||
               as->nchannels);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
        if (live) {
 | 
			
		||||
            old_sw = sw;
 | 
			
		||||
            old_sw->callback.fn = NULL;
 | 
			
		||||
            sw = NULL;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    if (!glue (conf.fixed_, TYPE).enabled && sw) {
 | 
			
		||||
        glue (AUD_close_, TYPE) (card, sw);
 | 
			
		||||
        sw = NULL;
 | 
			
		||||
@@ -454,6 +481,20 @@ SW *glue (AUD_open_, TYPE) (
 | 
			
		||||
    sw->callback.fn = callback_fn;
 | 
			
		||||
    sw->callback.opaque = callback_opaque;
 | 
			
		||||
 | 
			
		||||
#ifdef DAC
 | 
			
		||||
    if (live) {
 | 
			
		||||
        int mixed =
 | 
			
		||||
            (live << old_sw->info.shift)
 | 
			
		||||
            * old_sw->info.bytes_per_second
 | 
			
		||||
            / sw->info.bytes_per_second;
 | 
			
		||||
 | 
			
		||||
#ifdef DEBUG_PLIVE
 | 
			
		||||
        dolog ("Silence will be mixed %d\n", mixed);
 | 
			
		||||
#endif
 | 
			
		||||
        sw->total_hw_samples_mixed += mixed;
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef DEBUG_AUDIO
 | 
			
		||||
    dolog ("%s\n", name);
 | 
			
		||||
    audio_pcm_print_info ("hw", &sw->hw->info);
 | 
			
		||||
 
 | 
			
		||||
@@ -32,16 +32,20 @@
 | 
			
		||||
#define AUDIO_CAP "coreaudio"
 | 
			
		||||
#include "audio_int.h"
 | 
			
		||||
 | 
			
		||||
static int isAtexit;
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
struct {
 | 
			
		||||
    int buffer_frames;
 | 
			
		||||
    int nbuffers;
 | 
			
		||||
} CoreaudioConf;
 | 
			
		||||
    int isAtexit;
 | 
			
		||||
} conf = {
 | 
			
		||||
    .buffer_frames = 512,
 | 
			
		||||
    .nbuffers = 4,
 | 
			
		||||
    .isAtexit = 0
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
typedef struct coreaudioVoiceOut {
 | 
			
		||||
    HWVoiceOut hw;
 | 
			
		||||
    pthread_mutex_t mutex;
 | 
			
		||||
    int isAtexit;
 | 
			
		||||
    AudioDeviceID outputDeviceID;
 | 
			
		||||
    UInt32 audioDevicePropertyBufferFrameSize;
 | 
			
		||||
    AudioStreamBasicDescription outputStreamBasicDescription;
 | 
			
		||||
@@ -157,7 +161,7 @@ static inline UInt32 isPlaying (AudioDeviceID outputDeviceID)
 | 
			
		||||
 | 
			
		||||
static void coreaudio_atexit (void)
 | 
			
		||||
{
 | 
			
		||||
    isAtexit = 1;
 | 
			
		||||
    conf.isAtexit = 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int coreaudio_lock (coreaudioVoiceOut *core, const char *fn_name)
 | 
			
		||||
@@ -283,8 +287,7 @@ static int coreaudio_write (SWVoiceOut *sw, void *buf, int len)
 | 
			
		||||
    return audio_pcm_sw_write (sw, buf, len);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
                              void *drv_opaque)
 | 
			
		||||
static int coreaudio_init_out (HWVoiceOut *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    OSStatus status;
 | 
			
		||||
    coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw;
 | 
			
		||||
@@ -292,7 +295,6 @@ static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
    int err;
 | 
			
		||||
    const char *typ = "playback";
 | 
			
		||||
    AudioValueRange frameRange;
 | 
			
		||||
    CoreaudioConf *conf = drv_opaque;
 | 
			
		||||
 | 
			
		||||
    /* create mutex */
 | 
			
		||||
    err = pthread_mutex_init(&core->mutex, NULL);
 | 
			
		||||
@@ -334,16 +336,16 @@ static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (frameRange.mMinimum > conf->buffer_frames) {
 | 
			
		||||
    if (frameRange.mMinimum > conf.buffer_frames) {
 | 
			
		||||
        core->audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMinimum;
 | 
			
		||||
        dolog ("warning: Upsizing Buffer Frames to %f\n", frameRange.mMinimum);
 | 
			
		||||
    }
 | 
			
		||||
    else if (frameRange.mMaximum < conf->buffer_frames) {
 | 
			
		||||
    else if (frameRange.mMaximum < conf.buffer_frames) {
 | 
			
		||||
        core->audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMaximum;
 | 
			
		||||
        dolog ("warning: Downsizing Buffer Frames to %f\n", frameRange.mMaximum);
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
        core->audioDevicePropertyBufferFrameSize = conf->buffer_frames;
 | 
			
		||||
        core->audioDevicePropertyBufferFrameSize = conf.buffer_frames;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* set Buffer Frame Size */
 | 
			
		||||
@@ -377,7 +379,7 @@ static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
                           "Could not get device buffer frame size\n");
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
    hw->samples = conf->nbuffers * core->audioDevicePropertyBufferFrameSize;
 | 
			
		||||
    hw->samples = conf.nbuffers * core->audioDevicePropertyBufferFrameSize;
 | 
			
		||||
 | 
			
		||||
    /* get StreamFormat */
 | 
			
		||||
    propertySize = sizeof(core->outputStreamBasicDescription);
 | 
			
		||||
@@ -441,7 +443,7 @@ static void coreaudio_fini_out (HWVoiceOut *hw)
 | 
			
		||||
    int err;
 | 
			
		||||
    coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw;
 | 
			
		||||
 | 
			
		||||
    if (!isAtexit) {
 | 
			
		||||
    if (!conf.isAtexit) {
 | 
			
		||||
        /* stop playback */
 | 
			
		||||
        if (isPlaying(core->outputDeviceID)) {
 | 
			
		||||
            status = AudioDeviceStop(core->outputDeviceID, audioDeviceIOProc);
 | 
			
		||||
@@ -484,7 +486,7 @@ static int coreaudio_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
 | 
			
		||||
    case VOICE_DISABLE:
 | 
			
		||||
        /* stop playback */
 | 
			
		||||
        if (!isAtexit) {
 | 
			
		||||
        if (!conf.isAtexit) {
 | 
			
		||||
            if (isPlaying(core->outputDeviceID)) {
 | 
			
		||||
                status = AudioDeviceStop(core->outputDeviceID, audioDeviceIOProc);
 | 
			
		||||
                if (status != kAudioHardwareNoError) {
 | 
			
		||||
@@ -497,36 +499,28 @@ static int coreaudio_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static CoreaudioConf glob_conf = {
 | 
			
		||||
    .buffer_frames = 512,
 | 
			
		||||
    .nbuffers = 4,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void *coreaudio_audio_init (void)
 | 
			
		||||
{
 | 
			
		||||
    CoreaudioConf *conf = g_malloc(sizeof(CoreaudioConf));
 | 
			
		||||
    *conf = glob_conf;
 | 
			
		||||
 | 
			
		||||
    atexit(coreaudio_atexit);
 | 
			
		||||
    return conf;
 | 
			
		||||
    return &coreaudio_audio_init;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void coreaudio_audio_fini (void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    g_free(opaque);
 | 
			
		||||
    (void) opaque;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct audio_option coreaudio_options[] = {
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "BUFFER_SIZE",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &glob_conf.buffer_frames,
 | 
			
		||||
        .valp  = &conf.buffer_frames,
 | 
			
		||||
        .descr = "Size of the buffer in frames"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "BUFFER_COUNT",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &glob_conf.nbuffers,
 | 
			
		||||
        .valp  = &conf.nbuffers,
 | 
			
		||||
        .descr = "Number of buffers"
 | 
			
		||||
    },
 | 
			
		||||
    { /* End of list */ }
 | 
			
		||||
 
 | 
			
		||||
@@ -67,11 +67,11 @@ static int glue (dsound_lock_, TYPE) (
 | 
			
		||||
    LPVOID *p2p,
 | 
			
		||||
    DWORD *blen1p,
 | 
			
		||||
    DWORD *blen2p,
 | 
			
		||||
    int entire,
 | 
			
		||||
    dsound *s
 | 
			
		||||
    int entire
 | 
			
		||||
    )
 | 
			
		||||
{
 | 
			
		||||
    HRESULT hr;
 | 
			
		||||
    int i;
 | 
			
		||||
    LPVOID p1 = NULL, p2 = NULL;
 | 
			
		||||
    DWORD blen1 = 0, blen2 = 0;
 | 
			
		||||
    DWORD flag;
 | 
			
		||||
@@ -81,18 +81,37 @@ static int glue (dsound_lock_, TYPE) (
 | 
			
		||||
#else
 | 
			
		||||
    flag = entire ? DSBLOCK_ENTIREBUFFER : 0;
 | 
			
		||||
#endif
 | 
			
		||||
    hr = glue(IFACE, _Lock)(buf, pos, len, &p1, &blen1, &p2, &blen2, flag);
 | 
			
		||||
    for (i = 0; i < conf.lock_retries; ++i) {
 | 
			
		||||
        hr = glue (IFACE, _Lock) (
 | 
			
		||||
            buf,
 | 
			
		||||
            pos,
 | 
			
		||||
            len,
 | 
			
		||||
            &p1,
 | 
			
		||||
            &blen1,
 | 
			
		||||
            &p2,
 | 
			
		||||
            &blen2,
 | 
			
		||||
            flag
 | 
			
		||||
            );
 | 
			
		||||
 | 
			
		||||
    if (FAILED (hr)) {
 | 
			
		||||
        if (FAILED (hr)) {
 | 
			
		||||
#ifndef DSBTYPE_IN
 | 
			
		||||
        if (hr == DSERR_BUFFERLOST) {
 | 
			
		||||
            if (glue (dsound_restore_, TYPE) (buf, s)) {
 | 
			
		||||
                dsound_logerr (hr, "Could not lock " NAME "\n");
 | 
			
		||||
            if (hr == DSERR_BUFFERLOST) {
 | 
			
		||||
                if (glue (dsound_restore_, TYPE) (buf)) {
 | 
			
		||||
                    dsound_logerr (hr, "Could not lock " NAME "\n");
 | 
			
		||||
                    goto fail;
 | 
			
		||||
                }
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
#endif
 | 
			
		||||
            dsound_logerr (hr, "Could not lock " NAME "\n");
 | 
			
		||||
            goto fail;
 | 
			
		||||
        }
 | 
			
		||||
#endif
 | 
			
		||||
        dsound_logerr (hr, "Could not lock " NAME "\n");
 | 
			
		||||
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (i == conf.lock_retries) {
 | 
			
		||||
        dolog ("%d attempts to lock " NAME " failed\n", i);
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -155,19 +174,16 @@ static void dsound_fini_out (HWVoiceOut *hw)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#ifdef DSBTYPE_IN
 | 
			
		||||
static int dsound_init_in(HWVoiceIn *hw, struct audsettings *as,
 | 
			
		||||
                          void *drv_opaque)
 | 
			
		||||
static int dsound_init_in (HWVoiceIn *hw, struct audsettings *as)
 | 
			
		||||
#else
 | 
			
		||||
static int dsound_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
                           void *drv_opaque)
 | 
			
		||||
static int dsound_init_out (HWVoiceOut *hw, struct audsettings *as)
 | 
			
		||||
#endif
 | 
			
		||||
{
 | 
			
		||||
    int err;
 | 
			
		||||
    HRESULT hr;
 | 
			
		||||
    dsound *s = drv_opaque;
 | 
			
		||||
    dsound *s = &glob_dsound;
 | 
			
		||||
    WAVEFORMATEX wfx;
 | 
			
		||||
    struct audsettings obt_as;
 | 
			
		||||
    DSoundConf *conf = &s->conf;
 | 
			
		||||
#ifdef DSBTYPE_IN
 | 
			
		||||
    const char *typ = "ADC";
 | 
			
		||||
    DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
 | 
			
		||||
@@ -194,7 +210,7 @@ static int dsound_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
    bd.dwSize = sizeof (bd);
 | 
			
		||||
    bd.lpwfxFormat = &wfx;
 | 
			
		||||
#ifdef DSBTYPE_IN
 | 
			
		||||
    bd.dwBufferBytes = conf->bufsize_in;
 | 
			
		||||
    bd.dwBufferBytes = conf.bufsize_in;
 | 
			
		||||
    hr = IDirectSoundCapture_CreateCaptureBuffer (
 | 
			
		||||
        s->dsound_capture,
 | 
			
		||||
        &bd,
 | 
			
		||||
@@ -203,7 +219,7 @@ static int dsound_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
        );
 | 
			
		||||
#else
 | 
			
		||||
    bd.dwFlags = DSBCAPS_STICKYFOCUS | DSBCAPS_GETCURRENTPOSITION2;
 | 
			
		||||
    bd.dwBufferBytes = conf->bufsize_out;
 | 
			
		||||
    bd.dwBufferBytes = conf.bufsize_out;
 | 
			
		||||
    hr = IDirectSound_CreateSoundBuffer (
 | 
			
		||||
        s->dsound,
 | 
			
		||||
        &bd,
 | 
			
		||||
@@ -253,7 +269,6 @@ static int dsound_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
            );
 | 
			
		||||
    }
 | 
			
		||||
    hw->samples = bc.dwBufferBytes >> hw->info.shift;
 | 
			
		||||
    ds->s = s;
 | 
			
		||||
 | 
			
		||||
#ifdef DEBUG_DSOUND
 | 
			
		||||
    dolog ("caps %ld, desc %ld\n",
 | 
			
		||||
 
 | 
			
		||||
@@ -41,25 +41,42 @@
 | 
			
		||||
 | 
			
		||||
/* #define DEBUG_DSOUND */
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
static struct {
 | 
			
		||||
    int lock_retries;
 | 
			
		||||
    int restore_retries;
 | 
			
		||||
    int getstatus_retries;
 | 
			
		||||
    int set_primary;
 | 
			
		||||
    int bufsize_in;
 | 
			
		||||
    int bufsize_out;
 | 
			
		||||
    struct audsettings settings;
 | 
			
		||||
    int latency_millis;
 | 
			
		||||
} DSoundConf;
 | 
			
		||||
} conf = {
 | 
			
		||||
    .lock_retries       = 1,
 | 
			
		||||
    .restore_retries    = 1,
 | 
			
		||||
    .getstatus_retries  = 1,
 | 
			
		||||
    .set_primary        = 0,
 | 
			
		||||
    .bufsize_in         = 16384,
 | 
			
		||||
    .bufsize_out        = 16384,
 | 
			
		||||
    .settings.freq      = 44100,
 | 
			
		||||
    .settings.nchannels = 2,
 | 
			
		||||
    .settings.fmt       = AUD_FMT_S16,
 | 
			
		||||
    .latency_millis     = 10
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    LPDIRECTSOUND dsound;
 | 
			
		||||
    LPDIRECTSOUNDCAPTURE dsound_capture;
 | 
			
		||||
    LPDIRECTSOUNDBUFFER dsound_primary_buffer;
 | 
			
		||||
    struct audsettings settings;
 | 
			
		||||
    DSoundConf conf;
 | 
			
		||||
} dsound;
 | 
			
		||||
 | 
			
		||||
static dsound glob_dsound;
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    HWVoiceOut hw;
 | 
			
		||||
    LPDIRECTSOUNDBUFFER dsound_buffer;
 | 
			
		||||
    DWORD old_pos;
 | 
			
		||||
    int first_time;
 | 
			
		||||
    dsound *s;
 | 
			
		||||
#ifdef DEBUG_DSOUND
 | 
			
		||||
    DWORD old_ppos;
 | 
			
		||||
    DWORD played;
 | 
			
		||||
@@ -71,7 +88,6 @@ typedef struct {
 | 
			
		||||
    HWVoiceIn hw;
 | 
			
		||||
    int first_time;
 | 
			
		||||
    LPDIRECTSOUNDCAPTUREBUFFER dsound_capture_buffer;
 | 
			
		||||
    dsound *s;
 | 
			
		||||
} DSoundVoiceIn;
 | 
			
		||||
 | 
			
		||||
static void dsound_log_hresult (HRESULT hr)
 | 
			
		||||
@@ -265,17 +281,29 @@ static void print_wave_format (WAVEFORMATEX *wfx)
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb, dsound *s)
 | 
			
		||||
static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb)
 | 
			
		||||
{
 | 
			
		||||
    HRESULT hr;
 | 
			
		||||
    int i;
 | 
			
		||||
 | 
			
		||||
    hr = IDirectSoundBuffer_Restore (dsb);
 | 
			
		||||
    for (i = 0; i < conf.restore_retries; ++i) {
 | 
			
		||||
        hr = IDirectSoundBuffer_Restore (dsb);
 | 
			
		||||
 | 
			
		||||
    if (hr != DS_OK) {
 | 
			
		||||
        dsound_logerr (hr, "Could not restore playback buffer\n");
 | 
			
		||||
        return -1;
 | 
			
		||||
        switch (hr) {
 | 
			
		||||
        case DS_OK:
 | 
			
		||||
            return 0;
 | 
			
		||||
 | 
			
		||||
        case DSERR_BUFFERLOST:
 | 
			
		||||
            continue;
 | 
			
		||||
 | 
			
		||||
        default:
 | 
			
		||||
            dsound_logerr (hr, "Could not restore playback buffer\n");
 | 
			
		||||
            return -1;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return 0;
 | 
			
		||||
 | 
			
		||||
    dolog ("%d attempts to restore playback buffer failed\n", i);
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#include "dsound_template.h"
 | 
			
		||||
@@ -283,20 +311,25 @@ static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb, dsound *s)
 | 
			
		||||
#include "dsound_template.h"
 | 
			
		||||
#undef DSBTYPE_IN
 | 
			
		||||
 | 
			
		||||
static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb, DWORD *statusp,
 | 
			
		||||
                                  dsound *s)
 | 
			
		||||
static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb, DWORD *statusp)
 | 
			
		||||
{
 | 
			
		||||
    HRESULT hr;
 | 
			
		||||
    int i;
 | 
			
		||||
 | 
			
		||||
    hr = IDirectSoundBuffer_GetStatus (dsb, statusp);
 | 
			
		||||
    if (FAILED (hr)) {
 | 
			
		||||
        dsound_logerr (hr, "Could not get playback buffer status\n");
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
    for (i = 0; i < conf.getstatus_retries; ++i) {
 | 
			
		||||
        hr = IDirectSoundBuffer_GetStatus (dsb, statusp);
 | 
			
		||||
        if (FAILED (hr)) {
 | 
			
		||||
            dsound_logerr (hr, "Could not get playback buffer status\n");
 | 
			
		||||
            return -1;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    if (*statusp & DSERR_BUFFERLOST) {
 | 
			
		||||
        dsound_restore_out(dsb, s);
 | 
			
		||||
        return -1;
 | 
			
		||||
        if (*statusp & DSERR_BUFFERLOST) {
 | 
			
		||||
            if (dsound_restore_out (dsb)) {
 | 
			
		||||
                return -1;
 | 
			
		||||
            }
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
@@ -343,8 +376,7 @@ static void dsound_write_sample (HWVoiceOut *hw, uint8_t *dst, int dst_len)
 | 
			
		||||
    hw->rpos = pos % hw->samples;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb,
 | 
			
		||||
                                 dsound *s)
 | 
			
		||||
static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb)
 | 
			
		||||
{
 | 
			
		||||
    int err;
 | 
			
		||||
    LPVOID p1, p2;
 | 
			
		||||
@@ -357,8 +389,7 @@ static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb,
 | 
			
		||||
        hw->samples << hw->info.shift,
 | 
			
		||||
        &p1, &p2,
 | 
			
		||||
        &blen1, &blen2,
 | 
			
		||||
        1,
 | 
			
		||||
        s
 | 
			
		||||
        1
 | 
			
		||||
        );
 | 
			
		||||
    if (err) {
 | 
			
		||||
        return;
 | 
			
		||||
@@ -384,9 +415,25 @@ static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb,
 | 
			
		||||
    dsound_unlock_out (dsb, p1, p2, blen1, blen2);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dsound_open (dsound *s)
 | 
			
		||||
static void dsound_close (dsound *s)
 | 
			
		||||
{
 | 
			
		||||
    HRESULT hr;
 | 
			
		||||
 | 
			
		||||
    if (s->dsound_primary_buffer) {
 | 
			
		||||
        hr = IDirectSoundBuffer_Release (s->dsound_primary_buffer);
 | 
			
		||||
        if (FAILED (hr)) {
 | 
			
		||||
            dsound_logerr (hr, "Could not release primary buffer\n");
 | 
			
		||||
        }
 | 
			
		||||
        s->dsound_primary_buffer = NULL;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dsound_open (dsound *s)
 | 
			
		||||
{
 | 
			
		||||
    int err;
 | 
			
		||||
    HRESULT hr;
 | 
			
		||||
    WAVEFORMATEX wfx;
 | 
			
		||||
    DSBUFFERDESC dsbd;
 | 
			
		||||
    HWND hwnd;
 | 
			
		||||
 | 
			
		||||
    hwnd = GetForegroundWindow ();
 | 
			
		||||
@@ -402,7 +449,63 @@ static int dsound_open (dsound *s)
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!conf.set_primary) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    err = waveformat_from_audio_settings (&wfx, &conf.settings);
 | 
			
		||||
    if (err) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    memset (&dsbd, 0, sizeof (dsbd));
 | 
			
		||||
    dsbd.dwSize = sizeof (dsbd);
 | 
			
		||||
    dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;
 | 
			
		||||
    dsbd.dwBufferBytes = 0;
 | 
			
		||||
    dsbd.lpwfxFormat = NULL;
 | 
			
		||||
 | 
			
		||||
    hr = IDirectSound_CreateSoundBuffer (
 | 
			
		||||
        s->dsound,
 | 
			
		||||
        &dsbd,
 | 
			
		||||
        &s->dsound_primary_buffer,
 | 
			
		||||
        NULL
 | 
			
		||||
        );
 | 
			
		||||
    if (FAILED (hr)) {
 | 
			
		||||
        dsound_logerr (hr, "Could not create primary playback buffer\n");
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    hr = IDirectSoundBuffer_SetFormat (s->dsound_primary_buffer, &wfx);
 | 
			
		||||
    if (FAILED (hr)) {
 | 
			
		||||
        dsound_logerr (hr, "Could not set primary playback buffer format\n");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    hr = IDirectSoundBuffer_GetFormat (
 | 
			
		||||
        s->dsound_primary_buffer,
 | 
			
		||||
        &wfx,
 | 
			
		||||
        sizeof (wfx),
 | 
			
		||||
        NULL
 | 
			
		||||
        );
 | 
			
		||||
    if (FAILED (hr)) {
 | 
			
		||||
        dsound_logerr (hr, "Could not get primary playback buffer format\n");
 | 
			
		||||
        goto fail0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
#ifdef DEBUG_DSOUND
 | 
			
		||||
    dolog ("Primary\n");
 | 
			
		||||
    print_wave_format (&wfx);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    err = waveformat_to_audio_settings (&wfx, &s->settings);
 | 
			
		||||
    if (err) {
 | 
			
		||||
        goto fail0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
 | 
			
		||||
 fail0:
 | 
			
		||||
    dsound_close (s);
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dsound_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
@@ -411,7 +514,6 @@ static int dsound_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
    DWORD status;
 | 
			
		||||
    DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
 | 
			
		||||
    LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
 | 
			
		||||
    dsound *s = ds->s;
 | 
			
		||||
 | 
			
		||||
    if (!dsb) {
 | 
			
		||||
        dolog ("Attempt to control voice without a buffer\n");
 | 
			
		||||
@@ -420,7 +522,7 @@ static int dsound_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
 | 
			
		||||
    switch (cmd) {
 | 
			
		||||
    case VOICE_ENABLE:
 | 
			
		||||
        if (dsound_get_status_out (dsb, &status, s)) {
 | 
			
		||||
        if (dsound_get_status_out (dsb, &status)) {
 | 
			
		||||
            return -1;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -429,7 +531,7 @@ static int dsound_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        dsound_clear_sample (hw, dsb, s);
 | 
			
		||||
        dsound_clear_sample (hw, dsb);
 | 
			
		||||
 | 
			
		||||
        hr = IDirectSoundBuffer_Play (dsb, 0, 0, DSBPLAY_LOOPING);
 | 
			
		||||
        if (FAILED (hr)) {
 | 
			
		||||
@@ -439,7 +541,7 @@ static int dsound_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case VOICE_DISABLE:
 | 
			
		||||
        if (dsound_get_status_out (dsb, &status, s)) {
 | 
			
		||||
        if (dsound_get_status_out (dsb, &status)) {
 | 
			
		||||
            return -1;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -476,8 +578,6 @@ static int dsound_run_out (HWVoiceOut *hw, int live)
 | 
			
		||||
    DWORD wpos, ppos, old_pos;
 | 
			
		||||
    LPVOID p1, p2;
 | 
			
		||||
    int bufsize;
 | 
			
		||||
    dsound *s = ds->s;
 | 
			
		||||
    DSoundConf *conf = &s->conf;
 | 
			
		||||
 | 
			
		||||
    if (!dsb) {
 | 
			
		||||
        dolog ("Attempt to run empty with playback buffer\n");
 | 
			
		||||
@@ -500,14 +600,14 @@ static int dsound_run_out (HWVoiceOut *hw, int live)
 | 
			
		||||
    len = live << hwshift;
 | 
			
		||||
 | 
			
		||||
    if (ds->first_time) {
 | 
			
		||||
        if (conf->latency_millis) {
 | 
			
		||||
        if (conf.latency_millis) {
 | 
			
		||||
            DWORD cur_blat;
 | 
			
		||||
 | 
			
		||||
            cur_blat = audio_ring_dist (wpos, ppos, bufsize);
 | 
			
		||||
            ds->first_time = 0;
 | 
			
		||||
            old_pos = wpos;
 | 
			
		||||
            old_pos +=
 | 
			
		||||
                millis_to_bytes (&hw->info, conf->latency_millis) - cur_blat;
 | 
			
		||||
                millis_to_bytes (&hw->info, conf.latency_millis) - cur_blat;
 | 
			
		||||
            old_pos %= bufsize;
 | 
			
		||||
            old_pos &= ~hw->info.align;
 | 
			
		||||
        }
 | 
			
		||||
@@ -563,8 +663,7 @@ static int dsound_run_out (HWVoiceOut *hw, int live)
 | 
			
		||||
        len,
 | 
			
		||||
        &p1, &p2,
 | 
			
		||||
        &blen1, &blen2,
 | 
			
		||||
        0,
 | 
			
		||||
        s
 | 
			
		||||
        0
 | 
			
		||||
        );
 | 
			
		||||
    if (err) {
 | 
			
		||||
        return 0;
 | 
			
		||||
@@ -667,7 +766,6 @@ static int dsound_run_in (HWVoiceIn *hw)
 | 
			
		||||
    DWORD cpos, rpos;
 | 
			
		||||
    LPVOID p1, p2;
 | 
			
		||||
    int hwshift;
 | 
			
		||||
    dsound *s = ds->s;
 | 
			
		||||
 | 
			
		||||
    if (!dscb) {
 | 
			
		||||
        dolog ("Attempt to run without capture buffer\n");
 | 
			
		||||
@@ -722,8 +820,7 @@ static int dsound_run_in (HWVoiceIn *hw)
 | 
			
		||||
        &p2,
 | 
			
		||||
        &blen1,
 | 
			
		||||
        &blen2,
 | 
			
		||||
        0,
 | 
			
		||||
        s
 | 
			
		||||
        0
 | 
			
		||||
        );
 | 
			
		||||
    if (err) {
 | 
			
		||||
        return 0;
 | 
			
		||||
@@ -746,19 +843,12 @@ static int dsound_run_in (HWVoiceIn *hw)
 | 
			
		||||
    return decr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static DSoundConf glob_conf = {
 | 
			
		||||
    .bufsize_in         = 16384,
 | 
			
		||||
    .bufsize_out        = 16384,
 | 
			
		||||
    .latency_millis     = 10
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void dsound_audio_fini (void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    HRESULT hr;
 | 
			
		||||
    dsound *s = opaque;
 | 
			
		||||
 | 
			
		||||
    if (!s->dsound) {
 | 
			
		||||
        g_free(s);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -769,7 +859,6 @@ static void dsound_audio_fini (void *opaque)
 | 
			
		||||
    s->dsound = NULL;
 | 
			
		||||
 | 
			
		||||
    if (!s->dsound_capture) {
 | 
			
		||||
        g_free(s);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -778,21 +867,17 @@ static void dsound_audio_fini (void *opaque)
 | 
			
		||||
        dsound_logerr (hr, "Could not release DirectSoundCapture\n");
 | 
			
		||||
    }
 | 
			
		||||
    s->dsound_capture = NULL;
 | 
			
		||||
 | 
			
		||||
    g_free(s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void *dsound_audio_init (void)
 | 
			
		||||
{
 | 
			
		||||
    int err;
 | 
			
		||||
    HRESULT hr;
 | 
			
		||||
    dsound *s = g_malloc0(sizeof(dsound));
 | 
			
		||||
    dsound *s = &glob_dsound;
 | 
			
		||||
 | 
			
		||||
    s->conf = glob_conf;
 | 
			
		||||
    hr = CoInitialize (NULL);
 | 
			
		||||
    if (FAILED (hr)) {
 | 
			
		||||
        dsound_logerr (hr, "Could not initialize COM\n");
 | 
			
		||||
        g_free(s);
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -805,7 +890,6 @@ static void *dsound_audio_init (void)
 | 
			
		||||
        );
 | 
			
		||||
    if (FAILED (hr)) {
 | 
			
		||||
        dsound_logerr (hr, "Could not create DirectSound instance\n");
 | 
			
		||||
        g_free(s);
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -817,7 +901,7 @@ static void *dsound_audio_init (void)
 | 
			
		||||
        if (FAILED (hr)) {
 | 
			
		||||
            dsound_logerr (hr, "Could not release DirectSound\n");
 | 
			
		||||
        }
 | 
			
		||||
        g_free(s);
 | 
			
		||||
        s->dsound = NULL;
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -854,22 +938,64 @@ static void *dsound_audio_init (void)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct audio_option dsound_options[] = {
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "LOCK_RETRIES",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &conf.lock_retries,
 | 
			
		||||
        .descr = "Number of times to attempt locking the buffer"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "RESTOURE_RETRIES",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &conf.restore_retries,
 | 
			
		||||
        .descr = "Number of times to attempt restoring the buffer"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "GETSTATUS_RETRIES",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &conf.getstatus_retries,
 | 
			
		||||
        .descr = "Number of times to attempt getting status of the buffer"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "SET_PRIMARY",
 | 
			
		||||
        .tag   = AUD_OPT_BOOL,
 | 
			
		||||
        .valp  = &conf.set_primary,
 | 
			
		||||
        .descr = "Set the parameters of primary buffer"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "LATENCY_MILLIS",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &glob_conf.latency_millis,
 | 
			
		||||
        .valp  = &conf.latency_millis,
 | 
			
		||||
        .descr = "(undocumented)"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "PRIMARY_FREQ",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &conf.settings.freq,
 | 
			
		||||
        .descr = "Primary buffer frequency"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "PRIMARY_CHANNELS",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &conf.settings.nchannels,
 | 
			
		||||
        .descr = "Primary buffer number of channels (1 - mono, 2 - stereo)"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "PRIMARY_FMT",
 | 
			
		||||
        .tag   = AUD_OPT_FMT,
 | 
			
		||||
        .valp  = &conf.settings.fmt,
 | 
			
		||||
        .descr = "Primary buffer format"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "BUFSIZE_OUT",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &glob_conf.bufsize_out,
 | 
			
		||||
        .valp  = &conf.bufsize_out,
 | 
			
		||||
        .descr = "(undocumented)"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "BUFSIZE_IN",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &glob_conf.bufsize_in,
 | 
			
		||||
        .valp  = &conf.bufsize_in,
 | 
			
		||||
        .descr = "(undocumented)"
 | 
			
		||||
    },
 | 
			
		||||
    { /* End of list */ }
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										557
									
								
								audio/esdaudio.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										557
									
								
								audio/esdaudio.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,557 @@
 | 
			
		||||
/*
 | 
			
		||||
 * QEMU ESD audio driver
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2006 Frederick Reeve (brushed up by malc)
 | 
			
		||||
 *
 | 
			
		||||
 * 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.
 | 
			
		||||
 */
 | 
			
		||||
#include <esd.h>
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "audio.h"
 | 
			
		||||
 | 
			
		||||
#define AUDIO_CAP "esd"
 | 
			
		||||
#include "audio_int.h"
 | 
			
		||||
#include "audio_pt_int.h"
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    HWVoiceOut hw;
 | 
			
		||||
    int done;
 | 
			
		||||
    int live;
 | 
			
		||||
    int decr;
 | 
			
		||||
    int rpos;
 | 
			
		||||
    void *pcm_buf;
 | 
			
		||||
    int fd;
 | 
			
		||||
    struct audio_pt pt;
 | 
			
		||||
} ESDVoiceOut;
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    HWVoiceIn hw;
 | 
			
		||||
    int done;
 | 
			
		||||
    int dead;
 | 
			
		||||
    int incr;
 | 
			
		||||
    int wpos;
 | 
			
		||||
    void *pcm_buf;
 | 
			
		||||
    int fd;
 | 
			
		||||
    struct audio_pt pt;
 | 
			
		||||
} ESDVoiceIn;
 | 
			
		||||
 | 
			
		||||
static struct {
 | 
			
		||||
    int samples;
 | 
			
		||||
    int divisor;
 | 
			
		||||
    char *dac_host;
 | 
			
		||||
    char *adc_host;
 | 
			
		||||
} conf = {
 | 
			
		||||
    .samples = 1024,
 | 
			
		||||
    .divisor = 2,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void GCC_FMT_ATTR (2, 3) qesd_logerr (int err, const char *fmt, ...)
 | 
			
		||||
{
 | 
			
		||||
    va_list ap;
 | 
			
		||||
 | 
			
		||||
    va_start (ap, fmt);
 | 
			
		||||
    AUD_vlog (AUDIO_CAP, fmt, ap);
 | 
			
		||||
    va_end (ap);
 | 
			
		||||
 | 
			
		||||
    AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* playback */
 | 
			
		||||
static void *qesd_thread_out (void *arg)
 | 
			
		||||
{
 | 
			
		||||
    ESDVoiceOut *esd = arg;
 | 
			
		||||
    HWVoiceOut *hw = &esd->hw;
 | 
			
		||||
    int threshold;
 | 
			
		||||
 | 
			
		||||
    threshold = conf.divisor ? hw->samples / conf.divisor : 0;
 | 
			
		||||
 | 
			
		||||
    if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for (;;) {
 | 
			
		||||
        int decr, to_mix, rpos;
 | 
			
		||||
 | 
			
		||||
        for (;;) {
 | 
			
		||||
            if (esd->done) {
 | 
			
		||||
                goto exit;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (esd->live > threshold) {
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (audio_pt_wait (&esd->pt, AUDIO_FUNC)) {
 | 
			
		||||
                goto exit;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        decr = to_mix = esd->live;
 | 
			
		||||
        rpos = hw->rpos;
 | 
			
		||||
 | 
			
		||||
        if (audio_pt_unlock (&esd->pt, AUDIO_FUNC)) {
 | 
			
		||||
            return NULL;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        while (to_mix) {
 | 
			
		||||
            ssize_t written;
 | 
			
		||||
            int chunk = audio_MIN (to_mix, hw->samples - rpos);
 | 
			
		||||
            struct st_sample *src = hw->mix_buf + rpos;
 | 
			
		||||
 | 
			
		||||
            hw->clip (esd->pcm_buf, src, chunk);
 | 
			
		||||
 | 
			
		||||
        again:
 | 
			
		||||
            written = write (esd->fd, esd->pcm_buf, chunk << hw->info.shift);
 | 
			
		||||
            if (written == -1) {
 | 
			
		||||
                if (errno == EINTR || errno == EAGAIN) {
 | 
			
		||||
                    goto again;
 | 
			
		||||
                }
 | 
			
		||||
                qesd_logerr (errno, "write failed\n");
 | 
			
		||||
                return NULL;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (written != chunk << hw->info.shift) {
 | 
			
		||||
                int wsamples = written >> hw->info.shift;
 | 
			
		||||
                int wbytes = wsamples << hw->info.shift;
 | 
			
		||||
                if (wbytes != written) {
 | 
			
		||||
                    dolog ("warning: Misaligned write %d (requested %zd), "
 | 
			
		||||
                           "alignment %d\n",
 | 
			
		||||
                           wbytes, written, hw->info.align + 1);
 | 
			
		||||
                }
 | 
			
		||||
                to_mix -= wsamples;
 | 
			
		||||
                rpos = (rpos + wsamples) % hw->samples;
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            rpos = (rpos + chunk) % hw->samples;
 | 
			
		||||
            to_mix -= chunk;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
 | 
			
		||||
            return NULL;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        esd->rpos = rpos;
 | 
			
		||||
        esd->live -= decr;
 | 
			
		||||
        esd->decr += decr;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 exit:
 | 
			
		||||
    audio_pt_unlock (&esd->pt, AUDIO_FUNC);
 | 
			
		||||
    return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int qesd_run_out (HWVoiceOut *hw, int live)
 | 
			
		||||
{
 | 
			
		||||
    int decr;
 | 
			
		||||
    ESDVoiceOut *esd = (ESDVoiceOut *) hw;
 | 
			
		||||
 | 
			
		||||
    if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    decr = audio_MIN (live, esd->decr);
 | 
			
		||||
    esd->decr -= decr;
 | 
			
		||||
    esd->live = live - decr;
 | 
			
		||||
    hw->rpos = esd->rpos;
 | 
			
		||||
    if (esd->live > 0) {
 | 
			
		||||
        audio_pt_unlock_and_signal (&esd->pt, AUDIO_FUNC);
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
        audio_pt_unlock (&esd->pt, AUDIO_FUNC);
 | 
			
		||||
    }
 | 
			
		||||
    return decr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int qesd_write (SWVoiceOut *sw, void *buf, int len)
 | 
			
		||||
{
 | 
			
		||||
    return audio_pcm_sw_write (sw, buf, len);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int qesd_init_out (HWVoiceOut *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    ESDVoiceOut *esd = (ESDVoiceOut *) hw;
 | 
			
		||||
    struct audsettings obt_as = *as;
 | 
			
		||||
    int esdfmt = ESD_STREAM | ESD_PLAY;
 | 
			
		||||
 | 
			
		||||
    esdfmt |= (as->nchannels == 2) ? ESD_STEREO : ESD_MONO;
 | 
			
		||||
    switch (as->fmt) {
 | 
			
		||||
    case AUD_FMT_S8:
 | 
			
		||||
    case AUD_FMT_U8:
 | 
			
		||||
        esdfmt |= ESD_BITS8;
 | 
			
		||||
        obt_as.fmt = AUD_FMT_U8;
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case AUD_FMT_S32:
 | 
			
		||||
    case AUD_FMT_U32:
 | 
			
		||||
        dolog ("Will use 16 instead of 32 bit samples\n");
 | 
			
		||||
        /* fall through */
 | 
			
		||||
    case AUD_FMT_S16:
 | 
			
		||||
    case AUD_FMT_U16:
 | 
			
		||||
    deffmt:
 | 
			
		||||
        esdfmt |= ESD_BITS16;
 | 
			
		||||
        obt_as.fmt = AUD_FMT_S16;
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    default:
 | 
			
		||||
        dolog ("Internal logic error: Bad audio format %d\n", as->fmt);
 | 
			
		||||
        goto deffmt;
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
    obt_as.endianness = AUDIO_HOST_ENDIANNESS;
 | 
			
		||||
 | 
			
		||||
    audio_pcm_init_info (&hw->info, &obt_as);
 | 
			
		||||
 | 
			
		||||
    hw->samples = conf.samples;
 | 
			
		||||
    esd->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
 | 
			
		||||
    if (!esd->pcm_buf) {
 | 
			
		||||
        dolog ("Could not allocate buffer (%d bytes)\n",
 | 
			
		||||
               hw->samples << hw->info.shift);
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    esd->fd = esd_play_stream (esdfmt, as->freq, conf.dac_host, NULL);
 | 
			
		||||
    if (esd->fd < 0) {
 | 
			
		||||
        qesd_logerr (errno, "esd_play_stream failed\n");
 | 
			
		||||
        goto fail1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (audio_pt_init (&esd->pt, qesd_thread_out, esd, AUDIO_CAP, AUDIO_FUNC)) {
 | 
			
		||||
        goto fail2;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
 | 
			
		||||
 fail2:
 | 
			
		||||
    if (close (esd->fd)) {
 | 
			
		||||
        qesd_logerr (errno, "%s: close on esd socket(%d) failed\n",
 | 
			
		||||
                     AUDIO_FUNC, esd->fd);
 | 
			
		||||
    }
 | 
			
		||||
    esd->fd = -1;
 | 
			
		||||
 | 
			
		||||
 fail1:
 | 
			
		||||
    g_free (esd->pcm_buf);
 | 
			
		||||
    esd->pcm_buf = NULL;
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void qesd_fini_out (HWVoiceOut *hw)
 | 
			
		||||
{
 | 
			
		||||
    void *ret;
 | 
			
		||||
    ESDVoiceOut *esd = (ESDVoiceOut *) hw;
 | 
			
		||||
 | 
			
		||||
    audio_pt_lock (&esd->pt, AUDIO_FUNC);
 | 
			
		||||
    esd->done = 1;
 | 
			
		||||
    audio_pt_unlock_and_signal (&esd->pt, AUDIO_FUNC);
 | 
			
		||||
    audio_pt_join (&esd->pt, &ret, AUDIO_FUNC);
 | 
			
		||||
 | 
			
		||||
    if (esd->fd >= 0) {
 | 
			
		||||
        if (close (esd->fd)) {
 | 
			
		||||
            qesd_logerr (errno, "failed to close esd socket\n");
 | 
			
		||||
        }
 | 
			
		||||
        esd->fd = -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    audio_pt_fini (&esd->pt, AUDIO_FUNC);
 | 
			
		||||
 | 
			
		||||
    g_free (esd->pcm_buf);
 | 
			
		||||
    esd->pcm_buf = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int qesd_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
{
 | 
			
		||||
    (void) hw;
 | 
			
		||||
    (void) cmd;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* capture */
 | 
			
		||||
static void *qesd_thread_in (void *arg)
 | 
			
		||||
{
 | 
			
		||||
    ESDVoiceIn *esd = arg;
 | 
			
		||||
    HWVoiceIn *hw = &esd->hw;
 | 
			
		||||
    int threshold;
 | 
			
		||||
 | 
			
		||||
    threshold = conf.divisor ? hw->samples / conf.divisor : 0;
 | 
			
		||||
 | 
			
		||||
    if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for (;;) {
 | 
			
		||||
        int incr, to_grab, wpos;
 | 
			
		||||
 | 
			
		||||
        for (;;) {
 | 
			
		||||
            if (esd->done) {
 | 
			
		||||
                goto exit;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (esd->dead > threshold) {
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (audio_pt_wait (&esd->pt, AUDIO_FUNC)) {
 | 
			
		||||
                goto exit;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        incr = to_grab = esd->dead;
 | 
			
		||||
        wpos = hw->wpos;
 | 
			
		||||
 | 
			
		||||
        if (audio_pt_unlock (&esd->pt, AUDIO_FUNC)) {
 | 
			
		||||
            return NULL;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        while (to_grab) {
 | 
			
		||||
            ssize_t nread;
 | 
			
		||||
            int chunk = audio_MIN (to_grab, hw->samples - wpos);
 | 
			
		||||
            void *buf = advance (esd->pcm_buf, wpos);
 | 
			
		||||
 | 
			
		||||
        again:
 | 
			
		||||
            nread = read (esd->fd, buf, chunk << hw->info.shift);
 | 
			
		||||
            if (nread == -1) {
 | 
			
		||||
                if (errno == EINTR || errno == EAGAIN) {
 | 
			
		||||
                    goto again;
 | 
			
		||||
                }
 | 
			
		||||
                qesd_logerr (errno, "read failed\n");
 | 
			
		||||
                return NULL;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (nread != chunk << hw->info.shift) {
 | 
			
		||||
                int rsamples = nread >> hw->info.shift;
 | 
			
		||||
                int rbytes = rsamples << hw->info.shift;
 | 
			
		||||
                if (rbytes != nread) {
 | 
			
		||||
                    dolog ("warning: Misaligned write %d (requested %zd), "
 | 
			
		||||
                           "alignment %d\n",
 | 
			
		||||
                           rbytes, nread, hw->info.align + 1);
 | 
			
		||||
                }
 | 
			
		||||
                to_grab -= rsamples;
 | 
			
		||||
                wpos = (wpos + rsamples) % hw->samples;
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            hw->conv (hw->conv_buf + wpos, buf, nread >> hw->info.shift);
 | 
			
		||||
            wpos = (wpos + chunk) % hw->samples;
 | 
			
		||||
            to_grab -= chunk;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
 | 
			
		||||
            return NULL;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        esd->wpos = wpos;
 | 
			
		||||
        esd->dead -= incr;
 | 
			
		||||
        esd->incr += incr;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 exit:
 | 
			
		||||
    audio_pt_unlock (&esd->pt, AUDIO_FUNC);
 | 
			
		||||
    return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int qesd_run_in (HWVoiceIn *hw)
 | 
			
		||||
{
 | 
			
		||||
    int live, incr, dead;
 | 
			
		||||
    ESDVoiceIn *esd = (ESDVoiceIn *) hw;
 | 
			
		||||
 | 
			
		||||
    if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    live = audio_pcm_hw_get_live_in (hw);
 | 
			
		||||
    dead = hw->samples - live;
 | 
			
		||||
    incr = audio_MIN (dead, esd->incr);
 | 
			
		||||
    esd->incr -= incr;
 | 
			
		||||
    esd->dead = dead - incr;
 | 
			
		||||
    hw->wpos = esd->wpos;
 | 
			
		||||
    if (esd->dead > 0) {
 | 
			
		||||
        audio_pt_unlock_and_signal (&esd->pt, AUDIO_FUNC);
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
        audio_pt_unlock (&esd->pt, AUDIO_FUNC);
 | 
			
		||||
    }
 | 
			
		||||
    return incr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int qesd_read (SWVoiceIn *sw, void *buf, int len)
 | 
			
		||||
{
 | 
			
		||||
    return audio_pcm_sw_read (sw, buf, len);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int qesd_init_in (HWVoiceIn *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    ESDVoiceIn *esd = (ESDVoiceIn *) hw;
 | 
			
		||||
    struct audsettings obt_as = *as;
 | 
			
		||||
    int esdfmt = ESD_STREAM | ESD_RECORD;
 | 
			
		||||
 | 
			
		||||
    esdfmt |= (as->nchannels == 2) ? ESD_STEREO : ESD_MONO;
 | 
			
		||||
    switch (as->fmt) {
 | 
			
		||||
    case AUD_FMT_S8:
 | 
			
		||||
    case AUD_FMT_U8:
 | 
			
		||||
        esdfmt |= ESD_BITS8;
 | 
			
		||||
        obt_as.fmt = AUD_FMT_U8;
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case AUD_FMT_S16:
 | 
			
		||||
    case AUD_FMT_U16:
 | 
			
		||||
        esdfmt |= ESD_BITS16;
 | 
			
		||||
        obt_as.fmt = AUD_FMT_S16;
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case AUD_FMT_S32:
 | 
			
		||||
    case AUD_FMT_U32:
 | 
			
		||||
        dolog ("Will use 16 instead of 32 bit samples\n");
 | 
			
		||||
        esdfmt |= ESD_BITS16;
 | 
			
		||||
        obt_as.fmt = AUD_FMT_S16;
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
    obt_as.endianness = AUDIO_HOST_ENDIANNESS;
 | 
			
		||||
 | 
			
		||||
    audio_pcm_init_info (&hw->info, &obt_as);
 | 
			
		||||
 | 
			
		||||
    hw->samples = conf.samples;
 | 
			
		||||
    esd->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
 | 
			
		||||
    if (!esd->pcm_buf) {
 | 
			
		||||
        dolog ("Could not allocate buffer (%d bytes)\n",
 | 
			
		||||
               hw->samples << hw->info.shift);
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    esd->fd = esd_record_stream (esdfmt, as->freq, conf.adc_host, NULL);
 | 
			
		||||
    if (esd->fd < 0) {
 | 
			
		||||
        qesd_logerr (errno, "esd_record_stream failed\n");
 | 
			
		||||
        goto fail1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (audio_pt_init (&esd->pt, qesd_thread_in, esd, AUDIO_CAP, AUDIO_FUNC)) {
 | 
			
		||||
        goto fail2;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
 | 
			
		||||
 fail2:
 | 
			
		||||
    if (close (esd->fd)) {
 | 
			
		||||
        qesd_logerr (errno, "%s: close on esd socket(%d) failed\n",
 | 
			
		||||
                     AUDIO_FUNC, esd->fd);
 | 
			
		||||
    }
 | 
			
		||||
    esd->fd = -1;
 | 
			
		||||
 | 
			
		||||
 fail1:
 | 
			
		||||
    g_free (esd->pcm_buf);
 | 
			
		||||
    esd->pcm_buf = NULL;
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void qesd_fini_in (HWVoiceIn *hw)
 | 
			
		||||
{
 | 
			
		||||
    void *ret;
 | 
			
		||||
    ESDVoiceIn *esd = (ESDVoiceIn *) hw;
 | 
			
		||||
 | 
			
		||||
    audio_pt_lock (&esd->pt, AUDIO_FUNC);
 | 
			
		||||
    esd->done = 1;
 | 
			
		||||
    audio_pt_unlock_and_signal (&esd->pt, AUDIO_FUNC);
 | 
			
		||||
    audio_pt_join (&esd->pt, &ret, AUDIO_FUNC);
 | 
			
		||||
 | 
			
		||||
    if (esd->fd >= 0) {
 | 
			
		||||
        if (close (esd->fd)) {
 | 
			
		||||
            qesd_logerr (errno, "failed to close esd socket\n");
 | 
			
		||||
        }
 | 
			
		||||
        esd->fd = -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    audio_pt_fini (&esd->pt, AUDIO_FUNC);
 | 
			
		||||
 | 
			
		||||
    g_free (esd->pcm_buf);
 | 
			
		||||
    esd->pcm_buf = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int qesd_ctl_in (HWVoiceIn *hw, int cmd, ...)
 | 
			
		||||
{
 | 
			
		||||
    (void) hw;
 | 
			
		||||
    (void) cmd;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* common */
 | 
			
		||||
static void *qesd_audio_init (void)
 | 
			
		||||
{
 | 
			
		||||
    return &conf;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void qesd_audio_fini (void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    (void) opaque;
 | 
			
		||||
    ldebug ("esd_fini");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct audio_option qesd_options[] = {
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "SAMPLES",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &conf.samples,
 | 
			
		||||
        .descr = "buffer size in samples"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "DIVISOR",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &conf.divisor,
 | 
			
		||||
        .descr = "threshold divisor"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "DAC_HOST",
 | 
			
		||||
        .tag   = AUD_OPT_STR,
 | 
			
		||||
        .valp  = &conf.dac_host,
 | 
			
		||||
        .descr = "playback host"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "ADC_HOST",
 | 
			
		||||
        .tag   = AUD_OPT_STR,
 | 
			
		||||
        .valp  = &conf.adc_host,
 | 
			
		||||
        .descr = "capture host"
 | 
			
		||||
    },
 | 
			
		||||
    { /* End of list */ }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static struct audio_pcm_ops qesd_pcm_ops = {
 | 
			
		||||
    .init_out = qesd_init_out,
 | 
			
		||||
    .fini_out = qesd_fini_out,
 | 
			
		||||
    .run_out  = qesd_run_out,
 | 
			
		||||
    .write    = qesd_write,
 | 
			
		||||
    .ctl_out  = qesd_ctl_out,
 | 
			
		||||
 | 
			
		||||
    .init_in  = qesd_init_in,
 | 
			
		||||
    .fini_in  = qesd_fini_in,
 | 
			
		||||
    .run_in   = qesd_run_in,
 | 
			
		||||
    .read     = qesd_read,
 | 
			
		||||
    .ctl_in   = qesd_ctl_in,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct audio_driver esd_audio_driver = {
 | 
			
		||||
    .name           = "esd",
 | 
			
		||||
    .descr          = "http://en.wikipedia.org/wiki/Esound",
 | 
			
		||||
    .options        = qesd_options,
 | 
			
		||||
    .init           = qesd_audio_init,
 | 
			
		||||
    .fini           = qesd_audio_fini,
 | 
			
		||||
    .pcm_ops        = &qesd_pcm_ops,
 | 
			
		||||
    .can_be_default = 0,
 | 
			
		||||
    .max_voices_out = INT_MAX,
 | 
			
		||||
    .max_voices_in  = INT_MAX,
 | 
			
		||||
    .voice_size_out = sizeof (ESDVoiceOut),
 | 
			
		||||
    .voice_size_in  = sizeof (ESDVoiceIn)
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										685
									
								
								audio/fmodaudio.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										685
									
								
								audio/fmodaudio.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,685 @@
 | 
			
		||||
/*
 | 
			
		||||
 * QEMU FMOD audio driver
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2004-2005 Vassili Karpov (malc)
 | 
			
		||||
 *
 | 
			
		||||
 * 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.
 | 
			
		||||
 */
 | 
			
		||||
#include <fmod.h>
 | 
			
		||||
#include <fmod_errors.h>
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "audio.h"
 | 
			
		||||
 | 
			
		||||
#define AUDIO_CAP "fmod"
 | 
			
		||||
#include "audio_int.h"
 | 
			
		||||
 | 
			
		||||
typedef struct FMODVoiceOut {
 | 
			
		||||
    HWVoiceOut hw;
 | 
			
		||||
    unsigned int old_pos;
 | 
			
		||||
    FSOUND_SAMPLE *fmod_sample;
 | 
			
		||||
    int channel;
 | 
			
		||||
} FMODVoiceOut;
 | 
			
		||||
 | 
			
		||||
typedef struct FMODVoiceIn {
 | 
			
		||||
    HWVoiceIn hw;
 | 
			
		||||
    FSOUND_SAMPLE *fmod_sample;
 | 
			
		||||
} FMODVoiceIn;
 | 
			
		||||
 | 
			
		||||
static struct {
 | 
			
		||||
    const char *drvname;
 | 
			
		||||
    int nb_samples;
 | 
			
		||||
    int freq;
 | 
			
		||||
    int nb_channels;
 | 
			
		||||
    int bufsize;
 | 
			
		||||
    int broken_adc;
 | 
			
		||||
} conf = {
 | 
			
		||||
    .nb_samples  = 2048 * 2,
 | 
			
		||||
    .freq        = 44100,
 | 
			
		||||
    .nb_channels = 2,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void GCC_FMT_ATTR (1, 2) fmod_logerr (const char *fmt, ...)
 | 
			
		||||
{
 | 
			
		||||
    va_list ap;
 | 
			
		||||
 | 
			
		||||
    va_start (ap, fmt);
 | 
			
		||||
    AUD_vlog (AUDIO_CAP, fmt, ap);
 | 
			
		||||
    va_end (ap);
 | 
			
		||||
 | 
			
		||||
    AUD_log (AUDIO_CAP, "Reason: %s\n",
 | 
			
		||||
             FMOD_ErrorString (FSOUND_GetError ()));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void GCC_FMT_ATTR (2, 3) fmod_logerr2 (
 | 
			
		||||
    const char *typ,
 | 
			
		||||
    const char *fmt,
 | 
			
		||||
    ...
 | 
			
		||||
    )
 | 
			
		||||
{
 | 
			
		||||
    va_list ap;
 | 
			
		||||
 | 
			
		||||
    AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
 | 
			
		||||
 | 
			
		||||
    va_start (ap, fmt);
 | 
			
		||||
    AUD_vlog (AUDIO_CAP, fmt, ap);
 | 
			
		||||
    va_end (ap);
 | 
			
		||||
 | 
			
		||||
    AUD_log (AUDIO_CAP, "Reason: %s\n",
 | 
			
		||||
             FMOD_ErrorString (FSOUND_GetError ()));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int fmod_write (SWVoiceOut *sw, void *buf, int len)
 | 
			
		||||
{
 | 
			
		||||
    return audio_pcm_sw_write (sw, buf, len);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void fmod_clear_sample (FMODVoiceOut *fmd)
 | 
			
		||||
{
 | 
			
		||||
    HWVoiceOut *hw = &fmd->hw;
 | 
			
		||||
    int status;
 | 
			
		||||
    void *p1 = 0, *p2 = 0;
 | 
			
		||||
    unsigned int len1 = 0, len2 = 0;
 | 
			
		||||
 | 
			
		||||
    status = FSOUND_Sample_Lock (
 | 
			
		||||
        fmd->fmod_sample,
 | 
			
		||||
        0,
 | 
			
		||||
        hw->samples << hw->info.shift,
 | 
			
		||||
        &p1,
 | 
			
		||||
        &p2,
 | 
			
		||||
        &len1,
 | 
			
		||||
        &len2
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
    if (!status) {
 | 
			
		||||
        fmod_logerr ("Failed to lock sample\n");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if ((len1 & hw->info.align) || (len2 & hw->info.align)) {
 | 
			
		||||
        dolog ("Lock returned misaligned length %d, %d, alignment %d\n",
 | 
			
		||||
               len1, len2, hw->info.align + 1);
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if ((len1 + len2) - (hw->samples << hw->info.shift)) {
 | 
			
		||||
        dolog ("Lock returned incomplete length %d, %d\n",
 | 
			
		||||
               len1 + len2, hw->samples << hw->info.shift);
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    audio_pcm_info_clear_buf (&hw->info, p1, hw->samples);
 | 
			
		||||
 | 
			
		||||
 fail:
 | 
			
		||||
    status = FSOUND_Sample_Unlock (fmd->fmod_sample, p1, p2, len1, len2);
 | 
			
		||||
    if (!status) {
 | 
			
		||||
        fmod_logerr ("Failed to unlock sample\n");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void fmod_write_sample (HWVoiceOut *hw, uint8_t *dst, int dst_len)
 | 
			
		||||
{
 | 
			
		||||
    int src_len1 = dst_len;
 | 
			
		||||
    int src_len2 = 0;
 | 
			
		||||
    int pos = hw->rpos + dst_len;
 | 
			
		||||
    struct st_sample *src1 = hw->mix_buf + hw->rpos;
 | 
			
		||||
    struct st_sample *src2 = NULL;
 | 
			
		||||
 | 
			
		||||
    if (pos > hw->samples) {
 | 
			
		||||
        src_len1 = hw->samples - hw->rpos;
 | 
			
		||||
        src2 = hw->mix_buf;
 | 
			
		||||
        src_len2 = dst_len - src_len1;
 | 
			
		||||
        pos = src_len2;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (src_len1) {
 | 
			
		||||
        hw->clip (dst, src1, src_len1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (src_len2) {
 | 
			
		||||
        dst = advance (dst, src_len1 << hw->info.shift);
 | 
			
		||||
        hw->clip (dst, src2, src_len2);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    hw->rpos = pos % hw->samples;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int fmod_unlock_sample (FSOUND_SAMPLE *sample, void *p1, void *p2,
 | 
			
		||||
                               unsigned int blen1, unsigned int blen2)
 | 
			
		||||
{
 | 
			
		||||
    int status = FSOUND_Sample_Unlock (sample, p1, p2, blen1, blen2);
 | 
			
		||||
    if (!status) {
 | 
			
		||||
        fmod_logerr ("Failed to unlock sample\n");
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int fmod_lock_sample (
 | 
			
		||||
    FSOUND_SAMPLE *sample,
 | 
			
		||||
    struct audio_pcm_info *info,
 | 
			
		||||
    int pos,
 | 
			
		||||
    int len,
 | 
			
		||||
    void **p1,
 | 
			
		||||
    void **p2,
 | 
			
		||||
    unsigned int *blen1,
 | 
			
		||||
    unsigned int *blen2
 | 
			
		||||
    )
 | 
			
		||||
{
 | 
			
		||||
    int status;
 | 
			
		||||
 | 
			
		||||
    status = FSOUND_Sample_Lock (
 | 
			
		||||
        sample,
 | 
			
		||||
        pos << info->shift,
 | 
			
		||||
        len << info->shift,
 | 
			
		||||
        p1,
 | 
			
		||||
        p2,
 | 
			
		||||
        blen1,
 | 
			
		||||
        blen2
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
    if (!status) {
 | 
			
		||||
        fmod_logerr ("Failed to lock sample\n");
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if ((*blen1 & info->align) || (*blen2 & info->align)) {
 | 
			
		||||
        dolog ("Lock returned misaligned length %d, %d, alignment %d\n",
 | 
			
		||||
               *blen1, *blen2, info->align + 1);
 | 
			
		||||
 | 
			
		||||
        fmod_unlock_sample (sample, *p1, *p2, *blen1, *blen2);
 | 
			
		||||
 | 
			
		||||
        *p1 = NULL - 1;
 | 
			
		||||
        *p2 = NULL - 1;
 | 
			
		||||
        *blen1 = ~0U;
 | 
			
		||||
        *blen2 = ~0U;
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!*p1 && *blen1) {
 | 
			
		||||
        dolog ("warning: !p1 && blen1=%d\n", *blen1);
 | 
			
		||||
        *blen1 = 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!p2 && *blen2) {
 | 
			
		||||
        dolog ("warning: !p2 && blen2=%d\n", *blen2);
 | 
			
		||||
        *blen2 = 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int fmod_run_out (HWVoiceOut *hw, int live)
 | 
			
		||||
{
 | 
			
		||||
    FMODVoiceOut *fmd = (FMODVoiceOut *) hw;
 | 
			
		||||
    int decr;
 | 
			
		||||
    void *p1 = 0, *p2 = 0;
 | 
			
		||||
    unsigned int blen1 = 0, blen2 = 0;
 | 
			
		||||
    unsigned int len1 = 0, len2 = 0;
 | 
			
		||||
 | 
			
		||||
    if (!hw->pending_disable) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    decr = live;
 | 
			
		||||
 | 
			
		||||
    if (fmd->channel >= 0) {
 | 
			
		||||
        int len = decr;
 | 
			
		||||
        int old_pos = fmd->old_pos;
 | 
			
		||||
        int ppos = FSOUND_GetCurrentPosition (fmd->channel);
 | 
			
		||||
 | 
			
		||||
        if (ppos == old_pos || !ppos) {
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if ((old_pos < ppos) && ((old_pos + len) > ppos)) {
 | 
			
		||||
            len = ppos - old_pos;
 | 
			
		||||
        }
 | 
			
		||||
        else {
 | 
			
		||||
            if ((old_pos > ppos) && ((old_pos + len) > (ppos + hw->samples))) {
 | 
			
		||||
                len = hw->samples - old_pos + ppos;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        decr = len;
 | 
			
		||||
 | 
			
		||||
        if (audio_bug (AUDIO_FUNC, decr < 0)) {
 | 
			
		||||
            dolog ("decr=%d live=%d ppos=%d old_pos=%d len=%d\n",
 | 
			
		||||
                   decr, live, ppos, old_pos, len);
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    if (!decr) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (fmod_lock_sample (fmd->fmod_sample, &fmd->hw.info,
 | 
			
		||||
                          fmd->old_pos, decr,
 | 
			
		||||
                          &p1, &p2,
 | 
			
		||||
                          &blen1, &blen2)) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    len1 = blen1 >> hw->info.shift;
 | 
			
		||||
    len2 = blen2 >> hw->info.shift;
 | 
			
		||||
    ldebug ("%p %p %d %d %d %d\n", p1, p2, len1, len2, blen1, blen2);
 | 
			
		||||
    decr = len1 + len2;
 | 
			
		||||
 | 
			
		||||
    if (p1 && len1) {
 | 
			
		||||
        fmod_write_sample (hw, p1, len1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (p2 && len2) {
 | 
			
		||||
        fmod_write_sample (hw, p2, len2);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fmod_unlock_sample (fmd->fmod_sample, p1, p2, blen1, blen2);
 | 
			
		||||
 | 
			
		||||
    fmd->old_pos = (fmd->old_pos + decr) % hw->samples;
 | 
			
		||||
    return decr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int aud_to_fmodfmt (audfmt_e fmt, int stereo)
 | 
			
		||||
{
 | 
			
		||||
    int mode = FSOUND_LOOP_NORMAL;
 | 
			
		||||
 | 
			
		||||
    switch (fmt) {
 | 
			
		||||
    case AUD_FMT_S8:
 | 
			
		||||
        mode |= FSOUND_SIGNED | FSOUND_8BITS;
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case AUD_FMT_U8:
 | 
			
		||||
        mode |= FSOUND_UNSIGNED | FSOUND_8BITS;
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case AUD_FMT_S16:
 | 
			
		||||
        mode |= FSOUND_SIGNED | FSOUND_16BITS;
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case AUD_FMT_U16:
 | 
			
		||||
        mode |= FSOUND_UNSIGNED | FSOUND_16BITS;
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    default:
 | 
			
		||||
        dolog ("Internal logic error: Bad audio format %d\n", fmt);
 | 
			
		||||
#ifdef DEBUG_FMOD
 | 
			
		||||
        abort ();
 | 
			
		||||
#endif
 | 
			
		||||
        mode |= FSOUND_8BITS;
 | 
			
		||||
    }
 | 
			
		||||
    mode |= stereo ? FSOUND_STEREO : FSOUND_MONO;
 | 
			
		||||
    return mode;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void fmod_fini_out (HWVoiceOut *hw)
 | 
			
		||||
{
 | 
			
		||||
    FMODVoiceOut *fmd = (FMODVoiceOut *) hw;
 | 
			
		||||
 | 
			
		||||
    if (fmd->fmod_sample) {
 | 
			
		||||
        FSOUND_Sample_Free (fmd->fmod_sample);
 | 
			
		||||
        fmd->fmod_sample = 0;
 | 
			
		||||
 | 
			
		||||
        if (fmd->channel >= 0) {
 | 
			
		||||
            FSOUND_StopSound (fmd->channel);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int fmod_init_out (HWVoiceOut *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    int mode, channel;
 | 
			
		||||
    FMODVoiceOut *fmd = (FMODVoiceOut *) hw;
 | 
			
		||||
    struct audsettings obt_as = *as;
 | 
			
		||||
 | 
			
		||||
    mode = aud_to_fmodfmt (as->fmt, as->nchannels == 2 ? 1 : 0);
 | 
			
		||||
    fmd->fmod_sample = FSOUND_Sample_Alloc (
 | 
			
		||||
        FSOUND_FREE,            /* index */
 | 
			
		||||
        conf.nb_samples,        /* length */
 | 
			
		||||
        mode,                   /* mode */
 | 
			
		||||
        as->freq,               /* freq */
 | 
			
		||||
        255,                    /* volume */
 | 
			
		||||
        128,                    /* pan */
 | 
			
		||||
        255                     /* priority */
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
    if (!fmd->fmod_sample) {
 | 
			
		||||
        fmod_logerr2 ("DAC", "Failed to allocate FMOD sample\n");
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    channel = FSOUND_PlaySoundEx (FSOUND_FREE, fmd->fmod_sample, 0, 1);
 | 
			
		||||
    if (channel < 0) {
 | 
			
		||||
        fmod_logerr2 ("DAC", "Failed to start playing sound\n");
 | 
			
		||||
        FSOUND_Sample_Free (fmd->fmod_sample);
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
    fmd->channel = channel;
 | 
			
		||||
 | 
			
		||||
    /* FMOD always operates on little endian frames? */
 | 
			
		||||
    obt_as.endianness = 0;
 | 
			
		||||
    audio_pcm_init_info (&hw->info, &obt_as);
 | 
			
		||||
    hw->samples = conf.nb_samples;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int fmod_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
{
 | 
			
		||||
    int status;
 | 
			
		||||
    FMODVoiceOut *fmd = (FMODVoiceOut *) hw;
 | 
			
		||||
 | 
			
		||||
    switch (cmd) {
 | 
			
		||||
    case VOICE_ENABLE:
 | 
			
		||||
        fmod_clear_sample (fmd);
 | 
			
		||||
        status = FSOUND_SetPaused (fmd->channel, 0);
 | 
			
		||||
        if (!status) {
 | 
			
		||||
            fmod_logerr ("Failed to resume channel %d\n", fmd->channel);
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case VOICE_DISABLE:
 | 
			
		||||
        status = FSOUND_SetPaused (fmd->channel, 1);
 | 
			
		||||
        if (!status) {
 | 
			
		||||
            fmod_logerr ("Failed to pause channel %d\n", fmd->channel);
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int fmod_init_in (HWVoiceIn *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    int mode;
 | 
			
		||||
    FMODVoiceIn *fmd = (FMODVoiceIn *) hw;
 | 
			
		||||
    struct audsettings obt_as = *as;
 | 
			
		||||
 | 
			
		||||
    if (conf.broken_adc) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    mode = aud_to_fmodfmt (as->fmt, as->nchannels == 2 ? 1 : 0);
 | 
			
		||||
    fmd->fmod_sample = FSOUND_Sample_Alloc (
 | 
			
		||||
        FSOUND_FREE,            /* index */
 | 
			
		||||
        conf.nb_samples,        /* length */
 | 
			
		||||
        mode,                   /* mode */
 | 
			
		||||
        as->freq,               /* freq */
 | 
			
		||||
        255,                    /* volume */
 | 
			
		||||
        128,                    /* pan */
 | 
			
		||||
        255                     /* priority */
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
    if (!fmd->fmod_sample) {
 | 
			
		||||
        fmod_logerr2 ("ADC", "Failed to allocate FMOD sample\n");
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* FMOD always operates on little endian frames? */
 | 
			
		||||
    obt_as.endianness = 0;
 | 
			
		||||
    audio_pcm_init_info (&hw->info, &obt_as);
 | 
			
		||||
    hw->samples = conf.nb_samples;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void fmod_fini_in (HWVoiceIn *hw)
 | 
			
		||||
{
 | 
			
		||||
    FMODVoiceIn *fmd = (FMODVoiceIn *) hw;
 | 
			
		||||
 | 
			
		||||
    if (fmd->fmod_sample) {
 | 
			
		||||
        FSOUND_Record_Stop ();
 | 
			
		||||
        FSOUND_Sample_Free (fmd->fmod_sample);
 | 
			
		||||
        fmd->fmod_sample = 0;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int fmod_run_in (HWVoiceIn *hw)
 | 
			
		||||
{
 | 
			
		||||
    FMODVoiceIn *fmd = (FMODVoiceIn *) hw;
 | 
			
		||||
    int hwshift = hw->info.shift;
 | 
			
		||||
    int live, dead, new_pos, len;
 | 
			
		||||
    unsigned int blen1 = 0, blen2 = 0;
 | 
			
		||||
    unsigned int len1, len2;
 | 
			
		||||
    unsigned int decr;
 | 
			
		||||
    void *p1, *p2;
 | 
			
		||||
 | 
			
		||||
    live = audio_pcm_hw_get_live_in (hw);
 | 
			
		||||
    dead = hw->samples - live;
 | 
			
		||||
    if (!dead) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    new_pos = FSOUND_Record_GetPosition ();
 | 
			
		||||
    if (new_pos < 0) {
 | 
			
		||||
        fmod_logerr ("Could not get recording position\n");
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    len = audio_ring_dist (new_pos,  hw->wpos, hw->samples);
 | 
			
		||||
    if (!len) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
    len = audio_MIN (len, dead);
 | 
			
		||||
 | 
			
		||||
    if (fmod_lock_sample (fmd->fmod_sample, &fmd->hw.info,
 | 
			
		||||
                          hw->wpos, len,
 | 
			
		||||
                          &p1, &p2,
 | 
			
		||||
                          &blen1, &blen2)) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    len1 = blen1 >> hwshift;
 | 
			
		||||
    len2 = blen2 >> hwshift;
 | 
			
		||||
    decr = len1 + len2;
 | 
			
		||||
 | 
			
		||||
    if (p1 && blen1) {
 | 
			
		||||
        hw->conv (hw->conv_buf + hw->wpos, p1, len1);
 | 
			
		||||
    }
 | 
			
		||||
    if (p2 && len2) {
 | 
			
		||||
        hw->conv (hw->conv_buf, p2, len2);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fmod_unlock_sample (fmd->fmod_sample, p1, p2, blen1, blen2);
 | 
			
		||||
    hw->wpos = (hw->wpos + decr) % hw->samples;
 | 
			
		||||
    return decr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct {
 | 
			
		||||
    const char *name;
 | 
			
		||||
    int type;
 | 
			
		||||
} drvtab[] = {
 | 
			
		||||
    { .name = "none",   .type = FSOUND_OUTPUT_NOSOUND },
 | 
			
		||||
#ifdef _WIN32
 | 
			
		||||
    { .name = "winmm",  .type = FSOUND_OUTPUT_WINMM   },
 | 
			
		||||
    { .name = "dsound", .type = FSOUND_OUTPUT_DSOUND  },
 | 
			
		||||
    { .name = "a3d",    .type = FSOUND_OUTPUT_A3D     },
 | 
			
		||||
    { .name = "asio",   .type = FSOUND_OUTPUT_ASIO    },
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef __linux__
 | 
			
		||||
    { .name = "oss",    .type = FSOUND_OUTPUT_OSS     },
 | 
			
		||||
    { .name = "alsa",   .type = FSOUND_OUTPUT_ALSA    },
 | 
			
		||||
    { .name = "esd",    .type = FSOUND_OUTPUT_ESD     },
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef __APPLE__
 | 
			
		||||
    { .name = "mac",    .type = FSOUND_OUTPUT_MAC     },
 | 
			
		||||
#endif
 | 
			
		||||
#if 0
 | 
			
		||||
    { .name = "xbox",   .type = FSOUND_OUTPUT_XBOX    },
 | 
			
		||||
    { .name = "ps2",    .type = FSOUND_OUTPUT_PS2     },
 | 
			
		||||
    { .name = "gcube",  .type = FSOUND_OUTPUT_GC      },
 | 
			
		||||
#endif
 | 
			
		||||
    { .name = "none-realtime", .type = FSOUND_OUTPUT_NOSOUND_NONREALTIME }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void *fmod_audio_init (void)
 | 
			
		||||
{
 | 
			
		||||
    size_t i;
 | 
			
		||||
    double ver;
 | 
			
		||||
    int status;
 | 
			
		||||
    int output_type = -1;
 | 
			
		||||
    const char *drv = conf.drvname;
 | 
			
		||||
 | 
			
		||||
    ver = FSOUND_GetVersion ();
 | 
			
		||||
    if (ver < FMOD_VERSION) {
 | 
			
		||||
        dolog ("Wrong FMOD version %f, need at least %f\n", ver, FMOD_VERSION);
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
#ifdef __linux__
 | 
			
		||||
    if (ver < 3.75) {
 | 
			
		||||
        dolog ("FMOD before 3.75 has bug preventing ADC from working\n"
 | 
			
		||||
               "ADC will be disabled.\n");
 | 
			
		||||
        conf.broken_adc = 1;
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    if (drv) {
 | 
			
		||||
        int found = 0;
 | 
			
		||||
        for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
 | 
			
		||||
            if (!strcmp (drv, drvtab[i].name)) {
 | 
			
		||||
                output_type = drvtab[i].type;
 | 
			
		||||
                found = 1;
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        if (!found) {
 | 
			
		||||
            dolog ("Unknown FMOD driver `%s'\n", drv);
 | 
			
		||||
            dolog ("Valid drivers:\n");
 | 
			
		||||
            for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
 | 
			
		||||
                dolog ("  %s\n", drvtab[i].name);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (output_type != -1) {
 | 
			
		||||
        status = FSOUND_SetOutput (output_type);
 | 
			
		||||
        if (!status) {
 | 
			
		||||
            fmod_logerr ("FSOUND_SetOutput(%d) failed\n", output_type);
 | 
			
		||||
            return NULL;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (conf.bufsize) {
 | 
			
		||||
        status = FSOUND_SetBufferSize (conf.bufsize);
 | 
			
		||||
        if (!status) {
 | 
			
		||||
            fmod_logerr ("FSOUND_SetBufferSize (%d) failed\n", conf.bufsize);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    status = FSOUND_Init (conf.freq, conf.nb_channels, 0);
 | 
			
		||||
    if (!status) {
 | 
			
		||||
        fmod_logerr ("FSOUND_Init failed\n");
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return &conf;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int fmod_read (SWVoiceIn *sw, void *buf, int size)
 | 
			
		||||
{
 | 
			
		||||
    return audio_pcm_sw_read (sw, buf, size);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int fmod_ctl_in (HWVoiceIn *hw, int cmd, ...)
 | 
			
		||||
{
 | 
			
		||||
    int status;
 | 
			
		||||
    FMODVoiceIn *fmd = (FMODVoiceIn *) hw;
 | 
			
		||||
 | 
			
		||||
    switch (cmd) {
 | 
			
		||||
    case VOICE_ENABLE:
 | 
			
		||||
        status = FSOUND_Record_StartSample (fmd->fmod_sample, 1);
 | 
			
		||||
        if (!status) {
 | 
			
		||||
            fmod_logerr ("Failed to start recording\n");
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case VOICE_DISABLE:
 | 
			
		||||
        status = FSOUND_Record_Stop ();
 | 
			
		||||
        if (!status) {
 | 
			
		||||
            fmod_logerr ("Failed to stop recording\n");
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void fmod_audio_fini (void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    (void) opaque;
 | 
			
		||||
    FSOUND_Close ();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct audio_option fmod_options[] = {
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "DRV",
 | 
			
		||||
        .tag   = AUD_OPT_STR,
 | 
			
		||||
        .valp  = &conf.drvname,
 | 
			
		||||
        .descr = "FMOD driver"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "FREQ",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &conf.freq,
 | 
			
		||||
        .descr = "Default frequency"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "SAMPLES",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &conf.nb_samples,
 | 
			
		||||
        .descr = "Buffer size in samples"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "CHANNELS",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &conf.nb_channels,
 | 
			
		||||
        .descr = "Number of default channels (1 - mono, 2 - stereo)"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "BUFSIZE",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &conf.bufsize,
 | 
			
		||||
        .descr = "(undocumented)"
 | 
			
		||||
    },
 | 
			
		||||
    { /* End of list */ }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static struct audio_pcm_ops fmod_pcm_ops = {
 | 
			
		||||
    .init_out = fmod_init_out,
 | 
			
		||||
    .fini_out = fmod_fini_out,
 | 
			
		||||
    .run_out  = fmod_run_out,
 | 
			
		||||
    .write    = fmod_write,
 | 
			
		||||
    .ctl_out  = fmod_ctl_out,
 | 
			
		||||
 | 
			
		||||
    .init_in  = fmod_init_in,
 | 
			
		||||
    .fini_in  = fmod_fini_in,
 | 
			
		||||
    .run_in   = fmod_run_in,
 | 
			
		||||
    .read     = fmod_read,
 | 
			
		||||
    .ctl_in   = fmod_ctl_in
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct audio_driver fmod_audio_driver = {
 | 
			
		||||
    .name           = "fmod",
 | 
			
		||||
    .descr          = "FMOD 3.xx http://www.fmod.org",
 | 
			
		||||
    .options        = fmod_options,
 | 
			
		||||
    .init           = fmod_audio_init,
 | 
			
		||||
    .fini           = fmod_audio_fini,
 | 
			
		||||
    .pcm_ops        = &fmod_pcm_ops,
 | 
			
		||||
    .can_be_default = 1,
 | 
			
		||||
    .max_voices_out = INT_MAX,
 | 
			
		||||
    .max_voices_in  = INT_MAX,
 | 
			
		||||
    .voice_size_out = sizeof (FMODVoiceOut),
 | 
			
		||||
    .voice_size_in  = sizeof (FMODVoiceIn)
 | 
			
		||||
};
 | 
			
		||||
@@ -63,7 +63,7 @@ static int no_write (SWVoiceOut *sw, void *buf, int len)
 | 
			
		||||
    return audio_pcm_sw_write (sw, buf, len);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int no_init_out(HWVoiceOut *hw, struct audsettings *as, void *drv_opaque)
 | 
			
		||||
static int no_init_out (HWVoiceOut *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    audio_pcm_init_info (&hw->info, as);
 | 
			
		||||
    hw->samples = 1024;
 | 
			
		||||
@@ -82,7 +82,7 @@ static int no_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int no_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
 | 
			
		||||
static int no_init_in (HWVoiceIn *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    audio_pcm_init_info (&hw->info, as);
 | 
			
		||||
    hw->samples = 1024;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										140
									
								
								audio/ossaudio.c
									
									
									
									
									
								
							
							
						
						
									
										140
									
								
								audio/ossaudio.c
									
									
									
									
									
								
							@@ -30,7 +30,6 @@
 | 
			
		||||
#include "qemu/main-loop.h"
 | 
			
		||||
#include "qemu/host-utils.h"
 | 
			
		||||
#include "audio.h"
 | 
			
		||||
#include "trace.h"
 | 
			
		||||
 | 
			
		||||
#define AUDIO_CAP "oss"
 | 
			
		||||
#include "audio_int.h"
 | 
			
		||||
@@ -39,16 +38,6 @@
 | 
			
		||||
#define USE_DSP_POLICY
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
typedef struct OSSConf {
 | 
			
		||||
    int try_mmap;
 | 
			
		||||
    int nfrags;
 | 
			
		||||
    int fragsize;
 | 
			
		||||
    const char *devpath_out;
 | 
			
		||||
    const char *devpath_in;
 | 
			
		||||
    int exclusive;
 | 
			
		||||
    int policy;
 | 
			
		||||
} OSSConf;
 | 
			
		||||
 | 
			
		||||
typedef struct OSSVoiceOut {
 | 
			
		||||
    HWVoiceOut hw;
 | 
			
		||||
    void *pcm_buf;
 | 
			
		||||
@@ -58,7 +47,6 @@ typedef struct OSSVoiceOut {
 | 
			
		||||
    int fragsize;
 | 
			
		||||
    int mmapped;
 | 
			
		||||
    int pending;
 | 
			
		||||
    OSSConf *conf;
 | 
			
		||||
} OSSVoiceOut;
 | 
			
		||||
 | 
			
		||||
typedef struct OSSVoiceIn {
 | 
			
		||||
@@ -67,9 +55,28 @@ typedef struct OSSVoiceIn {
 | 
			
		||||
    int fd;
 | 
			
		||||
    int nfrags;
 | 
			
		||||
    int fragsize;
 | 
			
		||||
    OSSConf *conf;
 | 
			
		||||
} OSSVoiceIn;
 | 
			
		||||
 | 
			
		||||
static struct {
 | 
			
		||||
    int try_mmap;
 | 
			
		||||
    int nfrags;
 | 
			
		||||
    int fragsize;
 | 
			
		||||
    const char *devpath_out;
 | 
			
		||||
    const char *devpath_in;
 | 
			
		||||
    int debug;
 | 
			
		||||
    int exclusive;
 | 
			
		||||
    int policy;
 | 
			
		||||
} conf = {
 | 
			
		||||
    .try_mmap = 0,
 | 
			
		||||
    .nfrags = 4,
 | 
			
		||||
    .fragsize = 4096,
 | 
			
		||||
    .devpath_out = "/dev/dsp",
 | 
			
		||||
    .devpath_in = "/dev/dsp",
 | 
			
		||||
    .debug = 0,
 | 
			
		||||
    .exclusive = 0,
 | 
			
		||||
    .policy = 5
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct oss_params {
 | 
			
		||||
    int freq;
 | 
			
		||||
    audfmt_e fmt;
 | 
			
		||||
@@ -131,18 +138,18 @@ static void oss_helper_poll_in (void *opaque)
 | 
			
		||||
    audio_run ("oss_poll_in");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void oss_poll_out (HWVoiceOut *hw)
 | 
			
		||||
static int oss_poll_out (HWVoiceOut *hw)
 | 
			
		||||
{
 | 
			
		||||
    OSSVoiceOut *oss = (OSSVoiceOut *) hw;
 | 
			
		||||
 | 
			
		||||
    qemu_set_fd_handler (oss->fd, NULL, oss_helper_poll_out, NULL);
 | 
			
		||||
    return qemu_set_fd_handler (oss->fd, NULL, oss_helper_poll_out, NULL);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void oss_poll_in (HWVoiceIn *hw)
 | 
			
		||||
static int oss_poll_in (HWVoiceIn *hw)
 | 
			
		||||
{
 | 
			
		||||
    OSSVoiceIn *oss = (OSSVoiceIn *) hw;
 | 
			
		||||
 | 
			
		||||
    qemu_set_fd_handler (oss->fd, oss_helper_poll_in, NULL, NULL);
 | 
			
		||||
    return qemu_set_fd_handler (oss->fd, oss_helper_poll_in, NULL, NULL);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int oss_write (SWVoiceOut *sw, void *buf, int len)
 | 
			
		||||
@@ -265,18 +272,18 @@ static int oss_get_version (int fd, int *version, const char *typ)
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
static int oss_open (int in, struct oss_params *req,
 | 
			
		||||
                     struct oss_params *obt, int *pfd, OSSConf* conf)
 | 
			
		||||
                     struct oss_params *obt, int *pfd)
 | 
			
		||||
{
 | 
			
		||||
    int fd;
 | 
			
		||||
    int oflags = conf->exclusive ? O_EXCL : 0;
 | 
			
		||||
    int oflags = conf.exclusive ? O_EXCL : 0;
 | 
			
		||||
    audio_buf_info abinfo;
 | 
			
		||||
    int fmt, freq, nchannels;
 | 
			
		||||
    int setfragment = 1;
 | 
			
		||||
    const char *dspname = in ? conf->devpath_in : conf->devpath_out;
 | 
			
		||||
    const char *dspname = in ? conf.devpath_in : conf.devpath_out;
 | 
			
		||||
    const char *typ = in ? "ADC" : "DAC";
 | 
			
		||||
 | 
			
		||||
    /* Kludge needed to have working mmap on Linux */
 | 
			
		||||
    oflags |= conf->try_mmap ? O_RDWR : (in ? O_RDONLY : O_WRONLY);
 | 
			
		||||
    oflags |= conf.try_mmap ? O_RDWR : (in ? O_RDONLY : O_WRONLY);
 | 
			
		||||
 | 
			
		||||
    fd = open (dspname, oflags | O_NONBLOCK);
 | 
			
		||||
    if (-1 == fd) {
 | 
			
		||||
@@ -310,18 +317,20 @@ static int oss_open (int in, struct oss_params *req,
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
#ifdef USE_DSP_POLICY
 | 
			
		||||
    if (conf->policy >= 0) {
 | 
			
		||||
    if (conf.policy >= 0) {
 | 
			
		||||
        int version;
 | 
			
		||||
 | 
			
		||||
        if (!oss_get_version (fd, &version, typ)) {
 | 
			
		||||
            trace_oss_version(version);
 | 
			
		||||
            if (conf.debug) {
 | 
			
		||||
                dolog ("OSS version = %#x\n", version);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (version >= 0x040000) {
 | 
			
		||||
                int policy = conf->policy;
 | 
			
		||||
                int policy = conf.policy;
 | 
			
		||||
                if (ioctl (fd, SNDCTL_DSP_POLICY, &policy)) {
 | 
			
		||||
                    oss_logerr2 (errno, typ,
 | 
			
		||||
                                 "Failed to set timing policy to %d\n",
 | 
			
		||||
                                 conf->policy);
 | 
			
		||||
                                 conf.policy);
 | 
			
		||||
                    goto err;
 | 
			
		||||
                }
 | 
			
		||||
                setfragment = 0;
 | 
			
		||||
@@ -449,12 +458,19 @@ static int oss_run_out (HWVoiceOut *hw, int live)
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (abinfo.bytes > bufsize) {
 | 
			
		||||
            trace_oss_invalid_available_size(abinfo.bytes, bufsize);
 | 
			
		||||
            if (conf.debug) {
 | 
			
		||||
                dolog ("warning: Invalid available size, size=%d bufsize=%d\n"
 | 
			
		||||
                       "please report your OS/audio hw to av1474@comtv.ru\n",
 | 
			
		||||
                       abinfo.bytes, bufsize);
 | 
			
		||||
            }
 | 
			
		||||
            abinfo.bytes = bufsize;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (abinfo.bytes < 0) {
 | 
			
		||||
            trace_oss_invalid_available_size(abinfo.bytes, bufsize);
 | 
			
		||||
            if (conf.debug) {
 | 
			
		||||
                dolog ("warning: Invalid available size, size=%d bufsize=%d\n",
 | 
			
		||||
                       abinfo.bytes, bufsize);
 | 
			
		||||
            }
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -494,8 +510,7 @@ static void oss_fini_out (HWVoiceOut *hw)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int oss_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
                        void *drv_opaque)
 | 
			
		||||
static int oss_init_out (HWVoiceOut *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    OSSVoiceOut *oss = (OSSVoiceOut *) hw;
 | 
			
		||||
    struct oss_params req, obt;
 | 
			
		||||
@@ -504,17 +519,16 @@ static int oss_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
    int fd;
 | 
			
		||||
    audfmt_e effective_fmt;
 | 
			
		||||
    struct audsettings obt_as;
 | 
			
		||||
    OSSConf *conf = drv_opaque;
 | 
			
		||||
 | 
			
		||||
    oss->fd = -1;
 | 
			
		||||
 | 
			
		||||
    req.fmt = aud_to_ossfmt (as->fmt, as->endianness);
 | 
			
		||||
    req.freq = as->freq;
 | 
			
		||||
    req.nchannels = as->nchannels;
 | 
			
		||||
    req.fragsize = conf->fragsize;
 | 
			
		||||
    req.nfrags = conf->nfrags;
 | 
			
		||||
    req.fragsize = conf.fragsize;
 | 
			
		||||
    req.nfrags = conf.nfrags;
 | 
			
		||||
 | 
			
		||||
    if (oss_open (0, &req, &obt, &fd, conf)) {
 | 
			
		||||
    if (oss_open (0, &req, &obt, &fd)) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -541,7 +555,7 @@ static int oss_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
    hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift;
 | 
			
		||||
 | 
			
		||||
    oss->mmapped = 0;
 | 
			
		||||
    if (conf->try_mmap) {
 | 
			
		||||
    if (conf.try_mmap) {
 | 
			
		||||
        oss->pcm_buf = mmap (
 | 
			
		||||
            NULL,
 | 
			
		||||
            hw->samples << hw->info.shift,
 | 
			
		||||
@@ -601,7 +615,6 @@ static int oss_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    oss->fd = fd;
 | 
			
		||||
    oss->conf = conf;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -621,8 +634,7 @@ static int oss_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
            va_end (ap);
 | 
			
		||||
 | 
			
		||||
            ldebug ("enabling voice\n");
 | 
			
		||||
            if (poll_mode) {
 | 
			
		||||
                oss_poll_out (hw);
 | 
			
		||||
            if (poll_mode && oss_poll_out (hw)) {
 | 
			
		||||
                poll_mode = 0;
 | 
			
		||||
            }
 | 
			
		||||
            hw->poll_mode = poll_mode;
 | 
			
		||||
@@ -664,7 +676,7 @@ static int oss_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int oss_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
 | 
			
		||||
static int oss_init_in (HWVoiceIn *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    OSSVoiceIn *oss = (OSSVoiceIn *) hw;
 | 
			
		||||
    struct oss_params req, obt;
 | 
			
		||||
@@ -673,16 +685,15 @@ static int oss_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
 | 
			
		||||
    int fd;
 | 
			
		||||
    audfmt_e effective_fmt;
 | 
			
		||||
    struct audsettings obt_as;
 | 
			
		||||
    OSSConf *conf = drv_opaque;
 | 
			
		||||
 | 
			
		||||
    oss->fd = -1;
 | 
			
		||||
 | 
			
		||||
    req.fmt = aud_to_ossfmt (as->fmt, as->endianness);
 | 
			
		||||
    req.freq = as->freq;
 | 
			
		||||
    req.nchannels = as->nchannels;
 | 
			
		||||
    req.fragsize = conf->fragsize;
 | 
			
		||||
    req.nfrags = conf->nfrags;
 | 
			
		||||
    if (oss_open (1, &req, &obt, &fd, conf)) {
 | 
			
		||||
    req.fragsize = conf.fragsize;
 | 
			
		||||
    req.nfrags = conf.nfrags;
 | 
			
		||||
    if (oss_open (1, &req, &obt, &fd)) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -716,7 +727,6 @@ static int oss_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    oss->fd = fd;
 | 
			
		||||
    oss->conf = conf;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -818,8 +828,7 @@ static int oss_ctl_in (HWVoiceIn *hw, int cmd, ...)
 | 
			
		||||
            poll_mode = va_arg (ap, int);
 | 
			
		||||
            va_end (ap);
 | 
			
		||||
 | 
			
		||||
            if (poll_mode) {
 | 
			
		||||
                oss_poll_in (hw);
 | 
			
		||||
            if (poll_mode && oss_poll_in (hw)) {
 | 
			
		||||
                poll_mode = 0;
 | 
			
		||||
            }
 | 
			
		||||
            hw->poll_mode = poll_mode;
 | 
			
		||||
@@ -836,78 +845,71 @@ static int oss_ctl_in (HWVoiceIn *hw, int cmd, ...)
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static OSSConf glob_conf = {
 | 
			
		||||
    .try_mmap = 0,
 | 
			
		||||
    .nfrags = 4,
 | 
			
		||||
    .fragsize = 4096,
 | 
			
		||||
    .devpath_out = "/dev/dsp",
 | 
			
		||||
    .devpath_in = "/dev/dsp",
 | 
			
		||||
    .exclusive = 0,
 | 
			
		||||
    .policy = 5
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void *oss_audio_init (void)
 | 
			
		||||
{
 | 
			
		||||
    OSSConf *conf = g_malloc(sizeof(OSSConf));
 | 
			
		||||
    *conf = glob_conf;
 | 
			
		||||
 | 
			
		||||
    if (access(conf->devpath_in, R_OK | W_OK) < 0 ||
 | 
			
		||||
        access(conf->devpath_out, R_OK | W_OK) < 0) {
 | 
			
		||||
    if (access(conf.devpath_in, R_OK | W_OK) < 0 ||
 | 
			
		||||
        access(conf.devpath_out, R_OK | W_OK) < 0) {
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
    return conf;
 | 
			
		||||
    return &conf;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void oss_audio_fini (void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    g_free(opaque);
 | 
			
		||||
    (void) opaque;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct audio_option oss_options[] = {
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "FRAGSIZE",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &glob_conf.fragsize,
 | 
			
		||||
        .valp  = &conf.fragsize,
 | 
			
		||||
        .descr = "Fragment size in bytes"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "NFRAGS",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &glob_conf.nfrags,
 | 
			
		||||
        .valp  = &conf.nfrags,
 | 
			
		||||
        .descr = "Number of fragments"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "MMAP",
 | 
			
		||||
        .tag   = AUD_OPT_BOOL,
 | 
			
		||||
        .valp  = &glob_conf.try_mmap,
 | 
			
		||||
        .valp  = &conf.try_mmap,
 | 
			
		||||
        .descr = "Try using memory mapped access"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "DAC_DEV",
 | 
			
		||||
        .tag   = AUD_OPT_STR,
 | 
			
		||||
        .valp  = &glob_conf.devpath_out,
 | 
			
		||||
        .valp  = &conf.devpath_out,
 | 
			
		||||
        .descr = "Path to DAC device"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "ADC_DEV",
 | 
			
		||||
        .tag   = AUD_OPT_STR,
 | 
			
		||||
        .valp  = &glob_conf.devpath_in,
 | 
			
		||||
        .valp  = &conf.devpath_in,
 | 
			
		||||
        .descr = "Path to ADC device"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "EXCLUSIVE",
 | 
			
		||||
        .tag   = AUD_OPT_BOOL,
 | 
			
		||||
        .valp  = &glob_conf.exclusive,
 | 
			
		||||
        .valp  = &conf.exclusive,
 | 
			
		||||
        .descr = "Open device in exclusive mode (vmix wont work)"
 | 
			
		||||
    },
 | 
			
		||||
#ifdef USE_DSP_POLICY
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "POLICY",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &glob_conf.policy,
 | 
			
		||||
        .valp  = &conf.policy,
 | 
			
		||||
        .descr = "Set the timing policy of the device, -1 to use fragment mode",
 | 
			
		||||
    },
 | 
			
		||||
#endif
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "DEBUG",
 | 
			
		||||
        .tag   = AUD_OPT_BOOL,
 | 
			
		||||
        .valp  = &conf.debug,
 | 
			
		||||
        .descr = "Turn on some debugging messages"
 | 
			
		||||
    },
 | 
			
		||||
    { /* End of list */ }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										105
									
								
								audio/paaudio.c
									
									
									
									
									
								
							
							
						
						
									
										105
									
								
								audio/paaudio.c
									
									
									
									
									
								
							@@ -8,19 +8,6 @@
 | 
			
		||||
#include "audio_int.h"
 | 
			
		||||
#include "audio_pt_int.h"
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    int samples;
 | 
			
		||||
    char *server;
 | 
			
		||||
    char *sink;
 | 
			
		||||
    char *source;
 | 
			
		||||
} PAConf;
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    PAConf conf;
 | 
			
		||||
    pa_threaded_mainloop *mainloop;
 | 
			
		||||
    pa_context *context;
 | 
			
		||||
} paaudio;
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    HWVoiceOut hw;
 | 
			
		||||
    int done;
 | 
			
		||||
@@ -30,7 +17,6 @@ typedef struct {
 | 
			
		||||
    pa_stream *stream;
 | 
			
		||||
    void *pcm_buf;
 | 
			
		||||
    struct audio_pt pt;
 | 
			
		||||
    paaudio *g;
 | 
			
		||||
} PAVoiceOut;
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
@@ -44,10 +30,20 @@ typedef struct {
 | 
			
		||||
    struct audio_pt pt;
 | 
			
		||||
    const void *read_data;
 | 
			
		||||
    size_t read_index, read_length;
 | 
			
		||||
    paaudio *g;
 | 
			
		||||
} PAVoiceIn;
 | 
			
		||||
 | 
			
		||||
static void qpa_audio_fini(void *opaque);
 | 
			
		||||
typedef struct {
 | 
			
		||||
    int samples;
 | 
			
		||||
    char *server;
 | 
			
		||||
    char *sink;
 | 
			
		||||
    char *source;
 | 
			
		||||
    pa_threaded_mainloop *mainloop;
 | 
			
		||||
    pa_context *context;
 | 
			
		||||
} paaudio;
 | 
			
		||||
 | 
			
		||||
static paaudio glob_paaudio = {
 | 
			
		||||
    .samples = 4096,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void GCC_FMT_ATTR (2, 3) qpa_logerr (int err, const char *fmt, ...)
 | 
			
		||||
{
 | 
			
		||||
@@ -110,7 +106,7 @@ static inline int PA_STREAM_IS_GOOD(pa_stream_state_t x)
 | 
			
		||||
 | 
			
		||||
static int qpa_simple_read (PAVoiceIn *p, void *data, size_t length, int *rerror)
 | 
			
		||||
{
 | 
			
		||||
    paaudio *g = p->g;
 | 
			
		||||
    paaudio *g = &glob_paaudio;
 | 
			
		||||
 | 
			
		||||
    pa_threaded_mainloop_lock (g->mainloop);
 | 
			
		||||
 | 
			
		||||
@@ -164,7 +160,7 @@ unlock_and_fail:
 | 
			
		||||
 | 
			
		||||
static int qpa_simple_write (PAVoiceOut *p, const void *data, size_t length, int *rerror)
 | 
			
		||||
{
 | 
			
		||||
    paaudio *g = p->g;
 | 
			
		||||
    paaudio *g = &glob_paaudio;
 | 
			
		||||
 | 
			
		||||
    pa_threaded_mainloop_lock (g->mainloop);
 | 
			
		||||
 | 
			
		||||
@@ -226,7 +222,7 @@ static void *qpa_thread_out (void *arg)
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        decr = to_mix = audio_MIN (pa->live, pa->g->conf.samples >> 2);
 | 
			
		||||
        decr = to_mix = audio_MIN (pa->live, glob_paaudio.samples >> 2);
 | 
			
		||||
        rpos = pa->rpos;
 | 
			
		||||
 | 
			
		||||
        if (audio_pt_unlock (&pa->pt, AUDIO_FUNC)) {
 | 
			
		||||
@@ -318,7 +314,7 @@ static void *qpa_thread_in (void *arg)
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        incr = to_grab = audio_MIN (pa->dead, pa->g->conf.samples >> 2);
 | 
			
		||||
        incr = to_grab = audio_MIN (pa->dead, glob_paaudio.samples >> 2);
 | 
			
		||||
        wpos = pa->wpos;
 | 
			
		||||
 | 
			
		||||
        if (audio_pt_unlock (&pa->pt, AUDIO_FUNC)) {
 | 
			
		||||
@@ -434,7 +430,7 @@ static audfmt_e pa_to_audfmt (pa_sample_format_t fmt, int *endianness)
 | 
			
		||||
 | 
			
		||||
static void context_state_cb (pa_context *c, void *userdata)
 | 
			
		||||
{
 | 
			
		||||
    paaudio *g = userdata;
 | 
			
		||||
    paaudio *g = &glob_paaudio;
 | 
			
		||||
 | 
			
		||||
    switch (pa_context_get_state(c)) {
 | 
			
		||||
    case PA_CONTEXT_READY:
 | 
			
		||||
@@ -453,7 +449,7 @@ static void context_state_cb (pa_context *c, void *userdata)
 | 
			
		||||
 | 
			
		||||
static void stream_state_cb (pa_stream *s, void * userdata)
 | 
			
		||||
{
 | 
			
		||||
    paaudio *g = userdata;
 | 
			
		||||
    paaudio *g = &glob_paaudio;
 | 
			
		||||
 | 
			
		||||
    switch (pa_stream_get_state (s)) {
 | 
			
		||||
 | 
			
		||||
@@ -471,21 +467,23 @@ static void stream_state_cb (pa_stream *s, void * userdata)
 | 
			
		||||
 | 
			
		||||
static void stream_request_cb (pa_stream *s, size_t length, void *userdata)
 | 
			
		||||
{
 | 
			
		||||
    paaudio *g = userdata;
 | 
			
		||||
    paaudio *g = &glob_paaudio;
 | 
			
		||||
 | 
			
		||||
    pa_threaded_mainloop_signal (g->mainloop, 0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static pa_stream *qpa_simple_new (
 | 
			
		||||
        paaudio *g,
 | 
			
		||||
        const char *server,
 | 
			
		||||
        const char *name,
 | 
			
		||||
        pa_stream_direction_t dir,
 | 
			
		||||
        const char *dev,
 | 
			
		||||
        const char *stream_name,
 | 
			
		||||
        const pa_sample_spec *ss,
 | 
			
		||||
        const pa_channel_map *map,
 | 
			
		||||
        const pa_buffer_attr *attr,
 | 
			
		||||
        int *rerror)
 | 
			
		||||
{
 | 
			
		||||
    paaudio *g = &glob_paaudio;
 | 
			
		||||
    int r;
 | 
			
		||||
    pa_stream *stream;
 | 
			
		||||
 | 
			
		||||
@@ -536,15 +534,13 @@ fail:
 | 
			
		||||
    return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int qpa_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
                        void *drv_opaque)
 | 
			
		||||
static int qpa_init_out (HWVoiceOut *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    int error;
 | 
			
		||||
    pa_sample_spec ss;
 | 
			
		||||
    pa_buffer_attr ba;
 | 
			
		||||
    static pa_sample_spec ss;
 | 
			
		||||
    static pa_buffer_attr ba;
 | 
			
		||||
    struct audsettings obt_as = *as;
 | 
			
		||||
    PAVoiceOut *pa = (PAVoiceOut *) hw;
 | 
			
		||||
    paaudio *g = pa->g = drv_opaque;
 | 
			
		||||
 | 
			
		||||
    ss.format = audfmt_to_pa (as->fmt, as->endianness);
 | 
			
		||||
    ss.channels = as->nchannels;
 | 
			
		||||
@@ -562,10 +558,11 @@ static int qpa_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
    obt_as.fmt = pa_to_audfmt (ss.format, &obt_as.endianness);
 | 
			
		||||
 | 
			
		||||
    pa->stream = qpa_simple_new (
 | 
			
		||||
        g,
 | 
			
		||||
        glob_paaudio.server,
 | 
			
		||||
        "qemu",
 | 
			
		||||
        PA_STREAM_PLAYBACK,
 | 
			
		||||
        g->conf.sink,
 | 
			
		||||
        glob_paaudio.sink,
 | 
			
		||||
        "pcm.playback",
 | 
			
		||||
        &ss,
 | 
			
		||||
        NULL,                   /* channel map */
 | 
			
		||||
        &ba,                    /* buffering attributes */
 | 
			
		||||
@@ -577,7 +574,7 @@ static int qpa_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    audio_pcm_init_info (&hw->info, &obt_as);
 | 
			
		||||
    hw->samples = g->conf.samples;
 | 
			
		||||
    hw->samples = glob_paaudio.samples;
 | 
			
		||||
    pa->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
 | 
			
		||||
    pa->rpos = hw->rpos;
 | 
			
		||||
    if (!pa->pcm_buf) {
 | 
			
		||||
@@ -604,13 +601,12 @@ static int qpa_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int qpa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
 | 
			
		||||
static int qpa_init_in (HWVoiceIn *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    int error;
 | 
			
		||||
    pa_sample_spec ss;
 | 
			
		||||
    static pa_sample_spec ss;
 | 
			
		||||
    struct audsettings obt_as = *as;
 | 
			
		||||
    PAVoiceIn *pa = (PAVoiceIn *) hw;
 | 
			
		||||
    paaudio *g = pa->g = drv_opaque;
 | 
			
		||||
 | 
			
		||||
    ss.format = audfmt_to_pa (as->fmt, as->endianness);
 | 
			
		||||
    ss.channels = as->nchannels;
 | 
			
		||||
@@ -619,10 +615,11 @@ static int qpa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
 | 
			
		||||
    obt_as.fmt = pa_to_audfmt (ss.format, &obt_as.endianness);
 | 
			
		||||
 | 
			
		||||
    pa->stream = qpa_simple_new (
 | 
			
		||||
        g,
 | 
			
		||||
        glob_paaudio.server,
 | 
			
		||||
        "qemu",
 | 
			
		||||
        PA_STREAM_RECORD,
 | 
			
		||||
        g->conf.source,
 | 
			
		||||
        glob_paaudio.source,
 | 
			
		||||
        "pcm.capture",
 | 
			
		||||
        &ss,
 | 
			
		||||
        NULL,                   /* channel map */
 | 
			
		||||
        NULL,                   /* buffering attributes */
 | 
			
		||||
@@ -634,7 +631,7 @@ static int qpa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    audio_pcm_init_info (&hw->info, &obt_as);
 | 
			
		||||
    hw->samples = g->conf.samples;
 | 
			
		||||
    hw->samples = glob_paaudio.samples;
 | 
			
		||||
    pa->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
 | 
			
		||||
    pa->wpos = hw->wpos;
 | 
			
		||||
    if (!pa->pcm_buf) {
 | 
			
		||||
@@ -706,7 +703,7 @@ static int qpa_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
    PAVoiceOut *pa = (PAVoiceOut *) hw;
 | 
			
		||||
    pa_operation *op;
 | 
			
		||||
    pa_cvolume v;
 | 
			
		||||
    paaudio *g = pa->g;
 | 
			
		||||
    paaudio *g = &glob_paaudio;
 | 
			
		||||
 | 
			
		||||
#ifdef PA_CHECK_VERSION    /* macro is present in 0.9.16+ */
 | 
			
		||||
    pa_cvolume_init (&v);  /* function is present in 0.9.13+ */
 | 
			
		||||
@@ -758,7 +755,7 @@ static int qpa_ctl_in (HWVoiceIn *hw, int cmd, ...)
 | 
			
		||||
    PAVoiceIn *pa = (PAVoiceIn *) hw;
 | 
			
		||||
    pa_operation *op;
 | 
			
		||||
    pa_cvolume v;
 | 
			
		||||
    paaudio *g = pa->g;
 | 
			
		||||
    paaudio *g = &glob_paaudio;
 | 
			
		||||
 | 
			
		||||
#ifdef PA_CHECK_VERSION
 | 
			
		||||
    pa_cvolume_init (&v);
 | 
			
		||||
@@ -808,31 +805,23 @@ static int qpa_ctl_in (HWVoiceIn *hw, int cmd, ...)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* common */
 | 
			
		||||
static PAConf glob_conf = {
 | 
			
		||||
    .samples = 4096,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void *qpa_audio_init (void)
 | 
			
		||||
{
 | 
			
		||||
    paaudio *g = g_malloc(sizeof(paaudio));
 | 
			
		||||
    g->conf = glob_conf;
 | 
			
		||||
    g->mainloop = NULL;
 | 
			
		||||
    g->context = NULL;
 | 
			
		||||
    paaudio *g = &glob_paaudio;
 | 
			
		||||
 | 
			
		||||
    g->mainloop = pa_threaded_mainloop_new ();
 | 
			
		||||
    if (!g->mainloop) {
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    g->context = pa_context_new (pa_threaded_mainloop_get_api (g->mainloop),
 | 
			
		||||
                                 g->conf.server);
 | 
			
		||||
    g->context = pa_context_new (pa_threaded_mainloop_get_api (g->mainloop), glob_paaudio.server);
 | 
			
		||||
    if (!g->context) {
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pa_context_set_state_callback (g->context, context_state_cb, g);
 | 
			
		||||
 | 
			
		||||
    if (pa_context_connect (g->context, g->conf.server, 0, NULL) < 0) {
 | 
			
		||||
    if (pa_context_connect (g->context, glob_paaudio.server, 0, NULL) < 0) {
 | 
			
		||||
        qpa_logerr (pa_context_errno (g->context),
 | 
			
		||||
                    "pa_context_connect() failed\n");
 | 
			
		||||
        goto fail;
 | 
			
		||||
@@ -865,13 +854,12 @@ static void *qpa_audio_init (void)
 | 
			
		||||
 | 
			
		||||
    pa_threaded_mainloop_unlock (g->mainloop);
 | 
			
		||||
 | 
			
		||||
    return g;
 | 
			
		||||
    return &glob_paaudio;
 | 
			
		||||
 | 
			
		||||
unlock_and_fail:
 | 
			
		||||
    pa_threaded_mainloop_unlock (g->mainloop);
 | 
			
		||||
fail:
 | 
			
		||||
    AUD_log (AUDIO_CAP, "Failed to initialize PA context");
 | 
			
		||||
    qpa_audio_fini(g);
 | 
			
		||||
    return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -886,38 +874,39 @@ static void qpa_audio_fini (void *opaque)
 | 
			
		||||
    if (g->context) {
 | 
			
		||||
        pa_context_disconnect (g->context);
 | 
			
		||||
        pa_context_unref (g->context);
 | 
			
		||||
        g->context = NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (g->mainloop) {
 | 
			
		||||
        pa_threaded_mainloop_free (g->mainloop);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    g_free(g);
 | 
			
		||||
    g->mainloop = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct audio_option qpa_options[] = {
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "SAMPLES",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &glob_conf.samples,
 | 
			
		||||
        .valp  = &glob_paaudio.samples,
 | 
			
		||||
        .descr = "buffer size in samples"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "SERVER",
 | 
			
		||||
        .tag   = AUD_OPT_STR,
 | 
			
		||||
        .valp  = &glob_conf.server,
 | 
			
		||||
        .valp  = &glob_paaudio.server,
 | 
			
		||||
        .descr = "server address"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "SINK",
 | 
			
		||||
        .tag   = AUD_OPT_STR,
 | 
			
		||||
        .valp  = &glob_conf.sink,
 | 
			
		||||
        .valp  = &glob_paaudio.sink,
 | 
			
		||||
        .descr = "sink device name"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "SOURCE",
 | 
			
		||||
        .tag   = AUD_OPT_STR,
 | 
			
		||||
        .valp  = &glob_conf.source,
 | 
			
		||||
        .valp  = &glob_paaudio.source,
 | 
			
		||||
        .descr = "source device name"
 | 
			
		||||
    },
 | 
			
		||||
    { /* End of list */ }
 | 
			
		||||
 
 | 
			
		||||
@@ -55,7 +55,6 @@ static struct SDLAudioState {
 | 
			
		||||
    SDL_mutex *mutex;
 | 
			
		||||
    SDL_sem *sem;
 | 
			
		||||
    int initialized;
 | 
			
		||||
    bool driver_created;
 | 
			
		||||
} glob_sdl;
 | 
			
		||||
typedef struct SDLAudioState SDLAudioState;
 | 
			
		||||
 | 
			
		||||
@@ -333,8 +332,7 @@ static void sdl_fini_out (HWVoiceOut *hw)
 | 
			
		||||
    sdl_close (&glob_sdl);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int sdl_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
                        void *drv_opaque)
 | 
			
		||||
static int sdl_init_out (HWVoiceOut *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    SDLVoiceOut *sdl = (SDLVoiceOut *) hw;
 | 
			
		||||
    SDLAudioState *s = &glob_sdl;
 | 
			
		||||
@@ -394,10 +392,6 @@ static int sdl_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
static void *sdl_audio_init (void)
 | 
			
		||||
{
 | 
			
		||||
    SDLAudioState *s = &glob_sdl;
 | 
			
		||||
    if (s->driver_created) {
 | 
			
		||||
        sdl_logerr("Can't create multiple sdl backends\n");
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (SDL_InitSubSystem (SDL_INIT_AUDIO)) {
 | 
			
		||||
        sdl_logerr ("SDL failed to initialize audio subsystem\n");
 | 
			
		||||
@@ -419,7 +413,6 @@ static void *sdl_audio_init (void)
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    s->driver_created = true;
 | 
			
		||||
    return s;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -430,7 +423,6 @@ static void sdl_audio_fini (void *opaque)
 | 
			
		||||
    SDL_DestroySemaphore (s->sem);
 | 
			
		||||
    SDL_DestroyMutex (s->mutex);
 | 
			
		||||
    SDL_QuitSubSystem (SDL_INIT_AUDIO);
 | 
			
		||||
    s->driver_created = false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct audio_option sdl_options[] = {
 | 
			
		||||
 
 | 
			
		||||
@@ -18,7 +18,6 @@
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "hw/hw.h"
 | 
			
		||||
#include "qemu/error-report.h"
 | 
			
		||||
#include "qemu/timer.h"
 | 
			
		||||
#include "ui/qemu-spice.h"
 | 
			
		||||
 | 
			
		||||
@@ -116,8 +115,7 @@ static int rate_get_samples (struct audio_pcm_info *info, SpiceRateCtl *rate)
 | 
			
		||||
 | 
			
		||||
/* playback */
 | 
			
		||||
 | 
			
		||||
static int line_out_init(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
                         void *drv_opaque)
 | 
			
		||||
static int line_out_init (HWVoiceOut *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
 | 
			
		||||
    struct audsettings settings;
 | 
			
		||||
@@ -245,7 +243,7 @@ static int line_out_ctl (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
 | 
			
		||||
/* record */
 | 
			
		||||
 | 
			
		||||
static int line_in_init(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
 | 
			
		||||
static int line_in_init (HWVoiceIn *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
 | 
			
		||||
    struct audsettings settings;
 | 
			
		||||
 
 | 
			
		||||
@@ -36,10 +36,15 @@ typedef struct WAVVoiceOut {
 | 
			
		||||
    int total_samples;
 | 
			
		||||
} WAVVoiceOut;
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
static struct {
 | 
			
		||||
    struct audsettings settings;
 | 
			
		||||
    const char *wav_path;
 | 
			
		||||
} WAVConf;
 | 
			
		||||
} conf = {
 | 
			
		||||
    .settings.freq      = 44100,
 | 
			
		||||
    .settings.nchannels = 2,
 | 
			
		||||
    .settings.fmt       = AUD_FMT_S16,
 | 
			
		||||
    .wav_path           = "qemu.wav"
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static int wav_run_out (HWVoiceOut *hw, int live)
 | 
			
		||||
{
 | 
			
		||||
@@ -100,8 +105,7 @@ static void le_store (uint8_t *buf, uint32_t val, int len)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int wav_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
                        void *drv_opaque)
 | 
			
		||||
static int wav_init_out (HWVoiceOut *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    WAVVoiceOut *wav = (WAVVoiceOut *) hw;
 | 
			
		||||
    int bits16 = 0, stereo = 0;
 | 
			
		||||
@@ -111,8 +115,9 @@ static int wav_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
        0x02, 0x00, 0x44, 0xac, 0x00, 0x00, 0x10, 0xb1, 0x02, 0x00, 0x04,
 | 
			
		||||
        0x00, 0x10, 0x00, 0x64, 0x61, 0x74, 0x61, 0x00, 0x00, 0x00, 0x00
 | 
			
		||||
    };
 | 
			
		||||
    WAVConf *conf = drv_opaque;
 | 
			
		||||
    struct audsettings wav_as = conf->settings;
 | 
			
		||||
    struct audsettings wav_as = conf.settings;
 | 
			
		||||
 | 
			
		||||
    (void) as;
 | 
			
		||||
 | 
			
		||||
    stereo = wav_as.nchannels == 2;
 | 
			
		||||
    switch (wav_as.fmt) {
 | 
			
		||||
@@ -150,10 +155,10 @@ static int wav_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
    le_store (hdr + 28, hw->info.freq << (bits16 + stereo), 4);
 | 
			
		||||
    le_store (hdr + 32, 1 << (bits16 + stereo), 2);
 | 
			
		||||
 | 
			
		||||
    wav->f = fopen (conf->wav_path, "wb");
 | 
			
		||||
    wav->f = fopen (conf.wav_path, "wb");
 | 
			
		||||
    if (!wav->f) {
 | 
			
		||||
        dolog ("Failed to open wave file `%s'\nReason: %s\n",
 | 
			
		||||
               conf->wav_path, strerror (errno));
 | 
			
		||||
               conf.wav_path, strerror (errno));
 | 
			
		||||
        g_free (wav->pcm_buf);
 | 
			
		||||
        wav->pcm_buf = NULL;
 | 
			
		||||
        return -1;
 | 
			
		||||
@@ -221,49 +226,40 @@ static int wav_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static WAVConf glob_conf = {
 | 
			
		||||
    .settings.freq      = 44100,
 | 
			
		||||
    .settings.nchannels = 2,
 | 
			
		||||
    .settings.fmt       = AUD_FMT_S16,
 | 
			
		||||
    .wav_path           = "qemu.wav"
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void *wav_audio_init (void)
 | 
			
		||||
{
 | 
			
		||||
    WAVConf *conf = g_malloc(sizeof(WAVConf));
 | 
			
		||||
    *conf = glob_conf;
 | 
			
		||||
    return conf;
 | 
			
		||||
    return &conf;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void wav_audio_fini (void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    (void) opaque;
 | 
			
		||||
    ldebug ("wav_fini");
 | 
			
		||||
    g_free(opaque);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct audio_option wav_options[] = {
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "FREQUENCY",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &glob_conf.settings.freq,
 | 
			
		||||
        .valp  = &conf.settings.freq,
 | 
			
		||||
        .descr = "Frequency"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "FORMAT",
 | 
			
		||||
        .tag   = AUD_OPT_FMT,
 | 
			
		||||
        .valp  = &glob_conf.settings.fmt,
 | 
			
		||||
        .valp  = &conf.settings.fmt,
 | 
			
		||||
        .descr = "Format"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "DAC_FIXED_CHANNELS",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &glob_conf.settings.nchannels,
 | 
			
		||||
        .valp  = &conf.settings.nchannels,
 | 
			
		||||
        .descr = "Number of channels (1 - mono, 2 - stereo)"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "PATH",
 | 
			
		||||
        .tag   = AUD_OPT_STR,
 | 
			
		||||
        .valp  = &glob_conf.wav_path,
 | 
			
		||||
        .valp  = &conf.wav_path,
 | 
			
		||||
        .descr = "Path to wave file"
 | 
			
		||||
    },
 | 
			
		||||
    { /* End of list */ }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,5 @@
 | 
			
		||||
#include "hw/hw.h"
 | 
			
		||||
#include "monitor/monitor.h"
 | 
			
		||||
#include "qemu/error-report.h"
 | 
			
		||||
#include "audio.h"
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										717
									
								
								audio/winwaveaudio.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										717
									
								
								audio/winwaveaudio.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,717 @@
 | 
			
		||||
/* public domain */
 | 
			
		||||
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "sysemu/sysemu.h"
 | 
			
		||||
#include "audio.h"
 | 
			
		||||
 | 
			
		||||
#define AUDIO_CAP "winwave"
 | 
			
		||||
#include "audio_int.h"
 | 
			
		||||
 | 
			
		||||
#include <windows.h>
 | 
			
		||||
#include <mmsystem.h>
 | 
			
		||||
 | 
			
		||||
#include "audio_win_int.h"
 | 
			
		||||
 | 
			
		||||
static struct {
 | 
			
		||||
    int dac_headers;
 | 
			
		||||
    int dac_samples;
 | 
			
		||||
    int adc_headers;
 | 
			
		||||
    int adc_samples;
 | 
			
		||||
} conf = {
 | 
			
		||||
    .dac_headers = 4,
 | 
			
		||||
    .dac_samples = 1024,
 | 
			
		||||
    .adc_headers = 4,
 | 
			
		||||
    .adc_samples = 1024
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    HWVoiceOut hw;
 | 
			
		||||
    HWAVEOUT hwo;
 | 
			
		||||
    WAVEHDR *hdrs;
 | 
			
		||||
    HANDLE event;
 | 
			
		||||
    void *pcm_buf;
 | 
			
		||||
    int avail;
 | 
			
		||||
    int pending;
 | 
			
		||||
    int curhdr;
 | 
			
		||||
    int paused;
 | 
			
		||||
    CRITICAL_SECTION crit_sect;
 | 
			
		||||
} WaveVoiceOut;
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    HWVoiceIn hw;
 | 
			
		||||
    HWAVEIN hwi;
 | 
			
		||||
    WAVEHDR *hdrs;
 | 
			
		||||
    HANDLE event;
 | 
			
		||||
    void *pcm_buf;
 | 
			
		||||
    int curhdr;
 | 
			
		||||
    int paused;
 | 
			
		||||
    int rpos;
 | 
			
		||||
    int avail;
 | 
			
		||||
    CRITICAL_SECTION crit_sect;
 | 
			
		||||
} WaveVoiceIn;
 | 
			
		||||
 | 
			
		||||
static void winwave_log_mmresult (MMRESULT mr)
 | 
			
		||||
{
 | 
			
		||||
    const char *str = "BUG";
 | 
			
		||||
 | 
			
		||||
    switch (mr) {
 | 
			
		||||
    case MMSYSERR_NOERROR:
 | 
			
		||||
        str = "Success";
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case MMSYSERR_INVALHANDLE:
 | 
			
		||||
        str = "Specified device handle is invalid";
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case MMSYSERR_BADDEVICEID:
 | 
			
		||||
        str = "Specified device id is out of range";
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case MMSYSERR_NODRIVER:
 | 
			
		||||
        str = "No device driver is present";
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case MMSYSERR_NOMEM:
 | 
			
		||||
        str = "Unable to allocate or lock memory";
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case WAVERR_SYNC:
 | 
			
		||||
        str = "Device is synchronous but waveOutOpen was called "
 | 
			
		||||
            "without using the WINWAVE_ALLOWSYNC flag";
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case WAVERR_UNPREPARED:
 | 
			
		||||
        str = "The data block pointed to by the pwh parameter "
 | 
			
		||||
            "hasn't been prepared";
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case WAVERR_STILLPLAYING:
 | 
			
		||||
        str = "There are still buffers in the queue";
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    default:
 | 
			
		||||
        dolog ("Reason: Unknown (MMRESULT %#x)\n", mr);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    dolog ("Reason: %s\n", str);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void GCC_FMT_ATTR (2, 3) winwave_logerr (
 | 
			
		||||
    MMRESULT mr,
 | 
			
		||||
    const char *fmt,
 | 
			
		||||
    ...
 | 
			
		||||
    )
 | 
			
		||||
{
 | 
			
		||||
    va_list ap;
 | 
			
		||||
 | 
			
		||||
    va_start (ap, fmt);
 | 
			
		||||
    AUD_vlog (AUDIO_CAP, fmt, ap);
 | 
			
		||||
    va_end (ap);
 | 
			
		||||
 | 
			
		||||
    AUD_log (NULL, " failed\n");
 | 
			
		||||
    winwave_log_mmresult (mr);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void winwave_anal_close_out (WaveVoiceOut *wave)
 | 
			
		||||
{
 | 
			
		||||
    MMRESULT mr;
 | 
			
		||||
 | 
			
		||||
    mr = waveOutClose (wave->hwo);
 | 
			
		||||
    if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
        winwave_logerr (mr, "waveOutClose");
 | 
			
		||||
    }
 | 
			
		||||
    wave->hwo = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void CALLBACK winwave_callback_out (
 | 
			
		||||
    HWAVEOUT hwo,
 | 
			
		||||
    UINT msg,
 | 
			
		||||
    DWORD_PTR dwInstance,
 | 
			
		||||
    DWORD_PTR dwParam1,
 | 
			
		||||
    DWORD_PTR dwParam2
 | 
			
		||||
    )
 | 
			
		||||
{
 | 
			
		||||
    WaveVoiceOut *wave = (WaveVoiceOut *) dwInstance;
 | 
			
		||||
 | 
			
		||||
    switch (msg) {
 | 
			
		||||
    case WOM_DONE:
 | 
			
		||||
        {
 | 
			
		||||
            WAVEHDR *h = (WAVEHDR *) dwParam1;
 | 
			
		||||
            if (!h->dwUser) {
 | 
			
		||||
                h->dwUser = 1;
 | 
			
		||||
                EnterCriticalSection (&wave->crit_sect);
 | 
			
		||||
                {
 | 
			
		||||
                    wave->avail += conf.dac_samples;
 | 
			
		||||
                }
 | 
			
		||||
                LeaveCriticalSection (&wave->crit_sect);
 | 
			
		||||
                if (wave->hw.poll_mode) {
 | 
			
		||||
                    if (!SetEvent (wave->event)) {
 | 
			
		||||
                        dolog ("DAC SetEvent failed %lx\n", GetLastError ());
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case WOM_CLOSE:
 | 
			
		||||
    case WOM_OPEN:
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    default:
 | 
			
		||||
        dolog ("unknown wave out callback msg %x\n", msg);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int winwave_init_out (HWVoiceOut *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    int i;
 | 
			
		||||
    int err;
 | 
			
		||||
    MMRESULT mr;
 | 
			
		||||
    WAVEFORMATEX wfx;
 | 
			
		||||
    WaveVoiceOut *wave;
 | 
			
		||||
 | 
			
		||||
    wave = (WaveVoiceOut *) hw;
 | 
			
		||||
 | 
			
		||||
    InitializeCriticalSection (&wave->crit_sect);
 | 
			
		||||
 | 
			
		||||
    err = waveformat_from_audio_settings (&wfx, as);
 | 
			
		||||
    if (err) {
 | 
			
		||||
        goto err0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    mr = waveOutOpen (&wave->hwo, WAVE_MAPPER, &wfx,
 | 
			
		||||
                      (DWORD_PTR) winwave_callback_out,
 | 
			
		||||
                      (DWORD_PTR) wave, CALLBACK_FUNCTION);
 | 
			
		||||
    if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
        winwave_logerr (mr, "waveOutOpen");
 | 
			
		||||
        goto err1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    wave->hdrs = audio_calloc (AUDIO_FUNC, conf.dac_headers,
 | 
			
		||||
                               sizeof (*wave->hdrs));
 | 
			
		||||
    if (!wave->hdrs) {
 | 
			
		||||
        goto err2;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    audio_pcm_init_info (&hw->info, as);
 | 
			
		||||
    hw->samples = conf.dac_samples * conf.dac_headers;
 | 
			
		||||
    wave->avail = hw->samples;
 | 
			
		||||
 | 
			
		||||
    wave->pcm_buf = audio_calloc (AUDIO_FUNC, conf.dac_samples,
 | 
			
		||||
                                  conf.dac_headers << hw->info.shift);
 | 
			
		||||
    if (!wave->pcm_buf) {
 | 
			
		||||
        goto err3;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < conf.dac_headers; ++i) {
 | 
			
		||||
        WAVEHDR *h = &wave->hdrs[i];
 | 
			
		||||
 | 
			
		||||
        h->dwUser = 0;
 | 
			
		||||
        h->dwBufferLength = conf.dac_samples << hw->info.shift;
 | 
			
		||||
        h->lpData = advance (wave->pcm_buf, i * h->dwBufferLength);
 | 
			
		||||
        h->dwFlags = 0;
 | 
			
		||||
 | 
			
		||||
        mr = waveOutPrepareHeader (wave->hwo, h, sizeof (*h));
 | 
			
		||||
        if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
            winwave_logerr (mr, "waveOutPrepareHeader(%d)", i);
 | 
			
		||||
            goto err4;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
 | 
			
		||||
 err4:
 | 
			
		||||
    g_free (wave->pcm_buf);
 | 
			
		||||
 err3:
 | 
			
		||||
    g_free (wave->hdrs);
 | 
			
		||||
 err2:
 | 
			
		||||
    winwave_anal_close_out (wave);
 | 
			
		||||
 err1:
 | 
			
		||||
 err0:
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int winwave_write (SWVoiceOut *sw, void *buf, int len)
 | 
			
		||||
{
 | 
			
		||||
    return audio_pcm_sw_write (sw, buf, len);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int winwave_run_out (HWVoiceOut *hw, int live)
 | 
			
		||||
{
 | 
			
		||||
    WaveVoiceOut *wave = (WaveVoiceOut *) hw;
 | 
			
		||||
    int decr;
 | 
			
		||||
    int doreset;
 | 
			
		||||
 | 
			
		||||
    EnterCriticalSection (&wave->crit_sect);
 | 
			
		||||
    {
 | 
			
		||||
        decr = audio_MIN (live, wave->avail);
 | 
			
		||||
        decr = audio_pcm_hw_clip_out (hw, wave->pcm_buf, decr, wave->pending);
 | 
			
		||||
        wave->pending += decr;
 | 
			
		||||
        wave->avail -= decr;
 | 
			
		||||
    }
 | 
			
		||||
    LeaveCriticalSection (&wave->crit_sect);
 | 
			
		||||
 | 
			
		||||
    doreset = hw->poll_mode && (wave->pending >= conf.dac_samples);
 | 
			
		||||
    if (doreset && !ResetEvent (wave->event)) {
 | 
			
		||||
        dolog ("DAC ResetEvent failed %lx\n", GetLastError ());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    while (wave->pending >= conf.dac_samples) {
 | 
			
		||||
        MMRESULT mr;
 | 
			
		||||
        WAVEHDR *h = &wave->hdrs[wave->curhdr];
 | 
			
		||||
 | 
			
		||||
        h->dwUser = 0;
 | 
			
		||||
        mr = waveOutWrite (wave->hwo, h, sizeof (*h));
 | 
			
		||||
        if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
            winwave_logerr (mr, "waveOutWrite(%d)", wave->curhdr);
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        wave->pending -= conf.dac_samples;
 | 
			
		||||
        wave->curhdr = (wave->curhdr + 1) % conf.dac_headers;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return decr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void winwave_poll (void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    (void) opaque;
 | 
			
		||||
    audio_run ("winwave_poll");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void winwave_fini_out (HWVoiceOut *hw)
 | 
			
		||||
{
 | 
			
		||||
    int i;
 | 
			
		||||
    MMRESULT mr;
 | 
			
		||||
    WaveVoiceOut *wave = (WaveVoiceOut *) hw;
 | 
			
		||||
 | 
			
		||||
    mr = waveOutReset (wave->hwo);
 | 
			
		||||
    if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
        winwave_logerr (mr, "waveOutReset");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < conf.dac_headers; ++i) {
 | 
			
		||||
        mr = waveOutUnprepareHeader (wave->hwo, &wave->hdrs[i],
 | 
			
		||||
                                     sizeof (wave->hdrs[i]));
 | 
			
		||||
        if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
            winwave_logerr (mr, "waveOutUnprepareHeader(%d)", i);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    winwave_anal_close_out (wave);
 | 
			
		||||
 | 
			
		||||
    if (wave->event) {
 | 
			
		||||
        qemu_del_wait_object (wave->event, winwave_poll, wave);
 | 
			
		||||
        if (!CloseHandle (wave->event)) {
 | 
			
		||||
            dolog ("DAC CloseHandle failed %lx\n", GetLastError ());
 | 
			
		||||
        }
 | 
			
		||||
        wave->event = NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    g_free (wave->pcm_buf);
 | 
			
		||||
    wave->pcm_buf = NULL;
 | 
			
		||||
 | 
			
		||||
    g_free (wave->hdrs);
 | 
			
		||||
    wave->hdrs = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int winwave_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
{
 | 
			
		||||
    MMRESULT mr;
 | 
			
		||||
    WaveVoiceOut *wave = (WaveVoiceOut *) hw;
 | 
			
		||||
 | 
			
		||||
    switch (cmd) {
 | 
			
		||||
    case VOICE_ENABLE:
 | 
			
		||||
        {
 | 
			
		||||
            va_list ap;
 | 
			
		||||
            int poll_mode;
 | 
			
		||||
 | 
			
		||||
            va_start (ap, cmd);
 | 
			
		||||
            poll_mode = va_arg (ap, int);
 | 
			
		||||
            va_end (ap);
 | 
			
		||||
 | 
			
		||||
            if (poll_mode && !wave->event) {
 | 
			
		||||
                wave->event = CreateEvent (NULL, TRUE, TRUE, NULL);
 | 
			
		||||
                if (!wave->event) {
 | 
			
		||||
                    dolog ("DAC CreateEvent: %lx, poll mode will be disabled\n",
 | 
			
		||||
                           GetLastError ());
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (wave->event) {
 | 
			
		||||
                int ret;
 | 
			
		||||
 | 
			
		||||
                ret = qemu_add_wait_object (wave->event, winwave_poll, wave);
 | 
			
		||||
                hw->poll_mode = (ret == 0);
 | 
			
		||||
            }
 | 
			
		||||
            else {
 | 
			
		||||
                hw->poll_mode = 0;
 | 
			
		||||
            }
 | 
			
		||||
            wave->paused = 0;
 | 
			
		||||
        }
 | 
			
		||||
        return 0;
 | 
			
		||||
 | 
			
		||||
    case VOICE_DISABLE:
 | 
			
		||||
        if (!wave->paused) {
 | 
			
		||||
            mr = waveOutReset (wave->hwo);
 | 
			
		||||
            if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
                winwave_logerr (mr, "waveOutReset");
 | 
			
		||||
            }
 | 
			
		||||
            else {
 | 
			
		||||
                wave->paused = 1;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        if (wave->event) {
 | 
			
		||||
            qemu_del_wait_object (wave->event, winwave_poll, wave);
 | 
			
		||||
        }
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void winwave_anal_close_in (WaveVoiceIn *wave)
 | 
			
		||||
{
 | 
			
		||||
    MMRESULT mr;
 | 
			
		||||
 | 
			
		||||
    mr = waveInClose (wave->hwi);
 | 
			
		||||
    if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
        winwave_logerr (mr, "waveInClose");
 | 
			
		||||
    }
 | 
			
		||||
    wave->hwi = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void CALLBACK winwave_callback_in (
 | 
			
		||||
    HWAVEIN *hwi,
 | 
			
		||||
    UINT msg,
 | 
			
		||||
    DWORD_PTR dwInstance,
 | 
			
		||||
    DWORD_PTR dwParam1,
 | 
			
		||||
    DWORD_PTR dwParam2
 | 
			
		||||
    )
 | 
			
		||||
{
 | 
			
		||||
    WaveVoiceIn *wave = (WaveVoiceIn *) dwInstance;
 | 
			
		||||
 | 
			
		||||
    switch (msg) {
 | 
			
		||||
    case WIM_DATA:
 | 
			
		||||
        {
 | 
			
		||||
            WAVEHDR *h = (WAVEHDR *) dwParam1;
 | 
			
		||||
            if (!h->dwUser) {
 | 
			
		||||
                h->dwUser = 1;
 | 
			
		||||
                EnterCriticalSection (&wave->crit_sect);
 | 
			
		||||
                {
 | 
			
		||||
                    wave->avail += conf.adc_samples;
 | 
			
		||||
                }
 | 
			
		||||
                LeaveCriticalSection (&wave->crit_sect);
 | 
			
		||||
                if (wave->hw.poll_mode) {
 | 
			
		||||
                    if (!SetEvent (wave->event)) {
 | 
			
		||||
                        dolog ("ADC SetEvent failed %lx\n", GetLastError ());
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case WIM_CLOSE:
 | 
			
		||||
    case WIM_OPEN:
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    default:
 | 
			
		||||
        dolog ("unknown wave in callback msg %x\n", msg);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void winwave_add_buffers (WaveVoiceIn *wave, int samples)
 | 
			
		||||
{
 | 
			
		||||
    int doreset;
 | 
			
		||||
 | 
			
		||||
    doreset = wave->hw.poll_mode && (samples >= conf.adc_samples);
 | 
			
		||||
    if (doreset && !ResetEvent (wave->event)) {
 | 
			
		||||
        dolog ("ADC ResetEvent failed %lx\n", GetLastError ());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    while (samples >= conf.adc_samples) {
 | 
			
		||||
        MMRESULT mr;
 | 
			
		||||
        WAVEHDR *h = &wave->hdrs[wave->curhdr];
 | 
			
		||||
 | 
			
		||||
        h->dwUser = 0;
 | 
			
		||||
        mr = waveInAddBuffer (wave->hwi, h, sizeof (*h));
 | 
			
		||||
        if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
            winwave_logerr (mr, "waveInAddBuffer(%d)", wave->curhdr);
 | 
			
		||||
        }
 | 
			
		||||
        wave->curhdr = (wave->curhdr + 1) % conf.adc_headers;
 | 
			
		||||
        samples -= conf.adc_samples;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int winwave_init_in (HWVoiceIn *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    int i;
 | 
			
		||||
    int err;
 | 
			
		||||
    MMRESULT mr;
 | 
			
		||||
    WAVEFORMATEX wfx;
 | 
			
		||||
    WaveVoiceIn *wave;
 | 
			
		||||
 | 
			
		||||
    wave = (WaveVoiceIn *) hw;
 | 
			
		||||
 | 
			
		||||
    InitializeCriticalSection (&wave->crit_sect);
 | 
			
		||||
 | 
			
		||||
    err = waveformat_from_audio_settings (&wfx, as);
 | 
			
		||||
    if (err) {
 | 
			
		||||
        goto err0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    mr = waveInOpen (&wave->hwi, WAVE_MAPPER, &wfx,
 | 
			
		||||
                     (DWORD_PTR) winwave_callback_in,
 | 
			
		||||
                     (DWORD_PTR) wave, CALLBACK_FUNCTION);
 | 
			
		||||
    if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
        winwave_logerr (mr, "waveInOpen");
 | 
			
		||||
        goto err1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    wave->hdrs = audio_calloc (AUDIO_FUNC, conf.dac_headers,
 | 
			
		||||
                               sizeof (*wave->hdrs));
 | 
			
		||||
    if (!wave->hdrs) {
 | 
			
		||||
        goto err2;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    audio_pcm_init_info (&hw->info, as);
 | 
			
		||||
    hw->samples = conf.adc_samples * conf.adc_headers;
 | 
			
		||||
    wave->avail = 0;
 | 
			
		||||
 | 
			
		||||
    wave->pcm_buf = audio_calloc (AUDIO_FUNC, conf.adc_samples,
 | 
			
		||||
                                  conf.adc_headers << hw->info.shift);
 | 
			
		||||
    if (!wave->pcm_buf) {
 | 
			
		||||
        goto err3;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < conf.adc_headers; ++i) {
 | 
			
		||||
        WAVEHDR *h = &wave->hdrs[i];
 | 
			
		||||
 | 
			
		||||
        h->dwUser = 0;
 | 
			
		||||
        h->dwBufferLength = conf.adc_samples << hw->info.shift;
 | 
			
		||||
        h->lpData = advance (wave->pcm_buf, i * h->dwBufferLength);
 | 
			
		||||
        h->dwFlags = 0;
 | 
			
		||||
 | 
			
		||||
        mr = waveInPrepareHeader (wave->hwi, h, sizeof (*h));
 | 
			
		||||
        if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
            winwave_logerr (mr, "waveInPrepareHeader(%d)", i);
 | 
			
		||||
            goto err4;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    wave->paused = 1;
 | 
			
		||||
    winwave_add_buffers (wave, hw->samples);
 | 
			
		||||
    return 0;
 | 
			
		||||
 | 
			
		||||
 err4:
 | 
			
		||||
    g_free (wave->pcm_buf);
 | 
			
		||||
 err3:
 | 
			
		||||
    g_free (wave->hdrs);
 | 
			
		||||
 err2:
 | 
			
		||||
    winwave_anal_close_in (wave);
 | 
			
		||||
 err1:
 | 
			
		||||
 err0:
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void winwave_fini_in (HWVoiceIn *hw)
 | 
			
		||||
{
 | 
			
		||||
    int i;
 | 
			
		||||
    MMRESULT mr;
 | 
			
		||||
    WaveVoiceIn *wave = (WaveVoiceIn *) hw;
 | 
			
		||||
 | 
			
		||||
    mr = waveInReset (wave->hwi);
 | 
			
		||||
    if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
        winwave_logerr (mr, "waveInReset");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < conf.adc_headers; ++i) {
 | 
			
		||||
        mr = waveInUnprepareHeader (wave->hwi, &wave->hdrs[i],
 | 
			
		||||
                                     sizeof (wave->hdrs[i]));
 | 
			
		||||
        if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
            winwave_logerr (mr, "waveInUnprepareHeader(%d)", i);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    winwave_anal_close_in (wave);
 | 
			
		||||
 | 
			
		||||
    if (wave->event) {
 | 
			
		||||
        qemu_del_wait_object (wave->event, winwave_poll, wave);
 | 
			
		||||
        if (!CloseHandle (wave->event)) {
 | 
			
		||||
            dolog ("ADC CloseHandle failed %lx\n", GetLastError ());
 | 
			
		||||
        }
 | 
			
		||||
        wave->event = NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    g_free (wave->pcm_buf);
 | 
			
		||||
    wave->pcm_buf = NULL;
 | 
			
		||||
 | 
			
		||||
    g_free (wave->hdrs);
 | 
			
		||||
    wave->hdrs = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int winwave_run_in (HWVoiceIn *hw)
 | 
			
		||||
{
 | 
			
		||||
    WaveVoiceIn *wave = (WaveVoiceIn *) hw;
 | 
			
		||||
    int live = audio_pcm_hw_get_live_in (hw);
 | 
			
		||||
    int dead = hw->samples - live;
 | 
			
		||||
    int decr, ret;
 | 
			
		||||
 | 
			
		||||
    if (!dead) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    EnterCriticalSection (&wave->crit_sect);
 | 
			
		||||
    {
 | 
			
		||||
        decr = audio_MIN (dead, wave->avail);
 | 
			
		||||
        wave->avail -= decr;
 | 
			
		||||
    }
 | 
			
		||||
    LeaveCriticalSection (&wave->crit_sect);
 | 
			
		||||
 | 
			
		||||
    ret = decr;
 | 
			
		||||
    while (decr) {
 | 
			
		||||
        int left = hw->samples - hw->wpos;
 | 
			
		||||
        int conv = audio_MIN (left, decr);
 | 
			
		||||
        hw->conv (hw->conv_buf + hw->wpos,
 | 
			
		||||
                  advance (wave->pcm_buf, wave->rpos << hw->info.shift),
 | 
			
		||||
                  conv);
 | 
			
		||||
 | 
			
		||||
        wave->rpos = (wave->rpos + conv) % hw->samples;
 | 
			
		||||
        hw->wpos = (hw->wpos + conv) % hw->samples;
 | 
			
		||||
        decr -= conv;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    winwave_add_buffers (wave, ret);
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int winwave_read (SWVoiceIn *sw, void *buf, int size)
 | 
			
		||||
{
 | 
			
		||||
    return audio_pcm_sw_read (sw, buf, size);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int winwave_ctl_in (HWVoiceIn *hw, int cmd, ...)
 | 
			
		||||
{
 | 
			
		||||
    MMRESULT mr;
 | 
			
		||||
    WaveVoiceIn *wave = (WaveVoiceIn *) hw;
 | 
			
		||||
 | 
			
		||||
    switch (cmd) {
 | 
			
		||||
    case VOICE_ENABLE:
 | 
			
		||||
        {
 | 
			
		||||
            va_list ap;
 | 
			
		||||
            int poll_mode;
 | 
			
		||||
 | 
			
		||||
            va_start (ap, cmd);
 | 
			
		||||
            poll_mode = va_arg (ap, int);
 | 
			
		||||
            va_end (ap);
 | 
			
		||||
 | 
			
		||||
            if (poll_mode && !wave->event) {
 | 
			
		||||
                wave->event = CreateEvent (NULL, TRUE, TRUE, NULL);
 | 
			
		||||
                if (!wave->event) {
 | 
			
		||||
                    dolog ("ADC CreateEvent: %lx, poll mode will be disabled\n",
 | 
			
		||||
                           GetLastError ());
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (wave->event) {
 | 
			
		||||
                int ret;
 | 
			
		||||
 | 
			
		||||
                ret = qemu_add_wait_object (wave->event, winwave_poll, wave);
 | 
			
		||||
                hw->poll_mode = (ret == 0);
 | 
			
		||||
            }
 | 
			
		||||
            else {
 | 
			
		||||
                hw->poll_mode = 0;
 | 
			
		||||
            }
 | 
			
		||||
            if (wave->paused) {
 | 
			
		||||
                mr = waveInStart (wave->hwi);
 | 
			
		||||
                if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
                    winwave_logerr (mr, "waveInStart");
 | 
			
		||||
                }
 | 
			
		||||
                wave->paused = 0;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return 0;
 | 
			
		||||
 | 
			
		||||
    case VOICE_DISABLE:
 | 
			
		||||
        if (!wave->paused) {
 | 
			
		||||
            mr = waveInStop (wave->hwi);
 | 
			
		||||
            if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
                winwave_logerr (mr, "waveInStop");
 | 
			
		||||
            }
 | 
			
		||||
            else {
 | 
			
		||||
                wave->paused = 1;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        if (wave->event) {
 | 
			
		||||
            qemu_del_wait_object (wave->event, winwave_poll, wave);
 | 
			
		||||
        }
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void *winwave_audio_init (void)
 | 
			
		||||
{
 | 
			
		||||
    return &conf;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void winwave_audio_fini (void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    (void) opaque;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct audio_option winwave_options[] = {
 | 
			
		||||
    {
 | 
			
		||||
        .name        = "DAC_HEADERS",
 | 
			
		||||
        .tag         = AUD_OPT_INT,
 | 
			
		||||
        .valp        = &conf.dac_headers,
 | 
			
		||||
        .descr       = "DAC number of headers",
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name        = "DAC_SAMPLES",
 | 
			
		||||
        .tag         = AUD_OPT_INT,
 | 
			
		||||
        .valp        = &conf.dac_samples,
 | 
			
		||||
        .descr       = "DAC number of samples per header",
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name        = "ADC_HEADERS",
 | 
			
		||||
        .tag         = AUD_OPT_INT,
 | 
			
		||||
        .valp        = &conf.adc_headers,
 | 
			
		||||
        .descr       = "ADC number of headers",
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name        = "ADC_SAMPLES",
 | 
			
		||||
        .tag         = AUD_OPT_INT,
 | 
			
		||||
        .valp        = &conf.adc_samples,
 | 
			
		||||
        .descr       = "ADC number of samples per header",
 | 
			
		||||
    },
 | 
			
		||||
    { /* End of list */ }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static struct audio_pcm_ops winwave_pcm_ops = {
 | 
			
		||||
    .init_out = winwave_init_out,
 | 
			
		||||
    .fini_out = winwave_fini_out,
 | 
			
		||||
    .run_out  = winwave_run_out,
 | 
			
		||||
    .write    = winwave_write,
 | 
			
		||||
    .ctl_out  = winwave_ctl_out,
 | 
			
		||||
    .init_in  = winwave_init_in,
 | 
			
		||||
    .fini_in  = winwave_fini_in,
 | 
			
		||||
    .run_in   = winwave_run_in,
 | 
			
		||||
    .read     = winwave_read,
 | 
			
		||||
    .ctl_in   = winwave_ctl_in
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct audio_driver winwave_audio_driver = {
 | 
			
		||||
    .name           = "winwave",
 | 
			
		||||
    .descr          = "Windows Waveform Audio http://msdn.microsoft.com",
 | 
			
		||||
    .options        = winwave_options,
 | 
			
		||||
    .init           = winwave_audio_init,
 | 
			
		||||
    .fini           = winwave_audio_fini,
 | 
			
		||||
    .pcm_ops        = &winwave_pcm_ops,
 | 
			
		||||
    .can_be_default = 1,
 | 
			
		||||
    .max_voices_out = INT_MAX,
 | 
			
		||||
    .max_voices_in  = INT_MAX,
 | 
			
		||||
    .voice_size_out = sizeof (WaveVoiceOut),
 | 
			
		||||
    .voice_size_in  = sizeof (WaveVoiceIn)
 | 
			
		||||
};
 | 
			
		||||
@@ -43,7 +43,7 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    if (!fb->mem_path) {
 | 
			
		||||
        error_setg(errp, "mem-path property not set");
 | 
			
		||||
        error_setg(errp, "mem_path property not set");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
#ifndef CONFIG_LINUX
 | 
			
		||||
 
 | 
			
		||||
@@ -13,6 +13,7 @@
 | 
			
		||||
#include "qapi/visitor.h"
 | 
			
		||||
#include "qapi-types.h"
 | 
			
		||||
#include "qapi-visit.h"
 | 
			
		||||
#include "qapi/qmp/qerror.h"
 | 
			
		||||
#include "qemu/config-file.h"
 | 
			
		||||
#include "qom/object_interfaces.h"
 | 
			
		||||
 | 
			
		||||
@@ -112,17 +113,24 @@ host_memory_backend_set_host_nodes(Object *obj, Visitor *v, void *opaque,
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int
 | 
			
		||||
host_memory_backend_get_policy(Object *obj, Error **errp G_GNUC_UNUSED)
 | 
			
		||||
static void
 | 
			
		||||
host_memory_backend_get_policy(Object *obj, Visitor *v, void *opaque,
 | 
			
		||||
                               const char *name, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    HostMemoryBackend *backend = MEMORY_BACKEND(obj);
 | 
			
		||||
    return backend->policy;
 | 
			
		||||
    int policy = backend->policy;
 | 
			
		||||
 | 
			
		||||
    visit_type_enum(v, &policy, HostMemPolicy_lookup, NULL, name, errp);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
host_memory_backend_set_policy(Object *obj, int policy, Error **errp)
 | 
			
		||||
host_memory_backend_set_policy(Object *obj, Visitor *v, void *opaque,
 | 
			
		||||
                               const char *name, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    HostMemoryBackend *backend = MEMORY_BACKEND(obj);
 | 
			
		||||
    int policy;
 | 
			
		||||
 | 
			
		||||
    visit_type_enum(v, &policy, HostMemPolicy_lookup, NULL, name, errp);
 | 
			
		||||
    backend->policy = policy;
 | 
			
		||||
 | 
			
		||||
#ifndef CONFIG_NUMA
 | 
			
		||||
@@ -244,10 +252,9 @@ static void host_memory_backend_init(Object *obj)
 | 
			
		||||
    object_property_add(obj, "host-nodes", "int",
 | 
			
		||||
                        host_memory_backend_get_host_nodes,
 | 
			
		||||
                        host_memory_backend_set_host_nodes, NULL, NULL, NULL);
 | 
			
		||||
    object_property_add_enum(obj, "policy", "HostMemPolicy",
 | 
			
		||||
                             HostMemPolicy_lookup,
 | 
			
		||||
                             host_memory_backend_get_policy,
 | 
			
		||||
                             host_memory_backend_set_policy, NULL);
 | 
			
		||||
    object_property_add(obj, "policy", "str",
 | 
			
		||||
                        host_memory_backend_get_policy,
 | 
			
		||||
                        host_memory_backend_set_policy, NULL, NULL, NULL);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
MemoryRegion *
 | 
			
		||||
 
 | 
			
		||||
@@ -140,20 +140,19 @@ static void rng_egd_opened(RngBackend *b, Error **errp)
 | 
			
		||||
    RngEgd *s = RNG_EGD(b);
 | 
			
		||||
 | 
			
		||||
    if (s->chr_name == NULL) {
 | 
			
		||||
        error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
 | 
			
		||||
                   "chardev", "a valid character device");
 | 
			
		||||
        error_set(errp, QERR_INVALID_PARAMETER_VALUE,
 | 
			
		||||
                  "chardev", "a valid character device");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    s->chr = qemu_chr_find(s->chr_name);
 | 
			
		||||
    if (s->chr == NULL) {
 | 
			
		||||
        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
 | 
			
		||||
                  "Device '%s' not found", s->chr_name);
 | 
			
		||||
        error_set(errp, QERR_DEVICE_NOT_FOUND, s->chr_name);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (qemu_chr_fe_claim(s->chr) != 0) {
 | 
			
		||||
        error_setg(errp, QERR_DEVICE_IN_USE, s->chr_name);
 | 
			
		||||
        error_set(errp, QERR_DEVICE_IN_USE, s->chr_name);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -168,7 +167,7 @@ static void rng_egd_set_chardev(Object *obj, const char *value, Error **errp)
 | 
			
		||||
    RngEgd *s = RNG_EGD(b);
 | 
			
		||||
 | 
			
		||||
    if (b->opened) {
 | 
			
		||||
        error_setg(errp, QERR_PERMISSION_DENIED);
 | 
			
		||||
        error_set(errp, QERR_PERMISSION_DENIED);
 | 
			
		||||
    } else {
 | 
			
		||||
        g_free(s->chr_name);
 | 
			
		||||
        s->chr_name = g_strdup(value);
 | 
			
		||||
 
 | 
			
		||||
@@ -74,8 +74,8 @@ static void rng_random_opened(RngBackend *b, Error **errp)
 | 
			
		||||
    RndRandom *s = RNG_RANDOM(b);
 | 
			
		||||
 | 
			
		||||
    if (s->filename == NULL) {
 | 
			
		||||
        error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
 | 
			
		||||
                   "filename", "a valid filename");
 | 
			
		||||
        error_set(errp, QERR_INVALID_PARAMETER_VALUE,
 | 
			
		||||
                  "filename", "a valid filename");
 | 
			
		||||
    } else {
 | 
			
		||||
        s->fd = qemu_open(s->filename, O_RDONLY | O_NONBLOCK);
 | 
			
		||||
        if (s->fd == -1) {
 | 
			
		||||
@@ -98,7 +98,7 @@ static void rng_random_set_filename(Object *obj, const char *filename,
 | 
			
		||||
    RndRandom *s = RNG_RANDOM(obj);
 | 
			
		||||
 | 
			
		||||
    if (b->opened) {
 | 
			
		||||
        error_setg(errp, QERR_PERMISSION_DENIED);
 | 
			
		||||
        error_set(errp, QERR_PERMISSION_DENIED);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -57,7 +57,7 @@ static void rng_backend_prop_set_opened(Object *obj, bool value, Error **errp)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!value && s->opened) {
 | 
			
		||||
        error_setg(errp, QERR_PERMISSION_DENIED);
 | 
			
		||||
        error_set(errp, QERR_PERMISSION_DENIED);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -96,20 +96,6 @@ bool tpm_backend_get_tpm_established_flag(TPMBackend *s)
 | 
			
		||||
    return k->ops->get_tpm_established_flag(s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty)
 | 
			
		||||
{
 | 
			
		||||
    TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
 | 
			
		||||
 | 
			
		||||
    return k->ops->reset_tpm_established_flag(s, locty);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TPMVersion tpm_backend_get_tpm_version(TPMBackend *s)
 | 
			
		||||
{
 | 
			
		||||
    TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
 | 
			
		||||
 | 
			
		||||
    return k->ops->get_tpm_version(s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool tpm_backend_prop_get_opened(Object *obj, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    TPMBackend *s = TPM_BACKEND(obj);
 | 
			
		||||
@@ -133,7 +119,7 @@ static void tpm_backend_prop_set_opened(Object *obj, bool value, Error **errp)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!value && s->opened) {
 | 
			
		||||
        error_setg(errp, QERR_PERMISSION_DENIED);
 | 
			
		||||
        error_set(errp, QERR_PERMISSION_DENIED);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -179,6 +165,17 @@ void tpm_backend_thread_end(TPMBackendThread *tbt)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void tpm_backend_thread_tpm_reset(TPMBackendThread *tbt,
 | 
			
		||||
                                  GFunc func, gpointer user_data)
 | 
			
		||||
{
 | 
			
		||||
    if (!tbt->pool) {
 | 
			
		||||
        tpm_backend_thread_create(tbt, func, user_data);
 | 
			
		||||
    } else {
 | 
			
		||||
        g_thread_pool_push(tbt->pool, (gpointer)TPM_BACKEND_CMD_TPM_RESET,
 | 
			
		||||
                           NULL);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const TypeInfo tpm_backend_info = {
 | 
			
		||||
    .name = TYPE_TPM_BACKEND,
 | 
			
		||||
    .parent = TYPE_OBJECT,
 | 
			
		||||
 
 | 
			
		||||
@@ -24,13 +24,12 @@
 | 
			
		||||
 * THE SOFTWARE.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "monitor/monitor.h"
 | 
			
		||||
#include "exec/cpu-common.h"
 | 
			
		||||
#include "sysemu/kvm.h"
 | 
			
		||||
#include "sysemu/balloon.h"
 | 
			
		||||
#include "trace.h"
 | 
			
		||||
#include "qmp-commands.h"
 | 
			
		||||
#include "qapi/qmp/qerror.h"
 | 
			
		||||
#include "qapi/qmp/qjson.h"
 | 
			
		||||
 | 
			
		||||
static QEMUBalloonEvent *balloon_event_fn;
 | 
			
		||||
@@ -59,6 +58,7 @@ int qemu_add_balloon_handler(QEMUBalloonEvent *event_func,
 | 
			
		||||
        /* We're already registered one balloon handler.  How many can
 | 
			
		||||
         * a guest really have?
 | 
			
		||||
         */
 | 
			
		||||
        error_report("Another balloon device already registered");
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
    balloon_event_fn = event_func;
 | 
			
		||||
@@ -97,7 +97,7 @@ void qmp_balloon(int64_t target, Error **errp)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (target <= 0) {
 | 
			
		||||
        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "target", "a size");
 | 
			
		||||
        error_set(errp, QERR_INVALID_PARAMETER_VALUE, "target", "a size");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
block-obj-y += raw_bsd.o qcow.o vdi.o vmdk.o cloop.o bochs.o vpc.o vvfat.o
 | 
			
		||||
block-obj-y += raw_bsd.o qcow.o vdi.o vmdk.o cloop.o dmg.o bochs.o vpc.o vvfat.o
 | 
			
		||||
block-obj-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o qcow2-cache.o
 | 
			
		||||
block-obj-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o
 | 
			
		||||
block-obj-y += qed-check.o
 | 
			
		||||
@@ -9,8 +9,7 @@ block-obj-y += block-backend.o snapshot.o qapi.o
 | 
			
		||||
block-obj-$(CONFIG_WIN32) += raw-win32.o win32-aio.o
 | 
			
		||||
block-obj-$(CONFIG_POSIX) += raw-posix.o
 | 
			
		||||
block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
 | 
			
		||||
block-obj-y += null.o mirror.o io.o
 | 
			
		||||
block-obj-y += throttle-groups.o
 | 
			
		||||
block-obj-y += null.o mirror.o
 | 
			
		||||
 | 
			
		||||
block-obj-y += nbd.o nbd-client.o sheepdog.o
 | 
			
		||||
block-obj-$(CONFIG_LIBISCSI) += iscsi.o
 | 
			
		||||
@@ -20,6 +19,8 @@ block-obj-$(CONFIG_RBD) += rbd.o
 | 
			
		||||
block-obj-$(CONFIG_GLUSTERFS) += gluster.o
 | 
			
		||||
block-obj-$(CONFIG_ARCHIPELAGO) += archipelago.o
 | 
			
		||||
block-obj-$(CONFIG_LIBSSH2) += ssh.o
 | 
			
		||||
block-obj-y += dictzip.o
 | 
			
		||||
block-obj-y += tar.o
 | 
			
		||||
block-obj-y += accounting.o
 | 
			
		||||
block-obj-y += write-threshold.o
 | 
			
		||||
 | 
			
		||||
@@ -38,7 +39,6 @@ gluster.o-libs     := $(GLUSTERFS_LIBS)
 | 
			
		||||
ssh.o-cflags       := $(LIBSSH2_CFLAGS)
 | 
			
		||||
ssh.o-libs         := $(LIBSSH2_LIBS)
 | 
			
		||||
archipelago.o-libs := $(ARCHIPELAGO_LIBS)
 | 
			
		||||
block-obj-m        += dmg.o
 | 
			
		||||
dmg.o-libs         := $(BZIP2_LIBS)
 | 
			
		||||
qcow.o-libs        := -lz
 | 
			
		||||
linux-aio.o-libs   := -laio
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										160
									
								
								block/backup.c
									
									
									
									
									
								
							
							
						
						
									
										160
									
								
								block/backup.c
									
									
									
									
									
								
							@@ -19,7 +19,6 @@
 | 
			
		||||
#include "block/block.h"
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
#include "block/blockjob.h"
 | 
			
		||||
#include "qapi/qmp/qerror.h"
 | 
			
		||||
#include "qemu/ratelimit.h"
 | 
			
		||||
 | 
			
		||||
#define BACKUP_CLUSTER_BITS 16
 | 
			
		||||
@@ -38,8 +37,6 @@ typedef struct CowRequest {
 | 
			
		||||
typedef struct BackupBlockJob {
 | 
			
		||||
    BlockJob common;
 | 
			
		||||
    BlockDriverState *target;
 | 
			
		||||
    /* bitmap for sync=incremental */
 | 
			
		||||
    BdrvDirtyBitmap *sync_bitmap;
 | 
			
		||||
    MirrorSyncMode sync_mode;
 | 
			
		||||
    RateLimit limit;
 | 
			
		||||
    BlockdevOnError on_source_error;
 | 
			
		||||
@@ -198,7 +195,7 @@ static void backup_set_speed(BlockJob *job, int64_t speed, Error **errp)
 | 
			
		||||
    BackupBlockJob *s = container_of(job, BackupBlockJob, common);
 | 
			
		||||
 | 
			
		||||
    if (speed < 0) {
 | 
			
		||||
        error_setg(errp, QERR_INVALID_PARAMETER, "speed");
 | 
			
		||||
        error_set(errp, QERR_INVALID_PARAMETER, "speed");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
 | 
			
		||||
@@ -245,91 +242,6 @@ static void backup_complete(BlockJob *job, void *opaque)
 | 
			
		||||
    g_free(data);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool coroutine_fn yield_and_check(BackupBlockJob *job)
 | 
			
		||||
{
 | 
			
		||||
    if (block_job_is_cancelled(&job->common)) {
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* we need to yield so that bdrv_drain_all() returns.
 | 
			
		||||
     * (without, VM does not reboot)
 | 
			
		||||
     */
 | 
			
		||||
    if (job->common.speed) {
 | 
			
		||||
        uint64_t delay_ns = ratelimit_calculate_delay(&job->limit,
 | 
			
		||||
                                                      job->sectors_read);
 | 
			
		||||
        job->sectors_read = 0;
 | 
			
		||||
        block_job_sleep_ns(&job->common, QEMU_CLOCK_REALTIME, delay_ns);
 | 
			
		||||
    } else {
 | 
			
		||||
        block_job_sleep_ns(&job->common, QEMU_CLOCK_REALTIME, 0);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (block_job_is_cancelled(&job->common)) {
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int coroutine_fn backup_run_incremental(BackupBlockJob *job)
 | 
			
		||||
{
 | 
			
		||||
    bool error_is_read;
 | 
			
		||||
    int ret = 0;
 | 
			
		||||
    int clusters_per_iter;
 | 
			
		||||
    uint32_t granularity;
 | 
			
		||||
    int64_t sector;
 | 
			
		||||
    int64_t cluster;
 | 
			
		||||
    int64_t end;
 | 
			
		||||
    int64_t last_cluster = -1;
 | 
			
		||||
    BlockDriverState *bs = job->common.bs;
 | 
			
		||||
    HBitmapIter hbi;
 | 
			
		||||
 | 
			
		||||
    granularity = bdrv_dirty_bitmap_granularity(job->sync_bitmap);
 | 
			
		||||
    clusters_per_iter = MAX((granularity / BACKUP_CLUSTER_SIZE), 1);
 | 
			
		||||
    bdrv_dirty_iter_init(job->sync_bitmap, &hbi);
 | 
			
		||||
 | 
			
		||||
    /* Find the next dirty sector(s) */
 | 
			
		||||
    while ((sector = hbitmap_iter_next(&hbi)) != -1) {
 | 
			
		||||
        cluster = sector / BACKUP_SECTORS_PER_CLUSTER;
 | 
			
		||||
 | 
			
		||||
        /* Fake progress updates for any clusters we skipped */
 | 
			
		||||
        if (cluster != last_cluster + 1) {
 | 
			
		||||
            job->common.offset += ((cluster - last_cluster - 1) *
 | 
			
		||||
                                   BACKUP_CLUSTER_SIZE);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        for (end = cluster + clusters_per_iter; cluster < end; cluster++) {
 | 
			
		||||
            do {
 | 
			
		||||
                if (yield_and_check(job)) {
 | 
			
		||||
                    return ret;
 | 
			
		||||
                }
 | 
			
		||||
                ret = backup_do_cow(bs, cluster * BACKUP_SECTORS_PER_CLUSTER,
 | 
			
		||||
                                    BACKUP_SECTORS_PER_CLUSTER, &error_is_read);
 | 
			
		||||
                if ((ret < 0) &&
 | 
			
		||||
                    backup_error_action(job, error_is_read, -ret) ==
 | 
			
		||||
                    BLOCK_ERROR_ACTION_REPORT) {
 | 
			
		||||
                    return ret;
 | 
			
		||||
                }
 | 
			
		||||
            } while (ret < 0);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /* If the bitmap granularity is smaller than the backup granularity,
 | 
			
		||||
         * we need to advance the iterator pointer to the next cluster. */
 | 
			
		||||
        if (granularity < BACKUP_CLUSTER_SIZE) {
 | 
			
		||||
            bdrv_set_dirty_iter(&hbi, cluster * BACKUP_SECTORS_PER_CLUSTER);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        last_cluster = cluster - 1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Play some final catchup with the progress meter */
 | 
			
		||||
    end = DIV_ROUND_UP(job->common.len, BACKUP_CLUSTER_SIZE);
 | 
			
		||||
    if (last_cluster + 1 < end) {
 | 
			
		||||
        job->common.offset += ((end - last_cluster - 1) * BACKUP_CLUSTER_SIZE);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void coroutine_fn backup_run(void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    BackupBlockJob *job = opaque;
 | 
			
		||||
@@ -347,7 +259,8 @@ static void coroutine_fn backup_run(void *opaque)
 | 
			
		||||
    qemu_co_rwlock_init(&job->flush_rwlock);
 | 
			
		||||
 | 
			
		||||
    start = 0;
 | 
			
		||||
    end = DIV_ROUND_UP(job->common.len, BACKUP_CLUSTER_SIZE);
 | 
			
		||||
    end = DIV_ROUND_UP(job->common.len / BDRV_SECTOR_SIZE,
 | 
			
		||||
                       BACKUP_SECTORS_PER_CLUSTER);
 | 
			
		||||
 | 
			
		||||
    job->bitmap = hbitmap_alloc(end, 0);
 | 
			
		||||
 | 
			
		||||
@@ -365,13 +278,28 @@ static void coroutine_fn backup_run(void *opaque)
 | 
			
		||||
            qemu_coroutine_yield();
 | 
			
		||||
            job->common.busy = true;
 | 
			
		||||
        }
 | 
			
		||||
    } else if (job->sync_mode == MIRROR_SYNC_MODE_INCREMENTAL) {
 | 
			
		||||
        ret = backup_run_incremental(job);
 | 
			
		||||
    } else {
 | 
			
		||||
        /* Both FULL and TOP SYNC_MODE's require copying.. */
 | 
			
		||||
        for (; start < end; start++) {
 | 
			
		||||
            bool error_is_read;
 | 
			
		||||
            if (yield_and_check(job)) {
 | 
			
		||||
 | 
			
		||||
            if (block_job_is_cancelled(&job->common)) {
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            /* we need to yield so that qemu_aio_flush() returns.
 | 
			
		||||
             * (without, VM does not reboot)
 | 
			
		||||
             */
 | 
			
		||||
            if (job->common.speed) {
 | 
			
		||||
                uint64_t delay_ns = ratelimit_calculate_delay(
 | 
			
		||||
                        &job->limit, job->sectors_read);
 | 
			
		||||
                job->sectors_read = 0;
 | 
			
		||||
                block_job_sleep_ns(&job->common, QEMU_CLOCK_REALTIME, delay_ns);
 | 
			
		||||
            } else {
 | 
			
		||||
                block_job_sleep_ns(&job->common, QEMU_CLOCK_REALTIME, 0);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (block_job_is_cancelled(&job->common)) {
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
@@ -429,18 +357,6 @@ static void coroutine_fn backup_run(void *opaque)
 | 
			
		||||
    qemu_co_rwlock_wrlock(&job->flush_rwlock);
 | 
			
		||||
    qemu_co_rwlock_unlock(&job->flush_rwlock);
 | 
			
		||||
 | 
			
		||||
    if (job->sync_bitmap) {
 | 
			
		||||
        BdrvDirtyBitmap *bm;
 | 
			
		||||
        if (ret < 0) {
 | 
			
		||||
            /* Merge the successor back into the parent, delete nothing. */
 | 
			
		||||
            bm = bdrv_reclaim_dirty_bitmap(bs, job->sync_bitmap, NULL);
 | 
			
		||||
            assert(bm);
 | 
			
		||||
        } else {
 | 
			
		||||
            /* Everything is fine, delete this bitmap and install the backup. */
 | 
			
		||||
            bm = bdrv_dirty_bitmap_abdicate(bs, job->sync_bitmap, NULL);
 | 
			
		||||
            assert(bm);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    hbitmap_free(job->bitmap);
 | 
			
		||||
 | 
			
		||||
    bdrv_iostatus_disable(target);
 | 
			
		||||
@@ -453,7 +369,6 @@ static void coroutine_fn backup_run(void *opaque)
 | 
			
		||||
 | 
			
		||||
void backup_start(BlockDriverState *bs, BlockDriverState *target,
 | 
			
		||||
                  int64_t speed, MirrorSyncMode sync_mode,
 | 
			
		||||
                  BdrvDirtyBitmap *sync_bitmap,
 | 
			
		||||
                  BlockdevOnError on_source_error,
 | 
			
		||||
                  BlockdevOnError on_target_error,
 | 
			
		||||
                  BlockCompletionFunc *cb, void *opaque,
 | 
			
		||||
@@ -473,7 +388,7 @@ void backup_start(BlockDriverState *bs, BlockDriverState *target,
 | 
			
		||||
    if ((on_source_error == BLOCKDEV_ON_ERROR_STOP ||
 | 
			
		||||
         on_source_error == BLOCKDEV_ON_ERROR_ENOSPC) &&
 | 
			
		||||
        !bdrv_iostatus_is_enabled(bs)) {
 | 
			
		||||
        error_setg(errp, QERR_INVALID_PARAMETER, "on-source-error");
 | 
			
		||||
        error_set(errp, QERR_INVALID_PARAMETER, "on-source-error");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -497,36 +412,17 @@ void backup_start(BlockDriverState *bs, BlockDriverState *target,
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (sync_mode == MIRROR_SYNC_MODE_INCREMENTAL) {
 | 
			
		||||
        if (!sync_bitmap) {
 | 
			
		||||
            error_setg(errp, "must provide a valid bitmap name for "
 | 
			
		||||
                             "\"incremental\" sync mode");
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /* Create a new bitmap, and freeze/disable this one. */
 | 
			
		||||
        if (bdrv_dirty_bitmap_create_successor(bs, sync_bitmap, errp) < 0) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
    } else if (sync_bitmap) {
 | 
			
		||||
        error_setg(errp,
 | 
			
		||||
                   "a sync_bitmap was provided to backup_run, "
 | 
			
		||||
                   "but received an incompatible sync_mode (%s)",
 | 
			
		||||
                   MirrorSyncMode_lookup[sync_mode]);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    len = bdrv_getlength(bs);
 | 
			
		||||
    if (len < 0) {
 | 
			
		||||
        error_setg_errno(errp, -len, "unable to get length for '%s'",
 | 
			
		||||
                         bdrv_get_device_name(bs));
 | 
			
		||||
        goto error;
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    BackupBlockJob *job = block_job_create(&backup_job_driver, bs, speed,
 | 
			
		||||
                                           cb, opaque, errp);
 | 
			
		||||
    if (!job) {
 | 
			
		||||
        goto error;
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    bdrv_op_block_all(target, job->common.blocker);
 | 
			
		||||
@@ -535,15 +431,7 @@ void backup_start(BlockDriverState *bs, BlockDriverState *target,
 | 
			
		||||
    job->on_target_error = on_target_error;
 | 
			
		||||
    job->target = target;
 | 
			
		||||
    job->sync_mode = sync_mode;
 | 
			
		||||
    job->sync_bitmap = sync_mode == MIRROR_SYNC_MODE_INCREMENTAL ?
 | 
			
		||||
                       sync_bitmap : NULL;
 | 
			
		||||
    job->common.len = len;
 | 
			
		||||
    job->common.co = qemu_coroutine_create(backup_run);
 | 
			
		||||
    qemu_coroutine_enter(job->common.co, job);
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
 error:
 | 
			
		||||
    if (sync_bitmap) {
 | 
			
		||||
        bdrv_reclaim_dirty_bitmap(bs, sync_bitmap, NULL);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -216,9 +216,10 @@ static int get_event_by_name(const char *name, BlkDebugEvent *event)
 | 
			
		||||
struct add_rule_data {
 | 
			
		||||
    BDRVBlkdebugState *s;
 | 
			
		||||
    int action;
 | 
			
		||||
    Error **errp;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static int add_rule(void *opaque, QemuOpts *opts, Error **errp)
 | 
			
		||||
static int add_rule(QemuOpts *opts, void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    struct add_rule_data *d = opaque;
 | 
			
		||||
    BDRVBlkdebugState *s = d->s;
 | 
			
		||||
@@ -229,10 +230,10 @@ static int add_rule(void *opaque, QemuOpts *opts, Error **errp)
 | 
			
		||||
    /* Find the right event for the rule */
 | 
			
		||||
    event_name = qemu_opt_get(opts, "event");
 | 
			
		||||
    if (!event_name) {
 | 
			
		||||
        error_setg(errp, "Missing event name for rule");
 | 
			
		||||
        error_setg(d->errp, "Missing event name for rule");
 | 
			
		||||
        return -1;
 | 
			
		||||
    } else if (get_event_by_name(event_name, &event) < 0) {
 | 
			
		||||
        error_setg(errp, "Invalid event name \"%s\"", event_name);
 | 
			
		||||
        error_setg(d->errp, "Invalid event name \"%s\"", event_name);
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -318,7 +319,8 @@ static int read_config(BDRVBlkdebugState *s, const char *filename,
 | 
			
		||||
 | 
			
		||||
    d.s = s;
 | 
			
		||||
    d.action = ACTION_INJECT_ERROR;
 | 
			
		||||
    qemu_opts_foreach(&inject_error_opts, add_rule, &d, &local_err);
 | 
			
		||||
    d.errp = &local_err;
 | 
			
		||||
    qemu_opts_foreach(&inject_error_opts, add_rule, &d, 1);
 | 
			
		||||
    if (local_err) {
 | 
			
		||||
        error_propagate(errp, local_err);
 | 
			
		||||
        ret = -EINVAL;
 | 
			
		||||
@@ -326,7 +328,7 @@ static int read_config(BDRVBlkdebugState *s, const char *filename,
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    d.action = ACTION_SET_STATE;
 | 
			
		||||
    qemu_opts_foreach(&set_state_opts, add_rule, &d, &local_err);
 | 
			
		||||
    qemu_opts_foreach(&set_state_opts, add_rule, &d, 1);
 | 
			
		||||
    if (local_err) {
 | 
			
		||||
        error_propagate(errp, local_err);
 | 
			
		||||
        ret = -EINVAL;
 | 
			
		||||
@@ -429,7 +431,7 @@ static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
    /* Open the backing file */
 | 
			
		||||
    assert(bs->file == NULL);
 | 
			
		||||
    ret = bdrv_open_image(&bs->file, qemu_opt_get(opts, "x-image"), options, "image",
 | 
			
		||||
                          bs, &child_file, false, &local_err);
 | 
			
		||||
                          flags | BDRV_O_PROTOCOL, false, &local_err);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        error_propagate(errp, local_err);
 | 
			
		||||
        goto out;
 | 
			
		||||
@@ -719,11 +721,6 @@ static int64_t blkdebug_getlength(BlockDriverState *bs)
 | 
			
		||||
    return bdrv_getlength(bs->file);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int blkdebug_truncate(BlockDriverState *bs, int64_t offset)
 | 
			
		||||
{
 | 
			
		||||
    return bdrv_truncate(bs->file, offset);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void blkdebug_refresh_filename(BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
    QDict *opts;
 | 
			
		||||
@@ -782,7 +779,6 @@ static BlockDriver bdrv_blkdebug = {
 | 
			
		||||
    .bdrv_file_open         = blkdebug_open,
 | 
			
		||||
    .bdrv_close             = blkdebug_close,
 | 
			
		||||
    .bdrv_getlength         = blkdebug_getlength,
 | 
			
		||||
    .bdrv_truncate          = blkdebug_truncate,
 | 
			
		||||
    .bdrv_refresh_filename  = blkdebug_refresh_filename,
 | 
			
		||||
 | 
			
		||||
    .bdrv_aio_readv         = blkdebug_aio_readv,
 | 
			
		||||
 
 | 
			
		||||
@@ -125,7 +125,7 @@ static int blkverify_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
    /* Open the raw file */
 | 
			
		||||
    assert(bs->file == NULL);
 | 
			
		||||
    ret = bdrv_open_image(&bs->file, qemu_opt_get(opts, "x-raw"), options,
 | 
			
		||||
                          "raw", bs, &child_file, false, &local_err);
 | 
			
		||||
                          "raw", flags | BDRV_O_PROTOCOL, false, &local_err);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        error_propagate(errp, local_err);
 | 
			
		||||
        goto fail;
 | 
			
		||||
@@ -134,7 +134,7 @@ static int blkverify_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
    /* Open the test file */
 | 
			
		||||
    assert(s->test_file == NULL);
 | 
			
		||||
    ret = bdrv_open_image(&s->test_file, qemu_opt_get(opts, "x-image"), options,
 | 
			
		||||
                          "test", bs, &child_format, false, &local_err);
 | 
			
		||||
                          "test", flags, false, &local_err);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        error_propagate(errp, local_err);
 | 
			
		||||
        s->test_file = NULL;
 | 
			
		||||
 
 | 
			
		||||
@@ -515,17 +515,6 @@ int blk_write(BlockBackend *blk, int64_t sector_num, const uint8_t *buf,
 | 
			
		||||
    return bdrv_write(blk->bs, sector_num, buf, nb_sectors);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int blk_write_zeroes(BlockBackend *blk, int64_t sector_num,
 | 
			
		||||
                     int nb_sectors, BdrvRequestFlags flags)
 | 
			
		||||
{
 | 
			
		||||
    int ret = blk_check_request(blk, sector_num, nb_sectors);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return bdrv_write_zeroes(blk->bs, sector_num, nb_sectors, flags);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void error_callback_bh(void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    struct BlockBackendAIOCB *acb = opaque;
 | 
			
		||||
@@ -700,11 +689,6 @@ int blk_flush_all(void)
 | 
			
		||||
    return bdrv_flush_all();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void blk_drain(BlockBackend *blk)
 | 
			
		||||
{
 | 
			
		||||
    bdrv_drain(blk->bs);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void blk_drain_all(void)
 | 
			
		||||
{
 | 
			
		||||
    bdrv_drain_all();
 | 
			
		||||
 
 | 
			
		||||
@@ -15,7 +15,6 @@
 | 
			
		||||
#include "trace.h"
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
#include "block/blockjob.h"
 | 
			
		||||
#include "qapi/qmp/qerror.h"
 | 
			
		||||
#include "qemu/ratelimit.h"
 | 
			
		||||
 | 
			
		||||
enum {
 | 
			
		||||
@@ -187,7 +186,7 @@ static void commit_set_speed(BlockJob *job, int64_t speed, Error **errp)
 | 
			
		||||
    CommitBlockJob *s = container_of(job, CommitBlockJob, common);
 | 
			
		||||
 | 
			
		||||
    if (speed < 0) {
 | 
			
		||||
        error_setg(errp, QERR_INVALID_PARAMETER, "speed");
 | 
			
		||||
        error_set(errp, QERR_INVALID_PARAMETER, "speed");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
 | 
			
		||||
 
 | 
			
		||||
@@ -24,7 +24,6 @@
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
#include "qapi/qmp/qbool.h"
 | 
			
		||||
#include "qapi/qmp/qstring.h"
 | 
			
		||||
#include <curl/curl.h>
 | 
			
		||||
 | 
			
		||||
// #define DEBUG_CURL
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										584
									
								
								block/dictzip.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										584
									
								
								block/dictzip.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,584 @@
 | 
			
		||||
/*
 | 
			
		||||
 * DictZip Block driver for dictzip enabled gzip files
 | 
			
		||||
 *
 | 
			
		||||
 * Use the "dictzip" tool from the "dictd" package to create gzip files that
 | 
			
		||||
 * contain the extra DictZip headers.
 | 
			
		||||
 *
 | 
			
		||||
 * dictzip(1) is a compression program which creates compressed files in the
 | 
			
		||||
 * gzip format (see RFC 1952). However, unlike gzip(1), dictzip(1) compresses
 | 
			
		||||
 * the file in pieces and stores an index to the pieces in the gzip header.
 | 
			
		||||
 * This allows random access to the file at the granularity of the compressed
 | 
			
		||||
 * pieces (currently about 64kB) while maintaining good compression ratios
 | 
			
		||||
 * (within 5% of the expected ratio for dictionary data).
 | 
			
		||||
 * dictd(8) uses files stored in this format.
 | 
			
		||||
 *
 | 
			
		||||
 * For details on DictZip see http://dict.org/.
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
 | 
			
		||||
 *
 | 
			
		||||
 * 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.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
#include <zlib.h>
 | 
			
		||||
 | 
			
		||||
// #define DEBUG
 | 
			
		||||
 | 
			
		||||
#ifdef DEBUG
 | 
			
		||||
#define dprintf(fmt, ...) do { printf("dzip: " fmt, ## __VA_ARGS__); } while (0)
 | 
			
		||||
#else
 | 
			
		||||
#define dprintf(fmt, ...) do { } while (0)
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#define SECTOR_SIZE 512
 | 
			
		||||
#define Z_STREAM_COUNT 4
 | 
			
		||||
#define CACHE_COUNT 20
 | 
			
		||||
 | 
			
		||||
/* magic values */
 | 
			
		||||
 | 
			
		||||
#define GZ_MAGIC1     0x1f
 | 
			
		||||
#define GZ_MAGIC2     0x8b
 | 
			
		||||
#define DZ_MAGIC1      'R'
 | 
			
		||||
#define DZ_MAGIC2      'A'
 | 
			
		||||
 | 
			
		||||
#define GZ_FEXTRA     0x04      /* Optional field (random access index)    */
 | 
			
		||||
#define GZ_FNAME      0x08      /* Original name                           */
 | 
			
		||||
#define GZ_COMMENT    0x10      /* Zero-terminated, human-readable comment */
 | 
			
		||||
#define GZ_FHCRC      0x02      /* Header CRC16                            */
 | 
			
		||||
 | 
			
		||||
/* offsets */
 | 
			
		||||
 | 
			
		||||
#define GZ_ID            0      /* GZ_MAGIC (16bit)                        */
 | 
			
		||||
#define GZ_FLG           3      /* FLaGs (see above)                       */
 | 
			
		||||
#define GZ_XLEN         10      /* eXtra LENgth (16bit)                    */
 | 
			
		||||
#define GZ_SI           12      /* Subfield ID (16bit)                     */
 | 
			
		||||
#define GZ_VERSION      16      /* Version for subfield format             */
 | 
			
		||||
#define GZ_CHUNKSIZE    18      /* Chunk size (16bit)                      */
 | 
			
		||||
#define GZ_CHUNKCNT     20      /* Number of chunks (16bit)                */
 | 
			
		||||
#define GZ_RNDDATA      22      /* Random access data (16bit)              */
 | 
			
		||||
 | 
			
		||||
#define GZ_99_CHUNKSIZE 18      /* Chunk size (32bit)                      */
 | 
			
		||||
#define GZ_99_CHUNKCNT  22      /* Number of chunks (32bit)                */
 | 
			
		||||
#define GZ_99_FILESIZE  26      /* Size of unpacked file (64bit)           */
 | 
			
		||||
#define GZ_99_RNDDATA   34      /* Random access data (32bit)              */
 | 
			
		||||
 | 
			
		||||
struct BDRVDictZipState;
 | 
			
		||||
 | 
			
		||||
typedef struct DictZipAIOCB {
 | 
			
		||||
    BlockAIOCB common;
 | 
			
		||||
    struct BDRVDictZipState *s;
 | 
			
		||||
    QEMUIOVector *qiov;          /* QIOV of the original request */
 | 
			
		||||
    QEMUIOVector *qiov_gz;       /* QIOV of the gz subrequest */
 | 
			
		||||
    QEMUBH *bh;                  /* BH for cache */
 | 
			
		||||
    z_stream *zStream;           /* stream to use for decoding */
 | 
			
		||||
    int zStream_id;              /* stream id of the above pointer */
 | 
			
		||||
    size_t start;                /* offset into the uncompressed file */
 | 
			
		||||
    size_t len;                  /* uncompressed bytes to read */
 | 
			
		||||
    uint8_t *gzipped;            /* the gzipped data */
 | 
			
		||||
    uint8_t *buf;                /* cached result */
 | 
			
		||||
    size_t gz_len;               /* amount of gzip data */
 | 
			
		||||
    size_t gz_start;             /* uncompressed starting point of gzip data */
 | 
			
		||||
    uint64_t offset;             /* offset for "start" into the uncompressed chunk */
 | 
			
		||||
    int chunks_len;              /* amount of uncompressed data in all gzip data */
 | 
			
		||||
} DictZipAIOCB;
 | 
			
		||||
 | 
			
		||||
typedef struct dict_cache {
 | 
			
		||||
    size_t start;
 | 
			
		||||
    size_t len;
 | 
			
		||||
    uint8_t *buf;
 | 
			
		||||
} DictCache;
 | 
			
		||||
 | 
			
		||||
typedef struct BDRVDictZipState {
 | 
			
		||||
    BlockDriverState *hd;
 | 
			
		||||
    z_stream zStream[Z_STREAM_COUNT];
 | 
			
		||||
    DictCache cache[CACHE_COUNT];
 | 
			
		||||
    int cache_index;
 | 
			
		||||
    uint8_t  stream_in_use;
 | 
			
		||||
    uint64_t chunk_len;
 | 
			
		||||
    uint32_t chunk_cnt;
 | 
			
		||||
    uint16_t *chunks;
 | 
			
		||||
    uint32_t *chunks32;
 | 
			
		||||
    uint64_t *offsets;
 | 
			
		||||
    int64_t file_len;
 | 
			
		||||
} BDRVDictZipState;
 | 
			
		||||
 | 
			
		||||
static int start_zStream(z_stream *zStream)
 | 
			
		||||
{
 | 
			
		||||
    zStream->zalloc    = NULL;
 | 
			
		||||
    zStream->zfree     = NULL;
 | 
			
		||||
    zStream->opaque    = NULL;
 | 
			
		||||
    zStream->next_in   = 0;
 | 
			
		||||
    zStream->avail_in  = 0;
 | 
			
		||||
    zStream->next_out  = NULL;
 | 
			
		||||
    zStream->avail_out = 0;
 | 
			
		||||
 | 
			
		||||
    return inflateInit2( zStream, -15 );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static QemuOptsList runtime_opts = {
 | 
			
		||||
    .name = "dzip",
 | 
			
		||||
    .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
 | 
			
		||||
    .desc = {
 | 
			
		||||
        {
 | 
			
		||||
            .name = "filename",
 | 
			
		||||
            .type = QEMU_OPT_STRING,
 | 
			
		||||
            .help = "URL to the dictzip file",
 | 
			
		||||
        },
 | 
			
		||||
        { /* end of list */ }
 | 
			
		||||
    },
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static int dictzip_open(BlockDriverState *bs, QDict *options, int flags, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    BDRVDictZipState *s = bs->opaque;
 | 
			
		||||
    const char *err = "Unknown (read error?)";
 | 
			
		||||
    uint8_t magic[2];
 | 
			
		||||
    char buf[100];
 | 
			
		||||
    uint8_t header_flags;
 | 
			
		||||
    uint16_t chunk_len16;
 | 
			
		||||
    uint16_t chunk_cnt16;
 | 
			
		||||
    uint32_t chunk_len32;
 | 
			
		||||
    uint16_t header_ver;
 | 
			
		||||
    uint16_t tmp_short;
 | 
			
		||||
    uint64_t offset;
 | 
			
		||||
    int chunks_len;
 | 
			
		||||
    int headerLength = GZ_XLEN - 1;
 | 
			
		||||
    int rnd_offs;
 | 
			
		||||
    int ret;
 | 
			
		||||
    int i;
 | 
			
		||||
    QemuOpts *opts;
 | 
			
		||||
    Error *local_err = NULL;
 | 
			
		||||
    const char *filename;
 | 
			
		||||
 | 
			
		||||
    opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
 | 
			
		||||
    qemu_opts_absorb_qdict(opts, options, &local_err);
 | 
			
		||||
    if (local_err != NULL) {
 | 
			
		||||
        error_propagate(errp, local_err);
 | 
			
		||||
        ret = -EINVAL;
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    filename = qemu_opt_get(opts, "filename");
 | 
			
		||||
 | 
			
		||||
    if (!strncmp(filename, "dzip://", 7))
 | 
			
		||||
        filename += 7;
 | 
			
		||||
    else if (!strncmp(filename, "dzip:", 5))
 | 
			
		||||
        filename += 5;
 | 
			
		||||
 | 
			
		||||
    ret = bdrv_open(&s->hd, filename, NULL, NULL, flags | BDRV_O_PROTOCOL, NULL, &local_err);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        error_propagate(errp, local_err);
 | 
			
		||||
        qemu_opts_del(opts);
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* initialize zlib streams */
 | 
			
		||||
    for (i = 0; i < Z_STREAM_COUNT; i++) {
 | 
			
		||||
        if (start_zStream( &s->zStream[i] ) != Z_OK) {
 | 
			
		||||
            err = s->zStream[i].msg;
 | 
			
		||||
            goto fail;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* gzip header */
 | 
			
		||||
    if (bdrv_pread(s->hd, GZ_ID, &magic, sizeof(magic)) != sizeof(magic))
 | 
			
		||||
        goto fail;
 | 
			
		||||
 | 
			
		||||
    if (!((magic[0] == GZ_MAGIC1) && (magic[1] == GZ_MAGIC2))) {
 | 
			
		||||
        err = "No gzip file";
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* dzip header */
 | 
			
		||||
    if (bdrv_pread(s->hd, GZ_FLG, &header_flags, 1) != 1)
 | 
			
		||||
        goto fail;
 | 
			
		||||
 | 
			
		||||
    if (!(header_flags & GZ_FEXTRA)) {
 | 
			
		||||
        err = "Not a dictzip file (wrong flags)";
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* extra length */
 | 
			
		||||
    if (bdrv_pread(s->hd, GZ_XLEN, &tmp_short, 2) != 2)
 | 
			
		||||
        goto fail;
 | 
			
		||||
 | 
			
		||||
    headerLength += le16_to_cpu(tmp_short) + 2;
 | 
			
		||||
 | 
			
		||||
    /* DictZip magic */
 | 
			
		||||
    if (bdrv_pread(s->hd, GZ_SI, &magic, 2) != 2)
 | 
			
		||||
        goto fail;
 | 
			
		||||
 | 
			
		||||
    if (magic[0] != DZ_MAGIC1 || magic[1] != DZ_MAGIC2) {
 | 
			
		||||
        err = "Not a dictzip file (missing extra magic)";
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* DictZip version */
 | 
			
		||||
    if (bdrv_pread(s->hd, GZ_VERSION, &header_ver, 2) != 2)
 | 
			
		||||
        goto fail;
 | 
			
		||||
 | 
			
		||||
    header_ver = le16_to_cpu(header_ver);
 | 
			
		||||
 | 
			
		||||
    switch (header_ver) {
 | 
			
		||||
        case 1: /* Normal DictZip */
 | 
			
		||||
            /* number of chunks */
 | 
			
		||||
            if (bdrv_pread(s->hd, GZ_CHUNKSIZE, &chunk_len16, 2) != 2)
 | 
			
		||||
                goto fail;
 | 
			
		||||
 | 
			
		||||
            s->chunk_len = le16_to_cpu(chunk_len16);
 | 
			
		||||
 | 
			
		||||
            /* chunk count */
 | 
			
		||||
            if (bdrv_pread(s->hd, GZ_CHUNKCNT, &chunk_cnt16, 2) != 2)
 | 
			
		||||
                goto fail;
 | 
			
		||||
 | 
			
		||||
            s->chunk_cnt = le16_to_cpu(chunk_cnt16);
 | 
			
		||||
            chunks_len = sizeof(short) * s->chunk_cnt;
 | 
			
		||||
            rnd_offs = GZ_RNDDATA;
 | 
			
		||||
            break;
 | 
			
		||||
        case 99: /* Special Alex pigz version */
 | 
			
		||||
            /* number of chunks */
 | 
			
		||||
            if (bdrv_pread(s->hd, GZ_99_CHUNKSIZE, &chunk_len32, 4) != 4)
 | 
			
		||||
                goto fail;
 | 
			
		||||
 | 
			
		||||
            dprintf("chunk len [%#x] = %d\n", GZ_99_CHUNKSIZE, chunk_len32);
 | 
			
		||||
            s->chunk_len = le32_to_cpu(chunk_len32);
 | 
			
		||||
 | 
			
		||||
            /* chunk count */
 | 
			
		||||
            if (bdrv_pread(s->hd, GZ_99_CHUNKCNT, &s->chunk_cnt, 4) != 4)
 | 
			
		||||
                goto fail;
 | 
			
		||||
 | 
			
		||||
            s->chunk_cnt = le32_to_cpu(s->chunk_cnt);
 | 
			
		||||
 | 
			
		||||
            dprintf("chunk len | count = %"PRId64" | %d\n", s->chunk_len, s->chunk_cnt);
 | 
			
		||||
 | 
			
		||||
            /* file size */
 | 
			
		||||
            if (bdrv_pread(s->hd, GZ_99_FILESIZE, &s->file_len, 8) != 8)
 | 
			
		||||
                goto fail;
 | 
			
		||||
 | 
			
		||||
            s->file_len = le64_to_cpu(s->file_len);
 | 
			
		||||
            chunks_len = sizeof(int) * s->chunk_cnt;
 | 
			
		||||
            rnd_offs = GZ_99_RNDDATA;
 | 
			
		||||
            break;
 | 
			
		||||
        default:
 | 
			
		||||
            err = "Invalid DictZip version";
 | 
			
		||||
            goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* random access data */
 | 
			
		||||
    s->chunks = g_malloc(chunks_len);
 | 
			
		||||
    if (header_ver == 99)
 | 
			
		||||
        s->chunks32 = (uint32_t *)s->chunks;
 | 
			
		||||
 | 
			
		||||
    if (bdrv_pread(s->hd, rnd_offs, s->chunks, chunks_len) != chunks_len)
 | 
			
		||||
        goto fail;
 | 
			
		||||
 | 
			
		||||
    /* orig filename */
 | 
			
		||||
    if (header_flags & GZ_FNAME) {
 | 
			
		||||
        if (bdrv_pread(s->hd, headerLength + 1, buf, sizeof(buf)) != sizeof(buf))
 | 
			
		||||
            goto fail;
 | 
			
		||||
 | 
			
		||||
        buf[sizeof(buf) - 1] = '\0';
 | 
			
		||||
        headerLength += strlen(buf) + 1;
 | 
			
		||||
 | 
			
		||||
        if (strlen(buf) == sizeof(buf))
 | 
			
		||||
            goto fail;
 | 
			
		||||
 | 
			
		||||
        dprintf("filename: %s\n", buf);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* comment field */
 | 
			
		||||
    if (header_flags & GZ_COMMENT) {
 | 
			
		||||
        if (bdrv_pread(s->hd, headerLength, buf, sizeof(buf)) != sizeof(buf))
 | 
			
		||||
            goto fail;
 | 
			
		||||
 | 
			
		||||
        buf[sizeof(buf) - 1] = '\0';
 | 
			
		||||
        headerLength += strlen(buf) + 1;
 | 
			
		||||
 | 
			
		||||
        if (strlen(buf) == sizeof(buf))
 | 
			
		||||
            goto fail;
 | 
			
		||||
 | 
			
		||||
        dprintf("comment: %s\n", buf);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (header_flags & GZ_FHCRC)
 | 
			
		||||
        headerLength += 2;
 | 
			
		||||
 | 
			
		||||
    /* uncompressed file length*/
 | 
			
		||||
    if (!s->file_len) {
 | 
			
		||||
        uint32_t file_len;
 | 
			
		||||
 | 
			
		||||
        if (bdrv_pread(s->hd, bdrv_getlength(s->hd) - 4, &file_len, 4) != 4)
 | 
			
		||||
            goto fail;
 | 
			
		||||
 | 
			
		||||
        s->file_len = le32_to_cpu(file_len);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* compute offsets */
 | 
			
		||||
    s->offsets = g_malloc(sizeof( *s->offsets ) * s->chunk_cnt);
 | 
			
		||||
 | 
			
		||||
    for (offset = headerLength + 1, i = 0; i < s->chunk_cnt; i++) {
 | 
			
		||||
        s->offsets[i] = offset;
 | 
			
		||||
        switch (header_ver) {
 | 
			
		||||
        case 1:
 | 
			
		||||
            offset += le16_to_cpu(s->chunks[i]);
 | 
			
		||||
            break;
 | 
			
		||||
        case 99:
 | 
			
		||||
            offset += le32_to_cpu(s->chunks32[i]);
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        dprintf("chunk %#"PRIx64" - %#"PRIx64" = offset %#"PRIx64" -> %#"PRIx64"\n", i * s->chunk_len, (i+1) * s->chunk_len, s->offsets[i], offset);
 | 
			
		||||
    }
 | 
			
		||||
    qemu_opts_del(opts);
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
 | 
			
		||||
fail:
 | 
			
		||||
    fprintf(stderr, "DictZip: Error opening file: %s\n", err);
 | 
			
		||||
    bdrv_unref(s->hd);
 | 
			
		||||
    if (s->chunks)
 | 
			
		||||
        g_free(s->chunks);
 | 
			
		||||
    qemu_opts_del(opts);
 | 
			
		||||
    return -EINVAL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* This callback gets invoked when we have the result in cache already */
 | 
			
		||||
static void dictzip_cache_cb(void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    DictZipAIOCB *acb = (DictZipAIOCB *)opaque;
 | 
			
		||||
 | 
			
		||||
    qemu_iovec_from_buf(acb->qiov, 0, acb->buf, acb->len);
 | 
			
		||||
    acb->common.cb(acb->common.opaque, 0);
 | 
			
		||||
    qemu_bh_delete(acb->bh);
 | 
			
		||||
    qemu_aio_unref(acb);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* This callback gets invoked by the underlying block reader when we have
 | 
			
		||||
 * all compressed data. We uncompress in here. */
 | 
			
		||||
static void dictzip_read_cb(void *opaque, int ret)
 | 
			
		||||
{
 | 
			
		||||
    DictZipAIOCB *acb = (DictZipAIOCB *)opaque;
 | 
			
		||||
    struct BDRVDictZipState *s = acb->s;
 | 
			
		||||
    uint8_t *buf;
 | 
			
		||||
    DictCache *cache;
 | 
			
		||||
    int r, i;
 | 
			
		||||
 | 
			
		||||
    buf = g_malloc(acb->chunks_len);
 | 
			
		||||
 | 
			
		||||
    /* try to find zlib stream for decoding */
 | 
			
		||||
    do {
 | 
			
		||||
        for (i = 0; i < Z_STREAM_COUNT; i++) {
 | 
			
		||||
            if (!(s->stream_in_use & (1 << i))) {
 | 
			
		||||
                s->stream_in_use |= (1 << i);
 | 
			
		||||
                acb->zStream_id = i;
 | 
			
		||||
                acb->zStream = &s->zStream[i];
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    } while(!acb->zStream);
 | 
			
		||||
 | 
			
		||||
    /* sure, we could handle more streams, but this callback should be single
 | 
			
		||||
       threaded and when it's not, we really want to know! */
 | 
			
		||||
    assert(i == 0);
 | 
			
		||||
 | 
			
		||||
    /* uncompress the chunk */
 | 
			
		||||
    acb->zStream->next_in   = acb->gzipped;
 | 
			
		||||
    acb->zStream->avail_in  = acb->gz_len;
 | 
			
		||||
    acb->zStream->next_out  = buf;
 | 
			
		||||
    acb->zStream->avail_out = acb->chunks_len;
 | 
			
		||||
 | 
			
		||||
    r = inflate( acb->zStream,  Z_PARTIAL_FLUSH );
 | 
			
		||||
    if ( (r != Z_OK) && (r != Z_STREAM_END) )
 | 
			
		||||
        fprintf(stderr, "Error inflating: [%d] %s\n", r, acb->zStream->msg);
 | 
			
		||||
 | 
			
		||||
    if ( r == Z_STREAM_END )
 | 
			
		||||
        inflateReset(acb->zStream);
 | 
			
		||||
 | 
			
		||||
    dprintf("inflating [%d] left: %d | %d bytes\n", r, acb->zStream->avail_in, acb->zStream->avail_out);
 | 
			
		||||
    s->stream_in_use &= ~(1 << acb->zStream_id);
 | 
			
		||||
 | 
			
		||||
    /* nofity the caller */
 | 
			
		||||
    qemu_iovec_from_buf(acb->qiov, 0, buf + acb->offset, acb->len);
 | 
			
		||||
    acb->common.cb(acb->common.opaque, 0);
 | 
			
		||||
 | 
			
		||||
    /* fill the cache */
 | 
			
		||||
    cache = &s->cache[s->cache_index];
 | 
			
		||||
    s->cache_index++;
 | 
			
		||||
    if (s->cache_index == CACHE_COUNT)
 | 
			
		||||
        s->cache_index = 0;
 | 
			
		||||
 | 
			
		||||
    cache->len = 0;
 | 
			
		||||
    if (cache->buf)
 | 
			
		||||
        g_free(cache->buf);
 | 
			
		||||
    cache->start = acb->gz_start;
 | 
			
		||||
    cache->buf = buf;
 | 
			
		||||
    cache->len = acb->chunks_len;
 | 
			
		||||
 | 
			
		||||
    /* free occupied ressources */
 | 
			
		||||
    g_free(acb->qiov_gz);
 | 
			
		||||
    qemu_aio_unref(acb);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const AIOCBInfo dictzip_aiocb_info = {
 | 
			
		||||
    .aiocb_size         = sizeof(DictZipAIOCB),
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/* This is where we get a request from a caller to read something */
 | 
			
		||||
static BlockAIOCB *dictzip_aio_readv(BlockDriverState *bs,
 | 
			
		||||
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
 | 
			
		||||
        BlockCompletionFunc *cb, void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    BDRVDictZipState *s = bs->opaque;
 | 
			
		||||
    DictZipAIOCB *acb;
 | 
			
		||||
    QEMUIOVector *qiov_gz;
 | 
			
		||||
    struct iovec *iov;
 | 
			
		||||
    uint8_t *buf;
 | 
			
		||||
    size_t  start = sector_num * SECTOR_SIZE;
 | 
			
		||||
    size_t  len = nb_sectors * SECTOR_SIZE;
 | 
			
		||||
    size_t  end = start + len;
 | 
			
		||||
    size_t  gz_start;
 | 
			
		||||
    size_t  gz_len;
 | 
			
		||||
    int64_t gz_sector_num;
 | 
			
		||||
    int     gz_nb_sectors;
 | 
			
		||||
    int     first_chunk, last_chunk;
 | 
			
		||||
    int     first_offset;
 | 
			
		||||
    int     i;
 | 
			
		||||
 | 
			
		||||
    acb = qemu_aio_get(&dictzip_aiocb_info, bs, cb, opaque);
 | 
			
		||||
    if (!acb)
 | 
			
		||||
        return NULL;
 | 
			
		||||
 | 
			
		||||
    /* Search Cache */
 | 
			
		||||
    for (i = 0; i < CACHE_COUNT; i++) {
 | 
			
		||||
        if (!s->cache[i].len)
 | 
			
		||||
            continue;
 | 
			
		||||
 | 
			
		||||
        if ((start >= s->cache[i].start) &&
 | 
			
		||||
            (end <= (s->cache[i].start + s->cache[i].len))) {
 | 
			
		||||
            acb->buf = s->cache[i].buf + (start - s->cache[i].start);
 | 
			
		||||
            acb->len = len;
 | 
			
		||||
            acb->qiov = qiov;
 | 
			
		||||
            acb->bh = qemu_bh_new(dictzip_cache_cb, acb);
 | 
			
		||||
            qemu_bh_schedule(acb->bh);
 | 
			
		||||
 | 
			
		||||
            return &acb->common;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* No cache, so let's decode */
 | 
			
		||||
    /* We need to read these chunks */
 | 
			
		||||
    first_chunk  = start / s->chunk_len;
 | 
			
		||||
    first_offset = start - first_chunk * s->chunk_len;
 | 
			
		||||
    last_chunk   = end / s->chunk_len;
 | 
			
		||||
 | 
			
		||||
    gz_start = s->offsets[first_chunk];
 | 
			
		||||
    gz_len = 0;
 | 
			
		||||
    for (i = first_chunk; i <= last_chunk; i++) {
 | 
			
		||||
        if (s->chunks32)
 | 
			
		||||
            gz_len += le32_to_cpu(s->chunks32[i]);
 | 
			
		||||
        else
 | 
			
		||||
            gz_len += le16_to_cpu(s->chunks[i]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    gz_sector_num = gz_start / SECTOR_SIZE;
 | 
			
		||||
    gz_nb_sectors = (gz_len / SECTOR_SIZE);
 | 
			
		||||
 | 
			
		||||
    /* account for tail and heads */
 | 
			
		||||
    while ((gz_start + gz_len) > ((gz_sector_num + gz_nb_sectors) * SECTOR_SIZE))
 | 
			
		||||
        gz_nb_sectors++;
 | 
			
		||||
 | 
			
		||||
    /* Allocate qiov, iov and buf in one chunk so we only need to free qiov */
 | 
			
		||||
    qiov_gz = g_malloc0(sizeof(QEMUIOVector) + sizeof(struct iovec) +
 | 
			
		||||
                           (gz_nb_sectors * SECTOR_SIZE));
 | 
			
		||||
    iov = (struct iovec *)(((char *)qiov_gz) + sizeof(QEMUIOVector));
 | 
			
		||||
    buf = ((uint8_t *)iov) + sizeof(struct iovec *);
 | 
			
		||||
 | 
			
		||||
    /* Kick off the read by the backing file, so we can start decompressing */
 | 
			
		||||
    iov->iov_base = (void *)buf;
 | 
			
		||||
    iov->iov_len = gz_nb_sectors * 512;
 | 
			
		||||
    qemu_iovec_init_external(qiov_gz, iov, 1);
 | 
			
		||||
 | 
			
		||||
    dprintf("read %zd - %zd => %zd - %zd\n", start, end, gz_start, gz_start + gz_len);
 | 
			
		||||
 | 
			
		||||
    acb->s = s;
 | 
			
		||||
    acb->qiov = qiov;
 | 
			
		||||
    acb->qiov_gz = qiov_gz;
 | 
			
		||||
    acb->start = start;
 | 
			
		||||
    acb->len = len;
 | 
			
		||||
    acb->gzipped = buf + (gz_start % SECTOR_SIZE);
 | 
			
		||||
    acb->gz_len = gz_len;
 | 
			
		||||
    acb->gz_start = first_chunk * s->chunk_len;
 | 
			
		||||
    acb->offset = first_offset;
 | 
			
		||||
    acb->chunks_len = (last_chunk - first_chunk + 1) * s->chunk_len;
 | 
			
		||||
 | 
			
		||||
    return bdrv_aio_readv(s->hd, gz_sector_num, qiov_gz, gz_nb_sectors,
 | 
			
		||||
                          dictzip_read_cb, acb);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void dictzip_close(BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
    BDRVDictZipState *s = bs->opaque;
 | 
			
		||||
    int i;
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < CACHE_COUNT; i++) {
 | 
			
		||||
        if (!s->cache[i].len)
 | 
			
		||||
            continue;
 | 
			
		||||
 | 
			
		||||
        g_free(s->cache[i].buf);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < Z_STREAM_COUNT; i++) {
 | 
			
		||||
        inflateEnd(&s->zStream[i]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (s->chunks)
 | 
			
		||||
        g_free(s->chunks);
 | 
			
		||||
 | 
			
		||||
    if (s->offsets)
 | 
			
		||||
        g_free(s->offsets);
 | 
			
		||||
 | 
			
		||||
    dprintf("Close\n");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int64_t dictzip_getlength(BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
    BDRVDictZipState *s = bs->opaque;
 | 
			
		||||
    dprintf("getlength -> %ld\n", s->file_len);
 | 
			
		||||
    return s->file_len;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static BlockDriver bdrv_dictzip = {
 | 
			
		||||
    .format_name     = "dzip",
 | 
			
		||||
    .protocol_name   = "dzip",
 | 
			
		||||
 | 
			
		||||
    .instance_size   = sizeof(BDRVDictZipState),
 | 
			
		||||
    .bdrv_file_open  = dictzip_open,
 | 
			
		||||
    .bdrv_close      = dictzip_close,
 | 
			
		||||
    .bdrv_getlength  = dictzip_getlength,
 | 
			
		||||
 | 
			
		||||
    .bdrv_aio_readv  = dictzip_aio_readv,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void dictzip_block_init(void)
 | 
			
		||||
{
 | 
			
		||||
    bdrv_register(&bdrv_dictzip);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
block_init(dictzip_block_init);
 | 
			
		||||
@@ -24,7 +24,6 @@
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
#include "qemu/bswap.h"
 | 
			
		||||
#include "qemu/error-report.h"
 | 
			
		||||
#include "qemu/module.h"
 | 
			
		||||
#include <zlib.h>
 | 
			
		||||
#ifdef CONFIG_BZIP2
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2610
									
								
								block/io.c
									
									
									
									
									
								
							
							
						
						
									
										2610
									
								
								block/io.c
									
									
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										165
									
								
								block/iscsi.c
									
									
									
									
									
								
							
							
						
						
									
										165
									
								
								block/iscsi.c
									
									
									
									
									
								
							@@ -2,7 +2,7 @@
 | 
			
		||||
 * QEMU Block driver for iSCSI images
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2010-2011 Ronnie Sahlberg <ronniesahlberg@gmail.com>
 | 
			
		||||
 * Copyright (c) 2012-2015 Peter Lieven <pl@kamp.de>
 | 
			
		||||
 * Copyright (c) 2012-2014 Peter Lieven <pl@kamp.de>
 | 
			
		||||
 *
 | 
			
		||||
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
			
		||||
 * of this software and associated documentation files (the "Software"), to deal
 | 
			
		||||
@@ -38,7 +38,6 @@
 | 
			
		||||
#include "qemu/iov.h"
 | 
			
		||||
#include "sysemu/sysemu.h"
 | 
			
		||||
#include "qmp-commands.h"
 | 
			
		||||
#include "qapi/qmp/qstring.h"
 | 
			
		||||
 | 
			
		||||
#include <iscsi/iscsi.h>
 | 
			
		||||
#include <iscsi/scsi-lowlevel.h>
 | 
			
		||||
@@ -58,6 +57,9 @@ typedef struct IscsiLun {
 | 
			
		||||
    int events;
 | 
			
		||||
    QEMUTimer *nop_timer;
 | 
			
		||||
    QEMUTimer *event_timer;
 | 
			
		||||
    uint8_t lbpme;
 | 
			
		||||
    uint8_t lbprz;
 | 
			
		||||
    uint8_t has_write_same;
 | 
			
		||||
    struct scsi_inquiry_logical_block_provisioning lbp;
 | 
			
		||||
    struct scsi_inquiry_block_limits bl;
 | 
			
		||||
    unsigned char *zeroblock;
 | 
			
		||||
@@ -65,12 +67,6 @@ typedef struct IscsiLun {
 | 
			
		||||
    int cluster_sectors;
 | 
			
		||||
    bool use_16_for_rw;
 | 
			
		||||
    bool write_protected;
 | 
			
		||||
    bool lbpme;
 | 
			
		||||
    bool lbprz;
 | 
			
		||||
    bool dpofua;
 | 
			
		||||
    bool has_write_same;
 | 
			
		||||
    bool force_next_flush;
 | 
			
		||||
    bool request_timed_out;
 | 
			
		||||
} IscsiLun;
 | 
			
		||||
 | 
			
		||||
typedef struct IscsiTask {
 | 
			
		||||
@@ -83,7 +79,6 @@ typedef struct IscsiTask {
 | 
			
		||||
    QEMUBH *bh;
 | 
			
		||||
    IscsiLun *iscsilun;
 | 
			
		||||
    QEMUTimer retry_timer;
 | 
			
		||||
    bool force_next_flush;
 | 
			
		||||
} IscsiTask;
 | 
			
		||||
 | 
			
		||||
typedef struct IscsiAIOCB {
 | 
			
		||||
@@ -101,12 +96,11 @@ typedef struct IscsiAIOCB {
 | 
			
		||||
#endif
 | 
			
		||||
} IscsiAIOCB;
 | 
			
		||||
 | 
			
		||||
/* libiscsi uses time_t so its enough to process events every second */
 | 
			
		||||
#define EVENT_INTERVAL 1000
 | 
			
		||||
#define EVENT_INTERVAL 250
 | 
			
		||||
#define NOP_INTERVAL 5000
 | 
			
		||||
#define MAX_NOP_FAILURES 3
 | 
			
		||||
#define ISCSI_CMD_RETRIES ARRAY_SIZE(iscsi_retry_times)
 | 
			
		||||
static const unsigned iscsi_retry_times[] = {8, 32, 128, 512, 2048, 8192, 32768};
 | 
			
		||||
static const unsigned iscsi_retry_times[] = {8, 32, 128, 512, 2048};
 | 
			
		||||
 | 
			
		||||
/* this threshold is a trade-off knob to choose between
 | 
			
		||||
 * the potential additional overhead of an extra GET_LBA_STATUS request
 | 
			
		||||
@@ -169,19 +163,6 @@ static inline unsigned exp_random(double mean)
 | 
			
		||||
    return -mean * log((double)rand() / RAND_MAX);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* SCSI_STATUS_TASK_SET_FULL and SCSI_STATUS_TIMEOUT were introduced
 | 
			
		||||
 * in libiscsi 1.10.0 as part of an enum. The LIBISCSI_API_VERSION
 | 
			
		||||
 * macro was introduced in 1.11.0. So use the API_VERSION macro as
 | 
			
		||||
 * a hint that the macros are defined and define them ourselves
 | 
			
		||||
 * otherwise to keep the required libiscsi version at 1.9.0 */
 | 
			
		||||
#if !defined(LIBISCSI_API_VERSION)
 | 
			
		||||
#define QEMU_SCSI_STATUS_TASK_SET_FULL  0x28
 | 
			
		||||
#define QEMU_SCSI_STATUS_TIMEOUT        0x0f000002
 | 
			
		||||
#else
 | 
			
		||||
#define QEMU_SCSI_STATUS_TASK_SET_FULL  SCSI_STATUS_TASK_SET_FULL
 | 
			
		||||
#define QEMU_SCSI_STATUS_TIMEOUT        SCSI_STATUS_TIMEOUT
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
iscsi_co_generic_cb(struct iscsi_context *iscsi, int status,
 | 
			
		||||
                        void *command_data, void *opaque)
 | 
			
		||||
@@ -202,19 +183,10 @@ iscsi_co_generic_cb(struct iscsi_context *iscsi, int status,
 | 
			
		||||
                iTask->do_retry = 1;
 | 
			
		||||
                goto out;
 | 
			
		||||
            }
 | 
			
		||||
            if (status == SCSI_STATUS_BUSY ||
 | 
			
		||||
                status == QEMU_SCSI_STATUS_TIMEOUT ||
 | 
			
		||||
                status == QEMU_SCSI_STATUS_TASK_SET_FULL) {
 | 
			
		||||
            if (status == SCSI_STATUS_BUSY) {
 | 
			
		||||
                unsigned retry_time =
 | 
			
		||||
                    exp_random(iscsi_retry_times[iTask->retries - 1]);
 | 
			
		||||
                if (status == QEMU_SCSI_STATUS_TIMEOUT) {
 | 
			
		||||
                    /* make sure the request is rescheduled AFTER the
 | 
			
		||||
                     * reconnect is initiated */
 | 
			
		||||
                    retry_time = EVENT_INTERVAL * 2;
 | 
			
		||||
                    iTask->iscsilun->request_timed_out = true;
 | 
			
		||||
                }
 | 
			
		||||
                error_report("iSCSI Busy/TaskSetFull/TimeOut"
 | 
			
		||||
                             " (retry #%u in %u ms): %s",
 | 
			
		||||
                error_report("iSCSI Busy (retry #%u in %u ms): %s",
 | 
			
		||||
                             iTask->retries, retry_time,
 | 
			
		||||
                             iscsi_get_error(iscsi));
 | 
			
		||||
                aio_timer_init(iTask->iscsilun->aio_context,
 | 
			
		||||
@@ -227,8 +199,6 @@ iscsi_co_generic_cb(struct iscsi_context *iscsi, int status,
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        error_report("iSCSI Failure: %s", iscsi_get_error(iscsi));
 | 
			
		||||
    } else {
 | 
			
		||||
        iTask->iscsilun->force_next_flush |= iTask->force_next_flush;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
out:
 | 
			
		||||
@@ -298,26 +268,20 @@ iscsi_set_events(IscsiLun *iscsilun)
 | 
			
		||||
                           iscsilun);
 | 
			
		||||
        iscsilun->events = ev;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* newer versions of libiscsi may return zero events. In this
 | 
			
		||||
     * case start a timer to ensure we are able to return to service
 | 
			
		||||
     * once this situation changes. */
 | 
			
		||||
    if (!ev) {
 | 
			
		||||
        timer_mod(iscsilun->event_timer,
 | 
			
		||||
                  qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void iscsi_timed_check_events(void *opaque)
 | 
			
		||||
static void iscsi_timed_set_events(void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    IscsiLun *iscsilun = opaque;
 | 
			
		||||
 | 
			
		||||
    /* check for timed out requests */
 | 
			
		||||
    iscsi_service(iscsilun->iscsi, 0);
 | 
			
		||||
 | 
			
		||||
    if (iscsilun->request_timed_out) {
 | 
			
		||||
        iscsilun->request_timed_out = false;
 | 
			
		||||
        iscsi_reconnect(iscsilun->iscsi);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* newer versions of libiscsi may return zero events. Ensure we are able
 | 
			
		||||
     * to return to service once this situation changes. */
 | 
			
		||||
    iscsi_set_events(iscsilun);
 | 
			
		||||
 | 
			
		||||
    timer_mod(iscsilun->event_timer,
 | 
			
		||||
              qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
@@ -405,7 +369,6 @@ static int coroutine_fn iscsi_co_writev(BlockDriverState *bs,
 | 
			
		||||
    struct IscsiTask iTask;
 | 
			
		||||
    uint64_t lba;
 | 
			
		||||
    uint32_t num_sectors;
 | 
			
		||||
    int fua;
 | 
			
		||||
 | 
			
		||||
    if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
 | 
			
		||||
        return -EINVAL;
 | 
			
		||||
@@ -421,17 +384,15 @@ static int coroutine_fn iscsi_co_writev(BlockDriverState *bs,
 | 
			
		||||
    num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
 | 
			
		||||
    iscsi_co_init_iscsitask(iscsilun, &iTask);
 | 
			
		||||
retry:
 | 
			
		||||
    fua = iscsilun->dpofua && !bs->enable_write_cache;
 | 
			
		||||
    iTask.force_next_flush = !fua;
 | 
			
		||||
    if (iscsilun->use_16_for_rw) {
 | 
			
		||||
        iTask.task = iscsi_write16_task(iscsilun->iscsi, iscsilun->lun, lba,
 | 
			
		||||
                                        NULL, num_sectors * iscsilun->block_size,
 | 
			
		||||
                                        iscsilun->block_size, 0, 0, fua, 0, 0,
 | 
			
		||||
                                        iscsilun->block_size, 0, 0, 0, 0, 0,
 | 
			
		||||
                                        iscsi_co_generic_cb, &iTask);
 | 
			
		||||
    } else {
 | 
			
		||||
        iTask.task = iscsi_write10_task(iscsilun->iscsi, iscsilun->lun, lba,
 | 
			
		||||
                                        NULL, num_sectors * iscsilun->block_size,
 | 
			
		||||
                                        iscsilun->block_size, 0, 0, fua, 0, 0,
 | 
			
		||||
                                        iscsilun->block_size, 0, 0, 0, 0, 0,
 | 
			
		||||
                                        iscsi_co_generic_cb, &iTask);
 | 
			
		||||
    }
 | 
			
		||||
    if (iTask.task == NULL) {
 | 
			
		||||
@@ -499,7 +460,7 @@ static int64_t coroutine_fn iscsi_co_get_block_status(BlockDriverState *bs,
 | 
			
		||||
    *pnum = nb_sectors;
 | 
			
		||||
 | 
			
		||||
    /* LUN does not support logical block provisioning */
 | 
			
		||||
    if (!iscsilun->lbpme) {
 | 
			
		||||
    if (iscsilun->lbpme == 0) {
 | 
			
		||||
        goto out;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -655,12 +616,12 @@ static int coroutine_fn iscsi_co_flush(BlockDriverState *bs)
 | 
			
		||||
    IscsiLun *iscsilun = bs->opaque;
 | 
			
		||||
    struct IscsiTask iTask;
 | 
			
		||||
 | 
			
		||||
    if (!iscsilun->force_next_flush) {
 | 
			
		||||
    if (bs->sg) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
    iscsilun->force_next_flush = false;
 | 
			
		||||
 | 
			
		||||
    iscsi_co_init_iscsitask(iscsilun, &iTask);
 | 
			
		||||
 | 
			
		||||
retry:
 | 
			
		||||
    if (iscsi_synchronizecache10_task(iscsilun->iscsi, iscsilun->lun, 0, 0, 0,
 | 
			
		||||
                                      0, iscsi_co_generic_cb, &iTask) == NULL) {
 | 
			
		||||
@@ -956,7 +917,6 @@ coroutine_fn iscsi_co_write_zeroes(BlockDriverState *bs, int64_t sector_num,
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    iscsi_co_init_iscsitask(iscsilun, &iTask);
 | 
			
		||||
    iTask.force_next_flush = true;
 | 
			
		||||
retry:
 | 
			
		||||
    if (use_16_for_ws) {
 | 
			
		||||
        iTask.task = iscsi_writesame16_task(iscsilun->iscsi, iscsilun->lun, lba,
 | 
			
		||||
@@ -1120,37 +1080,16 @@ static char *parse_initiator_name(const char *target)
 | 
			
		||||
    return iscsi_name;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int parse_timeout(const char *target)
 | 
			
		||||
{
 | 
			
		||||
    QemuOptsList *list;
 | 
			
		||||
    QemuOpts *opts;
 | 
			
		||||
    const char *timeout;
 | 
			
		||||
 | 
			
		||||
    list = qemu_find_opts("iscsi");
 | 
			
		||||
    if (list) {
 | 
			
		||||
        opts = qemu_opts_find(list, target);
 | 
			
		||||
        if (!opts) {
 | 
			
		||||
            opts = QTAILQ_FIRST(&list->head);
 | 
			
		||||
        }
 | 
			
		||||
        if (opts) {
 | 
			
		||||
            timeout = qemu_opt_get(opts, "timeout");
 | 
			
		||||
            if (timeout) {
 | 
			
		||||
                return atoi(timeout);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void iscsi_nop_timed_event(void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    IscsiLun *iscsilun = opaque;
 | 
			
		||||
 | 
			
		||||
    if (iscsi_get_nops_in_flight(iscsilun->iscsi) >= MAX_NOP_FAILURES) {
 | 
			
		||||
    if (iscsi_get_nops_in_flight(iscsilun->iscsi) > MAX_NOP_FAILURES) {
 | 
			
		||||
        error_report("iSCSI: NOP timeout. Reconnecting...");
 | 
			
		||||
        iscsilun->request_timed_out = true;
 | 
			
		||||
    } else if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) {
 | 
			
		||||
        iscsi_reconnect(iscsilun->iscsi);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) {
 | 
			
		||||
        error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
@@ -1182,8 +1121,8 @@ static void iscsi_readcapacity_sync(IscsiLun *iscsilun, Error **errp)
 | 
			
		||||
                } else {
 | 
			
		||||
                    iscsilun->block_size = rc16->block_length;
 | 
			
		||||
                    iscsilun->num_blocks = rc16->returned_lba + 1;
 | 
			
		||||
                    iscsilun->lbpme = !!rc16->lbpme;
 | 
			
		||||
                    iscsilun->lbprz = !!rc16->lbprz;
 | 
			
		||||
                    iscsilun->lbpme = rc16->lbpme;
 | 
			
		||||
                    iscsilun->lbprz = rc16->lbprz;
 | 
			
		||||
                    iscsilun->use_16_for_rw = (rc16->returned_lba > 0xffffffff);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
@@ -1308,21 +1247,17 @@ static void iscsi_attach_aio_context(BlockDriverState *bs,
 | 
			
		||||
    timer_mod(iscsilun->nop_timer,
 | 
			
		||||
              qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
 | 
			
		||||
 | 
			
		||||
    /* Set up a timer for periodic calls to iscsi_set_events and to
 | 
			
		||||
     * scan for command timeout */
 | 
			
		||||
    /* Prepare a timer for a delayed call to iscsi_set_events */
 | 
			
		||||
    iscsilun->event_timer = aio_timer_new(iscsilun->aio_context,
 | 
			
		||||
                                          QEMU_CLOCK_REALTIME, SCALE_MS,
 | 
			
		||||
                                          iscsi_timed_check_events, iscsilun);
 | 
			
		||||
    timer_mod(iscsilun->event_timer,
 | 
			
		||||
              qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL);
 | 
			
		||||
                                          iscsi_timed_set_events, iscsilun);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void iscsi_modesense_sync(IscsiLun *iscsilun)
 | 
			
		||||
static bool iscsi_is_write_protected(IscsiLun *iscsilun)
 | 
			
		||||
{
 | 
			
		||||
    struct scsi_task *task;
 | 
			
		||||
    struct scsi_mode_sense *ms = NULL;
 | 
			
		||||
    iscsilun->write_protected = false;
 | 
			
		||||
    iscsilun->dpofua = false;
 | 
			
		||||
    bool wrprotected = false;
 | 
			
		||||
 | 
			
		||||
    task = iscsi_modesense6_sync(iscsilun->iscsi, iscsilun->lun,
 | 
			
		||||
                                 1, SCSI_MODESENSE_PC_CURRENT,
 | 
			
		||||
@@ -1343,13 +1278,13 @@ static void iscsi_modesense_sync(IscsiLun *iscsilun)
 | 
			
		||||
                     iscsi_get_error(iscsilun->iscsi));
 | 
			
		||||
        goto out;
 | 
			
		||||
    }
 | 
			
		||||
    iscsilun->write_protected = ms->device_specific_parameter & 0x80;
 | 
			
		||||
    iscsilun->dpofua          = ms->device_specific_parameter & 0x10;
 | 
			
		||||
    wrprotected = ms->device_specific_parameter & 0x80;
 | 
			
		||||
 | 
			
		||||
out:
 | 
			
		||||
    if (task) {
 | 
			
		||||
        scsi_free_scsi_task(task);
 | 
			
		||||
    }
 | 
			
		||||
    return wrprotected;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
@@ -1369,7 +1304,14 @@ static int iscsi_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
    QemuOpts *opts;
 | 
			
		||||
    Error *local_err = NULL;
 | 
			
		||||
    const char *filename;
 | 
			
		||||
    int i, ret = 0, timeout = 0;
 | 
			
		||||
    int i, ret = 0;
 | 
			
		||||
 | 
			
		||||
    if ((BDRV_SECTOR_SIZE % 512) != 0) {
 | 
			
		||||
        error_setg(errp, "iSCSI: Invalid BDRV_SECTOR_SIZE. "
 | 
			
		||||
                   "BDRV_SECTOR_SIZE(%lld) is not a multiple "
 | 
			
		||||
                   "of 512", BDRV_SECTOR_SIZE);
 | 
			
		||||
        return -EINVAL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
 | 
			
		||||
    qemu_opts_absorb_qdict(opts, options, &local_err);
 | 
			
		||||
@@ -1439,16 +1381,6 @@ static int iscsi_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
        goto out;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* timeout handling is broken in libiscsi before 1.15.0 */
 | 
			
		||||
    timeout = parse_timeout(iscsi_url->target);
 | 
			
		||||
#if defined(LIBISCSI_API_VERSION) && LIBISCSI_API_VERSION >= 20150621
 | 
			
		||||
    iscsi_set_timeout(iscsi, timeout);
 | 
			
		||||
#else
 | 
			
		||||
    if (timeout) {
 | 
			
		||||
        error_report("iSCSI: ignoring timeout value for libiscsi <1.15.0");
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) {
 | 
			
		||||
        error_setg(errp, "iSCSI: Failed to connect to LUN : %s",
 | 
			
		||||
            iscsi_get_error(iscsi));
 | 
			
		||||
@@ -1471,8 +1403,7 @@ static int iscsi_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
    scsi_free_scsi_task(task);
 | 
			
		||||
    task = NULL;
 | 
			
		||||
 | 
			
		||||
    iscsi_modesense_sync(iscsilun);
 | 
			
		||||
 | 
			
		||||
    iscsilun->write_protected = iscsi_is_write_protected(iscsilun);
 | 
			
		||||
    /* Check the write protect flag of the LUN if we want to write */
 | 
			
		||||
    if (iscsilun->type == TYPE_DISK && (flags & BDRV_O_RDWR) &&
 | 
			
		||||
        iscsilun->write_protected) {
 | 
			
		||||
@@ -1550,7 +1481,7 @@ static int iscsi_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
        iscsilun->bl.opt_unmap_gran * iscsilun->block_size <= 16 * 1024 * 1024) {
 | 
			
		||||
        iscsilun->cluster_sectors = (iscsilun->bl.opt_unmap_gran *
 | 
			
		||||
                                     iscsilun->block_size) >> BDRV_SECTOR_BITS;
 | 
			
		||||
        if (iscsilun->lbprz) {
 | 
			
		||||
        if (iscsilun->lbprz && !(bs->open_flags & BDRV_O_NOCACHE)) {
 | 
			
		||||
            iscsilun->allocationmap = iscsi_allocationmap_init(iscsilun);
 | 
			
		||||
            if (iscsilun->allocationmap == NULL) {
 | 
			
		||||
                ret = -ENOMEM;
 | 
			
		||||
@@ -1724,7 +1655,7 @@ out:
 | 
			
		||||
static int iscsi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
 | 
			
		||||
{
 | 
			
		||||
    IscsiLun *iscsilun = bs->opaque;
 | 
			
		||||
    bdi->unallocated_blocks_are_zero = iscsilun->lbprz;
 | 
			
		||||
    bdi->unallocated_blocks_are_zero = !!iscsilun->lbprz;
 | 
			
		||||
    bdi->can_write_zeroes_with_unmap = iscsilun->lbprz && iscsilun->lbp.lbpws;
 | 
			
		||||
    bdi->cluster_size = iscsilun->cluster_sectors * BDRV_SECTOR_SIZE;
 | 
			
		||||
    return 0;
 | 
			
		||||
@@ -1797,10 +1728,6 @@ static QemuOptsList qemu_iscsi_opts = {
 | 
			
		||||
            .name = "initiator-name",
 | 
			
		||||
            .type = QEMU_OPT_STRING,
 | 
			
		||||
            .help = "Initiator iqn name to use when connecting",
 | 
			
		||||
        },{
 | 
			
		||||
            .name = "timeout",
 | 
			
		||||
            .type = QEMU_OPT_NUMBER,
 | 
			
		||||
            .help = "Request timeout in seconds (default 0 = no timeout)",
 | 
			
		||||
        },
 | 
			
		||||
        { /* end of list */ }
 | 
			
		||||
    },
 | 
			
		||||
 
 | 
			
		||||
@@ -14,7 +14,6 @@
 | 
			
		||||
#include "trace.h"
 | 
			
		||||
#include "block/blockjob.h"
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
#include "qapi/qmp/qerror.h"
 | 
			
		||||
#include "qemu/ratelimit.h"
 | 
			
		||||
#include "qemu/bitmap.h"
 | 
			
		||||
 | 
			
		||||
@@ -127,9 +126,11 @@ static void mirror_write_complete(void *opaque, int ret)
 | 
			
		||||
    MirrorOp *op = opaque;
 | 
			
		||||
    MirrorBlockJob *s = op->s;
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        BlockDriverState *source = s->common.bs;
 | 
			
		||||
        BlockErrorAction action;
 | 
			
		||||
 | 
			
		||||
        bdrv_set_dirty_bitmap(s->dirty_bitmap, op->sector_num, op->nb_sectors);
 | 
			
		||||
        bdrv_set_dirty_bitmap(source, s->dirty_bitmap, op->sector_num,
 | 
			
		||||
                              op->nb_sectors);
 | 
			
		||||
        action = mirror_error_action(s, false, -ret);
 | 
			
		||||
        if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) {
 | 
			
		||||
            s->ret = ret;
 | 
			
		||||
@@ -143,9 +144,11 @@ static void mirror_read_complete(void *opaque, int ret)
 | 
			
		||||
    MirrorOp *op = opaque;
 | 
			
		||||
    MirrorBlockJob *s = op->s;
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        BlockDriverState *source = s->common.bs;
 | 
			
		||||
        BlockErrorAction action;
 | 
			
		||||
 | 
			
		||||
        bdrv_set_dirty_bitmap(s->dirty_bitmap, op->sector_num, op->nb_sectors);
 | 
			
		||||
        bdrv_set_dirty_bitmap(source, s->dirty_bitmap, op->sector_num,
 | 
			
		||||
                              op->nb_sectors);
 | 
			
		||||
        action = mirror_error_action(s, true, -ret);
 | 
			
		||||
        if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) {
 | 
			
		||||
            s->ret = ret;
 | 
			
		||||
@@ -170,9 +173,10 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
 | 
			
		||||
 | 
			
		||||
    s->sector_num = hbitmap_iter_next(&s->hbi);
 | 
			
		||||
    if (s->sector_num < 0) {
 | 
			
		||||
        bdrv_dirty_iter_init(s->dirty_bitmap, &s->hbi);
 | 
			
		||||
        bdrv_dirty_iter_init(source, s->dirty_bitmap, &s->hbi);
 | 
			
		||||
        s->sector_num = hbitmap_iter_next(&s->hbi);
 | 
			
		||||
        trace_mirror_restart_iter(s, bdrv_get_dirty_count(s->dirty_bitmap));
 | 
			
		||||
        trace_mirror_restart_iter(s,
 | 
			
		||||
                                  bdrv_get_dirty_count(source, s->dirty_bitmap));
 | 
			
		||||
        assert(s->sector_num >= 0);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -287,7 +291,8 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
 | 
			
		||||
        next_sector += sectors_per_chunk;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    bdrv_reset_dirty_bitmap(s->dirty_bitmap, sector_num, nb_sectors);
 | 
			
		||||
    bdrv_reset_dirty_bitmap(source, s->dirty_bitmap, sector_num,
 | 
			
		||||
                            nb_sectors);
 | 
			
		||||
 | 
			
		||||
    /* Copy the dirty cluster.  */
 | 
			
		||||
    s->in_flight++;
 | 
			
		||||
@@ -458,7 +463,7 @@ static void coroutine_fn mirror_run(void *opaque)
 | 
			
		||||
 | 
			
		||||
            assert(n > 0);
 | 
			
		||||
            if (ret == 1) {
 | 
			
		||||
                bdrv_set_dirty_bitmap(s->dirty_bitmap, sector_num, n);
 | 
			
		||||
                bdrv_set_dirty_bitmap(bs, s->dirty_bitmap, sector_num, n);
 | 
			
		||||
                sector_num = next;
 | 
			
		||||
            } else {
 | 
			
		||||
                sector_num += n;
 | 
			
		||||
@@ -466,7 +471,7 @@ static void coroutine_fn mirror_run(void *opaque)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    bdrv_dirty_iter_init(s->dirty_bitmap, &s->hbi);
 | 
			
		||||
    bdrv_dirty_iter_init(bs, s->dirty_bitmap, &s->hbi);
 | 
			
		||||
    last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
 | 
			
		||||
    for (;;) {
 | 
			
		||||
        uint64_t delay_ns = 0;
 | 
			
		||||
@@ -478,7 +483,7 @@ static void coroutine_fn mirror_run(void *opaque)
 | 
			
		||||
            goto immediate_exit;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        cnt = bdrv_get_dirty_count(s->dirty_bitmap);
 | 
			
		||||
        cnt = bdrv_get_dirty_count(bs, s->dirty_bitmap);
 | 
			
		||||
        /* s->common.offset contains the number of bytes already processed so
 | 
			
		||||
         * far, cnt is the number of dirty sectors remaining and
 | 
			
		||||
         * s->sectors_in_flight is the number of sectors currently being
 | 
			
		||||
@@ -487,7 +492,7 @@ static void coroutine_fn mirror_run(void *opaque)
 | 
			
		||||
                        (cnt + s->sectors_in_flight) * BDRV_SECTOR_SIZE;
 | 
			
		||||
 | 
			
		||||
        /* Note that even when no rate limit is applied we need to yield
 | 
			
		||||
         * periodically with no pending I/O so that bdrv_drain_all() returns.
 | 
			
		||||
         * periodically with no pending I/O so that qemu_aio_flush() returns.
 | 
			
		||||
         * We do so every SLICE_TIME nanoseconds, or when there is an error,
 | 
			
		||||
         * or when the source is clean, whichever comes first.
 | 
			
		||||
         */
 | 
			
		||||
@@ -500,6 +505,9 @@ static void coroutine_fn mirror_run(void *opaque)
 | 
			
		||||
                continue;
 | 
			
		||||
            } else if (cnt != 0) {
 | 
			
		||||
                delay_ns = mirror_iteration(s);
 | 
			
		||||
                if (delay_ns == 0) {
 | 
			
		||||
                    continue;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -525,7 +533,7 @@ static void coroutine_fn mirror_run(void *opaque)
 | 
			
		||||
 | 
			
		||||
                should_complete = s->should_complete ||
 | 
			
		||||
                    block_job_is_cancelled(&s->common);
 | 
			
		||||
                cnt = bdrv_get_dirty_count(s->dirty_bitmap);
 | 
			
		||||
                cnt = bdrv_get_dirty_count(bs, s->dirty_bitmap);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -540,7 +548,7 @@ static void coroutine_fn mirror_run(void *opaque)
 | 
			
		||||
             */
 | 
			
		||||
            trace_mirror_before_drain(s, cnt);
 | 
			
		||||
            bdrv_drain(bs);
 | 
			
		||||
            cnt = bdrv_get_dirty_count(s->dirty_bitmap);
 | 
			
		||||
            cnt = bdrv_get_dirty_count(bs, s->dirty_bitmap);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        ret = 0;
 | 
			
		||||
@@ -591,7 +599,7 @@ static void mirror_set_speed(BlockJob *job, int64_t speed, Error **errp)
 | 
			
		||||
    MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
 | 
			
		||||
 | 
			
		||||
    if (speed < 0) {
 | 
			
		||||
        error_setg(errp, QERR_INVALID_PARAMETER, "speed");
 | 
			
		||||
        error_set(errp, QERR_INVALID_PARAMETER, "speed");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
 | 
			
		||||
@@ -616,8 +624,8 @@ static void mirror_complete(BlockJob *job, Error **errp)
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    if (!s->synced) {
 | 
			
		||||
        error_setg(errp, QERR_BLOCK_JOB_NOT_READY,
 | 
			
		||||
                   bdrv_get_device_name(job->bs));
 | 
			
		||||
        error_set(errp, QERR_BLOCK_JOB_NOT_READY,
 | 
			
		||||
                  bdrv_get_device_name(job->bs));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -643,7 +651,7 @@ static void mirror_complete(BlockJob *job, Error **errp)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    s->should_complete = true;
 | 
			
		||||
    block_job_enter(&s->common);
 | 
			
		||||
    block_job_resume(job);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const BlockJobDriver mirror_job_driver = {
 | 
			
		||||
@@ -665,7 +673,7 @@ static const BlockJobDriver commit_active_job_driver = {
 | 
			
		||||
 | 
			
		||||
static void mirror_start_job(BlockDriverState *bs, BlockDriverState *target,
 | 
			
		||||
                             const char *replaces,
 | 
			
		||||
                             int64_t speed, uint32_t granularity,
 | 
			
		||||
                             int64_t speed, int64_t granularity,
 | 
			
		||||
                             int64_t buf_size,
 | 
			
		||||
                             BlockdevOnError on_source_error,
 | 
			
		||||
                             BlockdevOnError on_target_error,
 | 
			
		||||
@@ -678,7 +686,15 @@ static void mirror_start_job(BlockDriverState *bs, BlockDriverState *target,
 | 
			
		||||
    MirrorBlockJob *s;
 | 
			
		||||
 | 
			
		||||
    if (granularity == 0) {
 | 
			
		||||
        granularity = bdrv_get_default_bitmap_granularity(target);
 | 
			
		||||
        /* Choose the default granularity based on the target file's cluster
 | 
			
		||||
         * size, clamped between 4k and 64k.  */
 | 
			
		||||
        BlockDriverInfo bdi;
 | 
			
		||||
        if (bdrv_get_info(target, &bdi) >= 0 && bdi.cluster_size != 0) {
 | 
			
		||||
            granularity = MAX(4096, bdi.cluster_size);
 | 
			
		||||
            granularity = MIN(65536, granularity);
 | 
			
		||||
        } else {
 | 
			
		||||
            granularity = 65536;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    assert ((granularity & (granularity - 1)) == 0);
 | 
			
		||||
@@ -686,7 +702,7 @@ static void mirror_start_job(BlockDriverState *bs, BlockDriverState *target,
 | 
			
		||||
    if ((on_source_error == BLOCKDEV_ON_ERROR_STOP ||
 | 
			
		||||
         on_source_error == BLOCKDEV_ON_ERROR_ENOSPC) &&
 | 
			
		||||
        !bdrv_iostatus_is_enabled(bs)) {
 | 
			
		||||
        error_setg(errp, QERR_INVALID_PARAMETER, "on-source-error");
 | 
			
		||||
        error_set(errp, QERR_INVALID_PARAMETER, "on-source-error");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -706,7 +722,7 @@ static void mirror_start_job(BlockDriverState *bs, BlockDriverState *target,
 | 
			
		||||
    s->buf_size = MAX(buf_size, granularity);
 | 
			
		||||
    s->unmap = unmap;
 | 
			
		||||
 | 
			
		||||
    s->dirty_bitmap = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp);
 | 
			
		||||
    s->dirty_bitmap = bdrv_create_dirty_bitmap(bs, granularity, errp);
 | 
			
		||||
    if (!s->dirty_bitmap) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
@@ -720,7 +736,7 @@ static void mirror_start_job(BlockDriverState *bs, BlockDriverState *target,
 | 
			
		||||
 | 
			
		||||
void mirror_start(BlockDriverState *bs, BlockDriverState *target,
 | 
			
		||||
                  const char *replaces,
 | 
			
		||||
                  int64_t speed, uint32_t granularity, int64_t buf_size,
 | 
			
		||||
                  int64_t speed, int64_t granularity, int64_t buf_size,
 | 
			
		||||
                  MirrorSyncMode mode, BlockdevOnError on_source_error,
 | 
			
		||||
                  BlockdevOnError on_target_error,
 | 
			
		||||
                  bool unmap,
 | 
			
		||||
@@ -730,10 +746,6 @@ void mirror_start(BlockDriverState *bs, BlockDriverState *target,
 | 
			
		||||
    bool is_none_mode;
 | 
			
		||||
    BlockDriverState *base;
 | 
			
		||||
 | 
			
		||||
    if (mode == MIRROR_SYNC_MODE_INCREMENTAL) {
 | 
			
		||||
        error_setg(errp, "Sync mode 'incremental' not supported");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    is_none_mode = mode == MIRROR_SYNC_MODE_NONE;
 | 
			
		||||
    base = mode == MIRROR_SYNC_MODE_TOP ? bs->backing_hd : NULL;
 | 
			
		||||
    mirror_start_job(bs, target, replaces,
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										66
									
								
								block/null.c
									
									
									
									
									
								
							
							
						
						
									
										66
									
								
								block/null.c
									
									
									
									
									
								
							@@ -12,11 +12,8 @@
 | 
			
		||||
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
 | 
			
		||||
#define NULL_OPT_LATENCY "latency-ns"
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    int64_t length;
 | 
			
		||||
    int64_t latency_ns;
 | 
			
		||||
} BDRVNullState;
 | 
			
		||||
 | 
			
		||||
static QemuOptsList runtime_opts = {
 | 
			
		||||
@@ -33,12 +30,6 @@ static QemuOptsList runtime_opts = {
 | 
			
		||||
            .type = QEMU_OPT_SIZE,
 | 
			
		||||
            .help = "size of the null block",
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
            .name = NULL_OPT_LATENCY,
 | 
			
		||||
            .type = QEMU_OPT_NUMBER,
 | 
			
		||||
            .help = "nanoseconds (approximated) to wait "
 | 
			
		||||
                    "before completing request",
 | 
			
		||||
        },
 | 
			
		||||
        { /* end of list */ }
 | 
			
		||||
    },
 | 
			
		||||
};
 | 
			
		||||
@@ -48,20 +39,13 @@ static int null_file_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
{
 | 
			
		||||
    QemuOpts *opts;
 | 
			
		||||
    BDRVNullState *s = bs->opaque;
 | 
			
		||||
    int ret = 0;
 | 
			
		||||
 | 
			
		||||
    opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
 | 
			
		||||
    qemu_opts_absorb_qdict(opts, options, &error_abort);
 | 
			
		||||
    s->length =
 | 
			
		||||
        qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 1 << 30);
 | 
			
		||||
    s->latency_ns =
 | 
			
		||||
        qemu_opt_get_number(opts, NULL_OPT_LATENCY, 0);
 | 
			
		||||
    if (s->latency_ns < 0) {
 | 
			
		||||
        error_setg(errp, "latency-ns is invalid");
 | 
			
		||||
        ret = -EINVAL;
 | 
			
		||||
    }
 | 
			
		||||
    qemu_opts_del(opts);
 | 
			
		||||
    return ret;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void null_close(BlockDriverState *bs)
 | 
			
		||||
@@ -74,40 +58,28 @@ static int64_t null_getlength(BlockDriverState *bs)
 | 
			
		||||
    return s->length;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static coroutine_fn int null_co_common(BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
    BDRVNullState *s = bs->opaque;
 | 
			
		||||
 | 
			
		||||
    if (s->latency_ns) {
 | 
			
		||||
        co_aio_sleep_ns(bdrv_get_aio_context(bs), QEMU_CLOCK_REALTIME,
 | 
			
		||||
                        s->latency_ns);
 | 
			
		||||
    }
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static coroutine_fn int null_co_readv(BlockDriverState *bs,
 | 
			
		||||
                                      int64_t sector_num, int nb_sectors,
 | 
			
		||||
                                      QEMUIOVector *qiov)
 | 
			
		||||
{
 | 
			
		||||
    return null_co_common(bs);
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static coroutine_fn int null_co_writev(BlockDriverState *bs,
 | 
			
		||||
                                       int64_t sector_num, int nb_sectors,
 | 
			
		||||
                                       QEMUIOVector *qiov)
 | 
			
		||||
{
 | 
			
		||||
    return null_co_common(bs);
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static coroutine_fn int null_co_flush(BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
    return null_co_common(bs);
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    BlockAIOCB common;
 | 
			
		||||
    QEMUBH *bh;
 | 
			
		||||
    QEMUTimer timer;
 | 
			
		||||
} NullAIOCB;
 | 
			
		||||
 | 
			
		||||
static const AIOCBInfo null_aiocb_info = {
 | 
			
		||||
@@ -122,33 +94,15 @@ static void null_bh_cb(void *opaque)
 | 
			
		||||
    qemu_aio_unref(acb);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void null_timer_cb(void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    NullAIOCB *acb = opaque;
 | 
			
		||||
    acb->common.cb(acb->common.opaque, 0);
 | 
			
		||||
    timer_deinit(&acb->timer);
 | 
			
		||||
    qemu_aio_unref(acb);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static inline BlockAIOCB *null_aio_common(BlockDriverState *bs,
 | 
			
		||||
                                          BlockCompletionFunc *cb,
 | 
			
		||||
                                          void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    NullAIOCB *acb;
 | 
			
		||||
    BDRVNullState *s = bs->opaque;
 | 
			
		||||
 | 
			
		||||
    acb = qemu_aio_get(&null_aiocb_info, bs, cb, opaque);
 | 
			
		||||
    /* Only emulate latency after vcpu is running. */
 | 
			
		||||
    if (s->latency_ns) {
 | 
			
		||||
        aio_timer_init(bdrv_get_aio_context(bs), &acb->timer,
 | 
			
		||||
                       QEMU_CLOCK_REALTIME, SCALE_NS,
 | 
			
		||||
                       null_timer_cb, acb);
 | 
			
		||||
        timer_mod_ns(&acb->timer,
 | 
			
		||||
                     qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + s->latency_ns);
 | 
			
		||||
    } else {
 | 
			
		||||
        acb->bh = aio_bh_new(bdrv_get_aio_context(bs), null_bh_cb, acb);
 | 
			
		||||
        qemu_bh_schedule(acb->bh);
 | 
			
		||||
    }
 | 
			
		||||
    acb->bh = aio_bh_new(bdrv_get_aio_context(bs), null_bh_cb, acb);
 | 
			
		||||
    qemu_bh_schedule(acb->bh);
 | 
			
		||||
    return &acb->common;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -177,12 +131,6 @@ static BlockAIOCB *null_aio_flush(BlockDriverState *bs,
 | 
			
		||||
    return null_aio_common(bs, cb, opaque);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int null_reopen_prepare(BDRVReopenState *reopen_state,
 | 
			
		||||
                               BlockReopenQueue *queue, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static BlockDriver bdrv_null_co = {
 | 
			
		||||
    .format_name            = "null-co",
 | 
			
		||||
    .protocol_name          = "null-co",
 | 
			
		||||
@@ -195,7 +143,6 @@ static BlockDriver bdrv_null_co = {
 | 
			
		||||
    .bdrv_co_readv          = null_co_readv,
 | 
			
		||||
    .bdrv_co_writev         = null_co_writev,
 | 
			
		||||
    .bdrv_co_flush_to_disk  = null_co_flush,
 | 
			
		||||
    .bdrv_reopen_prepare    = null_reopen_prepare,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static BlockDriver bdrv_null_aio = {
 | 
			
		||||
@@ -210,7 +157,6 @@ static BlockDriver bdrv_null_aio = {
 | 
			
		||||
    .bdrv_aio_readv         = null_aio_readv,
 | 
			
		||||
    .bdrv_aio_writev        = null_aio_writev,
 | 
			
		||||
    .bdrv_aio_flush         = null_aio_flush,
 | 
			
		||||
    .bdrv_reopen_prepare    = null_reopen_prepare,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void bdrv_null_init(void)
 | 
			
		||||
 
 | 
			
		||||
@@ -2,12 +2,8 @@
 | 
			
		||||
 * Block driver for Parallels disk image format
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2007 Alex Beregszaszi
 | 
			
		||||
 * Copyright (c) 2015 Denis V. Lunev <den@openvz.org>
 | 
			
		||||
 *
 | 
			
		||||
 * This code was originally based on comparing different disk images created
 | 
			
		||||
 * by Parallels. Currently it is based on opened OpenVZ sources
 | 
			
		||||
 * available at
 | 
			
		||||
 *     http://git.openvz.org/?p=ploop;a=summary
 | 
			
		||||
 * This code is based on comparing different disk images created by Parallels.
 | 
			
		||||
 *
 | 
			
		||||
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
			
		||||
 * of this software and associated documentation files (the "Software"), to deal
 | 
			
		||||
@@ -30,539 +26,63 @@
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
#include "qemu/module.h"
 | 
			
		||||
#include "qemu/bitmap.h"
 | 
			
		||||
#include "qapi/util.h"
 | 
			
		||||
 | 
			
		||||
/**************************************************************/
 | 
			
		||||
 | 
			
		||||
#define HEADER_MAGIC "WithoutFreeSpace"
 | 
			
		||||
#define HEADER_MAGIC2 "WithouFreSpacExt"
 | 
			
		||||
#define HEADER_VERSION 2
 | 
			
		||||
#define HEADER_INUSE_MAGIC  (0x746F6E59)
 | 
			
		||||
 | 
			
		||||
#define DEFAULT_CLUSTER_SIZE 1048576        /* 1 MiB */
 | 
			
		||||
 | 
			
		||||
#define HEADER_SIZE 64
 | 
			
		||||
 | 
			
		||||
// always little-endian
 | 
			
		||||
typedef struct ParallelsHeader {
 | 
			
		||||
struct parallels_header {
 | 
			
		||||
    char magic[16]; // "WithoutFreeSpace"
 | 
			
		||||
    uint32_t version;
 | 
			
		||||
    uint32_t heads;
 | 
			
		||||
    uint32_t cylinders;
 | 
			
		||||
    uint32_t tracks;
 | 
			
		||||
    uint32_t bat_entries;
 | 
			
		||||
    uint32_t catalog_entries;
 | 
			
		||||
    uint64_t nb_sectors;
 | 
			
		||||
    uint32_t inuse;
 | 
			
		||||
    uint32_t data_off;
 | 
			
		||||
    char padding[12];
 | 
			
		||||
} QEMU_PACKED ParallelsHeader;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
typedef enum ParallelsPreallocMode {
 | 
			
		||||
    PRL_PREALLOC_MODE_FALLOCATE = 0,
 | 
			
		||||
    PRL_PREALLOC_MODE_TRUNCATE = 1,
 | 
			
		||||
    PRL_PREALLOC_MODE_MAX = 2,
 | 
			
		||||
} ParallelsPreallocMode;
 | 
			
		||||
 | 
			
		||||
static const char *prealloc_mode_lookup[] = {
 | 
			
		||||
    "falloc",
 | 
			
		||||
    "truncate",
 | 
			
		||||
    NULL,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
} QEMU_PACKED;
 | 
			
		||||
 | 
			
		||||
typedef struct BDRVParallelsState {
 | 
			
		||||
    /** Locking is conservative, the lock protects
 | 
			
		||||
     *   - image file extending (truncate, fallocate)
 | 
			
		||||
     *   - any access to block allocation table
 | 
			
		||||
     */
 | 
			
		||||
    CoMutex lock;
 | 
			
		||||
 | 
			
		||||
    ParallelsHeader *header;
 | 
			
		||||
    uint32_t header_size;
 | 
			
		||||
    bool header_unclean;
 | 
			
		||||
 | 
			
		||||
    unsigned long *bat_dirty_bmap;
 | 
			
		||||
    unsigned int  bat_dirty_block;
 | 
			
		||||
 | 
			
		||||
    uint32_t *bat_bitmap;
 | 
			
		||||
    unsigned int bat_size;
 | 
			
		||||
 | 
			
		||||
    int64_t  data_end;
 | 
			
		||||
    uint64_t prealloc_size;
 | 
			
		||||
    ParallelsPreallocMode prealloc_mode;
 | 
			
		||||
    uint32_t *catalog_bitmap;
 | 
			
		||||
    unsigned int catalog_size;
 | 
			
		||||
 | 
			
		||||
    unsigned int tracks;
 | 
			
		||||
 | 
			
		||||
    unsigned int off_multiplier;
 | 
			
		||||
} BDRVParallelsState;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define PARALLELS_OPT_PREALLOC_MODE     "prealloc-mode"
 | 
			
		||||
#define PARALLELS_OPT_PREALLOC_SIZE     "prealloc-size"
 | 
			
		||||
 | 
			
		||||
static QemuOptsList parallels_runtime_opts = {
 | 
			
		||||
    .name = "parallels",
 | 
			
		||||
    .head = QTAILQ_HEAD_INITIALIZER(parallels_runtime_opts.head),
 | 
			
		||||
    .desc = {
 | 
			
		||||
        {
 | 
			
		||||
            .name = PARALLELS_OPT_PREALLOC_SIZE,
 | 
			
		||||
            .type = QEMU_OPT_SIZE,
 | 
			
		||||
            .help = "Preallocation size on image expansion",
 | 
			
		||||
            .def_value_str = "128MiB",
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
            .name = PARALLELS_OPT_PREALLOC_MODE,
 | 
			
		||||
            .type = QEMU_OPT_STRING,
 | 
			
		||||
            .help = "Preallocation mode on image expansion "
 | 
			
		||||
                    "(allowed values: falloc, truncate)",
 | 
			
		||||
            .def_value_str = "falloc",
 | 
			
		||||
        },
 | 
			
		||||
        { /* end of list */ },
 | 
			
		||||
    },
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static int64_t bat2sect(BDRVParallelsState *s, uint32_t idx)
 | 
			
		||||
static int parallels_probe(const uint8_t *buf, int buf_size, const char *filename)
 | 
			
		||||
{
 | 
			
		||||
    return (uint64_t)le32_to_cpu(s->bat_bitmap[idx]) * s->off_multiplier;
 | 
			
		||||
}
 | 
			
		||||
    const struct parallels_header *ph = (const void *)buf;
 | 
			
		||||
 | 
			
		||||
static uint32_t bat_entry_off(uint32_t idx)
 | 
			
		||||
{
 | 
			
		||||
    return sizeof(ParallelsHeader) + sizeof(uint32_t) * idx;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int64_t seek_to_sector(BDRVParallelsState *s, int64_t sector_num)
 | 
			
		||||
{
 | 
			
		||||
    uint32_t index, offset;
 | 
			
		||||
 | 
			
		||||
    index = sector_num / s->tracks;
 | 
			
		||||
    offset = sector_num % s->tracks;
 | 
			
		||||
 | 
			
		||||
    /* not allocated */
 | 
			
		||||
    if ((index >= s->bat_size) || (s->bat_bitmap[index] == 0)) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
    return bat2sect(s, index) + offset;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int cluster_remainder(BDRVParallelsState *s, int64_t sector_num,
 | 
			
		||||
        int nb_sectors)
 | 
			
		||||
{
 | 
			
		||||
    int ret = s->tracks - sector_num % s->tracks;
 | 
			
		||||
    return MIN(nb_sectors, ret);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int64_t block_status(BDRVParallelsState *s, int64_t sector_num,
 | 
			
		||||
                            int nb_sectors, int *pnum)
 | 
			
		||||
{
 | 
			
		||||
    int64_t start_off = -2, prev_end_off = -2;
 | 
			
		||||
 | 
			
		||||
    *pnum = 0;
 | 
			
		||||
    while (nb_sectors > 0 || start_off == -2) {
 | 
			
		||||
        int64_t offset = seek_to_sector(s, sector_num);
 | 
			
		||||
        int to_end;
 | 
			
		||||
 | 
			
		||||
        if (start_off == -2) {
 | 
			
		||||
            start_off = offset;
 | 
			
		||||
            prev_end_off = offset;
 | 
			
		||||
        } else if (offset != prev_end_off) {
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        to_end = cluster_remainder(s, sector_num, nb_sectors);
 | 
			
		||||
        nb_sectors -= to_end;
 | 
			
		||||
        sector_num += to_end;
 | 
			
		||||
        *pnum += to_end;
 | 
			
		||||
 | 
			
		||||
        if (offset > 0) {
 | 
			
		||||
            prev_end_off += to_end;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return start_off;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int64_t allocate_clusters(BlockDriverState *bs, int64_t sector_num,
 | 
			
		||||
                                 int nb_sectors, int *pnum)
 | 
			
		||||
{
 | 
			
		||||
    BDRVParallelsState *s = bs->opaque;
 | 
			
		||||
    uint32_t idx, to_allocate, i;
 | 
			
		||||
    int64_t pos, space;
 | 
			
		||||
 | 
			
		||||
    pos = block_status(s, sector_num, nb_sectors, pnum);
 | 
			
		||||
    if (pos > 0) {
 | 
			
		||||
        return pos;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    idx = sector_num / s->tracks;
 | 
			
		||||
    if (idx >= s->bat_size) {
 | 
			
		||||
        return -EINVAL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    to_allocate = (sector_num + *pnum + s->tracks - 1) / s->tracks - idx;
 | 
			
		||||
    space = to_allocate * s->tracks;
 | 
			
		||||
    if (s->data_end + space > bdrv_getlength(bs->file) >> BDRV_SECTOR_BITS) {
 | 
			
		||||
        int ret;
 | 
			
		||||
        space += s->prealloc_size;
 | 
			
		||||
        if (s->prealloc_mode == PRL_PREALLOC_MODE_FALLOCATE) {
 | 
			
		||||
            ret = bdrv_write_zeroes(bs->file, s->data_end, space, 0);
 | 
			
		||||
        } else {
 | 
			
		||||
            ret = bdrv_truncate(bs->file,
 | 
			
		||||
                                (s->data_end + space) << BDRV_SECTOR_BITS);
 | 
			
		||||
        }
 | 
			
		||||
        if (ret < 0) {
 | 
			
		||||
            return ret;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < to_allocate; i++) {
 | 
			
		||||
        s->bat_bitmap[idx + i] = cpu_to_le32(s->data_end / s->off_multiplier);
 | 
			
		||||
        s->data_end += s->tracks;
 | 
			
		||||
        bitmap_set(s->bat_dirty_bmap,
 | 
			
		||||
                   bat_entry_off(idx) / s->bat_dirty_block, 1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return bat2sect(s, idx) + sector_num % s->tracks;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static coroutine_fn int parallels_co_flush_to_os(BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
    BDRVParallelsState *s = bs->opaque;
 | 
			
		||||
    unsigned long size = DIV_ROUND_UP(s->header_size, s->bat_dirty_block);
 | 
			
		||||
    unsigned long bit;
 | 
			
		||||
 | 
			
		||||
    qemu_co_mutex_lock(&s->lock);
 | 
			
		||||
 | 
			
		||||
    bit = find_first_bit(s->bat_dirty_bmap, size);
 | 
			
		||||
    while (bit < size) {
 | 
			
		||||
        uint32_t off = bit * s->bat_dirty_block;
 | 
			
		||||
        uint32_t to_write = s->bat_dirty_block;
 | 
			
		||||
        int ret;
 | 
			
		||||
 | 
			
		||||
        if (off + to_write > s->header_size) {
 | 
			
		||||
            to_write = s->header_size - off;
 | 
			
		||||
        }
 | 
			
		||||
        ret = bdrv_pwrite(bs->file, off, (uint8_t *)s->header + off, to_write);
 | 
			
		||||
        if (ret < 0) {
 | 
			
		||||
            qemu_co_mutex_unlock(&s->lock);
 | 
			
		||||
            return ret;
 | 
			
		||||
        }
 | 
			
		||||
        bit = find_next_bit(s->bat_dirty_bmap, size, bit + 1);
 | 
			
		||||
    }
 | 
			
		||||
    bitmap_zero(s->bat_dirty_bmap, size);
 | 
			
		||||
 | 
			
		||||
    qemu_co_mutex_unlock(&s->lock);
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static int64_t coroutine_fn parallels_co_get_block_status(BlockDriverState *bs,
 | 
			
		||||
        int64_t sector_num, int nb_sectors, int *pnum)
 | 
			
		||||
{
 | 
			
		||||
    BDRVParallelsState *s = bs->opaque;
 | 
			
		||||
    int64_t offset;
 | 
			
		||||
 | 
			
		||||
    qemu_co_mutex_lock(&s->lock);
 | 
			
		||||
    offset = block_status(s, sector_num, nb_sectors, pnum);
 | 
			
		||||
    qemu_co_mutex_unlock(&s->lock);
 | 
			
		||||
 | 
			
		||||
    if (offset < 0) {
 | 
			
		||||
    if (buf_size < HEADER_SIZE)
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return (offset << BDRV_SECTOR_BITS) |
 | 
			
		||||
        BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static coroutine_fn int parallels_co_writev(BlockDriverState *bs,
 | 
			
		||||
        int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
 | 
			
		||||
{
 | 
			
		||||
    BDRVParallelsState *s = bs->opaque;
 | 
			
		||||
    uint64_t bytes_done = 0;
 | 
			
		||||
    QEMUIOVector hd_qiov;
 | 
			
		||||
    int ret = 0;
 | 
			
		||||
 | 
			
		||||
    qemu_iovec_init(&hd_qiov, qiov->niov);
 | 
			
		||||
 | 
			
		||||
    while (nb_sectors > 0) {
 | 
			
		||||
        int64_t position;
 | 
			
		||||
        int n, nbytes;
 | 
			
		||||
 | 
			
		||||
        qemu_co_mutex_lock(&s->lock);
 | 
			
		||||
        position = allocate_clusters(bs, sector_num, nb_sectors, &n);
 | 
			
		||||
        qemu_co_mutex_unlock(&s->lock);
 | 
			
		||||
        if (position < 0) {
 | 
			
		||||
            ret = (int)position;
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        nbytes = n << BDRV_SECTOR_BITS;
 | 
			
		||||
 | 
			
		||||
        qemu_iovec_reset(&hd_qiov);
 | 
			
		||||
        qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes);
 | 
			
		||||
 | 
			
		||||
        ret = bdrv_co_writev(bs->file, position, n, &hd_qiov);
 | 
			
		||||
        if (ret < 0) {
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        nb_sectors -= n;
 | 
			
		||||
        sector_num += n;
 | 
			
		||||
        bytes_done += nbytes;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    qemu_iovec_destroy(&hd_qiov);
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static coroutine_fn int parallels_co_readv(BlockDriverState *bs,
 | 
			
		||||
        int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
 | 
			
		||||
{
 | 
			
		||||
    BDRVParallelsState *s = bs->opaque;
 | 
			
		||||
    uint64_t bytes_done = 0;
 | 
			
		||||
    QEMUIOVector hd_qiov;
 | 
			
		||||
    int ret = 0;
 | 
			
		||||
 | 
			
		||||
    qemu_iovec_init(&hd_qiov, qiov->niov);
 | 
			
		||||
 | 
			
		||||
    while (nb_sectors > 0) {
 | 
			
		||||
        int64_t position;
 | 
			
		||||
        int n, nbytes;
 | 
			
		||||
 | 
			
		||||
        qemu_co_mutex_lock(&s->lock);
 | 
			
		||||
        position = block_status(s, sector_num, nb_sectors, &n);
 | 
			
		||||
        qemu_co_mutex_unlock(&s->lock);
 | 
			
		||||
 | 
			
		||||
        nbytes = n << BDRV_SECTOR_BITS;
 | 
			
		||||
 | 
			
		||||
        if (position < 0) {
 | 
			
		||||
            qemu_iovec_memset(qiov, bytes_done, 0, nbytes);
 | 
			
		||||
        } else {
 | 
			
		||||
            qemu_iovec_reset(&hd_qiov);
 | 
			
		||||
            qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes);
 | 
			
		||||
 | 
			
		||||
            ret = bdrv_co_readv(bs->file, position, n, &hd_qiov);
 | 
			
		||||
            if (ret < 0) {
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        nb_sectors -= n;
 | 
			
		||||
        sector_num += n;
 | 
			
		||||
        bytes_done += nbytes;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    qemu_iovec_destroy(&hd_qiov);
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static int parallels_check(BlockDriverState *bs, BdrvCheckResult *res,
 | 
			
		||||
                           BdrvCheckMode fix)
 | 
			
		||||
{
 | 
			
		||||
    BDRVParallelsState *s = bs->opaque;
 | 
			
		||||
    int64_t size, prev_off, high_off;
 | 
			
		||||
    int ret;
 | 
			
		||||
    uint32_t i;
 | 
			
		||||
    bool flush_bat = false;
 | 
			
		||||
    int cluster_size = s->tracks << BDRV_SECTOR_BITS;
 | 
			
		||||
 | 
			
		||||
    size = bdrv_getlength(bs->file);
 | 
			
		||||
    if (size < 0) {
 | 
			
		||||
        res->check_errors++;
 | 
			
		||||
        return size;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (s->header_unclean) {
 | 
			
		||||
        fprintf(stderr, "%s image was not closed correctly\n",
 | 
			
		||||
                fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR");
 | 
			
		||||
        res->corruptions++;
 | 
			
		||||
        if (fix & BDRV_FIX_ERRORS) {
 | 
			
		||||
            /* parallels_close will do the job right */
 | 
			
		||||
            res->corruptions_fixed++;
 | 
			
		||||
            s->header_unclean = false;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    res->bfi.total_clusters = s->bat_size;
 | 
			
		||||
    res->bfi.compressed_clusters = 0; /* compression is not supported */
 | 
			
		||||
 | 
			
		||||
    high_off = 0;
 | 
			
		||||
    prev_off = 0;
 | 
			
		||||
    for (i = 0; i < s->bat_size; i++) {
 | 
			
		||||
        int64_t off = bat2sect(s, i) << BDRV_SECTOR_BITS;
 | 
			
		||||
        if (off == 0) {
 | 
			
		||||
            prev_off = 0;
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /* cluster outside the image */
 | 
			
		||||
        if (off > size) {
 | 
			
		||||
            fprintf(stderr, "%s cluster %u is outside image\n",
 | 
			
		||||
                    fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", i);
 | 
			
		||||
            res->corruptions++;
 | 
			
		||||
            if (fix & BDRV_FIX_ERRORS) {
 | 
			
		||||
                prev_off = 0;
 | 
			
		||||
                s->bat_bitmap[i] = 0;
 | 
			
		||||
                res->corruptions_fixed++;
 | 
			
		||||
                flush_bat = true;
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        res->bfi.allocated_clusters++;
 | 
			
		||||
        if (off > high_off) {
 | 
			
		||||
            high_off = off;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (prev_off != 0 && (prev_off + cluster_size) != off) {
 | 
			
		||||
            res->bfi.fragmented_clusters++;
 | 
			
		||||
        }
 | 
			
		||||
        prev_off = off;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (flush_bat) {
 | 
			
		||||
        ret = bdrv_pwrite_sync(bs->file, 0, s->header, s->header_size);
 | 
			
		||||
        if (ret < 0) {
 | 
			
		||||
            res->check_errors++;
 | 
			
		||||
            return ret;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    res->image_end_offset = high_off + cluster_size;
 | 
			
		||||
    if (size > res->image_end_offset) {
 | 
			
		||||
        int64_t count;
 | 
			
		||||
        count = DIV_ROUND_UP(size - res->image_end_offset, cluster_size);
 | 
			
		||||
        fprintf(stderr, "%s space leaked at the end of the image %" PRId64 "\n",
 | 
			
		||||
                fix & BDRV_FIX_LEAKS ? "Repairing" : "ERROR",
 | 
			
		||||
                size - res->image_end_offset);
 | 
			
		||||
        res->leaks += count;
 | 
			
		||||
        if (fix & BDRV_FIX_LEAKS) {
 | 
			
		||||
            ret = bdrv_truncate(bs->file, res->image_end_offset);
 | 
			
		||||
            if (ret < 0) {
 | 
			
		||||
                res->check_errors++;
 | 
			
		||||
                return ret;
 | 
			
		||||
            }
 | 
			
		||||
            res->leaks_fixed += count;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static int parallels_create(const char *filename, QemuOpts *opts, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    int64_t total_size, cl_size;
 | 
			
		||||
    uint8_t tmp[BDRV_SECTOR_SIZE];
 | 
			
		||||
    Error *local_err = NULL;
 | 
			
		||||
    BlockDriverState *file;
 | 
			
		||||
    uint32_t bat_entries, bat_sectors;
 | 
			
		||||
    ParallelsHeader header;
 | 
			
		||||
    int ret;
 | 
			
		||||
 | 
			
		||||
    total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
 | 
			
		||||
                          BDRV_SECTOR_SIZE);
 | 
			
		||||
    cl_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE,
 | 
			
		||||
                          DEFAULT_CLUSTER_SIZE), BDRV_SECTOR_SIZE);
 | 
			
		||||
 | 
			
		||||
    ret = bdrv_create_file(filename, opts, &local_err);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        error_propagate(errp, local_err);
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    file = NULL;
 | 
			
		||||
    ret = bdrv_open(&file, filename, NULL, NULL,
 | 
			
		||||
                    BDRV_O_RDWR | BDRV_O_PROTOCOL, NULL, &local_err);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        error_propagate(errp, local_err);
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
    ret = bdrv_truncate(file, 0);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        goto exit;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    bat_entries = DIV_ROUND_UP(total_size, cl_size);
 | 
			
		||||
    bat_sectors = DIV_ROUND_UP(bat_entry_off(bat_entries), cl_size);
 | 
			
		||||
    bat_sectors = (bat_sectors *  cl_size) >> BDRV_SECTOR_BITS;
 | 
			
		||||
 | 
			
		||||
    memset(&header, 0, sizeof(header));
 | 
			
		||||
    memcpy(header.magic, HEADER_MAGIC2, sizeof(header.magic));
 | 
			
		||||
    header.version = cpu_to_le32(HEADER_VERSION);
 | 
			
		||||
    /* don't care much about geometry, it is not used on image level */
 | 
			
		||||
    header.heads = cpu_to_le32(16);
 | 
			
		||||
    header.cylinders = cpu_to_le32(total_size / BDRV_SECTOR_SIZE / 16 / 32);
 | 
			
		||||
    header.tracks = cpu_to_le32(cl_size >> BDRV_SECTOR_BITS);
 | 
			
		||||
    header.bat_entries = cpu_to_le32(bat_entries);
 | 
			
		||||
    header.nb_sectors = cpu_to_le64(DIV_ROUND_UP(total_size, BDRV_SECTOR_SIZE));
 | 
			
		||||
    header.data_off = cpu_to_le32(bat_sectors);
 | 
			
		||||
 | 
			
		||||
    /* write all the data */
 | 
			
		||||
    memset(tmp, 0, sizeof(tmp));
 | 
			
		||||
    memcpy(tmp, &header, sizeof(header));
 | 
			
		||||
 | 
			
		||||
    ret = bdrv_pwrite(file, 0, tmp, BDRV_SECTOR_SIZE);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        goto exit;
 | 
			
		||||
    }
 | 
			
		||||
    ret = bdrv_write_zeroes(file, 1, bat_sectors - 1, 0);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        goto exit;
 | 
			
		||||
    }
 | 
			
		||||
    ret = 0;
 | 
			
		||||
 | 
			
		||||
done:
 | 
			
		||||
    bdrv_unref(file);
 | 
			
		||||
    return ret;
 | 
			
		||||
 | 
			
		||||
exit:
 | 
			
		||||
    error_setg_errno(errp, -ret, "Failed to create Parallels image");
 | 
			
		||||
    goto done;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static int parallels_probe(const uint8_t *buf, int buf_size,
 | 
			
		||||
                           const char *filename)
 | 
			
		||||
{
 | 
			
		||||
    const ParallelsHeader *ph = (const void *)buf;
 | 
			
		||||
 | 
			
		||||
    if (buf_size < sizeof(ParallelsHeader)) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if ((!memcmp(ph->magic, HEADER_MAGIC, 16) ||
 | 
			
		||||
           !memcmp(ph->magic, HEADER_MAGIC2, 16)) &&
 | 
			
		||||
           (le32_to_cpu(ph->version) == HEADER_VERSION)) {
 | 
			
		||||
        !memcmp(ph->magic, HEADER_MAGIC2, 16)) &&
 | 
			
		||||
        (le32_to_cpu(ph->version) == HEADER_VERSION))
 | 
			
		||||
        return 100;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int parallels_update_header(BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
    BDRVParallelsState *s = bs->opaque;
 | 
			
		||||
    unsigned size = MAX(bdrv_opt_mem_align(bs->file), sizeof(ParallelsHeader));
 | 
			
		||||
 | 
			
		||||
    if (size > s->header_size) {
 | 
			
		||||
        size = s->header_size;
 | 
			
		||||
    }
 | 
			
		||||
    return bdrv_pwrite_sync(bs->file, 0, s->header, size);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
                          Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    BDRVParallelsState *s = bs->opaque;
 | 
			
		||||
    ParallelsHeader ph;
 | 
			
		||||
    int ret, size, i;
 | 
			
		||||
    QemuOpts *opts = NULL;
 | 
			
		||||
    Error *local_err = NULL;
 | 
			
		||||
    char *buf;
 | 
			
		||||
    int i;
 | 
			
		||||
    struct parallels_header ph;
 | 
			
		||||
    int ret;
 | 
			
		||||
 | 
			
		||||
    bs->read_only = 1; // no write support yet
 | 
			
		||||
 | 
			
		||||
    ret = bdrv_pread(bs->file, 0, &ph, sizeof(ph));
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
@@ -595,90 +115,25 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    s->bat_size = le32_to_cpu(ph.bat_entries);
 | 
			
		||||
    if (s->bat_size > INT_MAX / sizeof(uint32_t)) {
 | 
			
		||||
    s->catalog_size = le32_to_cpu(ph.catalog_entries);
 | 
			
		||||
    if (s->catalog_size > INT_MAX / 4) {
 | 
			
		||||
        error_setg(errp, "Catalog too large");
 | 
			
		||||
        ret = -EFBIG;
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    size = bat_entry_off(s->bat_size);
 | 
			
		||||
    s->header_size = ROUND_UP(size, bdrv_opt_mem_align(bs->file));
 | 
			
		||||
    s->header = qemu_try_blockalign(bs->file, s->header_size);
 | 
			
		||||
    if (s->header == NULL) {
 | 
			
		||||
    s->catalog_bitmap = g_try_new(uint32_t, s->catalog_size);
 | 
			
		||||
    if (s->catalog_size && s->catalog_bitmap == NULL) {
 | 
			
		||||
        ret = -ENOMEM;
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
    s->data_end = le32_to_cpu(ph.data_off);
 | 
			
		||||
    if (s->data_end == 0) {
 | 
			
		||||
        s->data_end = ROUND_UP(bat_entry_off(s->bat_size), BDRV_SECTOR_SIZE);
 | 
			
		||||
    }
 | 
			
		||||
    if (s->data_end < s->header_size) {
 | 
			
		||||
        /* there is not enough unused space to fit to block align between BAT
 | 
			
		||||
           and actual data. We can't avoid read-modify-write... */
 | 
			
		||||
        s->header_size = size;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ret = bdrv_pread(bs->file, 0, s->header, s->header_size);
 | 
			
		||||
    ret = bdrv_pread(bs->file, 64, s->catalog_bitmap, s->catalog_size * 4);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
    s->bat_bitmap = (uint32_t *)(s->header + 1);
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < s->bat_size; i++) {
 | 
			
		||||
        int64_t off = bat2sect(s, i);
 | 
			
		||||
        if (off >= s->data_end) {
 | 
			
		||||
            s->data_end = off + s->tracks;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (le32_to_cpu(ph.inuse) == HEADER_INUSE_MAGIC) {
 | 
			
		||||
        /* Image was not closed correctly. The check is mandatory */
 | 
			
		||||
        s->header_unclean = true;
 | 
			
		||||
        if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) {
 | 
			
		||||
            error_setg(errp, "parallels: Image was not closed correctly; "
 | 
			
		||||
                       "cannot be opened read/write");
 | 
			
		||||
            ret = -EACCES;
 | 
			
		||||
            goto fail;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    opts = qemu_opts_create(¶llels_runtime_opts, NULL, 0, &local_err);
 | 
			
		||||
    if (local_err != NULL) {
 | 
			
		||||
        goto fail_options;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    qemu_opts_absorb_qdict(opts, options, &local_err);
 | 
			
		||||
    if (local_err != NULL) {
 | 
			
		||||
        goto fail_options;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    s->prealloc_size =
 | 
			
		||||
        qemu_opt_get_size_del(opts, PARALLELS_OPT_PREALLOC_SIZE, 0);
 | 
			
		||||
    s->prealloc_size = MAX(s->tracks, s->prealloc_size >> BDRV_SECTOR_BITS);
 | 
			
		||||
    buf = qemu_opt_get_del(opts, PARALLELS_OPT_PREALLOC_MODE);
 | 
			
		||||
    s->prealloc_mode = qapi_enum_parse(prealloc_mode_lookup, buf,
 | 
			
		||||
            PRL_PREALLOC_MODE_MAX, PRL_PREALLOC_MODE_FALLOCATE, &local_err);
 | 
			
		||||
    g_free(buf);
 | 
			
		||||
    if (local_err != NULL) {
 | 
			
		||||
        goto fail_options;
 | 
			
		||||
    }
 | 
			
		||||
    if (!bdrv_has_zero_init(bs->file) ||
 | 
			
		||||
            bdrv_truncate(bs->file, bdrv_getlength(bs->file)) != 0) {
 | 
			
		||||
        s->prealloc_mode = PRL_PREALLOC_MODE_FALLOCATE;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (flags & BDRV_O_RDWR) {
 | 
			
		||||
        s->header->inuse = cpu_to_le32(HEADER_INUSE_MAGIC);
 | 
			
		||||
        ret = parallels_update_header(bs);
 | 
			
		||||
        if (ret < 0) {
 | 
			
		||||
            goto fail;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    s->bat_dirty_block = 4 * getpagesize();
 | 
			
		||||
    s->bat_dirty_bmap =
 | 
			
		||||
        bitmap_new(DIV_ROUND_UP(s->header_size, s->bat_dirty_block));
 | 
			
		||||
    for (i = 0; i < s->catalog_size; i++)
 | 
			
		||||
        le32_to_cpus(&s->catalog_bitmap[i]);
 | 
			
		||||
 | 
			
		||||
    qemu_co_mutex_init(&s->lock);
 | 
			
		||||
    return 0;
 | 
			
		||||
@@ -687,67 +142,67 @@ fail_format:
 | 
			
		||||
    error_setg(errp, "Image not in Parallels format");
 | 
			
		||||
    ret = -EINVAL;
 | 
			
		||||
fail:
 | 
			
		||||
    qemu_vfree(s->header);
 | 
			
		||||
    g_free(s->catalog_bitmap);
 | 
			
		||||
    return ret;
 | 
			
		||||
 | 
			
		||||
fail_options:
 | 
			
		||||
    error_propagate(errp, local_err);
 | 
			
		||||
    ret = -EINVAL;
 | 
			
		||||
    goto fail;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num)
 | 
			
		||||
{
 | 
			
		||||
    BDRVParallelsState *s = bs->opaque;
 | 
			
		||||
    uint32_t index, offset;
 | 
			
		||||
 | 
			
		||||
    index = sector_num / s->tracks;
 | 
			
		||||
    offset = sector_num % s->tracks;
 | 
			
		||||
 | 
			
		||||
    /* not allocated */
 | 
			
		||||
    if ((index >= s->catalog_size) || (s->catalog_bitmap[index] == 0))
 | 
			
		||||
        return -1;
 | 
			
		||||
    return
 | 
			
		||||
        ((uint64_t)s->catalog_bitmap[index] * s->off_multiplier + offset) * 512;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int parallels_read(BlockDriverState *bs, int64_t sector_num,
 | 
			
		||||
                    uint8_t *buf, int nb_sectors)
 | 
			
		||||
{
 | 
			
		||||
    while (nb_sectors > 0) {
 | 
			
		||||
        int64_t position = seek_to_sector(bs, sector_num);
 | 
			
		||||
        if (position >= 0) {
 | 
			
		||||
            if (bdrv_pread(bs->file, position, buf, 512) != 512)
 | 
			
		||||
                return -1;
 | 
			
		||||
        } else {
 | 
			
		||||
            memset(buf, 0, 512);
 | 
			
		||||
        }
 | 
			
		||||
        nb_sectors--;
 | 
			
		||||
        sector_num++;
 | 
			
		||||
        buf += 512;
 | 
			
		||||
    }
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static coroutine_fn int parallels_co_read(BlockDriverState *bs, int64_t sector_num,
 | 
			
		||||
                                          uint8_t *buf, int nb_sectors)
 | 
			
		||||
{
 | 
			
		||||
    int ret;
 | 
			
		||||
    BDRVParallelsState *s = bs->opaque;
 | 
			
		||||
    qemu_co_mutex_lock(&s->lock);
 | 
			
		||||
    ret = parallels_read(bs, sector_num, buf, nb_sectors);
 | 
			
		||||
    qemu_co_mutex_unlock(&s->lock);
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void parallels_close(BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
    BDRVParallelsState *s = bs->opaque;
 | 
			
		||||
 | 
			
		||||
    if (bs->open_flags & BDRV_O_RDWR) {
 | 
			
		||||
        s->header->inuse = 0;
 | 
			
		||||
        parallels_update_header(bs);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (bs->open_flags & BDRV_O_RDWR) {
 | 
			
		||||
        bdrv_truncate(bs->file, s->data_end << BDRV_SECTOR_BITS);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    g_free(s->bat_dirty_bmap);
 | 
			
		||||
    qemu_vfree(s->header);
 | 
			
		||||
    g_free(s->catalog_bitmap);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static QemuOptsList parallels_create_opts = {
 | 
			
		||||
    .name = "parallels-create-opts",
 | 
			
		||||
    .head = QTAILQ_HEAD_INITIALIZER(parallels_create_opts.head),
 | 
			
		||||
    .desc = {
 | 
			
		||||
        {
 | 
			
		||||
            .name = BLOCK_OPT_SIZE,
 | 
			
		||||
            .type = QEMU_OPT_SIZE,
 | 
			
		||||
            .help = "Virtual disk size",
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
            .name = BLOCK_OPT_CLUSTER_SIZE,
 | 
			
		||||
            .type = QEMU_OPT_SIZE,
 | 
			
		||||
            .help = "Parallels image cluster size",
 | 
			
		||||
            .def_value_str = stringify(DEFAULT_CLUSTER_SIZE),
 | 
			
		||||
        },
 | 
			
		||||
        { /* end of list */ }
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static BlockDriver bdrv_parallels = {
 | 
			
		||||
    .format_name	= "parallels",
 | 
			
		||||
    .instance_size	= sizeof(BDRVParallelsState),
 | 
			
		||||
    .bdrv_probe		= parallels_probe,
 | 
			
		||||
    .bdrv_open		= parallels_open,
 | 
			
		||||
    .bdrv_read          = parallels_co_read,
 | 
			
		||||
    .bdrv_close		= parallels_close,
 | 
			
		||||
    .bdrv_co_get_block_status = parallels_co_get_block_status,
 | 
			
		||||
    .bdrv_has_zero_init       = bdrv_has_zero_init_1,
 | 
			
		||||
    .bdrv_co_flush_to_os      = parallels_co_flush_to_os,
 | 
			
		||||
    .bdrv_co_readv  = parallels_co_readv,
 | 
			
		||||
    .bdrv_co_writev = parallels_co_writev,
 | 
			
		||||
 | 
			
		||||
    .bdrv_create    = parallels_create,
 | 
			
		||||
    .bdrv_check     = parallels_check,
 | 
			
		||||
    .create_opts    = ¶llels_create_opts,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void bdrv_parallels_init(void)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										65
									
								
								block/qapi.c
									
									
									
									
									
								
							
							
						
						
									
										65
									
								
								block/qapi.c
									
									
									
									
									
								
							@@ -24,7 +24,6 @@
 | 
			
		||||
 | 
			
		||||
#include "block/qapi.h"
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
#include "block/throttle-groups.h"
 | 
			
		||||
#include "block/write-threshold.h"
 | 
			
		||||
#include "qmp-commands.h"
 | 
			
		||||
#include "qapi-visit.h"
 | 
			
		||||
@@ -32,10 +31,8 @@
 | 
			
		||||
#include "qapi/qmp/types.h"
 | 
			
		||||
#include "sysemu/block-backend.h"
 | 
			
		||||
 | 
			
		||||
BlockDeviceInfo *bdrv_block_device_info(BlockDriverState *bs, Error **errp)
 | 
			
		||||
BlockDeviceInfo *bdrv_block_device_info(BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
    ImageInfo **p_image_info;
 | 
			
		||||
    BlockDriverState *bs0;
 | 
			
		||||
    BlockDeviceInfo *info = g_malloc0(sizeof(*info));
 | 
			
		||||
 | 
			
		||||
    info->file                   = g_strdup(bs->filename);
 | 
			
		||||
@@ -66,9 +63,7 @@ BlockDeviceInfo *bdrv_block_device_info(BlockDriverState *bs, Error **errp)
 | 
			
		||||
 | 
			
		||||
    if (bs->io_limits_enabled) {
 | 
			
		||||
        ThrottleConfig cfg;
 | 
			
		||||
 | 
			
		||||
        throttle_group_get_config(bs, &cfg);
 | 
			
		||||
 | 
			
		||||
        throttle_get_config(&bs->throttle_state, &cfg);
 | 
			
		||||
        info->bps     = cfg.buckets[THROTTLE_BPS_TOTAL].avg;
 | 
			
		||||
        info->bps_rd  = cfg.buckets[THROTTLE_BPS_READ].avg;
 | 
			
		||||
        info->bps_wr  = cfg.buckets[THROTTLE_BPS_WRITE].avg;
 | 
			
		||||
@@ -93,32 +88,10 @@ BlockDeviceInfo *bdrv_block_device_info(BlockDriverState *bs, Error **errp)
 | 
			
		||||
 | 
			
		||||
        info->has_iops_size = cfg.op_size;
 | 
			
		||||
        info->iops_size = cfg.op_size;
 | 
			
		||||
 | 
			
		||||
        info->has_group = true;
 | 
			
		||||
        info->group = g_strdup(throttle_group_get_name(bs));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    info->write_threshold = bdrv_write_threshold_get(bs);
 | 
			
		||||
 | 
			
		||||
    bs0 = bs;
 | 
			
		||||
    p_image_info = &info->image;
 | 
			
		||||
    while (1) {
 | 
			
		||||
        Error *local_err = NULL;
 | 
			
		||||
        bdrv_query_image_info(bs0, p_image_info, &local_err);
 | 
			
		||||
        if (local_err) {
 | 
			
		||||
            error_propagate(errp, local_err);
 | 
			
		||||
            qapi_free_BlockDeviceInfo(info);
 | 
			
		||||
            return NULL;
 | 
			
		||||
        }
 | 
			
		||||
        if (bs0->drv && bs0->backing_hd) {
 | 
			
		||||
            bs0 = bs0->backing_hd;
 | 
			
		||||
            (*p_image_info)->has_backing_image = true;
 | 
			
		||||
            p_image_info = &((*p_image_info)->backing_image);
 | 
			
		||||
        } else {
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return info;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -291,6 +264,9 @@ static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info,
 | 
			
		||||
{
 | 
			
		||||
    BlockInfo *info = g_malloc0(sizeof(*info));
 | 
			
		||||
    BlockDriverState *bs = blk_bs(blk);
 | 
			
		||||
    BlockDriverState *bs0;
 | 
			
		||||
    ImageInfo **p_image_info;
 | 
			
		||||
    Error *local_err = NULL;
 | 
			
		||||
    info->device = g_strdup(blk_name(blk));
 | 
			
		||||
    info->type = g_strdup("unknown");
 | 
			
		||||
    info->locked = blk_dev_is_medium_locked(blk);
 | 
			
		||||
@@ -313,9 +289,23 @@ static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info,
 | 
			
		||||
 | 
			
		||||
    if (bs->drv) {
 | 
			
		||||
        info->has_inserted = true;
 | 
			
		||||
        info->inserted = bdrv_block_device_info(bs, errp);
 | 
			
		||||
        if (info->inserted == NULL) {
 | 
			
		||||
            goto err;
 | 
			
		||||
        info->inserted = bdrv_block_device_info(bs);
 | 
			
		||||
 | 
			
		||||
        bs0 = bs;
 | 
			
		||||
        p_image_info = &info->inserted->image;
 | 
			
		||||
        while (1) {
 | 
			
		||||
            bdrv_query_image_info(bs0, p_image_info, &local_err);
 | 
			
		||||
            if (local_err) {
 | 
			
		||||
                error_propagate(errp, local_err);
 | 
			
		||||
                goto err;
 | 
			
		||||
            }
 | 
			
		||||
            if (bs0->drv && bs0->backing_hd) {
 | 
			
		||||
                bs0 = bs0->backing_hd;
 | 
			
		||||
                (*p_image_info)->has_backing_image = true;
 | 
			
		||||
                p_image_info = &((*p_image_info)->backing_image);
 | 
			
		||||
            } else {
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -520,9 +510,18 @@ static void dump_qobject(fprintf_function func_fprintf, void *f,
 | 
			
		||||
        }
 | 
			
		||||
        case QTYPE_QBOOL: {
 | 
			
		||||
            QBool *value = qobject_to_qbool(obj);
 | 
			
		||||
            func_fprintf(f, "%s", qbool_get_bool(value) ? "true" : "false");
 | 
			
		||||
            func_fprintf(f, "%s", qbool_get_int(value) ? "true" : "false");
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
        case QTYPE_QERROR: {
 | 
			
		||||
            QString *value = qerror_human((QError *)obj);
 | 
			
		||||
            func_fprintf(f, "%s", qstring_get_str(value));
 | 
			
		||||
            QDECREF(value);
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
        case QTYPE_NONE:
 | 
			
		||||
            break;
 | 
			
		||||
        case QTYPE_MAX:
 | 
			
		||||
        default:
 | 
			
		||||
            abort();
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										21
									
								
								block/qcow.c
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								block/qcow.c
									
									
									
									
									
								
							@@ -25,7 +25,6 @@
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
#include "qemu/module.h"
 | 
			
		||||
#include <zlib.h>
 | 
			
		||||
#include "qapi/qmp/qerror.h"
 | 
			
		||||
#include "qemu/aes.h"
 | 
			
		||||
#include "migration/migration.h"
 | 
			
		||||
 | 
			
		||||
@@ -124,8 +123,8 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
        char version[64];
 | 
			
		||||
        snprintf(version, sizeof(version), "QCOW version %" PRIu32,
 | 
			
		||||
                 header.version);
 | 
			
		||||
        error_setg(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
 | 
			
		||||
                   bdrv_get_device_or_node_name(bs), "qcow", version);
 | 
			
		||||
        error_set(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
 | 
			
		||||
                  bdrv_get_device_name(bs), "qcow", version);
 | 
			
		||||
        ret = -ENOTSUP;
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
@@ -230,9 +229,9 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Disable migration when qcow images are used */
 | 
			
		||||
    error_setg(&s->migration_blocker, "The qcow format used by node '%s' "
 | 
			
		||||
               "does not support live migration",
 | 
			
		||||
               bdrv_get_device_or_node_name(bs));
 | 
			
		||||
    error_set(&s->migration_blocker,
 | 
			
		||||
              QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
 | 
			
		||||
              "qcow", bdrv_get_device_name(bs), "live migration");
 | 
			
		||||
    migrate_add_blocker(s->migration_blocker);
 | 
			
		||||
 | 
			
		||||
    qemu_co_mutex_init(&s->lock);
 | 
			
		||||
@@ -270,7 +269,6 @@ static int qcow_set_key(BlockDriverState *bs, const char *key)
 | 
			
		||||
    for(i = 0;i < len;i++) {
 | 
			
		||||
        keybuf[i] = key[i];
 | 
			
		||||
    }
 | 
			
		||||
    assert(bs->encrypted);
 | 
			
		||||
    s->crypt_method = s->crypt_method_header;
 | 
			
		||||
 | 
			
		||||
    if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
 | 
			
		||||
@@ -413,10 +411,9 @@ static uint64_t get_cluster_offset(BlockDriverState *bs,
 | 
			
		||||
                bdrv_truncate(bs->file, cluster_offset + s->cluster_size);
 | 
			
		||||
                /* if encrypted, we must initialize the cluster
 | 
			
		||||
                   content which won't be written */
 | 
			
		||||
                if (bs->encrypted &&
 | 
			
		||||
                if (s->crypt_method &&
 | 
			
		||||
                    (n_end - n_start) < s->cluster_sectors) {
 | 
			
		||||
                    uint64_t start_sect;
 | 
			
		||||
                    assert(s->crypt_method);
 | 
			
		||||
                    start_sect = (offset & ~(s->cluster_size - 1)) >> 9;
 | 
			
		||||
                    memset(s->cluster_data + 512, 0x00, 512);
 | 
			
		||||
                    for(i = 0; i < s->cluster_sectors; i++) {
 | 
			
		||||
@@ -593,8 +590,7 @@ static coroutine_fn int qcow_co_readv(BlockDriverState *bs, int64_t sector_num,
 | 
			
		||||
            if (ret < 0) {
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
            if (bs->encrypted) {
 | 
			
		||||
                assert(s->crypt_method);
 | 
			
		||||
            if (s->crypt_method) {
 | 
			
		||||
                encrypt_sectors(s, sector_num, buf, buf,
 | 
			
		||||
                                n, 0,
 | 
			
		||||
                                &s->aes_decrypt_key);
 | 
			
		||||
@@ -665,8 +661,7 @@ static coroutine_fn int qcow_co_writev(BlockDriverState *bs, int64_t sector_num,
 | 
			
		||||
            ret = -EIO;
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
        if (bs->encrypted) {
 | 
			
		||||
            assert(s->crypt_method);
 | 
			
		||||
        if (s->crypt_method) {
 | 
			
		||||
            if (!cluster_data) {
 | 
			
		||||
                cluster_data = g_malloc0(s->cluster_size);
 | 
			
		||||
            }
 | 
			
		||||
 
 | 
			
		||||
@@ -28,68 +28,62 @@
 | 
			
		||||
#include "trace.h"
 | 
			
		||||
 | 
			
		||||
typedef struct Qcow2CachedTable {
 | 
			
		||||
    int64_t  offset;
 | 
			
		||||
    bool     dirty;
 | 
			
		||||
    uint64_t lru_counter;
 | 
			
		||||
    int      ref;
 | 
			
		||||
    void*   table;
 | 
			
		||||
    int64_t offset;
 | 
			
		||||
    bool    dirty;
 | 
			
		||||
    int     cache_hits;
 | 
			
		||||
    int     ref;
 | 
			
		||||
} Qcow2CachedTable;
 | 
			
		||||
 | 
			
		||||
struct Qcow2Cache {
 | 
			
		||||
    Qcow2CachedTable       *entries;
 | 
			
		||||
    struct Qcow2Cache      *depends;
 | 
			
		||||
    Qcow2CachedTable*       entries;
 | 
			
		||||
    struct Qcow2Cache*      depends;
 | 
			
		||||
    int                     size;
 | 
			
		||||
    bool                    depends_on_flush;
 | 
			
		||||
    void                   *table_array;
 | 
			
		||||
    uint64_t                lru_counter;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static inline void *qcow2_cache_get_table_addr(BlockDriverState *bs,
 | 
			
		||||
                    Qcow2Cache *c, int table)
 | 
			
		||||
{
 | 
			
		||||
    BDRVQcowState *s = bs->opaque;
 | 
			
		||||
    return (uint8_t *) c->table_array + (size_t) table * s->cluster_size;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static inline int qcow2_cache_get_table_idx(BlockDriverState *bs,
 | 
			
		||||
                  Qcow2Cache *c, void *table)
 | 
			
		||||
{
 | 
			
		||||
    BDRVQcowState *s = bs->opaque;
 | 
			
		||||
    ptrdiff_t table_offset = (uint8_t *) table - (uint8_t *) c->table_array;
 | 
			
		||||
    int idx = table_offset / s->cluster_size;
 | 
			
		||||
    assert(idx >= 0 && idx < c->size && table_offset % s->cluster_size == 0);
 | 
			
		||||
    return idx;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables)
 | 
			
		||||
{
 | 
			
		||||
    BDRVQcowState *s = bs->opaque;
 | 
			
		||||
    Qcow2Cache *c;
 | 
			
		||||
    int i;
 | 
			
		||||
 | 
			
		||||
    c = g_new0(Qcow2Cache, 1);
 | 
			
		||||
    c->size = num_tables;
 | 
			
		||||
    c->entries = g_try_new0(Qcow2CachedTable, num_tables);
 | 
			
		||||
    c->table_array = qemu_try_blockalign(bs->file,
 | 
			
		||||
                                         (size_t) num_tables * s->cluster_size);
 | 
			
		||||
    if (!c->entries) {
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!c->entries || !c->table_array) {
 | 
			
		||||
        qemu_vfree(c->table_array);
 | 
			
		||||
        g_free(c->entries);
 | 
			
		||||
        g_free(c);
 | 
			
		||||
        c = NULL;
 | 
			
		||||
    for (i = 0; i < c->size; i++) {
 | 
			
		||||
        c->entries[i].table = qemu_try_blockalign(bs->file, s->cluster_size);
 | 
			
		||||
        if (c->entries[i].table == NULL) {
 | 
			
		||||
            goto fail;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return c;
 | 
			
		||||
 | 
			
		||||
fail:
 | 
			
		||||
    if (c->entries) {
 | 
			
		||||
        for (i = 0; i < c->size; i++) {
 | 
			
		||||
            qemu_vfree(c->entries[i].table);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    g_free(c->entries);
 | 
			
		||||
    g_free(c);
 | 
			
		||||
    return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int qcow2_cache_destroy(BlockDriverState *bs, Qcow2Cache *c)
 | 
			
		||||
int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c)
 | 
			
		||||
{
 | 
			
		||||
    int i;
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < c->size; i++) {
 | 
			
		||||
        assert(c->entries[i].ref == 0);
 | 
			
		||||
        qemu_vfree(c->entries[i].table);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    qemu_vfree(c->table_array);
 | 
			
		||||
    g_free(c->entries);
 | 
			
		||||
    g_free(c);
 | 
			
		||||
 | 
			
		||||
@@ -157,8 +151,8 @@ static int qcow2_cache_entry_flush(BlockDriverState *bs, Qcow2Cache *c, int i)
 | 
			
		||||
        BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ret = bdrv_pwrite(bs->file, c->entries[i].offset,
 | 
			
		||||
                      qcow2_cache_get_table_addr(bs, c, i), s->cluster_size);
 | 
			
		||||
    ret = bdrv_pwrite(bs->file, c->entries[i].offset, c->entries[i].table,
 | 
			
		||||
        s->cluster_size);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
@@ -234,51 +228,63 @@ int qcow2_cache_empty(BlockDriverState *bs, Qcow2Cache *c)
 | 
			
		||||
    for (i = 0; i < c->size; i++) {
 | 
			
		||||
        assert(c->entries[i].ref == 0);
 | 
			
		||||
        c->entries[i].offset = 0;
 | 
			
		||||
        c->entries[i].lru_counter = 0;
 | 
			
		||||
        c->entries[i].cache_hits = 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    c->lru_counter = 0;
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int qcow2_cache_find_entry_to_replace(Qcow2Cache *c)
 | 
			
		||||
{
 | 
			
		||||
    int i;
 | 
			
		||||
    int min_count = INT_MAX;
 | 
			
		||||
    int min_index = -1;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < c->size; i++) {
 | 
			
		||||
        if (c->entries[i].ref) {
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (c->entries[i].cache_hits < min_count) {
 | 
			
		||||
            min_index = i;
 | 
			
		||||
            min_count = c->entries[i].cache_hits;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /* Give newer hits priority */
 | 
			
		||||
        /* TODO Check how to optimize the replacement strategy */
 | 
			
		||||
        if (c->entries[i].cache_hits > 1) {
 | 
			
		||||
            c->entries[i].cache_hits /= 2;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (min_index == -1) {
 | 
			
		||||
        /* This can't happen in current synchronous code, but leave the check
 | 
			
		||||
         * here as a reminder for whoever starts using AIO with the cache */
 | 
			
		||||
        abort();
 | 
			
		||||
    }
 | 
			
		||||
    return min_index;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int qcow2_cache_do_get(BlockDriverState *bs, Qcow2Cache *c,
 | 
			
		||||
    uint64_t offset, void **table, bool read_from_disk)
 | 
			
		||||
{
 | 
			
		||||
    BDRVQcowState *s = bs->opaque;
 | 
			
		||||
    int i;
 | 
			
		||||
    int ret;
 | 
			
		||||
    int lookup_index;
 | 
			
		||||
    uint64_t min_lru_counter = UINT64_MAX;
 | 
			
		||||
    int min_lru_index = -1;
 | 
			
		||||
 | 
			
		||||
    trace_qcow2_cache_get(qemu_coroutine_self(), c == s->l2_table_cache,
 | 
			
		||||
                          offset, read_from_disk);
 | 
			
		||||
 | 
			
		||||
    /* Check if the table is already cached */
 | 
			
		||||
    i = lookup_index = (offset / s->cluster_size * 4) % c->size;
 | 
			
		||||
    do {
 | 
			
		||||
        const Qcow2CachedTable *t = &c->entries[i];
 | 
			
		||||
        if (t->offset == offset) {
 | 
			
		||||
    for (i = 0; i < c->size; i++) {
 | 
			
		||||
        if (c->entries[i].offset == offset) {
 | 
			
		||||
            goto found;
 | 
			
		||||
        }
 | 
			
		||||
        if (t->ref == 0 && t->lru_counter < min_lru_counter) {
 | 
			
		||||
            min_lru_counter = t->lru_counter;
 | 
			
		||||
            min_lru_index = i;
 | 
			
		||||
        }
 | 
			
		||||
        if (++i == c->size) {
 | 
			
		||||
            i = 0;
 | 
			
		||||
        }
 | 
			
		||||
    } while (i != lookup_index);
 | 
			
		||||
 | 
			
		||||
    if (min_lru_index == -1) {
 | 
			
		||||
        /* This can't happen in current synchronous code, but leave the check
 | 
			
		||||
         * here as a reminder for whoever starts using AIO with the cache */
 | 
			
		||||
        abort();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Cache miss: write a table back and replace it */
 | 
			
		||||
    i = min_lru_index;
 | 
			
		||||
    /* If not, write a table back and replace it */
 | 
			
		||||
    i = qcow2_cache_find_entry_to_replace(c);
 | 
			
		||||
    trace_qcow2_cache_get_replace_entry(qemu_coroutine_self(),
 | 
			
		||||
                                        c == s->l2_table_cache, i);
 | 
			
		||||
    if (i < 0) {
 | 
			
		||||
@@ -298,19 +304,22 @@ static int qcow2_cache_do_get(BlockDriverState *bs, Qcow2Cache *c,
 | 
			
		||||
            BLKDBG_EVENT(bs->file, BLKDBG_L2_LOAD);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        ret = bdrv_pread(bs->file, offset, qcow2_cache_get_table_addr(bs, c, i),
 | 
			
		||||
                         s->cluster_size);
 | 
			
		||||
        ret = bdrv_pread(bs->file, offset, c->entries[i].table, s->cluster_size);
 | 
			
		||||
        if (ret < 0) {
 | 
			
		||||
            return ret;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Give the table some hits for the start so that it won't be replaced
 | 
			
		||||
     * immediately. The number 32 is completely arbitrary. */
 | 
			
		||||
    c->entries[i].cache_hits = 32;
 | 
			
		||||
    c->entries[i].offset = offset;
 | 
			
		||||
 | 
			
		||||
    /* And return the right table */
 | 
			
		||||
found:
 | 
			
		||||
    c->entries[i].cache_hits++;
 | 
			
		||||
    c->entries[i].ref++;
 | 
			
		||||
    *table = qcow2_cache_get_table_addr(bs, c, i);
 | 
			
		||||
    *table = c->entries[i].table;
 | 
			
		||||
 | 
			
		||||
    trace_qcow2_cache_get_done(qemu_coroutine_self(),
 | 
			
		||||
                               c == s->l2_table_cache, i);
 | 
			
		||||
@@ -330,24 +339,36 @@ int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
 | 
			
		||||
    return qcow2_cache_do_get(bs, c, offset, table, false);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table)
 | 
			
		||||
int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table)
 | 
			
		||||
{
 | 
			
		||||
    int i = qcow2_cache_get_table_idx(bs, c, *table);
 | 
			
		||||
    int i;
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < c->size; i++) {
 | 
			
		||||
        if (c->entries[i].table == *table) {
 | 
			
		||||
            goto found;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return -ENOENT;
 | 
			
		||||
 | 
			
		||||
found:
 | 
			
		||||
    c->entries[i].ref--;
 | 
			
		||||
    *table = NULL;
 | 
			
		||||
 | 
			
		||||
    if (c->entries[i].ref == 0) {
 | 
			
		||||
        c->entries[i].lru_counter = ++c->lru_counter;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    assert(c->entries[i].ref >= 0);
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void qcow2_cache_entry_mark_dirty(BlockDriverState *bs, Qcow2Cache *c,
 | 
			
		||||
     void *table)
 | 
			
		||||
void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table)
 | 
			
		||||
{
 | 
			
		||||
    int i = qcow2_cache_get_table_idx(bs, c, table);
 | 
			
		||||
    assert(c->entries[i].offset != 0);
 | 
			
		||||
    int i;
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < c->size; i++) {
 | 
			
		||||
        if (c->entries[i].table == table) {
 | 
			
		||||
            goto found;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    abort();
 | 
			
		||||
 | 
			
		||||
found:
 | 
			
		||||
    c->entries[i].dirty = true;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -253,14 +253,17 @@ static int l2_allocate(BlockDriverState *bs, int l1_index, uint64_t **table)
 | 
			
		||||
 | 
			
		||||
        memcpy(l2_table, old_table, s->cluster_size);
 | 
			
		||||
 | 
			
		||||
        qcow2_cache_put(bs, s->l2_table_cache, (void **) &old_table);
 | 
			
		||||
        ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &old_table);
 | 
			
		||||
        if (ret < 0) {
 | 
			
		||||
            goto fail;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* write the l2 table to the file */
 | 
			
		||||
    BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_WRITE);
 | 
			
		||||
 | 
			
		||||
    trace_qcow2_l2_allocate_write_l2(bs, l1_index);
 | 
			
		||||
    qcow2_cache_entry_mark_dirty(bs, s->l2_table_cache, l2_table);
 | 
			
		||||
    qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
 | 
			
		||||
    ret = qcow2_cache_flush(bs, s->l2_table_cache);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        goto fail;
 | 
			
		||||
@@ -400,8 +403,7 @@ static int coroutine_fn copy_sectors(BlockDriverState *bs,
 | 
			
		||||
        goto out;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (bs->encrypted) {
 | 
			
		||||
        assert(s->crypt_method);
 | 
			
		||||
    if (s->crypt_method) {
 | 
			
		||||
        qcow2_encrypt_sectors(s, start_sect + n_start,
 | 
			
		||||
                        iov.iov_base, iov.iov_base, n, 1,
 | 
			
		||||
                        &s->aes_encrypt_key);
 | 
			
		||||
@@ -690,9 +692,12 @@ uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
 | 
			
		||||
    /* compressed clusters never have the copied flag */
 | 
			
		||||
 | 
			
		||||
    BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE_COMPRESSED);
 | 
			
		||||
    qcow2_cache_entry_mark_dirty(bs, s->l2_table_cache, l2_table);
 | 
			
		||||
    qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
 | 
			
		||||
    l2_table[l2_index] = cpu_to_be64(cluster_offset);
 | 
			
		||||
    qcow2_cache_put(bs, s->l2_table_cache, (void **) &l2_table);
 | 
			
		||||
    ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return cluster_offset;
 | 
			
		||||
}
 | 
			
		||||
@@ -766,7 +771,7 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        goto err;
 | 
			
		||||
    }
 | 
			
		||||
    qcow2_cache_entry_mark_dirty(bs, s->l2_table_cache, l2_table);
 | 
			
		||||
    qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
 | 
			
		||||
 | 
			
		||||
    assert(l2_index + m->nb_clusters <= s->l2_size);
 | 
			
		||||
    for (i = 0; i < m->nb_clusters; i++) {
 | 
			
		||||
@@ -784,7 +789,10 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)
 | 
			
		||||
     }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    qcow2_cache_put(bs, s->l2_table_cache, (void **) &l2_table);
 | 
			
		||||
    ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        goto err;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /*
 | 
			
		||||
     * If this was a COW, we need to decrease the refcount of the old cluster.
 | 
			
		||||
@@ -936,7 +944,7 @@ static int handle_copied(BlockDriverState *bs, uint64_t guest_offset,
 | 
			
		||||
    uint64_t *l2_table;
 | 
			
		||||
    unsigned int nb_clusters;
 | 
			
		||||
    unsigned int keep_clusters;
 | 
			
		||||
    int ret;
 | 
			
		||||
    int ret, pret;
 | 
			
		||||
 | 
			
		||||
    trace_qcow2_handle_copied(qemu_coroutine_self(), guest_offset, *host_offset,
 | 
			
		||||
                              *bytes);
 | 
			
		||||
@@ -1003,7 +1011,10 @@ static int handle_copied(BlockDriverState *bs, uint64_t guest_offset,
 | 
			
		||||
 | 
			
		||||
    /* Cleanup */
 | 
			
		||||
out:
 | 
			
		||||
    qcow2_cache_put(bs, s->l2_table_cache, (void **) &l2_table);
 | 
			
		||||
    pret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
 | 
			
		||||
    if (pret < 0) {
 | 
			
		||||
        return pret;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Only return a host offset if we actually made progress. Otherwise we
 | 
			
		||||
     * would make requirements for handle_alloc() that it can't fulfill */
 | 
			
		||||
@@ -1128,7 +1139,10 @@ static int handle_alloc(BlockDriverState *bs, uint64_t guest_offset,
 | 
			
		||||
     * wrong with our code. */
 | 
			
		||||
    assert(nb_clusters > 0);
 | 
			
		||||
 | 
			
		||||
    qcow2_cache_put(bs, s->l2_table_cache, (void **) &l2_table);
 | 
			
		||||
    ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Allocate, if necessary at a given offset in the image file */
 | 
			
		||||
    alloc_cluster_offset = start_of_cluster(s, *host_offset);
 | 
			
		||||
@@ -1456,7 +1470,7 @@ static int discard_single_l2(BlockDriverState *bs, uint64_t offset,
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /* First remove L2 entries */
 | 
			
		||||
        qcow2_cache_entry_mark_dirty(bs, s->l2_table_cache, l2_table);
 | 
			
		||||
        qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
 | 
			
		||||
        if (!full_discard && s->qcow_version >= 3) {
 | 
			
		||||
            l2_table[l2_index + i] = cpu_to_be64(QCOW_OFLAG_ZERO);
 | 
			
		||||
        } else {
 | 
			
		||||
@@ -1467,7 +1481,10 @@ static int discard_single_l2(BlockDriverState *bs, uint64_t offset,
 | 
			
		||||
        qcow2_free_any_clusters(bs, old_l2_entry, 1, type);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    qcow2_cache_put(bs, s->l2_table_cache, (void **) &l2_table);
 | 
			
		||||
    ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return nb_clusters;
 | 
			
		||||
}
 | 
			
		||||
@@ -1541,7 +1558,7 @@ static int zero_single_l2(BlockDriverState *bs, uint64_t offset,
 | 
			
		||||
        old_offset = be64_to_cpu(l2_table[l2_index + i]);
 | 
			
		||||
 | 
			
		||||
        /* Update L2 entries */
 | 
			
		||||
        qcow2_cache_entry_mark_dirty(bs, s->l2_table_cache, l2_table);
 | 
			
		||||
        qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
 | 
			
		||||
        if (old_offset & QCOW_OFLAG_COMPRESSED) {
 | 
			
		||||
            l2_table[l2_index + i] = cpu_to_be64(QCOW_OFLAG_ZERO);
 | 
			
		||||
            qcow2_free_any_clusters(bs, old_offset, 1, QCOW2_DISCARD_REQUEST);
 | 
			
		||||
@@ -1550,7 +1567,10 @@ static int zero_single_l2(BlockDriverState *bs, uint64_t offset,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    qcow2_cache_put(bs, s->l2_table_cache, (void **) &l2_table);
 | 
			
		||||
    ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return nb_clusters;
 | 
			
		||||
}
 | 
			
		||||
@@ -1740,10 +1760,14 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,
 | 
			
		||||
 | 
			
		||||
        if (is_active_l1) {
 | 
			
		||||
            if (l2_dirty) {
 | 
			
		||||
                qcow2_cache_entry_mark_dirty(bs, s->l2_table_cache, l2_table);
 | 
			
		||||
                qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
 | 
			
		||||
                qcow2_cache_depends_on_flush(s->l2_table_cache);
 | 
			
		||||
            }
 | 
			
		||||
            qcow2_cache_put(bs, s->l2_table_cache, (void **) &l2_table);
 | 
			
		||||
            ret = qcow2_cache_put(bs, s->l2_table_cache, (void **)&l2_table);
 | 
			
		||||
            if (ret < 0) {
 | 
			
		||||
                l2_table = NULL;
 | 
			
		||||
                goto fail;
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            if (l2_dirty) {
 | 
			
		||||
                ret = qcow2_pre_write_overlap_check(bs,
 | 
			
		||||
@@ -1774,7 +1798,12 @@ fail:
 | 
			
		||||
        if (!is_active_l1) {
 | 
			
		||||
            qemu_vfree(l2_table);
 | 
			
		||||
        } else {
 | 
			
		||||
            qcow2_cache_put(bs, s->l2_table_cache, (void **) &l2_table);
 | 
			
		||||
            if (ret < 0) {
 | 
			
		||||
                qcow2_cache_put(bs, s->l2_table_cache, (void **)&l2_table);
 | 
			
		||||
            } else {
 | 
			
		||||
                ret = qcow2_cache_put(bs, s->l2_table_cache,
 | 
			
		||||
                        (void **)&l2_table);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return ret;
 | 
			
		||||
 
 | 
			
		||||
@@ -265,7 +265,10 @@ int qcow2_get_refcount(BlockDriverState *bs, int64_t cluster_index,
 | 
			
		||||
    block_index = cluster_index & (s->refcount_block_size - 1);
 | 
			
		||||
    *refcount = s->get_refcount(refcount_block, block_index);
 | 
			
		||||
 | 
			
		||||
    qcow2_cache_put(bs, s->refcount_block_cache, &refcount_block);
 | 
			
		||||
    ret = qcow2_cache_put(bs, s->refcount_block_cache, &refcount_block);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
@@ -421,7 +424,7 @@ static int alloc_refcount_block(BlockDriverState *bs,
 | 
			
		||||
 | 
			
		||||
    /* Now the new refcount block needs to be written to disk */
 | 
			
		||||
    BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC_WRITE);
 | 
			
		||||
    qcow2_cache_entry_mark_dirty(bs, s->refcount_block_cache, *refcount_block);
 | 
			
		||||
    qcow2_cache_entry_mark_dirty(s->refcount_block_cache, *refcount_block);
 | 
			
		||||
    ret = qcow2_cache_flush(bs, s->refcount_block_cache);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        goto fail_block;
 | 
			
		||||
@@ -445,7 +448,10 @@ static int alloc_refcount_block(BlockDriverState *bs,
 | 
			
		||||
        return -EAGAIN;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    qcow2_cache_put(bs, s->refcount_block_cache, refcount_block);
 | 
			
		||||
    ret = qcow2_cache_put(bs, s->refcount_block_cache, refcount_block);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        goto fail_block;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /*
 | 
			
		||||
     * If we come here, we need to grow the refcount table. Again, a new
 | 
			
		||||
@@ -717,8 +723,13 @@ static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
 | 
			
		||||
        /* Load the refcount block and allocate it if needed */
 | 
			
		||||
        if (table_index != old_table_index) {
 | 
			
		||||
            if (refcount_block) {
 | 
			
		||||
                qcow2_cache_put(bs, s->refcount_block_cache, &refcount_block);
 | 
			
		||||
                ret = qcow2_cache_put(bs, s->refcount_block_cache,
 | 
			
		||||
                                      &refcount_block);
 | 
			
		||||
                if (ret < 0) {
 | 
			
		||||
                    goto fail;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            ret = alloc_refcount_block(bs, cluster_index, &refcount_block);
 | 
			
		||||
            if (ret < 0) {
 | 
			
		||||
                goto fail;
 | 
			
		||||
@@ -726,8 +737,7 @@ static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
 | 
			
		||||
        }
 | 
			
		||||
        old_table_index = table_index;
 | 
			
		||||
 | 
			
		||||
        qcow2_cache_entry_mark_dirty(bs, s->refcount_block_cache,
 | 
			
		||||
                                     refcount_block);
 | 
			
		||||
        qcow2_cache_entry_mark_dirty(s->refcount_block_cache, refcount_block);
 | 
			
		||||
 | 
			
		||||
        /* we can update the count and save it */
 | 
			
		||||
        block_index = cluster_index & (s->refcount_block_size - 1);
 | 
			
		||||
@@ -763,7 +773,11 @@ fail:
 | 
			
		||||
 | 
			
		||||
    /* Write last changed block to disk */
 | 
			
		||||
    if (refcount_block) {
 | 
			
		||||
        qcow2_cache_put(bs, s->refcount_block_cache, &refcount_block);
 | 
			
		||||
        int wret;
 | 
			
		||||
        wret = qcow2_cache_put(bs, s->refcount_block_cache, &refcount_block);
 | 
			
		||||
        if (wret < 0) {
 | 
			
		||||
            return ret < 0 ? ret : wret;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /*
 | 
			
		||||
@@ -949,11 +963,17 @@ int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size)
 | 
			
		||||
 | 
			
		||||
            if (!offset || ROUND_UP(offset, s->cluster_size) != new_cluster) {
 | 
			
		||||
                offset = new_cluster;
 | 
			
		||||
                free_in_cluster = s->cluster_size;
 | 
			
		||||
            } else {
 | 
			
		||||
                free_in_cluster += s->cluster_size;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        assert(offset);
 | 
			
		||||
        ret = update_refcount(bs, offset, size, 1, false, QCOW2_DISCARD_NEVER);
 | 
			
		||||
        if (ret < 0) {
 | 
			
		||||
            offset = 0;
 | 
			
		||||
        }
 | 
			
		||||
    } while (ret == -EAGAIN);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        return ret;
 | 
			
		||||
@@ -1170,12 +1190,15 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
 | 
			
		||||
                            s->refcount_block_cache);
 | 
			
		||||
                    }
 | 
			
		||||
                    l2_table[j] = cpu_to_be64(offset);
 | 
			
		||||
                    qcow2_cache_entry_mark_dirty(bs, s->l2_table_cache,
 | 
			
		||||
                                                 l2_table);
 | 
			
		||||
                    qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            qcow2_cache_put(bs, s->l2_table_cache, (void **) &l2_table);
 | 
			
		||||
            ret = qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
 | 
			
		||||
            if (ret < 0) {
 | 
			
		||||
                goto fail;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
            if (addend != 0) {
 | 
			
		||||
                ret = qcow2_update_cluster_refcount(bs, l2_offset >>
 | 
			
		||||
@@ -2440,7 +2463,7 @@ int qcow2_pre_write_overlap_check(BlockDriverState *bs, int ign, int64_t offset,
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        return ret;
 | 
			
		||||
    } else if (ret > 0) {
 | 
			
		||||
        int metadata_ol_bitnr = ctz32(ret);
 | 
			
		||||
        int metadata_ol_bitnr = ffs(ret) - 1;
 | 
			
		||||
        assert(metadata_ol_bitnr < QCOW2_OL_MAX_BITNR);
 | 
			
		||||
 | 
			
		||||
        qcow2_signal_corruption(bs, true, offset, size, "Preventing invalid "
 | 
			
		||||
 
 | 
			
		||||
@@ -25,7 +25,6 @@
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
#include "block/qcow2.h"
 | 
			
		||||
#include "qemu/error-report.h"
 | 
			
		||||
 | 
			
		||||
void qcow2_free_snapshots(BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
@@ -352,8 +351,10 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
 | 
			
		||||
 | 
			
		||||
    memset(sn, 0, sizeof(*sn));
 | 
			
		||||
 | 
			
		||||
    /* Generate an ID */
 | 
			
		||||
    find_new_snapshot_id(bs, sn_info->id_str, sizeof(sn_info->id_str));
 | 
			
		||||
    /* Generate an ID if it wasn't passed */
 | 
			
		||||
    if (sn_info->id_str[0] == '\0') {
 | 
			
		||||
        find_new_snapshot_id(bs, sn_info->id_str, sizeof(sn_info->id_str));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Check that the ID is unique */
 | 
			
		||||
    if (find_snapshot_by_id_and_name(bs, sn_info->id_str, NULL) >= 0) {
 | 
			
		||||
 
 | 
			
		||||
@@ -207,8 +207,8 @@ static void GCC_FMT_ATTR(3, 4) report_unsupported(BlockDriverState *bs,
 | 
			
		||||
    vsnprintf(msg, sizeof(msg), fmt, ap);
 | 
			
		||||
    va_end(ap);
 | 
			
		||||
 | 
			
		||||
    error_setg(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
 | 
			
		||||
               bdrv_get_device_or_node_name(bs), "qcow2", msg);
 | 
			
		||||
    error_set(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
 | 
			
		||||
              bdrv_get_device_name(bs), "qcow2", msg);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void report_unsupported_feature(BlockDriverState *bs,
 | 
			
		||||
@@ -483,11 +483,9 @@ static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = {
 | 
			
		||||
    [QCOW2_OL_INACTIVE_L2_BITNR]    = QCOW2_OPT_OVERLAP_INACTIVE_L2,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void read_cache_sizes(BlockDriverState *bs, QemuOpts *opts,
 | 
			
		||||
                             uint64_t *l2_cache_size,
 | 
			
		||||
static void read_cache_sizes(QemuOpts *opts, uint64_t *l2_cache_size,
 | 
			
		||||
                             uint64_t *refcount_cache_size, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    BDRVQcowState *s = bs->opaque;
 | 
			
		||||
    uint64_t combined_cache_size;
 | 
			
		||||
    bool l2_cache_size_set, refcount_cache_size_set, combined_cache_size_set;
 | 
			
		||||
 | 
			
		||||
@@ -527,9 +525,7 @@ static void read_cache_sizes(BlockDriverState *bs, QemuOpts *opts,
 | 
			
		||||
        }
 | 
			
		||||
    } else {
 | 
			
		||||
        if (!l2_cache_size_set && !refcount_cache_size_set) {
 | 
			
		||||
            *l2_cache_size = MAX(DEFAULT_L2_CACHE_BYTE_SIZE,
 | 
			
		||||
                                 (uint64_t)DEFAULT_L2_CACHE_CLUSTERS
 | 
			
		||||
                                 * s->cluster_size);
 | 
			
		||||
            *l2_cache_size = DEFAULT_L2_CACHE_BYTE_SIZE;
 | 
			
		||||
            *refcount_cache_size = *l2_cache_size
 | 
			
		||||
                                 / DEFAULT_L2_REFCOUNT_SIZE_RATIO;
 | 
			
		||||
        } else if (!l2_cache_size_set) {
 | 
			
		||||
@@ -807,8 +803,7 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    read_cache_sizes(bs, opts, &l2_cache_size, &refcount_cache_size,
 | 
			
		||||
                     &local_err);
 | 
			
		||||
    read_cache_sizes(opts, &l2_cache_size, &refcount_cache_size, &local_err);
 | 
			
		||||
    if (local_err) {
 | 
			
		||||
        error_propagate(errp, local_err);
 | 
			
		||||
        ret = -EINVAL;
 | 
			
		||||
@@ -1042,7 +1037,6 @@ static int qcow2_set_key(BlockDriverState *bs, const char *key)
 | 
			
		||||
    for(i = 0;i < len;i++) {
 | 
			
		||||
        keybuf[i] = key[i];
 | 
			
		||||
    }
 | 
			
		||||
    assert(bs->encrypted);
 | 
			
		||||
    s->crypt_method = s->crypt_method_header;
 | 
			
		||||
 | 
			
		||||
    if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
 | 
			
		||||
@@ -1230,9 +1224,7 @@ static coroutine_fn int qcow2_co_readv(BlockDriverState *bs, int64_t sector_num,
 | 
			
		||||
                goto fail;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (bs->encrypted) {
 | 
			
		||||
                assert(s->crypt_method);
 | 
			
		||||
 | 
			
		||||
            if (s->crypt_method) {
 | 
			
		||||
                /*
 | 
			
		||||
                 * For encrypted images, read everything into a temporary
 | 
			
		||||
                 * contiguous buffer on which the AES functions can work.
 | 
			
		||||
@@ -1263,8 +1255,7 @@ static coroutine_fn int qcow2_co_readv(BlockDriverState *bs, int64_t sector_num,
 | 
			
		||||
            if (ret < 0) {
 | 
			
		||||
                goto fail;
 | 
			
		||||
            }
 | 
			
		||||
            if (bs->encrypted) {
 | 
			
		||||
                assert(s->crypt_method);
 | 
			
		||||
            if (s->crypt_method) {
 | 
			
		||||
                qcow2_encrypt_sectors(s, sector_num,  cluster_data,
 | 
			
		||||
                    cluster_data, cur_nr_sectors, 0, &s->aes_decrypt_key);
 | 
			
		||||
                qemu_iovec_from_buf(qiov, bytes_done,
 | 
			
		||||
@@ -1324,7 +1315,7 @@ static coroutine_fn int qcow2_co_writev(BlockDriverState *bs,
 | 
			
		||||
        trace_qcow2_writev_start_part(qemu_coroutine_self());
 | 
			
		||||
        index_in_cluster = sector_num & (s->cluster_sectors - 1);
 | 
			
		||||
        cur_nr_sectors = remaining_sectors;
 | 
			
		||||
        if (bs->encrypted &&
 | 
			
		||||
        if (s->crypt_method &&
 | 
			
		||||
            cur_nr_sectors >
 | 
			
		||||
            QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors - index_in_cluster) {
 | 
			
		||||
            cur_nr_sectors =
 | 
			
		||||
@@ -1343,8 +1334,7 @@ static coroutine_fn int qcow2_co_writev(BlockDriverState *bs,
 | 
			
		||||
        qemu_iovec_concat(&hd_qiov, qiov, bytes_done,
 | 
			
		||||
            cur_nr_sectors * 512);
 | 
			
		||||
 | 
			
		||||
        if (bs->encrypted) {
 | 
			
		||||
            assert(s->crypt_method);
 | 
			
		||||
        if (s->crypt_method) {
 | 
			
		||||
            if (!cluster_data) {
 | 
			
		||||
                cluster_data = qemu_try_blockalign(bs->file,
 | 
			
		||||
                                                   QCOW_MAX_CRYPT_CLUSTERS
 | 
			
		||||
@@ -1494,8 +1484,7 @@ static void qcow2_invalidate_cache(BlockDriverState *bs, Error **errp)
 | 
			
		||||
     * that means we don't have to worry about reopening them here.
 | 
			
		||||
     */
 | 
			
		||||
 | 
			
		||||
    if (bs->encrypted) {
 | 
			
		||||
        assert(s->crypt_method);
 | 
			
		||||
    if (s->crypt_method) {
 | 
			
		||||
        crypt_method = s->crypt_method;
 | 
			
		||||
        memcpy(&aes_encrypt_key, &s->aes_encrypt_key, sizeof(aes_encrypt_key));
 | 
			
		||||
        memcpy(&aes_decrypt_key, &s->aes_decrypt_key, sizeof(aes_decrypt_key));
 | 
			
		||||
@@ -1524,7 +1513,7 @@ static void qcow2_invalidate_cache(BlockDriverState *bs, Error **errp)
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (bs->encrypted) {
 | 
			
		||||
    if (crypt_method) {
 | 
			
		||||
        s->crypt_method = crypt_method;
 | 
			
		||||
        memcpy(&s->aes_encrypt_key, &aes_encrypt_key, sizeof(aes_encrypt_key));
 | 
			
		||||
        memcpy(&s->aes_decrypt_key, &aes_decrypt_key, sizeof(aes_decrypt_key));
 | 
			
		||||
@@ -1813,7 +1802,7 @@ static int qcow2_create2(const char *filename, int64_t total_size,
 | 
			
		||||
{
 | 
			
		||||
    /* Calculate cluster_bits */
 | 
			
		||||
    int cluster_bits;
 | 
			
		||||
    cluster_bits = ctz32(cluster_size);
 | 
			
		||||
    cluster_bits = ffs(cluster_size) - 1;
 | 
			
		||||
    if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS ||
 | 
			
		||||
        (1 << cluster_bits) != cluster_size)
 | 
			
		||||
    {
 | 
			
		||||
@@ -2121,7 +2110,7 @@ static int qcow2_create(const char *filename, QemuOpts *opts, Error **errp)
 | 
			
		||||
        goto finish;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    refcount_order = ctz32(refcount_bits);
 | 
			
		||||
    refcount_order = ffs(refcount_bits) - 1;
 | 
			
		||||
 | 
			
		||||
    ret = qcow2_create2(filename, size, backing_file, backing_fmt, flags,
 | 
			
		||||
                        cluster_size, prealloc, opts, version, refcount_order,
 | 
			
		||||
@@ -2835,7 +2824,6 @@ void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset,
 | 
			
		||||
                             int64_t size, const char *message_format, ...)
 | 
			
		||||
{
 | 
			
		||||
    BDRVQcowState *s = bs->opaque;
 | 
			
		||||
    const char *node_name;
 | 
			
		||||
    char *message;
 | 
			
		||||
    va_list ap;
 | 
			
		||||
 | 
			
		||||
@@ -2859,11 +2847,8 @@ void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset,
 | 
			
		||||
                "corruption events will be suppressed\n", message);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    node_name = bdrv_get_node_name(bs);
 | 
			
		||||
    qapi_event_send_block_image_corrupted(bdrv_get_device_name(bs),
 | 
			
		||||
                                          *node_name != '\0', node_name,
 | 
			
		||||
                                          message, offset >= 0, offset,
 | 
			
		||||
                                          size >= 0, size,
 | 
			
		||||
    qapi_event_send_block_image_corrupted(bdrv_get_device_name(bs), message,
 | 
			
		||||
                                          offset >= 0, offset, size >= 0, size,
 | 
			
		||||
                                          fatal, &error_abort);
 | 
			
		||||
    g_free(message);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -68,8 +68,6 @@
 | 
			
		||||
/* Must be at least 4 to cover all cases of refcount table growth */
 | 
			
		||||
#define MIN_REFCOUNT_CACHE_SIZE 4 /* clusters */
 | 
			
		||||
 | 
			
		||||
/* Whichever is more */
 | 
			
		||||
#define DEFAULT_L2_CACHE_CLUSTERS 8 /* clusters */
 | 
			
		||||
#define DEFAULT_L2_CACHE_BYTE_SIZE 1048576 /* bytes */
 | 
			
		||||
 | 
			
		||||
/* The refblock cache needs only a fourth of the L2 cache size to cover as many
 | 
			
		||||
@@ -577,8 +575,7 @@ int qcow2_read_snapshots(BlockDriverState *bs);
 | 
			
		||||
Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables);
 | 
			
		||||
int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c);
 | 
			
		||||
 | 
			
		||||
void qcow2_cache_entry_mark_dirty(BlockDriverState *bs, Qcow2Cache *c,
 | 
			
		||||
     void *table);
 | 
			
		||||
void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table);
 | 
			
		||||
int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
 | 
			
		||||
int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
 | 
			
		||||
    Qcow2Cache *dependency);
 | 
			
		||||
@@ -590,6 +587,6 @@ int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
 | 
			
		||||
    void **table);
 | 
			
		||||
int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
 | 
			
		||||
    void **table);
 | 
			
		||||
void qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table);
 | 
			
		||||
int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 
 | 
			
		||||
@@ -407,8 +407,8 @@ static int bdrv_qed_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
        char buf[64];
 | 
			
		||||
        snprintf(buf, sizeof(buf), "%" PRIx64,
 | 
			
		||||
            s->header.features & ~QED_FEATURE_MASK);
 | 
			
		||||
        error_setg(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
 | 
			
		||||
                   bdrv_get_device_or_node_name(bs), "QED", buf);
 | 
			
		||||
        error_set(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
 | 
			
		||||
            bdrv_get_device_name(bs), "QED", buf);
 | 
			
		||||
        return -ENOTSUP;
 | 
			
		||||
    }
 | 
			
		||||
    if (!qed_is_cluster_size_valid(s->header.cluster_size)) {
 | 
			
		||||
@@ -436,9 +436,9 @@ static int bdrv_qed_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
 | 
			
		||||
    s->table_nelems = (s->header.cluster_size * s->header.table_size) /
 | 
			
		||||
                      sizeof(uint64_t);
 | 
			
		||||
    s->l2_shift = ctz32(s->header.cluster_size);
 | 
			
		||||
    s->l2_shift = ffs(s->header.cluster_size) - 1;
 | 
			
		||||
    s->l2_mask = s->table_nelems - 1;
 | 
			
		||||
    s->l1_shift = s->l2_shift + ctz32(s->table_nelems);
 | 
			
		||||
    s->l1_shift = s->l2_shift + ffs(s->table_nelems) - 1;
 | 
			
		||||
 | 
			
		||||
    /* Header size calculation must not overflow uint32_t */
 | 
			
		||||
    if (s->header.header_size > UINT32_MAX / s->header.cluster_size) {
 | 
			
		||||
 
 | 
			
		||||
@@ -18,7 +18,6 @@
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
#include "qapi/qmp/qbool.h"
 | 
			
		||||
#include "qapi/qmp/qdict.h"
 | 
			
		||||
#include "qapi/qmp/qerror.h"
 | 
			
		||||
#include "qapi/qmp/qint.h"
 | 
			
		||||
#include "qapi/qmp/qjson.h"
 | 
			
		||||
#include "qapi/qmp/qlist.h"
 | 
			
		||||
@@ -227,7 +226,10 @@ static void quorum_report_bad(QuorumAIOCB *acb, char *node_name, int ret)
 | 
			
		||||
 | 
			
		||||
static void quorum_report_failure(QuorumAIOCB *acb)
 | 
			
		||||
{
 | 
			
		||||
    const char *reference = bdrv_get_device_or_node_name(acb->common.bs);
 | 
			
		||||
    const char *reference = bdrv_get_device_name(acb->common.bs)[0] ?
 | 
			
		||||
                            bdrv_get_device_name(acb->common.bs) :
 | 
			
		||||
                            acb->common.bs->node_name;
 | 
			
		||||
 | 
			
		||||
    qapi_event_send_quorum_failure(reference, acb->sector_num,
 | 
			
		||||
                                   acb->nb_sectors, &error_abort);
 | 
			
		||||
}
 | 
			
		||||
@@ -801,8 +803,8 @@ static int quorum_valid_threshold(int threshold, int num_children, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
    if (threshold < 1) {
 | 
			
		||||
        error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
 | 
			
		||||
                   "vote-threshold", "value >= 1");
 | 
			
		||||
        error_set(errp, QERR_INVALID_PARAMETER_VALUE,
 | 
			
		||||
                  "vote-threshold", "value >= 1");
 | 
			
		||||
        return -ERANGE;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -867,18 +869,25 @@ static int quorum_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
    Error *local_err = NULL;
 | 
			
		||||
    QemuOpts *opts = NULL;
 | 
			
		||||
    bool *opened;
 | 
			
		||||
    QDict *sub = NULL;
 | 
			
		||||
    QList *list = NULL;
 | 
			
		||||
    const QListEntry *lentry;
 | 
			
		||||
    int i;
 | 
			
		||||
    int ret = 0;
 | 
			
		||||
 | 
			
		||||
    qdict_flatten(options);
 | 
			
		||||
    qdict_extract_subqdict(options, &sub, "children.");
 | 
			
		||||
    qdict_array_split(sub, &list);
 | 
			
		||||
 | 
			
		||||
    /* count how many different children are present */
 | 
			
		||||
    s->num_children = qdict_array_entries(options, "children.");
 | 
			
		||||
    if (s->num_children < 0) {
 | 
			
		||||
        error_setg(&local_err, "Option children is not a valid array");
 | 
			
		||||
    if (qdict_size(sub)) {
 | 
			
		||||
        error_setg(&local_err, "Invalid option children.%s",
 | 
			
		||||
                   qdict_first(sub)->key);
 | 
			
		||||
        ret = -EINVAL;
 | 
			
		||||
        goto exit;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* count how many different children are present */
 | 
			
		||||
    s->num_children = qlist_size(list);
 | 
			
		||||
    if (s->num_children < 2) {
 | 
			
		||||
        error_setg(&local_err,
 | 
			
		||||
                   "Number of provided children must be greater than 1");
 | 
			
		||||
@@ -931,17 +940,37 @@ static int quorum_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
    s->bs = g_new0(BlockDriverState *, s->num_children);
 | 
			
		||||
    opened = g_new0(bool, s->num_children);
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < s->num_children; i++) {
 | 
			
		||||
        char indexstr[32];
 | 
			
		||||
        ret = snprintf(indexstr, 32, "children.%d", i);
 | 
			
		||||
        assert(ret < 32);
 | 
			
		||||
    for (i = 0, lentry = qlist_first(list); lentry;
 | 
			
		||||
         lentry = qlist_next(lentry), i++) {
 | 
			
		||||
        QDict *d;
 | 
			
		||||
        QString *string;
 | 
			
		||||
 | 
			
		||||
        switch (qobject_type(lentry->value))
 | 
			
		||||
        {
 | 
			
		||||
            /* List of options */
 | 
			
		||||
            case QTYPE_QDICT:
 | 
			
		||||
                d = qobject_to_qdict(lentry->value);
 | 
			
		||||
                QINCREF(d);
 | 
			
		||||
                ret = bdrv_open(&s->bs[i], NULL, NULL, d, flags, NULL,
 | 
			
		||||
                                &local_err);
 | 
			
		||||
                break;
 | 
			
		||||
 | 
			
		||||
            /* QMP reference */
 | 
			
		||||
            case QTYPE_QSTRING:
 | 
			
		||||
                string = qobject_to_qstring(lentry->value);
 | 
			
		||||
                ret = bdrv_open(&s->bs[i], NULL, qstring_get_str(string), NULL,
 | 
			
		||||
                                flags, NULL, &local_err);
 | 
			
		||||
                break;
 | 
			
		||||
 | 
			
		||||
            default:
 | 
			
		||||
                error_setg(&local_err, "Specification of child block device %i "
 | 
			
		||||
                           "is invalid", i);
 | 
			
		||||
                ret = -EINVAL;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        ret = bdrv_open_image(&s->bs[i], NULL, options, indexstr, bs,
 | 
			
		||||
                              &child_format, false, &local_err);
 | 
			
		||||
        if (ret < 0) {
 | 
			
		||||
            goto close_exit;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        opened[i] = true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -964,6 +993,8 @@ exit:
 | 
			
		||||
    if (local_err) {
 | 
			
		||||
        error_propagate(errp, local_err);
 | 
			
		||||
    }
 | 
			
		||||
    QDECREF(list);
 | 
			
		||||
    QDECREF(sub);
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -1025,9 +1056,9 @@ static void quorum_refresh_filename(BlockDriverState *bs)
 | 
			
		||||
    qdict_put_obj(opts, QUORUM_OPT_VOTE_THRESHOLD,
 | 
			
		||||
                  QOBJECT(qint_from_int(s->threshold)));
 | 
			
		||||
    qdict_put_obj(opts, QUORUM_OPT_BLKVERIFY,
 | 
			
		||||
                  QOBJECT(qbool_from_bool(s->is_blkverify)));
 | 
			
		||||
                  QOBJECT(qbool_from_int(s->is_blkverify)));
 | 
			
		||||
    qdict_put_obj(opts, QUORUM_OPT_REWRITE,
 | 
			
		||||
                  QOBJECT(qbool_from_bool(s->rewrite_corrupted)));
 | 
			
		||||
                  QOBJECT(qbool_from_int(s->rewrite_corrupted)));
 | 
			
		||||
    qdict_put_obj(opts, "children", QOBJECT(children));
 | 
			
		||||
 | 
			
		||||
    bs->full_open_options = opts;
 | 
			
		||||
 
 | 
			
		||||
@@ -22,7 +22,6 @@
 | 
			
		||||
 * THE SOFTWARE.
 | 
			
		||||
 */
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "qemu/error-report.h"
 | 
			
		||||
#include "qemu/timer.h"
 | 
			
		||||
#include "qemu/log.h"
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
@@ -32,7 +31,6 @@
 | 
			
		||||
#include "qemu/iov.h"
 | 
			
		||||
#include "raw-aio.h"
 | 
			
		||||
#include "qapi/util.h"
 | 
			
		||||
#include "qapi/qmp/qstring.h"
 | 
			
		||||
 | 
			
		||||
#if defined(__APPLE__) && (__MACH__)
 | 
			
		||||
#include <paths.h>
 | 
			
		||||
@@ -59,7 +57,6 @@
 | 
			
		||||
#include <linux/fd.h>
 | 
			
		||||
#include <linux/fs.h>
 | 
			
		||||
#include <linux/hdreg.h>
 | 
			
		||||
#include <scsi/sg.h>
 | 
			
		||||
#ifdef __s390__
 | 
			
		||||
#include <asm/dasd.h>
 | 
			
		||||
#endif
 | 
			
		||||
@@ -97,19 +94,15 @@
 | 
			
		||||
#include <xfs/xfs.h>
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
//#define DEBUG_BLOCK
 | 
			
		||||
//#define DEBUG_FLOPPY
 | 
			
		||||
 | 
			
		||||
#ifdef DEBUG_BLOCK
 | 
			
		||||
# define DEBUG_BLOCK_PRINT 1
 | 
			
		||||
//#define DEBUG_BLOCK
 | 
			
		||||
#if defined(DEBUG_BLOCK)
 | 
			
		||||
#define DEBUG_BLOCK_PRINT(formatCstr, ...) do { if (qemu_log_enabled()) \
 | 
			
		||||
    { qemu_log(formatCstr, ## __VA_ARGS__); qemu_log_flush(); } } while (0)
 | 
			
		||||
#else
 | 
			
		||||
# define DEBUG_BLOCK_PRINT 0
 | 
			
		||||
#define DEBUG_BLOCK_PRINT(formatCstr, ...)
 | 
			
		||||
#endif
 | 
			
		||||
#define DPRINTF(fmt, ...) \
 | 
			
		||||
do { \
 | 
			
		||||
    if (DEBUG_BLOCK_PRINT) { \
 | 
			
		||||
        printf(fmt, ## __VA_ARGS__); \
 | 
			
		||||
    } \
 | 
			
		||||
} while (0)
 | 
			
		||||
 | 
			
		||||
/* OS X does not have O_DSYNC */
 | 
			
		||||
#ifndef O_DSYNC
 | 
			
		||||
@@ -308,11 +301,10 @@ static void raw_probe_alignment(BlockDriverState *bs, int fd, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    BDRVRawState *s = bs->opaque;
 | 
			
		||||
    char *buf;
 | 
			
		||||
    size_t max_align = MAX(MAX_BLOCKSIZE, getpagesize());
 | 
			
		||||
 | 
			
		||||
    /* For SCSI generic devices the alignment is not really used.
 | 
			
		||||
    /* For /dev/sg devices the alignment is not really used.
 | 
			
		||||
       With buffered I/O, we don't have any restrictions. */
 | 
			
		||||
    if (bdrv_is_sg(bs) || !s->needs_alignment) {
 | 
			
		||||
    if (bs->sg || !s->needs_alignment) {
 | 
			
		||||
        bs->request_alignment = 1;
 | 
			
		||||
        s->buf_align = 1;
 | 
			
		||||
        return;
 | 
			
		||||
@@ -338,9 +330,9 @@ static void raw_probe_alignment(BlockDriverState *bs, int fd, Error **errp)
 | 
			
		||||
    /* If we could not get the sizes so far, we can only guess them */
 | 
			
		||||
    if (!s->buf_align) {
 | 
			
		||||
        size_t align;
 | 
			
		||||
        buf = qemu_memalign(max_align, 2 * max_align);
 | 
			
		||||
        for (align = 512; align <= max_align; align <<= 1) {
 | 
			
		||||
            if (raw_is_io_aligned(fd, buf + align, max_align)) {
 | 
			
		||||
        buf = qemu_memalign(MAX_BLOCKSIZE, 2 * MAX_BLOCKSIZE);
 | 
			
		||||
        for (align = 512; align <= MAX_BLOCKSIZE; align <<= 1) {
 | 
			
		||||
            if (raw_is_io_aligned(fd, buf + align, MAX_BLOCKSIZE)) {
 | 
			
		||||
                s->buf_align = align;
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
@@ -350,8 +342,8 @@ static void raw_probe_alignment(BlockDriverState *bs, int fd, Error **errp)
 | 
			
		||||
 | 
			
		||||
    if (!bs->request_alignment) {
 | 
			
		||||
        size_t align;
 | 
			
		||||
        buf = qemu_memalign(s->buf_align, max_align);
 | 
			
		||||
        for (align = 512; align <= max_align; align <<= 1) {
 | 
			
		||||
        buf = qemu_memalign(s->buf_align, MAX_BLOCKSIZE);
 | 
			
		||||
        for (align = 512; align <= MAX_BLOCKSIZE; align <<= 1) {
 | 
			
		||||
            if (raw_is_io_aligned(fd, buf, align)) {
 | 
			
		||||
                bs->request_alignment = align;
 | 
			
		||||
                break;
 | 
			
		||||
@@ -733,8 +725,7 @@ static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
 | 
			
		||||
    BDRVRawState *s = bs->opaque;
 | 
			
		||||
 | 
			
		||||
    raw_probe_alignment(bs, s->fd, errp);
 | 
			
		||||
    bs->bl.min_mem_alignment = s->buf_align;
 | 
			
		||||
    bs->bl.opt_mem_alignment = MAX(s->buf_align, getpagesize());
 | 
			
		||||
    bs->bl.opt_mem_alignment = s->buf_align;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int check_for_dasd(int fd)
 | 
			
		||||
@@ -1025,7 +1016,6 @@ static ssize_t handle_aiocb_rw(RawPosixAIOData *aiocb)
 | 
			
		||||
static int xfs_write_zeroes(BDRVRawState *s, int64_t offset, uint64_t bytes)
 | 
			
		||||
{
 | 
			
		||||
    struct xfs_flock64 fl;
 | 
			
		||||
    int err;
 | 
			
		||||
 | 
			
		||||
    memset(&fl, 0, sizeof(fl));
 | 
			
		||||
    fl.l_whence = SEEK_SET;
 | 
			
		||||
@@ -1033,9 +1023,8 @@ static int xfs_write_zeroes(BDRVRawState *s, int64_t offset, uint64_t bytes)
 | 
			
		||||
    fl.l_len = bytes;
 | 
			
		||||
 | 
			
		||||
    if (xfsctl(NULL, s->fd, XFS_IOC_ZERO_RANGE, &fl) < 0) {
 | 
			
		||||
        err = errno;
 | 
			
		||||
        DPRINTF("cannot write zero range (%s)\n", strerror(errno));
 | 
			
		||||
        return -err;
 | 
			
		||||
        DEBUG_BLOCK_PRINT("cannot write zero range (%s)\n", strerror(errno));
 | 
			
		||||
        return -errno;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
@@ -1044,7 +1033,6 @@ static int xfs_write_zeroes(BDRVRawState *s, int64_t offset, uint64_t bytes)
 | 
			
		||||
static int xfs_discard(BDRVRawState *s, int64_t offset, uint64_t bytes)
 | 
			
		||||
{
 | 
			
		||||
    struct xfs_flock64 fl;
 | 
			
		||||
    int err;
 | 
			
		||||
 | 
			
		||||
    memset(&fl, 0, sizeof(fl));
 | 
			
		||||
    fl.l_whence = SEEK_SET;
 | 
			
		||||
@@ -1052,9 +1040,8 @@ static int xfs_discard(BDRVRawState *s, int64_t offset, uint64_t bytes)
 | 
			
		||||
    fl.l_len = bytes;
 | 
			
		||||
 | 
			
		||||
    if (xfsctl(NULL, s->fd, XFS_IOC_UNRESVSP64, &fl) < 0) {
 | 
			
		||||
        err = errno;
 | 
			
		||||
        DPRINTF("cannot punch hole (%s)\n", strerror(errno));
 | 
			
		||||
        return -err;
 | 
			
		||||
        DEBUG_BLOCK_PRINT("cannot punch hole (%s)\n", strerror(errno));
 | 
			
		||||
        return -errno;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
@@ -1859,9 +1846,8 @@ static int64_t coroutine_fn raw_co_get_block_status(BlockDriverState *bs,
 | 
			
		||||
        *pnum = nb_sectors;
 | 
			
		||||
        ret = BDRV_BLOCK_DATA;
 | 
			
		||||
    } else if (data == start) {
 | 
			
		||||
        /* On a data extent, compute sectors to the end of the extent,
 | 
			
		||||
         * possibly including a partial sector at EOF. */
 | 
			
		||||
        *pnum = MIN(nb_sectors, DIV_ROUND_UP(hole - start, BDRV_SECTOR_SIZE));
 | 
			
		||||
        /* On a data extent, compute sectors to the end of the extent.  */
 | 
			
		||||
        *pnum = MIN(nb_sectors, (hole - start) / BDRV_SECTOR_SIZE);
 | 
			
		||||
        ret = BDRV_BLOCK_DATA;
 | 
			
		||||
    } else {
 | 
			
		||||
        /* On a hole, compute sectors to the beginning of the next extent.  */
 | 
			
		||||
@@ -2087,38 +2073,15 @@ static void hdev_parse_filename(const char *filename, QDict *options,
 | 
			
		||||
    qdict_put_obj(options, "filename", QOBJECT(qstring_from_str(filename)));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool hdev_is_sg(BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
#if defined(__linux__)
 | 
			
		||||
 | 
			
		||||
    struct stat st;
 | 
			
		||||
    struct sg_scsi_id scsiid;
 | 
			
		||||
    int sg_version;
 | 
			
		||||
 | 
			
		||||
    if (stat(bs->filename, &st) >= 0 && S_ISCHR(st.st_mode) &&
 | 
			
		||||
        !bdrv_ioctl(bs, SG_GET_VERSION_NUM, &sg_version) &&
 | 
			
		||||
        !bdrv_ioctl(bs, SG_GET_SCSI_ID, &scsiid)) {
 | 
			
		||||
        DPRINTF("SG device found: type=%d, version=%d\n",
 | 
			
		||||
            scsiid.scsi_type, sg_version);
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
                     Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    BDRVRawState *s = bs->opaque;
 | 
			
		||||
    Error *local_err = NULL;
 | 
			
		||||
    int ret;
 | 
			
		||||
 | 
			
		||||
#if defined(__APPLE__) && defined(__MACH__)
 | 
			
		||||
    const char *filename = qdict_get_str(options, "filename");
 | 
			
		||||
 | 
			
		||||
#if defined(__APPLE__) && defined(__MACH__)
 | 
			
		||||
    if (strstart(filename, "/dev/cdrom", NULL)) {
 | 
			
		||||
        kern_return_t kernResult;
 | 
			
		||||
        io_iterator_t mediaIterator;
 | 
			
		||||
@@ -2147,6 +2110,16 @@ static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    s->type = FTYPE_FILE;
 | 
			
		||||
#if defined(__linux__)
 | 
			
		||||
    {
 | 
			
		||||
        char resolved_path[ MAXPATHLEN ], *temp;
 | 
			
		||||
 | 
			
		||||
        temp = realpath(filename, resolved_path);
 | 
			
		||||
        if (temp && strstart(temp, "/dev/sg", NULL)) {
 | 
			
		||||
            bs->sg = 1;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    ret = raw_open_common(bs, options, flags, 0, &local_err);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
@@ -2156,9 +2129,6 @@ static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Since this does ioctl the device must be already opened */
 | 
			
		||||
    bs->sg = hdev_is_sg(bs);
 | 
			
		||||
 | 
			
		||||
    if (flags & BDRV_O_RDWR) {
 | 
			
		||||
        ret = check_hdev_writable(s);
 | 
			
		||||
        if (ret < 0) {
 | 
			
		||||
@@ -2187,12 +2157,16 @@ static int fd_open(BlockDriverState *bs)
 | 
			
		||||
        (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
 | 
			
		||||
        qemu_close(s->fd);
 | 
			
		||||
        s->fd = -1;
 | 
			
		||||
        DPRINTF("Floppy closed\n");
 | 
			
		||||
#ifdef DEBUG_FLOPPY
 | 
			
		||||
        printf("Floppy closed\n");
 | 
			
		||||
#endif
 | 
			
		||||
    }
 | 
			
		||||
    if (s->fd < 0) {
 | 
			
		||||
        if (s->fd_got_error &&
 | 
			
		||||
            (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
 | 
			
		||||
            DPRINTF("No floppy (open delayed)\n");
 | 
			
		||||
#ifdef DEBUG_FLOPPY
 | 
			
		||||
            printf("No floppy (open delayed)\n");
 | 
			
		||||
#endif
 | 
			
		||||
            return -EIO;
 | 
			
		||||
        }
 | 
			
		||||
        s->fd = qemu_open(bs->filename, s->open_flags & ~O_NONBLOCK);
 | 
			
		||||
@@ -2201,10 +2175,14 @@ static int fd_open(BlockDriverState *bs)
 | 
			
		||||
            s->fd_got_error = 1;
 | 
			
		||||
            if (last_media_present)
 | 
			
		||||
                s->fd_media_changed = 1;
 | 
			
		||||
            DPRINTF("No floppy\n");
 | 
			
		||||
#ifdef DEBUG_FLOPPY
 | 
			
		||||
            printf("No floppy\n");
 | 
			
		||||
#endif
 | 
			
		||||
            return -EIO;
 | 
			
		||||
        }
 | 
			
		||||
        DPRINTF("Floppy opened\n");
 | 
			
		||||
#ifdef DEBUG_FLOPPY
 | 
			
		||||
        printf("Floppy opened\n");
 | 
			
		||||
#endif
 | 
			
		||||
    }
 | 
			
		||||
    if (!last_media_present)
 | 
			
		||||
        s->fd_media_changed = 1;
 | 
			
		||||
@@ -2472,7 +2450,9 @@ static int floppy_media_changed(BlockDriverState *bs)
 | 
			
		||||
    fd_open(bs);
 | 
			
		||||
    ret = s->fd_media_changed;
 | 
			
		||||
    s->fd_media_changed = 0;
 | 
			
		||||
    DPRINTF("Floppy changed=%d\n", ret);
 | 
			
		||||
#ifdef DEBUG_FLOPPY
 | 
			
		||||
    printf("Floppy changed=%d\n", ret);
 | 
			
		||||
#endif
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -29,7 +29,6 @@
 | 
			
		||||
#include "trace.h"
 | 
			
		||||
#include "block/thread-pool.h"
 | 
			
		||||
#include "qemu/iov.h"
 | 
			
		||||
#include "qapi/qmp/qstring.h"
 | 
			
		||||
#include <windows.h>
 | 
			
		||||
#include <winioctl.h>
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -325,7 +325,7 @@ static int qemu_rbd_create(const char *filename, QemuOpts *opts, Error **errp)
 | 
			
		||||
            error_setg(errp, "obj size too small");
 | 
			
		||||
            return -EINVAL;
 | 
			
		||||
        }
 | 
			
		||||
        obj_order = ctz32(objsize);
 | 
			
		||||
        obj_order = ffs(objsize) - 1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    clientname = qemu_rbd_parse_clientname(conf, clientname_buf);
 | 
			
		||||
 
 | 
			
		||||
@@ -1716,7 +1716,7 @@ static int parse_block_size_shift(BDRVSheepdogState *s, QemuOpts *opt)
 | 
			
		||||
        if ((object_size - 1) & object_size) {    /* not a power of 2? */
 | 
			
		||||
            return -EINVAL;
 | 
			
		||||
        }
 | 
			
		||||
        obj_order = ctz32(object_size);
 | 
			
		||||
        obj_order = ffs(object_size) - 1;
 | 
			
		||||
        if (obj_order < 20 || obj_order > 31) {
 | 
			
		||||
            return -EINVAL;
 | 
			
		||||
        }
 | 
			
		||||
@@ -2341,7 +2341,6 @@ static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        error_report("failed to create inode for snapshot: %s",
 | 
			
		||||
                     error_get_pretty(local_err));
 | 
			
		||||
        error_free(local_err);
 | 
			
		||||
        goto cleanup;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -24,7 +24,6 @@
 | 
			
		||||
 | 
			
		||||
#include "block/snapshot.h"
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
#include "qapi/qmp/qerror.h"
 | 
			
		||||
 | 
			
		||||
QemuOptsList internal_snapshot_opts = {
 | 
			
		||||
    .name = "snapshot",
 | 
			
		||||
@@ -230,7 +229,7 @@ int bdrv_snapshot_delete(BlockDriverState *bs,
 | 
			
		||||
{
 | 
			
		||||
    BlockDriver *drv = bs->drv;
 | 
			
		||||
    if (!drv) {
 | 
			
		||||
        error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs));
 | 
			
		||||
        error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs));
 | 
			
		||||
        return -ENOMEDIUM;
 | 
			
		||||
    }
 | 
			
		||||
    if (!snapshot_id && !name) {
 | 
			
		||||
@@ -247,9 +246,9 @@ int bdrv_snapshot_delete(BlockDriverState *bs,
 | 
			
		||||
    if (bs->file) {
 | 
			
		||||
        return bdrv_snapshot_delete(bs->file, snapshot_id, name, errp);
 | 
			
		||||
    }
 | 
			
		||||
    error_setg(errp, "Block format '%s' used by device '%s' "
 | 
			
		||||
               "does not support internal snapshot deletion",
 | 
			
		||||
               drv->format_name, bdrv_get_device_name(bs));
 | 
			
		||||
    error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
 | 
			
		||||
              drv->format_name, bdrv_get_device_name(bs),
 | 
			
		||||
              "internal snapshot deletion");
 | 
			
		||||
    return -ENOTSUP;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -316,7 +315,7 @@ int bdrv_snapshot_load_tmp(BlockDriverState *bs,
 | 
			
		||||
    BlockDriver *drv = bs->drv;
 | 
			
		||||
 | 
			
		||||
    if (!drv) {
 | 
			
		||||
        error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs));
 | 
			
		||||
        error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs));
 | 
			
		||||
        return -ENOMEDIUM;
 | 
			
		||||
    }
 | 
			
		||||
    if (!snapshot_id && !name) {
 | 
			
		||||
@@ -330,9 +329,9 @@ int bdrv_snapshot_load_tmp(BlockDriverState *bs,
 | 
			
		||||
    if (drv->bdrv_snapshot_load_tmp) {
 | 
			
		||||
        return drv->bdrv_snapshot_load_tmp(bs, snapshot_id, name, errp);
 | 
			
		||||
    }
 | 
			
		||||
    error_setg(errp, "Block format '%s' used by device '%s' "
 | 
			
		||||
               "does not support temporarily loading internal snapshots",
 | 
			
		||||
               drv->format_name, bdrv_get_device_name(bs));
 | 
			
		||||
    error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
 | 
			
		||||
              drv->format_name, bdrv_get_device_name(bs),
 | 
			
		||||
              "temporarily load internal snapshot");
 | 
			
		||||
    return -ENOTSUP;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -30,11 +30,9 @@
 | 
			
		||||
#include <libssh2_sftp.h>
 | 
			
		||||
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
#include "qemu/error-report.h"
 | 
			
		||||
#include "qemu/sockets.h"
 | 
			
		||||
#include "qemu/uri.h"
 | 
			
		||||
#include "qapi/qmp/qint.h"
 | 
			
		||||
#include "qapi/qmp/qstring.h"
 | 
			
		||||
 | 
			
		||||
/* DEBUG_SSH=1 enables the DPRINTF (debugging printf) statements in
 | 
			
		||||
 * this block driver code.
 | 
			
		||||
 
 | 
			
		||||
@@ -14,7 +14,6 @@
 | 
			
		||||
#include "trace.h"
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
#include "block/blockjob.h"
 | 
			
		||||
#include "qapi/qmp/qerror.h"
 | 
			
		||||
#include "qemu/ratelimit.h"
 | 
			
		||||
 | 
			
		||||
enum {
 | 
			
		||||
@@ -228,7 +227,7 @@ static void stream_set_speed(BlockJob *job, int64_t speed, Error **errp)
 | 
			
		||||
    StreamBlockJob *s = container_of(job, StreamBlockJob, common);
 | 
			
		||||
 | 
			
		||||
    if (speed < 0) {
 | 
			
		||||
        error_setg(errp, QERR_INVALID_PARAMETER, "speed");
 | 
			
		||||
        error_set(errp, QERR_INVALID_PARAMETER, "speed");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
 | 
			
		||||
@@ -251,7 +250,7 @@ void stream_start(BlockDriverState *bs, BlockDriverState *base,
 | 
			
		||||
    if ((on_error == BLOCKDEV_ON_ERROR_STOP ||
 | 
			
		||||
         on_error == BLOCKDEV_ON_ERROR_ENOSPC) &&
 | 
			
		||||
        !bdrv_iostatus_is_enabled(bs)) {
 | 
			
		||||
        error_setg(errp, QERR_INVALID_PARAMETER, "on-error");
 | 
			
		||||
        error_set(errp, QERR_INVALID_PARAMETER, "on-error");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										377
									
								
								block/tar.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										377
									
								
								block/tar.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,377 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Tar block driver
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
 | 
			
		||||
 *
 | 
			
		||||
 * 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.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
 | 
			
		||||
// #define DEBUG
 | 
			
		||||
 | 
			
		||||
#ifdef DEBUG
 | 
			
		||||
#define dprintf(fmt, ...) do { printf("tar: " fmt, ## __VA_ARGS__); } while (0)
 | 
			
		||||
#else
 | 
			
		||||
#define dprintf(fmt, ...) do { } while (0)
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#define SECTOR_SIZE      512
 | 
			
		||||
 | 
			
		||||
#define POSIX_TAR_MAGIC  "ustar"
 | 
			
		||||
#define OFFS_LENGTH      0x7c
 | 
			
		||||
#define OFFS_TYPE        0x9c
 | 
			
		||||
#define OFFS_MAGIC       0x101
 | 
			
		||||
 | 
			
		||||
#define OFFS_S_SP        0x182
 | 
			
		||||
#define OFFS_S_EXT       0x1e2
 | 
			
		||||
#define OFFS_S_LENGTH    0x1e3
 | 
			
		||||
#define OFFS_SX_EXT      0x1f8
 | 
			
		||||
 | 
			
		||||
typedef struct SparseCache {
 | 
			
		||||
    uint64_t start;
 | 
			
		||||
    uint64_t end;
 | 
			
		||||
} SparseCache;
 | 
			
		||||
 | 
			
		||||
typedef struct BDRVTarState {
 | 
			
		||||
    BlockDriverState *hd;
 | 
			
		||||
    size_t file_sec;
 | 
			
		||||
    uint64_t file_len;
 | 
			
		||||
    SparseCache *sparse;
 | 
			
		||||
    int sparse_num;
 | 
			
		||||
    uint64_t last_end;
 | 
			
		||||
    char longfile[2048];
 | 
			
		||||
} BDRVTarState;
 | 
			
		||||
 | 
			
		||||
static int str_ends(char *str, const char *end)
 | 
			
		||||
{
 | 
			
		||||
    int end_len = strlen(end);
 | 
			
		||||
    int str_len = strlen(str);
 | 
			
		||||
 | 
			
		||||
    if (str_len < end_len)
 | 
			
		||||
        return 0;
 | 
			
		||||
 | 
			
		||||
    return !strncmp(str + str_len - end_len, end, end_len);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int is_target_file(BlockDriverState *bs, char *filename,
 | 
			
		||||
                          char *header)
 | 
			
		||||
{
 | 
			
		||||
    int retval = 0;
 | 
			
		||||
 | 
			
		||||
    if (str_ends(filename, ".raw"))
 | 
			
		||||
        retval = 1;
 | 
			
		||||
 | 
			
		||||
    if (str_ends(filename, ".qcow"))
 | 
			
		||||
        retval = 1;
 | 
			
		||||
 | 
			
		||||
    if (str_ends(filename, ".qcow2"))
 | 
			
		||||
        retval = 1;
 | 
			
		||||
 | 
			
		||||
    if (str_ends(filename, ".vmdk"))
 | 
			
		||||
        retval = 1;
 | 
			
		||||
 | 
			
		||||
    if (retval &&
 | 
			
		||||
        (header[OFFS_TYPE] != '0') &&
 | 
			
		||||
        (header[OFFS_TYPE] != 'S')) {
 | 
			
		||||
        retval = 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    dprintf("does filename %s match? %s\n", filename, retval ? "yes" : "no");
 | 
			
		||||
 | 
			
		||||
    /* make sure we're not using this name again */
 | 
			
		||||
    filename[0] = '\0';
 | 
			
		||||
 | 
			
		||||
    return retval;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static uint64_t tar2u64(char *ptr)
 | 
			
		||||
{
 | 
			
		||||
    uint64_t retval;
 | 
			
		||||
    char oldend = ptr[12];
 | 
			
		||||
 | 
			
		||||
    ptr[12] = '\0';
 | 
			
		||||
    if (*ptr & 0x80) {
 | 
			
		||||
        /* XXX we only support files up to 64 bit length */
 | 
			
		||||
        retval = be64_to_cpu(*(uint64_t *)(ptr+4));
 | 
			
		||||
        dprintf("Convert %lx -> %#lx\n", *(uint64_t*)(ptr+4), retval);
 | 
			
		||||
    } else {
 | 
			
		||||
        retval = strtol(ptr, NULL, 8);
 | 
			
		||||
        dprintf("Convert %s -> %#lx\n", ptr, retval);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ptr[12] = oldend;
 | 
			
		||||
 | 
			
		||||
    return retval;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void tar_sparse(BDRVTarState *s, uint64_t offs, uint64_t len)
 | 
			
		||||
{
 | 
			
		||||
    SparseCache *sparse;
 | 
			
		||||
 | 
			
		||||
    if (!len)
 | 
			
		||||
        return;
 | 
			
		||||
    if (!(offs - s->last_end)) {
 | 
			
		||||
        s->last_end += len;
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    if (s->last_end > offs)
 | 
			
		||||
        return;
 | 
			
		||||
 | 
			
		||||
    dprintf("Last chunk until %lx new chunk at %lx\n", s->last_end, offs);
 | 
			
		||||
 | 
			
		||||
    s->sparse = g_realloc(s->sparse, (s->sparse_num + 1) * sizeof(SparseCache));
 | 
			
		||||
    sparse = &s->sparse[s->sparse_num];
 | 
			
		||||
    sparse->start = s->last_end;
 | 
			
		||||
    sparse->end = offs;
 | 
			
		||||
    s->last_end = offs + len;
 | 
			
		||||
    s->sparse_num++;
 | 
			
		||||
    dprintf("Sparse at %lx end=%lx\n", sparse->start,
 | 
			
		||||
                                       sparse->end);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static QemuOptsList runtime_opts = {
 | 
			
		||||
    .name = "tar",
 | 
			
		||||
    .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
 | 
			
		||||
    .desc = {
 | 
			
		||||
        {
 | 
			
		||||
            .name = "filename",
 | 
			
		||||
            .type = QEMU_OPT_STRING,
 | 
			
		||||
            .help = "URL to the tar file",
 | 
			
		||||
        },
 | 
			
		||||
        { /* end of list */ }
 | 
			
		||||
    },
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static int tar_open(BlockDriverState *bs, QDict *options, int flags, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    BDRVTarState *s = bs->opaque;
 | 
			
		||||
    char header[SECTOR_SIZE];
 | 
			
		||||
    char *real_file = header;
 | 
			
		||||
    char *magic;
 | 
			
		||||
    size_t header_offs = 0;
 | 
			
		||||
    int ret;
 | 
			
		||||
    QemuOpts *opts;
 | 
			
		||||
    Error *local_err = NULL;
 | 
			
		||||
    const char *filename;
 | 
			
		||||
 | 
			
		||||
    opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
 | 
			
		||||
    qemu_opts_absorb_qdict(opts, options, &local_err);
 | 
			
		||||
    if (local_err != NULL) {
 | 
			
		||||
        error_propagate(errp, local_err);
 | 
			
		||||
        ret = -EINVAL;
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    filename = qemu_opt_get(opts, "filename");
 | 
			
		||||
 | 
			
		||||
    if (!strncmp(filename, "tar://", 6))
 | 
			
		||||
        filename += 6;
 | 
			
		||||
    else if (!strncmp(filename, "tar:", 4))
 | 
			
		||||
        filename += 4;
 | 
			
		||||
 | 
			
		||||
    ret = bdrv_open(&s->hd, filename, NULL, NULL, flags | BDRV_O_PROTOCOL, NULL, &local_err);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        error_propagate(errp, local_err);
 | 
			
		||||
        qemu_opts_del(opts);
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Search the file for an image */
 | 
			
		||||
 | 
			
		||||
    do {
 | 
			
		||||
        /* tar header */
 | 
			
		||||
        if (bdrv_pread(s->hd, header_offs, header, SECTOR_SIZE) != SECTOR_SIZE)
 | 
			
		||||
            goto fail;
 | 
			
		||||
 | 
			
		||||
        if ((header_offs > 1) && !header[0]) {
 | 
			
		||||
            fprintf(stderr, "Tar: No image file found in archive\n");
 | 
			
		||||
            goto fail;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        magic = &header[OFFS_MAGIC];
 | 
			
		||||
        if (strncmp(magic, POSIX_TAR_MAGIC, 5)) {
 | 
			
		||||
            fprintf(stderr, "Tar: Invalid magic: %s\n", magic);
 | 
			
		||||
            goto fail;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        dprintf("file type: %c\n", header[OFFS_TYPE]);
 | 
			
		||||
 | 
			
		||||
        /* file length*/
 | 
			
		||||
        s->file_len = (tar2u64(&header[OFFS_LENGTH]) + (SECTOR_SIZE - 1)) &
 | 
			
		||||
                      ~(SECTOR_SIZE - 1);
 | 
			
		||||
        s->file_sec = (header_offs / SECTOR_SIZE) + 1;
 | 
			
		||||
 | 
			
		||||
        header_offs += s->file_len + SECTOR_SIZE;
 | 
			
		||||
 | 
			
		||||
        if (header[OFFS_TYPE] == 'L') {
 | 
			
		||||
            bdrv_pread(s->hd, header_offs - s->file_len, s->longfile,
 | 
			
		||||
                       sizeof(s->longfile));
 | 
			
		||||
            s->longfile[sizeof(s->longfile)-1] = '\0';
 | 
			
		||||
            real_file = header;
 | 
			
		||||
        } else if (s->longfile[0]) {
 | 
			
		||||
            real_file = s->longfile;
 | 
			
		||||
        } else {
 | 
			
		||||
            real_file = header;
 | 
			
		||||
        }
 | 
			
		||||
    } while(!is_target_file(bs, real_file, header));
 | 
			
		||||
 | 
			
		||||
    /* We found an image! */
 | 
			
		||||
 | 
			
		||||
    if (header[OFFS_TYPE] == 'S') {
 | 
			
		||||
        uint8_t isextended;
 | 
			
		||||
        int i;
 | 
			
		||||
 | 
			
		||||
        for (i = OFFS_S_SP; i < (OFFS_S_SP + (4 * 24)); i += 24)
 | 
			
		||||
            tar_sparse(s, tar2u64(&header[i]), tar2u64(&header[i+12]));
 | 
			
		||||
 | 
			
		||||
        s->file_len = tar2u64(&header[OFFS_S_LENGTH]);
 | 
			
		||||
        isextended = header[OFFS_S_EXT];
 | 
			
		||||
 | 
			
		||||
        while (isextended) {
 | 
			
		||||
            if (bdrv_pread(s->hd, s->file_sec * SECTOR_SIZE, header,
 | 
			
		||||
                           SECTOR_SIZE) != SECTOR_SIZE)
 | 
			
		||||
                goto fail;
 | 
			
		||||
 | 
			
		||||
            for (i = 0; i < (21 * 24); i += 24)
 | 
			
		||||
                tar_sparse(s, tar2u64(&header[i]), tar2u64(&header[i+12]));
 | 
			
		||||
            isextended = header[OFFS_SX_EXT];
 | 
			
		||||
            s->file_sec++;
 | 
			
		||||
        }
 | 
			
		||||
        tar_sparse(s, s->file_len, 1);
 | 
			
		||||
    }
 | 
			
		||||
    qemu_opts_del(opts);
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
 | 
			
		||||
fail:
 | 
			
		||||
    fprintf(stderr, "Tar: Error opening file\n");
 | 
			
		||||
    bdrv_unref(s->hd);
 | 
			
		||||
    qemu_opts_del(opts);
 | 
			
		||||
    return -EINVAL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
typedef struct TarAIOCB {
 | 
			
		||||
    BlockAIOCB common;
 | 
			
		||||
    QEMUBH *bh;
 | 
			
		||||
} TarAIOCB;
 | 
			
		||||
 | 
			
		||||
/* This callback gets invoked when we have pure sparseness */
 | 
			
		||||
static void tar_sparse_cb(void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    TarAIOCB *acb = (TarAIOCB *)opaque;
 | 
			
		||||
 | 
			
		||||
    acb->common.cb(acb->common.opaque, 0);
 | 
			
		||||
    qemu_bh_delete(acb->bh);
 | 
			
		||||
    qemu_aio_unref(acb);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static AIOCBInfo tar_aiocb_info = {
 | 
			
		||||
    .aiocb_size         = sizeof(TarAIOCB),
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/* This is where we get a request from a caller to read something */
 | 
			
		||||
static BlockAIOCB *tar_aio_readv(BlockDriverState *bs,
 | 
			
		||||
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
 | 
			
		||||
        BlockCompletionFunc *cb, void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    BDRVTarState *s = bs->opaque;
 | 
			
		||||
    SparseCache *sparse;
 | 
			
		||||
    int64_t sec_file = sector_num + s->file_sec;
 | 
			
		||||
    int64_t start = sector_num * SECTOR_SIZE;
 | 
			
		||||
    int64_t end = start + (nb_sectors * SECTOR_SIZE);
 | 
			
		||||
    int i;
 | 
			
		||||
    TarAIOCB *acb;
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < s->sparse_num; i++) {
 | 
			
		||||
        sparse = &s->sparse[i];
 | 
			
		||||
        if (sparse->start > end) {
 | 
			
		||||
            /* We expect the cache to be start increasing */
 | 
			
		||||
            break;
 | 
			
		||||
        } else if ((sparse->start < start) && (sparse->end <= start)) {
 | 
			
		||||
            /* sparse before our offset */
 | 
			
		||||
            sec_file -= (sparse->end - sparse->start) / SECTOR_SIZE;
 | 
			
		||||
        } else if ((sparse->start <= start) && (sparse->end >= end)) {
 | 
			
		||||
            /* all our sectors are sparse */
 | 
			
		||||
            char *buf = g_malloc0(nb_sectors * SECTOR_SIZE);
 | 
			
		||||
 | 
			
		||||
            acb = qemu_aio_get(&tar_aiocb_info, bs, cb, opaque);
 | 
			
		||||
            qemu_iovec_from_buf(qiov, 0, buf, nb_sectors * SECTOR_SIZE);
 | 
			
		||||
            g_free(buf);
 | 
			
		||||
            acb->bh = qemu_bh_new(tar_sparse_cb, acb);
 | 
			
		||||
            qemu_bh_schedule(acb->bh);
 | 
			
		||||
 | 
			
		||||
            return &acb->common;
 | 
			
		||||
        } else if (((sparse->start >= start) && (sparse->start < end)) ||
 | 
			
		||||
                   ((sparse->end >= start) && (sparse->end < end))) {
 | 
			
		||||
            /* we're semi-sparse (worst case) */
 | 
			
		||||
            /* let's go synchronous and read all sectors individually */
 | 
			
		||||
            char *buf = g_malloc(nb_sectors * SECTOR_SIZE);
 | 
			
		||||
            uint64_t offs;
 | 
			
		||||
 | 
			
		||||
            for (offs = 0; offs < (nb_sectors * SECTOR_SIZE);
 | 
			
		||||
                 offs += SECTOR_SIZE) {
 | 
			
		||||
                bdrv_pread(bs, (sector_num * SECTOR_SIZE) + offs,
 | 
			
		||||
                           buf + offs, SECTOR_SIZE);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            qemu_iovec_from_buf(qiov, 0, buf, nb_sectors * SECTOR_SIZE);
 | 
			
		||||
            acb = qemu_aio_get(&tar_aiocb_info, bs, cb, opaque);
 | 
			
		||||
            acb->bh = qemu_bh_new(tar_sparse_cb, acb);
 | 
			
		||||
            qemu_bh_schedule(acb->bh);
 | 
			
		||||
 | 
			
		||||
            return &acb->common;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return bdrv_aio_readv(s->hd, sec_file, qiov, nb_sectors,
 | 
			
		||||
                          cb, opaque);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void tar_close(BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
    dprintf("Close\n");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int64_t tar_getlength(BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
    BDRVTarState *s = bs->opaque;
 | 
			
		||||
    dprintf("getlength -> %ld\n", s->file_len);
 | 
			
		||||
    return s->file_len;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static BlockDriver bdrv_tar = {
 | 
			
		||||
    .format_name     = "tar",
 | 
			
		||||
    .protocol_name   = "tar",
 | 
			
		||||
 | 
			
		||||
    .instance_size   = sizeof(BDRVTarState),
 | 
			
		||||
    .bdrv_file_open  = tar_open,
 | 
			
		||||
    .bdrv_close      = tar_close,
 | 
			
		||||
    .bdrv_getlength  = tar_getlength,
 | 
			
		||||
 | 
			
		||||
    .bdrv_aio_readv  = tar_aio_readv,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void tar_block_init(void)
 | 
			
		||||
{
 | 
			
		||||
    bdrv_register(&bdrv_tar);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
block_init(tar_block_init);
 | 
			
		||||
@@ -1,501 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 * QEMU block throttling group infrastructure
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (C) Nodalink, EURL. 2014
 | 
			
		||||
 * Copyright (C) Igalia, S.L. 2015
 | 
			
		||||
 *
 | 
			
		||||
 * Authors:
 | 
			
		||||
 *   Benoît Canet <benoit.canet@nodalink.com>
 | 
			
		||||
 *   Alberto Garcia <berto@igalia.com>
 | 
			
		||||
 *
 | 
			
		||||
 * This program is free software; you can redistribute it and/or
 | 
			
		||||
 * modify it under the terms of the GNU General Public License as
 | 
			
		||||
 * published by the Free Software Foundation; either version 2 or
 | 
			
		||||
 * (at your option) version 3 of the License.
 | 
			
		||||
 *
 | 
			
		||||
 * This program is distributed in the hope that it will be useful,
 | 
			
		||||
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
 * GNU General Public License for more details.
 | 
			
		||||
 *
 | 
			
		||||
 * You should have received a copy of the GNU General Public License
 | 
			
		||||
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "block/throttle-groups.h"
 | 
			
		||||
#include "qemu/queue.h"
 | 
			
		||||
#include "qemu/thread.h"
 | 
			
		||||
#include "sysemu/qtest.h"
 | 
			
		||||
 | 
			
		||||
/* The ThrottleGroup structure (with its ThrottleState) is shared
 | 
			
		||||
 * among different BlockDriverState and it's independent from
 | 
			
		||||
 * AioContext, so in order to use it from different threads it needs
 | 
			
		||||
 * its own locking.
 | 
			
		||||
 *
 | 
			
		||||
 * This locking is however handled internally in this file, so it's
 | 
			
		||||
 * mostly transparent to outside users (but see the documentation in
 | 
			
		||||
 * throttle_groups_lock()).
 | 
			
		||||
 *
 | 
			
		||||
 * The whole ThrottleGroup structure is private and invisible to
 | 
			
		||||
 * outside users, that only use it through its ThrottleState.
 | 
			
		||||
 *
 | 
			
		||||
 * In addition to the ThrottleGroup structure, BlockDriverState has
 | 
			
		||||
 * fields that need to be accessed by other members of the group and
 | 
			
		||||
 * therefore also need to be protected by this lock. Once a BDS is
 | 
			
		||||
 * registered in a group those fields can be accessed by other threads
 | 
			
		||||
 * any time.
 | 
			
		||||
 *
 | 
			
		||||
 * Again, all this is handled internally and is mostly transparent to
 | 
			
		||||
 * the outside. The 'throttle_timers' field however has an additional
 | 
			
		||||
 * constraint because it may be temporarily invalid (see for example
 | 
			
		||||
 * bdrv_set_aio_context()). Therefore in this file a thread will
 | 
			
		||||
 * access some other BDS's timers only after verifying that that BDS
 | 
			
		||||
 * has throttled requests in the queue.
 | 
			
		||||
 */
 | 
			
		||||
typedef struct ThrottleGroup {
 | 
			
		||||
    char *name; /* This is constant during the lifetime of the group */
 | 
			
		||||
 | 
			
		||||
    QemuMutex lock; /* This lock protects the following four fields */
 | 
			
		||||
    ThrottleState ts;
 | 
			
		||||
    QLIST_HEAD(, BlockDriverState) head;
 | 
			
		||||
    BlockDriverState *tokens[2];
 | 
			
		||||
    bool any_timer_armed[2];
 | 
			
		||||
 | 
			
		||||
    /* These two are protected by the global throttle_groups_lock */
 | 
			
		||||
    unsigned refcount;
 | 
			
		||||
    QTAILQ_ENTRY(ThrottleGroup) list;
 | 
			
		||||
} ThrottleGroup;
 | 
			
		||||
 | 
			
		||||
static QemuMutex throttle_groups_lock;
 | 
			
		||||
static QTAILQ_HEAD(, ThrottleGroup) throttle_groups =
 | 
			
		||||
    QTAILQ_HEAD_INITIALIZER(throttle_groups);
 | 
			
		||||
 | 
			
		||||
/* Increments the reference count of a ThrottleGroup given its name.
 | 
			
		||||
 *
 | 
			
		||||
 * If no ThrottleGroup is found with the given name a new one is
 | 
			
		||||
 * created.
 | 
			
		||||
 *
 | 
			
		||||
 * @name: the name of the ThrottleGroup
 | 
			
		||||
 * @ret:  the ThrottleGroup
 | 
			
		||||
 */
 | 
			
		||||
static ThrottleGroup *throttle_group_incref(const char *name)
 | 
			
		||||
{
 | 
			
		||||
    ThrottleGroup *tg = NULL;
 | 
			
		||||
    ThrottleGroup *iter;
 | 
			
		||||
 | 
			
		||||
    qemu_mutex_lock(&throttle_groups_lock);
 | 
			
		||||
 | 
			
		||||
    /* Look for an existing group with that name */
 | 
			
		||||
    QTAILQ_FOREACH(iter, &throttle_groups, list) {
 | 
			
		||||
        if (!strcmp(name, iter->name)) {
 | 
			
		||||
            tg = iter;
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Create a new one if not found */
 | 
			
		||||
    if (!tg) {
 | 
			
		||||
        tg = g_new0(ThrottleGroup, 1);
 | 
			
		||||
        tg->name = g_strdup(name);
 | 
			
		||||
        qemu_mutex_init(&tg->lock);
 | 
			
		||||
        throttle_init(&tg->ts);
 | 
			
		||||
        QLIST_INIT(&tg->head);
 | 
			
		||||
 | 
			
		||||
        QTAILQ_INSERT_TAIL(&throttle_groups, tg, list);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    tg->refcount++;
 | 
			
		||||
 | 
			
		||||
    qemu_mutex_unlock(&throttle_groups_lock);
 | 
			
		||||
 | 
			
		||||
    return tg;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Decrease the reference count of a ThrottleGroup.
 | 
			
		||||
 *
 | 
			
		||||
 * When the reference count reaches zero the ThrottleGroup is
 | 
			
		||||
 * destroyed.
 | 
			
		||||
 *
 | 
			
		||||
 * @tg:  The ThrottleGroup to unref
 | 
			
		||||
 */
 | 
			
		||||
static void throttle_group_unref(ThrottleGroup *tg)
 | 
			
		||||
{
 | 
			
		||||
    qemu_mutex_lock(&throttle_groups_lock);
 | 
			
		||||
    if (--tg->refcount == 0) {
 | 
			
		||||
        QTAILQ_REMOVE(&throttle_groups, tg, list);
 | 
			
		||||
        qemu_mutex_destroy(&tg->lock);
 | 
			
		||||
        g_free(tg->name);
 | 
			
		||||
        g_free(tg);
 | 
			
		||||
    }
 | 
			
		||||
    qemu_mutex_unlock(&throttle_groups_lock);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Get the name from a BlockDriverState's ThrottleGroup. The name (and
 | 
			
		||||
 * the pointer) is guaranteed to remain constant during the lifetime
 | 
			
		||||
 * of the group.
 | 
			
		||||
 *
 | 
			
		||||
 * @bs:   a BlockDriverState that is member of a throttling group
 | 
			
		||||
 * @ret:  the name of the group.
 | 
			
		||||
 */
 | 
			
		||||
const char *throttle_group_get_name(BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
    ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
 | 
			
		||||
    return tg->name;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Return the next BlockDriverState in the round-robin sequence,
 | 
			
		||||
 * simulating a circular list.
 | 
			
		||||
 *
 | 
			
		||||
 * This assumes that tg->lock is held.
 | 
			
		||||
 *
 | 
			
		||||
 * @bs:  the current BlockDriverState
 | 
			
		||||
 * @ret: the next BlockDriverState in the sequence
 | 
			
		||||
 */
 | 
			
		||||
static BlockDriverState *throttle_group_next_bs(BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
    ThrottleState *ts = bs->throttle_state;
 | 
			
		||||
    ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
 | 
			
		||||
    BlockDriverState *next = QLIST_NEXT(bs, round_robin);
 | 
			
		||||
 | 
			
		||||
    if (!next) {
 | 
			
		||||
        return QLIST_FIRST(&tg->head);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return next;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Return the next BlockDriverState in the round-robin sequence with
 | 
			
		||||
 * pending I/O requests.
 | 
			
		||||
 *
 | 
			
		||||
 * This assumes that tg->lock is held.
 | 
			
		||||
 *
 | 
			
		||||
 * @bs:        the current BlockDriverState
 | 
			
		||||
 * @is_write:  the type of operation (read/write)
 | 
			
		||||
 * @ret:       the next BlockDriverState with pending requests, or bs
 | 
			
		||||
 *             if there is none.
 | 
			
		||||
 */
 | 
			
		||||
static BlockDriverState *next_throttle_token(BlockDriverState *bs,
 | 
			
		||||
                                             bool is_write)
 | 
			
		||||
{
 | 
			
		||||
    ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
 | 
			
		||||
    BlockDriverState *token, *start;
 | 
			
		||||
 | 
			
		||||
    start = token = tg->tokens[is_write];
 | 
			
		||||
 | 
			
		||||
    /* get next bs round in round robin style */
 | 
			
		||||
    token = throttle_group_next_bs(token);
 | 
			
		||||
    while (token != start && !token->pending_reqs[is_write]) {
 | 
			
		||||
        token = throttle_group_next_bs(token);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* If no IO are queued for scheduling on the next round robin token
 | 
			
		||||
     * then decide the token is the current bs because chances are
 | 
			
		||||
     * the current bs get the current request queued.
 | 
			
		||||
     */
 | 
			
		||||
    if (token == start && !token->pending_reqs[is_write]) {
 | 
			
		||||
        token = bs;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return token;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Check if the next I/O request for a BlockDriverState needs to be
 | 
			
		||||
 * throttled or not. If there's no timer set in this group, set one
 | 
			
		||||
 * and update the token accordingly.
 | 
			
		||||
 *
 | 
			
		||||
 * This assumes that tg->lock is held.
 | 
			
		||||
 *
 | 
			
		||||
 * @bs:         the current BlockDriverState
 | 
			
		||||
 * @is_write:   the type of operation (read/write)
 | 
			
		||||
 * @ret:        whether the I/O request needs to be throttled or not
 | 
			
		||||
 */
 | 
			
		||||
static bool throttle_group_schedule_timer(BlockDriverState *bs,
 | 
			
		||||
                                          bool is_write)
 | 
			
		||||
{
 | 
			
		||||
    ThrottleState *ts = bs->throttle_state;
 | 
			
		||||
    ThrottleTimers *tt = &bs->throttle_timers;
 | 
			
		||||
    ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
 | 
			
		||||
    bool must_wait;
 | 
			
		||||
 | 
			
		||||
    /* Check if any of the timers in this group is already armed */
 | 
			
		||||
    if (tg->any_timer_armed[is_write]) {
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    must_wait = throttle_schedule_timer(ts, tt, is_write);
 | 
			
		||||
 | 
			
		||||
    /* If a timer just got armed, set bs as the current token */
 | 
			
		||||
    if (must_wait) {
 | 
			
		||||
        tg->tokens[is_write] = bs;
 | 
			
		||||
        tg->any_timer_armed[is_write] = true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return must_wait;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Look for the next pending I/O request and schedule it.
 | 
			
		||||
 *
 | 
			
		||||
 * This assumes that tg->lock is held.
 | 
			
		||||
 *
 | 
			
		||||
 * @bs:        the current BlockDriverState
 | 
			
		||||
 * @is_write:  the type of operation (read/write)
 | 
			
		||||
 */
 | 
			
		||||
static void schedule_next_request(BlockDriverState *bs, bool is_write)
 | 
			
		||||
{
 | 
			
		||||
    ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
 | 
			
		||||
    bool must_wait;
 | 
			
		||||
    BlockDriverState *token;
 | 
			
		||||
 | 
			
		||||
    /* Check if there's any pending request to schedule next */
 | 
			
		||||
    token = next_throttle_token(bs, is_write);
 | 
			
		||||
    if (!token->pending_reqs[is_write]) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Set a timer for the request if it needs to be throttled */
 | 
			
		||||
    must_wait = throttle_group_schedule_timer(token, is_write);
 | 
			
		||||
 | 
			
		||||
    /* If it doesn't have to wait, queue it for immediate execution */
 | 
			
		||||
    if (!must_wait) {
 | 
			
		||||
        /* Give preference to requests from the current bs */
 | 
			
		||||
        if (qemu_in_coroutine() &&
 | 
			
		||||
            qemu_co_queue_next(&bs->throttled_reqs[is_write])) {
 | 
			
		||||
            token = bs;
 | 
			
		||||
        } else {
 | 
			
		||||
            ThrottleTimers *tt = &token->throttle_timers;
 | 
			
		||||
            int64_t now = qemu_clock_get_ns(tt->clock_type);
 | 
			
		||||
            timer_mod(tt->timers[is_write], now + 1);
 | 
			
		||||
            tg->any_timer_armed[is_write] = true;
 | 
			
		||||
        }
 | 
			
		||||
        tg->tokens[is_write] = token;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Check if an I/O request needs to be throttled, wait and set a timer
 | 
			
		||||
 * if necessary, and schedule the next request using a round robin
 | 
			
		||||
 * algorithm.
 | 
			
		||||
 *
 | 
			
		||||
 * @bs:        the current BlockDriverState
 | 
			
		||||
 * @bytes:     the number of bytes for this I/O
 | 
			
		||||
 * @is_write:  the type of operation (read/write)
 | 
			
		||||
 */
 | 
			
		||||
void coroutine_fn throttle_group_co_io_limits_intercept(BlockDriverState *bs,
 | 
			
		||||
                                                        unsigned int bytes,
 | 
			
		||||
                                                        bool is_write)
 | 
			
		||||
{
 | 
			
		||||
    bool must_wait;
 | 
			
		||||
    BlockDriverState *token;
 | 
			
		||||
 | 
			
		||||
    ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
 | 
			
		||||
    qemu_mutex_lock(&tg->lock);
 | 
			
		||||
 | 
			
		||||
    /* First we check if this I/O has to be throttled. */
 | 
			
		||||
    token = next_throttle_token(bs, is_write);
 | 
			
		||||
    must_wait = throttle_group_schedule_timer(token, is_write);
 | 
			
		||||
 | 
			
		||||
    /* Wait if there's a timer set or queued requests of this type */
 | 
			
		||||
    if (must_wait || bs->pending_reqs[is_write]) {
 | 
			
		||||
        bs->pending_reqs[is_write]++;
 | 
			
		||||
        qemu_mutex_unlock(&tg->lock);
 | 
			
		||||
        qemu_co_queue_wait(&bs->throttled_reqs[is_write]);
 | 
			
		||||
        qemu_mutex_lock(&tg->lock);
 | 
			
		||||
        bs->pending_reqs[is_write]--;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* The I/O will be executed, so do the accounting */
 | 
			
		||||
    throttle_account(bs->throttle_state, is_write, bytes);
 | 
			
		||||
 | 
			
		||||
    /* Schedule the next request */
 | 
			
		||||
    schedule_next_request(bs, is_write);
 | 
			
		||||
 | 
			
		||||
    qemu_mutex_unlock(&tg->lock);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Update the throttle configuration for a particular group. Similar
 | 
			
		||||
 * to throttle_config(), but guarantees atomicity within the
 | 
			
		||||
 * throttling group.
 | 
			
		||||
 *
 | 
			
		||||
 * @bs:  a BlockDriverState that is member of the group
 | 
			
		||||
 * @cfg: the configuration to set
 | 
			
		||||
 */
 | 
			
		||||
void throttle_group_config(BlockDriverState *bs, ThrottleConfig *cfg)
 | 
			
		||||
{
 | 
			
		||||
    ThrottleTimers *tt = &bs->throttle_timers;
 | 
			
		||||
    ThrottleState *ts = bs->throttle_state;
 | 
			
		||||
    ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
 | 
			
		||||
    qemu_mutex_lock(&tg->lock);
 | 
			
		||||
    /* throttle_config() cancels the timers */
 | 
			
		||||
    if (timer_pending(tt->timers[0])) {
 | 
			
		||||
        tg->any_timer_armed[0] = false;
 | 
			
		||||
    }
 | 
			
		||||
    if (timer_pending(tt->timers[1])) {
 | 
			
		||||
        tg->any_timer_armed[1] = false;
 | 
			
		||||
    }
 | 
			
		||||
    throttle_config(ts, tt, cfg);
 | 
			
		||||
    qemu_mutex_unlock(&tg->lock);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Get the throttle configuration from a particular group. Similar to
 | 
			
		||||
 * throttle_get_config(), but guarantees atomicity within the
 | 
			
		||||
 * throttling group.
 | 
			
		||||
 *
 | 
			
		||||
 * @bs:  a BlockDriverState that is member of the group
 | 
			
		||||
 * @cfg: the configuration will be written here
 | 
			
		||||
 */
 | 
			
		||||
void throttle_group_get_config(BlockDriverState *bs, ThrottleConfig *cfg)
 | 
			
		||||
{
 | 
			
		||||
    ThrottleState *ts = bs->throttle_state;
 | 
			
		||||
    ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
 | 
			
		||||
    qemu_mutex_lock(&tg->lock);
 | 
			
		||||
    throttle_get_config(ts, cfg);
 | 
			
		||||
    qemu_mutex_unlock(&tg->lock);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* ThrottleTimers callback. This wakes up a request that was waiting
 | 
			
		||||
 * because it had been throttled.
 | 
			
		||||
 *
 | 
			
		||||
 * @bs:        the BlockDriverState whose request had been throttled
 | 
			
		||||
 * @is_write:  the type of operation (read/write)
 | 
			
		||||
 */
 | 
			
		||||
static void timer_cb(BlockDriverState *bs, bool is_write)
 | 
			
		||||
{
 | 
			
		||||
    ThrottleState *ts = bs->throttle_state;
 | 
			
		||||
    ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
 | 
			
		||||
    bool empty_queue;
 | 
			
		||||
 | 
			
		||||
    /* The timer has just been fired, so we can update the flag */
 | 
			
		||||
    qemu_mutex_lock(&tg->lock);
 | 
			
		||||
    tg->any_timer_armed[is_write] = false;
 | 
			
		||||
    qemu_mutex_unlock(&tg->lock);
 | 
			
		||||
 | 
			
		||||
    /* Run the request that was waiting for this timer */
 | 
			
		||||
    empty_queue = !qemu_co_enter_next(&bs->throttled_reqs[is_write]);
 | 
			
		||||
 | 
			
		||||
    /* If the request queue was empty then we have to take care of
 | 
			
		||||
     * scheduling the next one */
 | 
			
		||||
    if (empty_queue) {
 | 
			
		||||
        qemu_mutex_lock(&tg->lock);
 | 
			
		||||
        schedule_next_request(bs, is_write);
 | 
			
		||||
        qemu_mutex_unlock(&tg->lock);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void read_timer_cb(void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    timer_cb(opaque, false);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void write_timer_cb(void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    timer_cb(opaque, true);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Register a BlockDriverState in the throttling group, also
 | 
			
		||||
 * initializing its timers and updating its throttle_state pointer to
 | 
			
		||||
 * point to it. If a throttling group with that name does not exist
 | 
			
		||||
 * yet, it will be created.
 | 
			
		||||
 *
 | 
			
		||||
 * @bs:        the BlockDriverState to insert
 | 
			
		||||
 * @groupname: the name of the group
 | 
			
		||||
 */
 | 
			
		||||
void throttle_group_register_bs(BlockDriverState *bs, const char *groupname)
 | 
			
		||||
{
 | 
			
		||||
    int i;
 | 
			
		||||
    ThrottleGroup *tg = throttle_group_incref(groupname);
 | 
			
		||||
    int clock_type = QEMU_CLOCK_REALTIME;
 | 
			
		||||
 | 
			
		||||
    if (qtest_enabled()) {
 | 
			
		||||
        /* For testing block IO throttling only */
 | 
			
		||||
        clock_type = QEMU_CLOCK_VIRTUAL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    bs->throttle_state = &tg->ts;
 | 
			
		||||
 | 
			
		||||
    qemu_mutex_lock(&tg->lock);
 | 
			
		||||
    /* If the ThrottleGroup is new set this BlockDriverState as the token */
 | 
			
		||||
    for (i = 0; i < 2; i++) {
 | 
			
		||||
        if (!tg->tokens[i]) {
 | 
			
		||||
            tg->tokens[i] = bs;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    QLIST_INSERT_HEAD(&tg->head, bs, round_robin);
 | 
			
		||||
 | 
			
		||||
    throttle_timers_init(&bs->throttle_timers,
 | 
			
		||||
                         bdrv_get_aio_context(bs),
 | 
			
		||||
                         clock_type,
 | 
			
		||||
                         read_timer_cb,
 | 
			
		||||
                         write_timer_cb,
 | 
			
		||||
                         bs);
 | 
			
		||||
 | 
			
		||||
    qemu_mutex_unlock(&tg->lock);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Unregister a BlockDriverState from its group, removing it from the
 | 
			
		||||
 * list, destroying the timers and setting the throttle_state pointer
 | 
			
		||||
 * to NULL.
 | 
			
		||||
 *
 | 
			
		||||
 * The group will be destroyed if it's empty after this operation.
 | 
			
		||||
 *
 | 
			
		||||
 * @bs: the BlockDriverState to remove
 | 
			
		||||
 */
 | 
			
		||||
void throttle_group_unregister_bs(BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
    ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
 | 
			
		||||
    int i;
 | 
			
		||||
 | 
			
		||||
    qemu_mutex_lock(&tg->lock);
 | 
			
		||||
    for (i = 0; i < 2; i++) {
 | 
			
		||||
        if (tg->tokens[i] == bs) {
 | 
			
		||||
            BlockDriverState *token = throttle_group_next_bs(bs);
 | 
			
		||||
            /* Take care of the case where this is the last bs in the group */
 | 
			
		||||
            if (token == bs) {
 | 
			
		||||
                token = NULL;
 | 
			
		||||
            }
 | 
			
		||||
            tg->tokens[i] = token;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* remove the current bs from the list */
 | 
			
		||||
    QLIST_REMOVE(bs, round_robin);
 | 
			
		||||
    throttle_timers_destroy(&bs->throttle_timers);
 | 
			
		||||
    qemu_mutex_unlock(&tg->lock);
 | 
			
		||||
 | 
			
		||||
    throttle_group_unref(tg);
 | 
			
		||||
    bs->throttle_state = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Acquire the lock of this throttling group.
 | 
			
		||||
 *
 | 
			
		||||
 * You won't normally need to use this. None of the functions from the
 | 
			
		||||
 * ThrottleGroup API require you to acquire the lock since all of them
 | 
			
		||||
 * deal with it internally.
 | 
			
		||||
 *
 | 
			
		||||
 * This should only be used in exceptional cases when you want to
 | 
			
		||||
 * access the protected fields of a BlockDriverState directly
 | 
			
		||||
 * (e.g. bdrv_swap()).
 | 
			
		||||
 *
 | 
			
		||||
 * @bs: a BlockDriverState that is member of the group
 | 
			
		||||
 */
 | 
			
		||||
void throttle_group_lock(BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
    ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
 | 
			
		||||
    qemu_mutex_lock(&tg->lock);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Release the lock of this throttling group.
 | 
			
		||||
 *
 | 
			
		||||
 * See the comments in throttle_group_lock().
 | 
			
		||||
 */
 | 
			
		||||
void throttle_group_unlock(BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
    ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
 | 
			
		||||
    qemu_mutex_unlock(&tg->lock);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void throttle_groups_init(void)
 | 
			
		||||
{
 | 
			
		||||
    qemu_mutex_init(&throttle_groups_lock);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
block_init(throttle_groups_init);
 | 
			
		||||
@@ -502,9 +502,9 @@ static int vdi_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Disable migration when vdi images are used */
 | 
			
		||||
    error_setg(&s->migration_blocker, "The vdi format used by node '%s' "
 | 
			
		||||
               "does not support live migration",
 | 
			
		||||
               bdrv_get_device_or_node_name(bs));
 | 
			
		||||
    error_set(&s->migration_blocker,
 | 
			
		||||
              QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
 | 
			
		||||
              "vdi", bdrv_get_device_name(bs), "live migration");
 | 
			
		||||
    migrate_add_blocker(s->migration_blocker);
 | 
			
		||||
 | 
			
		||||
    qemu_co_mutex_init(&s->write_lock);
 | 
			
		||||
 
 | 
			
		||||
@@ -19,7 +19,6 @@
 | 
			
		||||
 */
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
#include "qemu/error-report.h"
 | 
			
		||||
#include "qemu/module.h"
 | 
			
		||||
#include "block/vhdx.h"
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										10
									
								
								block/vhdx.c
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								block/vhdx.c
									
									
									
									
									
								
							@@ -1002,9 +1002,9 @@ static int vhdx_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
    /* TODO: differencing files */
 | 
			
		||||
 | 
			
		||||
    /* Disable migration when VHDX images are used */
 | 
			
		||||
    error_setg(&s->migration_blocker, "The vhdx format used by node '%s' "
 | 
			
		||||
               "does not support live migration",
 | 
			
		||||
               bdrv_get_device_or_node_name(bs));
 | 
			
		||||
    error_set(&s->migration_blocker,
 | 
			
		||||
            QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
 | 
			
		||||
            "vhdx", bdrv_get_device_name(bs), "live migration");
 | 
			
		||||
    migrate_add_blocker(s->migration_blocker);
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
@@ -1269,7 +1269,7 @@ static coroutine_fn int vhdx_co_writev(BlockDriverState *bs, int64_t sector_num,
 | 
			
		||||
                        iov1.iov_base = qemu_blockalign(bs, iov1.iov_len);
 | 
			
		||||
                        memset(iov1.iov_base, 0, iov1.iov_len);
 | 
			
		||||
                        qemu_iovec_concat_iov(&hd_qiov, &iov1, 1, 0,
 | 
			
		||||
                                              iov1.iov_len);
 | 
			
		||||
                                              sinfo.block_offset);
 | 
			
		||||
                        sectors_to_write += iov1.iov_len >> BDRV_SECTOR_BITS;
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
@@ -1285,7 +1285,7 @@ static coroutine_fn int vhdx_co_writev(BlockDriverState *bs, int64_t sector_num,
 | 
			
		||||
                        iov2.iov_base = qemu_blockalign(bs, iov2.iov_len);
 | 
			
		||||
                        memset(iov2.iov_base, 0, iov2.iov_len);
 | 
			
		||||
                        qemu_iovec_concat_iov(&hd_qiov, &iov2, 1, 0,
 | 
			
		||||
                                              iov2.iov_len);
 | 
			
		||||
                                              sinfo.block_offset);
 | 
			
		||||
                        sectors_to_write += iov2.iov_len >> BDRV_SECTOR_BITS;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										94
									
								
								block/vmdk.c
									
									
									
									
									
								
							
							
						
						
									
										94
									
								
								block/vmdk.c
									
									
									
									
									
								
							@@ -25,8 +25,6 @@
 | 
			
		||||
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
#include "qapi/qmp/qerror.h"
 | 
			
		||||
#include "qemu/error-report.h"
 | 
			
		||||
#include "qemu/module.h"
 | 
			
		||||
#include "migration/migration.h"
 | 
			
		||||
#include <zlib.h>
 | 
			
		||||
@@ -323,13 +321,37 @@ static int vmdk_is_cid_valid(BlockDriverState *bs)
 | 
			
		||||
    return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* We have nothing to do for VMDK reopen, stubs just return success */
 | 
			
		||||
/* Queue extents, if any, for reopen() */
 | 
			
		||||
static int vmdk_reopen_prepare(BDRVReopenState *state,
 | 
			
		||||
                               BlockReopenQueue *queue, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    BDRVVmdkState *s;
 | 
			
		||||
    int ret = -1;
 | 
			
		||||
    int i;
 | 
			
		||||
    VmdkExtent *e;
 | 
			
		||||
 | 
			
		||||
    assert(state != NULL);
 | 
			
		||||
    assert(state->bs != NULL);
 | 
			
		||||
    return 0;
 | 
			
		||||
 | 
			
		||||
    if (queue == NULL) {
 | 
			
		||||
        error_setg(errp, "No reopen queue for VMDK extents");
 | 
			
		||||
        goto exit;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    s = state->bs->opaque;
 | 
			
		||||
 | 
			
		||||
    assert(s != NULL);
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < s->num_extents; i++) {
 | 
			
		||||
        e = &s->extents[i];
 | 
			
		||||
        if (e->file != state->bs->file) {
 | 
			
		||||
            bdrv_reopen_queue(queue, e->file, state->flags);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    ret = 0;
 | 
			
		||||
 | 
			
		||||
exit:
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int vmdk_parent_open(BlockDriverState *bs)
 | 
			
		||||
@@ -502,7 +524,7 @@ static int vmdk_open_vmfs_sparse(BlockDriverState *bs,
 | 
			
		||||
    }
 | 
			
		||||
    ret = vmdk_add_extent(bs, file, false,
 | 
			
		||||
                          le32_to_cpu(header.disk_sectors),
 | 
			
		||||
                          (int64_t)le32_to_cpu(header.l1dir_offset) << 9,
 | 
			
		||||
                          le32_to_cpu(header.l1dir_offset) << 9,
 | 
			
		||||
                          0,
 | 
			
		||||
                          le32_to_cpu(header.l1dir_size),
 | 
			
		||||
                          4096,
 | 
			
		||||
@@ -521,7 +543,7 @@ static int vmdk_open_vmfs_sparse(BlockDriverState *bs,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int vmdk_open_desc_file(BlockDriverState *bs, int flags, char *buf,
 | 
			
		||||
                               QDict *options, Error **errp);
 | 
			
		||||
                               Error **errp);
 | 
			
		||||
 | 
			
		||||
static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset,
 | 
			
		||||
                            Error **errp)
 | 
			
		||||
@@ -560,7 +582,7 @@ static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset,
 | 
			
		||||
 | 
			
		||||
static int vmdk_open_vmdk4(BlockDriverState *bs,
 | 
			
		||||
                           BlockDriverState *file,
 | 
			
		||||
                           int flags, QDict *options, Error **errp)
 | 
			
		||||
                           int flags, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    int ret;
 | 
			
		||||
    uint32_t magic;
 | 
			
		||||
@@ -584,7 +606,7 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
 | 
			
		||||
            if (!buf) {
 | 
			
		||||
                return -EINVAL;
 | 
			
		||||
            }
 | 
			
		||||
            ret = vmdk_open_desc_file(bs, flags, buf, options, errp);
 | 
			
		||||
            ret = vmdk_open_desc_file(bs, flags, buf, errp);
 | 
			
		||||
            g_free(buf);
 | 
			
		||||
            return ret;
 | 
			
		||||
        }
 | 
			
		||||
@@ -647,8 +669,8 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
 | 
			
		||||
        char buf[64];
 | 
			
		||||
        snprintf(buf, sizeof(buf), "VMDK version %" PRId32,
 | 
			
		||||
                 le32_to_cpu(header.version));
 | 
			
		||||
        error_setg(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
 | 
			
		||||
                   bdrv_get_device_or_node_name(bs), "vmdk", buf);
 | 
			
		||||
        error_set(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
 | 
			
		||||
                  bdrv_get_device_name(bs), "vmdk", buf);
 | 
			
		||||
        return -ENOTSUP;
 | 
			
		||||
    } else if (le32_to_cpu(header.version) == 3 && (flags & BDRV_O_RDWR)) {
 | 
			
		||||
        /* VMware KB 2064959 explains that version 3 added support for
 | 
			
		||||
@@ -741,7 +763,7 @@ static int vmdk_parse_description(const char *desc, const char *opt_name,
 | 
			
		||||
/* Open an extent file and append to bs array */
 | 
			
		||||
static int vmdk_open_sparse(BlockDriverState *bs,
 | 
			
		||||
                            BlockDriverState *file, int flags,
 | 
			
		||||
                            char *buf, QDict *options, Error **errp)
 | 
			
		||||
                            char *buf, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    uint32_t magic;
 | 
			
		||||
 | 
			
		||||
@@ -751,7 +773,7 @@ static int vmdk_open_sparse(BlockDriverState *bs,
 | 
			
		||||
            return vmdk_open_vmfs_sparse(bs, file, flags, errp);
 | 
			
		||||
            break;
 | 
			
		||||
        case VMDK4_MAGIC:
 | 
			
		||||
            return vmdk_open_vmdk4(bs, file, flags, options, errp);
 | 
			
		||||
            return vmdk_open_vmdk4(bs, file, flags, errp);
 | 
			
		||||
            break;
 | 
			
		||||
        default:
 | 
			
		||||
            error_setg(errp, "Image not in VMDK format");
 | 
			
		||||
@@ -761,8 +783,7 @@ static int vmdk_open_sparse(BlockDriverState *bs,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
 | 
			
		||||
                              const char *desc_file_path, QDict *options,
 | 
			
		||||
                              Error **errp)
 | 
			
		||||
                              const char *desc_file_path, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    int ret;
 | 
			
		||||
    int matches;
 | 
			
		||||
@@ -776,7 +797,6 @@ static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
 | 
			
		||||
    BlockDriverState *extent_file;
 | 
			
		||||
    BDRVVmdkState *s = bs->opaque;
 | 
			
		||||
    VmdkExtent *extent;
 | 
			
		||||
    char extent_opt_prefix[32];
 | 
			
		||||
 | 
			
		||||
    while (*p) {
 | 
			
		||||
        /* parse extent line in one of below formats:
 | 
			
		||||
@@ -826,12 +846,8 @@ static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
 | 
			
		||||
        extent_path = g_malloc0(PATH_MAX);
 | 
			
		||||
        path_combine(extent_path, PATH_MAX, desc_file_path, fname);
 | 
			
		||||
        extent_file = NULL;
 | 
			
		||||
 | 
			
		||||
        ret = snprintf(extent_opt_prefix, 32, "extents.%d", s->num_extents);
 | 
			
		||||
        assert(ret < 32);
 | 
			
		||||
 | 
			
		||||
        ret = bdrv_open_image(&extent_file, extent_path, options,
 | 
			
		||||
                              extent_opt_prefix, bs, &child_file, false, errp);
 | 
			
		||||
        ret = bdrv_open(&extent_file, extent_path, NULL, NULL,
 | 
			
		||||
                        bs->open_flags | BDRV_O_PROTOCOL, NULL, errp);
 | 
			
		||||
        g_free(extent_path);
 | 
			
		||||
        if (ret) {
 | 
			
		||||
            return ret;
 | 
			
		||||
@@ -854,8 +870,7 @@ static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
 | 
			
		||||
            if (!buf) {
 | 
			
		||||
                ret = -EINVAL;
 | 
			
		||||
            } else {
 | 
			
		||||
                ret = vmdk_open_sparse(bs, extent_file, bs->open_flags, buf,
 | 
			
		||||
                                       options, errp);
 | 
			
		||||
                ret = vmdk_open_sparse(bs, extent_file, bs->open_flags, buf, errp);
 | 
			
		||||
            }
 | 
			
		||||
            g_free(buf);
 | 
			
		||||
            if (ret) {
 | 
			
		||||
@@ -883,7 +898,7 @@ next_line:
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int vmdk_open_desc_file(BlockDriverState *bs, int flags, char *buf,
 | 
			
		||||
                               QDict *options, Error **errp)
 | 
			
		||||
                               Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    int ret;
 | 
			
		||||
    char ct[128];
 | 
			
		||||
@@ -905,7 +920,7 @@ static int vmdk_open_desc_file(BlockDriverState *bs, int flags, char *buf,
 | 
			
		||||
    }
 | 
			
		||||
    s->create_type = g_strdup(ct);
 | 
			
		||||
    s->desc_offset = 0;
 | 
			
		||||
    ret = vmdk_parse_extents(buf, bs, bs->file->exact_filename, options, errp);
 | 
			
		||||
    ret = vmdk_parse_extents(buf, bs, bs->file->exact_filename, errp);
 | 
			
		||||
exit:
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
@@ -927,11 +942,11 @@ static int vmdk_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
    switch (magic) {
 | 
			
		||||
        case VMDK3_MAGIC:
 | 
			
		||||
        case VMDK4_MAGIC:
 | 
			
		||||
            ret = vmdk_open_sparse(bs, bs->file, flags, buf, options, errp);
 | 
			
		||||
            ret = vmdk_open_sparse(bs, bs->file, flags, buf, errp);
 | 
			
		||||
            s->desc_offset = 0x200;
 | 
			
		||||
            break;
 | 
			
		||||
        default:
 | 
			
		||||
            ret = vmdk_open_desc_file(bs, flags, buf, options, errp);
 | 
			
		||||
            ret = vmdk_open_desc_file(bs, flags, buf, errp);
 | 
			
		||||
            break;
 | 
			
		||||
    }
 | 
			
		||||
    if (ret) {
 | 
			
		||||
@@ -948,9 +963,9 @@ static int vmdk_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
    qemu_co_mutex_init(&s->lock);
 | 
			
		||||
 | 
			
		||||
    /* Disable migration when VMDK images are used */
 | 
			
		||||
    error_setg(&s->migration_blocker, "The vmdk format used by node '%s' "
 | 
			
		||||
               "does not support live migration",
 | 
			
		||||
               bdrv_get_device_or_node_name(bs));
 | 
			
		||||
    error_set(&s->migration_blocker,
 | 
			
		||||
              QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
 | 
			
		||||
              "vmdk", bdrv_get_device_name(bs), "live migration");
 | 
			
		||||
    migrate_add_blocker(s->migration_blocker);
 | 
			
		||||
    g_free(buf);
 | 
			
		||||
    return 0;
 | 
			
		||||
@@ -1690,12 +1705,12 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
 | 
			
		||||
    /* write all the data */
 | 
			
		||||
    ret = bdrv_pwrite(bs, 0, &magic, sizeof(magic));
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        error_setg(errp, QERR_IO_ERROR);
 | 
			
		||||
        error_set(errp, QERR_IO_ERROR);
 | 
			
		||||
        goto exit;
 | 
			
		||||
    }
 | 
			
		||||
    ret = bdrv_pwrite(bs, sizeof(magic), &header, sizeof(header));
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        error_setg(errp, QERR_IO_ERROR);
 | 
			
		||||
        error_set(errp, QERR_IO_ERROR);
 | 
			
		||||
        goto exit;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -1715,7 +1730,7 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
 | 
			
		||||
    ret = bdrv_pwrite(bs, le64_to_cpu(header.rgd_offset) * BDRV_SECTOR_SIZE,
 | 
			
		||||
                      gd_buf, gd_buf_size);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        error_setg(errp, QERR_IO_ERROR);
 | 
			
		||||
        error_set(errp, QERR_IO_ERROR);
 | 
			
		||||
        goto exit;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -1727,7 +1742,7 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
 | 
			
		||||
    ret = bdrv_pwrite(bs, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
 | 
			
		||||
                      gd_buf, gd_buf_size);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        error_setg(errp, QERR_IO_ERROR);
 | 
			
		||||
        error_set(errp, QERR_IO_ERROR);
 | 
			
		||||
        goto exit;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -1841,13 +1856,16 @@ static int vmdk_create(const char *filename, QemuOpts *opts, Error **errp)
 | 
			
		||||
    if (qemu_opt_get_bool_del(opts, BLOCK_OPT_COMPAT6, false)) {
 | 
			
		||||
        flags |= BLOCK_FLAG_COMPAT6;
 | 
			
		||||
    }
 | 
			
		||||
    if (qemu_opt_get_bool_del(opts, BLOCK_OPT_SCSI, false)) {
 | 
			
		||||
        flags |= BLOCK_FLAG_SCSI;
 | 
			
		||||
    }
 | 
			
		||||
    fmt = qemu_opt_get_del(opts, BLOCK_OPT_SUBFMT);
 | 
			
		||||
    if (qemu_opt_get_bool_del(opts, BLOCK_OPT_ZEROED_GRAIN, false)) {
 | 
			
		||||
        zeroed_grain = true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!adapter_type) {
 | 
			
		||||
        adapter_type = g_strdup("ide");
 | 
			
		||||
        adapter_type = g_strdup(flags & BLOCK_FLAG_SCSI ? "lsilogic" : "ide");
 | 
			
		||||
    } else if (strcmp(adapter_type, "ide") &&
 | 
			
		||||
               strcmp(adapter_type, "buslogic") &&
 | 
			
		||||
               strcmp(adapter_type, "lsilogic") &&
 | 
			
		||||
@@ -2262,6 +2280,12 @@ static QemuOptsList vmdk_create_opts = {
 | 
			
		||||
            .help = "Enable efficient zero writes "
 | 
			
		||||
                    "using the zeroed-grain GTE feature"
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
            .name = BLOCK_OPT_SCSI,
 | 
			
		||||
            .type = QEMU_OPT_BOOL,
 | 
			
		||||
            .help = "SCSI image",
 | 
			
		||||
            .def_value_str = "off"
 | 
			
		||||
        },
 | 
			
		||||
        { /* end of list */ }
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										28
									
								
								block/vpc.c
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								block/vpc.c
									
									
									
									
									
								
							@@ -168,6 +168,7 @@ static int vpc_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
    uint8_t buf[HEADER_SIZE];
 | 
			
		||||
    uint32_t checksum;
 | 
			
		||||
    uint64_t computed_size;
 | 
			
		||||
    uint64_t pagetable_size;
 | 
			
		||||
    int disk_type = VHD_DYNAMIC;
 | 
			
		||||
    int ret;
 | 
			
		||||
 | 
			
		||||
@@ -269,7 +270,17 @@ static int vpc_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
            goto fail;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        s->pagetable = qemu_try_blockalign(bs->file, s->max_table_entries * 4);
 | 
			
		||||
        if (s->max_table_entries > SIZE_MAX / 4 ||
 | 
			
		||||
            s->max_table_entries > (int) INT_MAX / 4) {
 | 
			
		||||
            error_setg(errp, "Max Table Entries too large (%" PRId32 ")",
 | 
			
		||||
                        s->max_table_entries);
 | 
			
		||||
            ret = -EINVAL;
 | 
			
		||||
            goto fail;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        pagetable_size = (uint64_t) s->max_table_entries * 4;
 | 
			
		||||
 | 
			
		||||
        s->pagetable = qemu_try_blockalign(bs->file, pagetable_size);
 | 
			
		||||
        if (s->pagetable == NULL) {
 | 
			
		||||
            ret = -ENOMEM;
 | 
			
		||||
            goto fail;
 | 
			
		||||
@@ -277,14 +288,13 @@ static int vpc_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
 | 
			
		||||
        s->bat_offset = be64_to_cpu(dyndisk_header->table_offset);
 | 
			
		||||
 | 
			
		||||
        ret = bdrv_pread(bs->file, s->bat_offset, s->pagetable,
 | 
			
		||||
                         s->max_table_entries * 4);
 | 
			
		||||
        ret = bdrv_pread(bs->file, s->bat_offset, s->pagetable, pagetable_size);
 | 
			
		||||
        if (ret < 0) {
 | 
			
		||||
            goto fail;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        s->free_data_block_offset =
 | 
			
		||||
            (s->bat_offset + (s->max_table_entries * 4) + 511) & ~511;
 | 
			
		||||
            ROUND_UP(s->bat_offset + pagetable_size, 512);
 | 
			
		||||
 | 
			
		||||
        for (i = 0; i < s->max_table_entries; i++) {
 | 
			
		||||
            be32_to_cpus(&s->pagetable[i]);
 | 
			
		||||
@@ -318,9 +328,9 @@ static int vpc_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
    qemu_co_mutex_init(&s->lock);
 | 
			
		||||
 | 
			
		||||
    /* Disable migration when VHD images are used */
 | 
			
		||||
    error_setg(&s->migration_blocker, "The vpc format used by node '%s' "
 | 
			
		||||
               "does not support live migration",
 | 
			
		||||
               bdrv_get_device_or_node_name(bs));
 | 
			
		||||
    error_set(&s->migration_blocker,
 | 
			
		||||
              QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
 | 
			
		||||
              "vpc", bdrv_get_device_name(bs), "live migration");
 | 
			
		||||
    migrate_add_blocker(s->migration_blocker);
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
@@ -813,7 +823,9 @@ static int vpc_create(const char *filename, QemuOpts *opts, Error **errp)
 | 
			
		||||
        }
 | 
			
		||||
    } else {
 | 
			
		||||
        total_sectors = (int64_t)cyls * heads * secs_per_cyl;
 | 
			
		||||
        total_size = total_sectors * BDRV_SECTOR_SIZE;
 | 
			
		||||
        if (disk_type != VHD_FIXED) {
 | 
			
		||||
            total_size = total_sectors * BDRV_SECTOR_SIZE;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Prepare the Hard Disk Footer */
 | 
			
		||||
 
 | 
			
		||||
@@ -30,7 +30,6 @@
 | 
			
		||||
#include "migration/migration.h"
 | 
			
		||||
#include "qapi/qmp/qint.h"
 | 
			
		||||
#include "qapi/qmp/qbool.h"
 | 
			
		||||
#include "qapi/qmp/qstring.h"
 | 
			
		||||
 | 
			
		||||
#ifndef S_IWGRP
 | 
			
		||||
#define S_IWGRP 0
 | 
			
		||||
@@ -323,7 +322,6 @@ typedef struct BDRVVVFATState {
 | 
			
		||||
 | 
			
		||||
    int fat_type; /* 16 or 32 */
 | 
			
		||||
    array_t fat,directory,mapping;
 | 
			
		||||
    char volume_label[11];
 | 
			
		||||
 | 
			
		||||
    unsigned int cluster_size;
 | 
			
		||||
    unsigned int sectors_per_cluster;
 | 
			
		||||
@@ -861,7 +859,7 @@ static int init_directories(BDRVVVFATState* s,
 | 
			
		||||
    {
 | 
			
		||||
	direntry_t* entry=array_get_next(&(s->directory));
 | 
			
		||||
	entry->attributes=0x28; /* archive | volume label */
 | 
			
		||||
        memcpy(entry->name, s->volume_label, sizeof(entry->name));
 | 
			
		||||
        memcpy(entry->name, "QEMU VVFAT ", sizeof(entry->name));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Now build FAT, and write back information into directory */
 | 
			
		||||
@@ -970,8 +968,7 @@ static int init_directories(BDRVVVFATState* s,
 | 
			
		||||
    bootsector->u.fat16.signature=0x29;
 | 
			
		||||
    bootsector->u.fat16.id=cpu_to_le32(0xfabe1afd);
 | 
			
		||||
 | 
			
		||||
    memcpy(bootsector->u.fat16.volume_label, s->volume_label,
 | 
			
		||||
           sizeof(bootsector->u.fat16.volume_label));
 | 
			
		||||
    memcpy(bootsector->u.fat16.volume_label,"QEMU VVFAT ",11);
 | 
			
		||||
    memcpy(bootsector->fat_type,(s->fat_type==12?"FAT12   ":s->fat_type==16?"FAT16   ":"FAT32   "),8);
 | 
			
		||||
    bootsector->magic[0]=0x55; bootsector->magic[1]=0xaa;
 | 
			
		||||
 | 
			
		||||
@@ -1010,11 +1007,6 @@ static QemuOptsList runtime_opts = {
 | 
			
		||||
            .type = QEMU_OPT_BOOL,
 | 
			
		||||
            .help = "Create a floppy rather than a hard disk image",
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
            .name = "label",
 | 
			
		||||
            .type = QEMU_OPT_STRING,
 | 
			
		||||
            .help = "Use a volume label other than QEMU VVFAT",
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
            .name = "rw",
 | 
			
		||||
            .type = QEMU_OPT_BOOL,
 | 
			
		||||
@@ -1067,8 +1059,8 @@ static void vvfat_parse_filename(const char *filename, QDict *options,
 | 
			
		||||
    /* Fill in the options QDict */
 | 
			
		||||
    qdict_put(options, "dir", qstring_from_str(filename));
 | 
			
		||||
    qdict_put(options, "fat-type", qint_from_int(fat_type));
 | 
			
		||||
    qdict_put(options, "floppy", qbool_from_bool(floppy));
 | 
			
		||||
    qdict_put(options, "rw", qbool_from_bool(rw));
 | 
			
		||||
    qdict_put(options, "floppy", qbool_from_int(floppy));
 | 
			
		||||
    qdict_put(options, "rw", qbool_from_int(rw));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
@@ -1077,7 +1069,7 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
    BDRVVVFATState *s = bs->opaque;
 | 
			
		||||
    int cyls, heads, secs;
 | 
			
		||||
    bool floppy;
 | 
			
		||||
    const char *dirname, *label;
 | 
			
		||||
    const char *dirname;
 | 
			
		||||
    QemuOpts *opts;
 | 
			
		||||
    Error *local_err = NULL;
 | 
			
		||||
    int ret;
 | 
			
		||||
@@ -1104,18 +1096,6 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
    s->fat_type = qemu_opt_get_number(opts, "fat-type", 0);
 | 
			
		||||
    floppy = qemu_opt_get_bool(opts, "floppy", false);
 | 
			
		||||
 | 
			
		||||
    memset(s->volume_label, ' ', sizeof(s->volume_label));
 | 
			
		||||
    label = qemu_opt_get(opts, "label");
 | 
			
		||||
    if (label) {
 | 
			
		||||
        size_t label_length = strlen(label);
 | 
			
		||||
        if (label_length > 11) {
 | 
			
		||||
            error_setg(errp, "vvfat label cannot be longer than 11 bytes");
 | 
			
		||||
            ret = -EINVAL;
 | 
			
		||||
            goto fail;
 | 
			
		||||
        }
 | 
			
		||||
        memcpy(s->volume_label, label, label_length);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (floppy) {
 | 
			
		||||
        /* 1.44MB or 2.88MB floppy.  2.88MB can be FAT12 (default) or FAT16. */
 | 
			
		||||
        if (!s->fat_type) {
 | 
			
		||||
@@ -1200,10 +1180,9 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
 | 
			
		||||
    /* Disable migration when vvfat is used rw */
 | 
			
		||||
    if (s->qcow) {
 | 
			
		||||
        error_setg(&s->migration_blocker,
 | 
			
		||||
                   "The vvfat (rw) format used by node '%s' "
 | 
			
		||||
                   "does not support live migration",
 | 
			
		||||
                   bdrv_get_device_or_node_name(bs));
 | 
			
		||||
        error_set(&s->migration_blocker,
 | 
			
		||||
                  QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
 | 
			
		||||
                  "vvfat (rw)", bdrv_get_device_name(bs), "live migration");
 | 
			
		||||
        migrate_add_blocker(s->migration_blocker);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -12,6 +12,7 @@
 | 
			
		||||
#include "sysemu/blockdev.h"
 | 
			
		||||
#include "sysemu/block-backend.h"
 | 
			
		||||
#include "hw/block/block.h"
 | 
			
		||||
#include "monitor/monitor.h"
 | 
			
		||||
#include "qapi/qmp/qerror.h"
 | 
			
		||||
#include "sysemu/sysemu.h"
 | 
			
		||||
#include "qmp-commands.h"
 | 
			
		||||
@@ -42,7 +43,7 @@ void qmp_nbd_server_start(SocketAddress *addr, Error **errp)
 | 
			
		||||
 | 
			
		||||
    server_fd = socket_listen(addr, errp);
 | 
			
		||||
    if (server_fd != -1) {
 | 
			
		||||
        qemu_set_fd_handler(server_fd, nbd_accept, NULL, NULL);
 | 
			
		||||
        qemu_set_fd_handler2(server_fd, NULL, nbd_accept, NULL, NULL);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -90,12 +91,11 @@ void qmp_nbd_server_add(const char *device, bool has_writable, bool writable,
 | 
			
		||||
 | 
			
		||||
    blk = blk_by_name(device);
 | 
			
		||||
    if (!blk) {
 | 
			
		||||
        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
 | 
			
		||||
                  "Device '%s' not found", device);
 | 
			
		||||
        error_set(errp, QERR_DEVICE_NOT_FOUND, device);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    if (!blk_is_inserted(blk)) {
 | 
			
		||||
        error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
 | 
			
		||||
        error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -129,7 +129,7 @@ void qmp_nbd_server_stop(Error **errp)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (server_fd != -1) {
 | 
			
		||||
        qemu_set_fd_handler(server_fd, NULL, NULL, NULL);
 | 
			
		||||
        qemu_set_fd_handler2(server_fd, NULL, NULL, NULL, NULL);
 | 
			
		||||
        close(server_fd);
 | 
			
		||||
        server_fd = -1;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										349
									
								
								blockdev.c
									
									
									
									
									
								
							
							
						
						
									
										349
									
								
								blockdev.c
									
									
									
									
									
								
							@@ -34,14 +34,11 @@
 | 
			
		||||
#include "sysemu/blockdev.h"
 | 
			
		||||
#include "hw/block/block.h"
 | 
			
		||||
#include "block/blockjob.h"
 | 
			
		||||
#include "block/throttle-groups.h"
 | 
			
		||||
#include "monitor/monitor.h"
 | 
			
		||||
#include "qemu/error-report.h"
 | 
			
		||||
#include "qemu/option.h"
 | 
			
		||||
#include "qemu/config-file.h"
 | 
			
		||||
#include "qapi/qmp/types.h"
 | 
			
		||||
#include "qapi-visit.h"
 | 
			
		||||
#include "qapi/qmp/qerror.h"
 | 
			
		||||
#include "qapi/qmp-output-visitor.h"
 | 
			
		||||
#include "qapi/util.h"
 | 
			
		||||
#include "sysemu/sysemu.h"
 | 
			
		||||
@@ -176,7 +173,7 @@ static int drive_index_to_unit_id(BlockInterfaceType type, int index)
 | 
			
		||||
 | 
			
		||||
QemuOpts *drive_def(const char *optstr)
 | 
			
		||||
{
 | 
			
		||||
    return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false);
 | 
			
		||||
    return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
 | 
			
		||||
@@ -360,7 +357,6 @@ static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
 | 
			
		||||
    const char *id;
 | 
			
		||||
    bool has_driver_specific_opts;
 | 
			
		||||
    BlockdevDetectZeroesOptions detect_zeroes;
 | 
			
		||||
    const char *throttling_group;
 | 
			
		||||
 | 
			
		||||
    /* Check common options by copying from bs_opts to opts, all other options
 | 
			
		||||
     * stay in bs_opts for processing by bdrv_open(). */
 | 
			
		||||
@@ -395,13 +391,13 @@ static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true)) {
 | 
			
		||||
    if (qemu_opt_get_bool(opts, "cache.writeback", true)) {
 | 
			
		||||
        bdrv_flags |= BDRV_O_CACHE_WB;
 | 
			
		||||
    }
 | 
			
		||||
    if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_DIRECT, false)) {
 | 
			
		||||
    if (qemu_opt_get_bool(opts, "cache.direct", false)) {
 | 
			
		||||
        bdrv_flags |= BDRV_O_NOCACHE;
 | 
			
		||||
    }
 | 
			
		||||
    if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, false)) {
 | 
			
		||||
    if (qemu_opt_get_bool(opts, "cache.no-flush", false)) {
 | 
			
		||||
        bdrv_flags |= BDRV_O_NO_FLUSH;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -463,8 +459,6 @@ static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
 | 
			
		||||
 | 
			
		||||
    cfg.op_size = qemu_opt_get_number(opts, "throttling.iops-size", 0);
 | 
			
		||||
 | 
			
		||||
    throttling_group = qemu_opt_get(opts, "throttling.group");
 | 
			
		||||
 | 
			
		||||
    if (!check_throttle_config(&cfg, &error)) {
 | 
			
		||||
        error_propagate(errp, error);
 | 
			
		||||
        goto early_err;
 | 
			
		||||
@@ -553,10 +547,7 @@ static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
 | 
			
		||||
 | 
			
		||||
    /* disk I/O throttling */
 | 
			
		||||
    if (throttle_enabled(&cfg)) {
 | 
			
		||||
        if (!throttling_group) {
 | 
			
		||||
            throttling_group = blk_name(blk);
 | 
			
		||||
        }
 | 
			
		||||
        bdrv_io_limits_enable(bs, throttling_group);
 | 
			
		||||
        bdrv_io_limits_enable(bs);
 | 
			
		||||
        bdrv_set_io_limits(bs, &cfg);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -720,8 +711,6 @@ DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
 | 
			
		||||
 | 
			
		||||
        { "iops_size",      "throttling.iops-size" },
 | 
			
		||||
 | 
			
		||||
        { "group",          "throttling.group" },
 | 
			
		||||
 | 
			
		||||
        { "readonly",       "read-only" },
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
@@ -744,16 +733,16 @@ DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /* Specific options take precedence */
 | 
			
		||||
        if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
 | 
			
		||||
            qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
 | 
			
		||||
        if (!qemu_opt_get(all_opts, "cache.writeback")) {
 | 
			
		||||
            qemu_opt_set_bool(all_opts, "cache.writeback",
 | 
			
		||||
                              !!(flags & BDRV_O_CACHE_WB), &error_abort);
 | 
			
		||||
        }
 | 
			
		||||
        if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
 | 
			
		||||
            qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
 | 
			
		||||
        if (!qemu_opt_get(all_opts, "cache.direct")) {
 | 
			
		||||
            qemu_opt_set_bool(all_opts, "cache.direct",
 | 
			
		||||
                              !!(flags & BDRV_O_NOCACHE), &error_abort);
 | 
			
		||||
        }
 | 
			
		||||
        if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
 | 
			
		||||
            qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
 | 
			
		||||
        if (!qemu_opt_get(all_opts, "cache.no-flush")) {
 | 
			
		||||
            qemu_opt_set_bool(all_opts, "cache.no-flush",
 | 
			
		||||
                              !!(flags & BDRV_O_NO_FLUSH), &error_abort);
 | 
			
		||||
        }
 | 
			
		||||
        qemu_opt_unset(all_opts, "cache");
 | 
			
		||||
@@ -1113,8 +1102,7 @@ SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
 | 
			
		||||
 | 
			
		||||
    blk = blk_by_name(device);
 | 
			
		||||
    if (!blk) {
 | 
			
		||||
        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
 | 
			
		||||
                  "Device '%s' not found", device);
 | 
			
		||||
        error_set(errp, QERR_DEVICE_NOT_FOUND, device);
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
    bs = blk_bs(blk);
 | 
			
		||||
@@ -1176,68 +1164,6 @@ out_aio_context:
 | 
			
		||||
    return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * block_dirty_bitmap_lookup:
 | 
			
		||||
 * Return a dirty bitmap (if present), after validating
 | 
			
		||||
 * the node reference and bitmap names.
 | 
			
		||||
 *
 | 
			
		||||
 * @node: The name of the BDS node to search for bitmaps
 | 
			
		||||
 * @name: The name of the bitmap to search for
 | 
			
		||||
 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
 | 
			
		||||
 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
 | 
			
		||||
 * @errp: Output pointer for error information. Can be NULL.
 | 
			
		||||
 *
 | 
			
		||||
 * @return: A bitmap object on success, or NULL on failure.
 | 
			
		||||
 */
 | 
			
		||||
static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node,
 | 
			
		||||
                                                  const char *name,
 | 
			
		||||
                                                  BlockDriverState **pbs,
 | 
			
		||||
                                                  AioContext **paio,
 | 
			
		||||
                                                  Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    BlockDriverState *bs;
 | 
			
		||||
    BdrvDirtyBitmap *bitmap;
 | 
			
		||||
    AioContext *aio_context;
 | 
			
		||||
 | 
			
		||||
    if (!node) {
 | 
			
		||||
        error_setg(errp, "Node cannot be NULL");
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
    if (!name) {
 | 
			
		||||
        error_setg(errp, "Bitmap name cannot be NULL");
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
    bs = bdrv_lookup_bs(node, node, NULL);
 | 
			
		||||
    if (!bs) {
 | 
			
		||||
        error_setg(errp, "Node '%s' not found", node);
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    aio_context = bdrv_get_aio_context(bs);
 | 
			
		||||
    aio_context_acquire(aio_context);
 | 
			
		||||
 | 
			
		||||
    bitmap = bdrv_find_dirty_bitmap(bs, name);
 | 
			
		||||
    if (!bitmap) {
 | 
			
		||||
        error_setg(errp, "Dirty bitmap '%s' not found", name);
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (pbs) {
 | 
			
		||||
        *pbs = bs;
 | 
			
		||||
    }
 | 
			
		||||
    if (paio) {
 | 
			
		||||
        *paio = aio_context;
 | 
			
		||||
    } else {
 | 
			
		||||
        aio_context_release(aio_context);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return bitmap;
 | 
			
		||||
 | 
			
		||||
 fail:
 | 
			
		||||
    aio_context_release(aio_context);
 | 
			
		||||
    return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* New and old BlockDriverState structs for atomic group operations */
 | 
			
		||||
 | 
			
		||||
typedef struct BlkTransactionState BlkTransactionState;
 | 
			
		||||
@@ -1303,8 +1229,7 @@ static void internal_snapshot_prepare(BlkTransactionState *common,
 | 
			
		||||
    /* 2. check for validation */
 | 
			
		||||
    blk = blk_by_name(device);
 | 
			
		||||
    if (!blk) {
 | 
			
		||||
        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
 | 
			
		||||
                  "Device '%s' not found", device);
 | 
			
		||||
        error_set(errp, QERR_DEVICE_NOT_FOUND, device);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    bs = blk_bs(blk);
 | 
			
		||||
@@ -1314,7 +1239,7 @@ static void internal_snapshot_prepare(BlkTransactionState *common,
 | 
			
		||||
    aio_context_acquire(state->aio_context);
 | 
			
		||||
 | 
			
		||||
    if (!bdrv_is_inserted(bs)) {
 | 
			
		||||
        error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
 | 
			
		||||
        error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -1323,14 +1248,13 @@ static void internal_snapshot_prepare(BlkTransactionState *common,
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (bdrv_is_read_only(bs)) {
 | 
			
		||||
        error_setg(errp, "Device '%s' is read only", device);
 | 
			
		||||
        error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!bdrv_can_snapshot(bs)) {
 | 
			
		||||
        error_setg(errp, "Block format '%s' used by device '%s' "
 | 
			
		||||
                   "does not support internal snapshots",
 | 
			
		||||
                   bs->drv->format_name, device);
 | 
			
		||||
        error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
 | 
			
		||||
                  bs->drv->format_name, device, "internal snapshot");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -1455,7 +1379,7 @@ static void external_snapshot_prepare(BlkTransactionState *common,
 | 
			
		||||
    /* start processing */
 | 
			
		||||
    drv = bdrv_find_format(format);
 | 
			
		||||
    if (!drv) {
 | 
			
		||||
        error_setg(errp, QERR_INVALID_BLOCK_FORMAT, format);
 | 
			
		||||
        error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -1482,7 +1406,7 @@ static void external_snapshot_prepare(BlkTransactionState *common,
 | 
			
		||||
    aio_context_acquire(state->aio_context);
 | 
			
		||||
 | 
			
		||||
    if (!bdrv_is_inserted(state->old_bs)) {
 | 
			
		||||
        error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
 | 
			
		||||
        error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -1493,13 +1417,13 @@ static void external_snapshot_prepare(BlkTransactionState *common,
 | 
			
		||||
 | 
			
		||||
    if (!bdrv_is_read_only(state->old_bs)) {
 | 
			
		||||
        if (bdrv_flush(state->old_bs)) {
 | 
			
		||||
            error_setg(errp, QERR_IO_ERROR);
 | 
			
		||||
            error_set(errp, QERR_IO_ERROR);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!bdrv_is_first_non_filter(state->old_bs)) {
 | 
			
		||||
        error_setg(errp, QERR_FEATURE_DISABLED, "snapshot");
 | 
			
		||||
        error_set(errp, QERR_FEATURE_DISABLED, "snapshot");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -1584,8 +1508,7 @@ static void drive_backup_prepare(BlkTransactionState *common, Error **errp)
 | 
			
		||||
 | 
			
		||||
    blk = blk_by_name(backup->device);
 | 
			
		||||
    if (!blk) {
 | 
			
		||||
        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
 | 
			
		||||
                  "Device '%s' not found", backup->device);
 | 
			
		||||
        error_set(errp, QERR_DEVICE_NOT_FOUND, backup->device);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    bs = blk_bs(blk);
 | 
			
		||||
@@ -1599,7 +1522,6 @@ static void drive_backup_prepare(BlkTransactionState *common, Error **errp)
 | 
			
		||||
                     backup->sync,
 | 
			
		||||
                     backup->has_mode, backup->mode,
 | 
			
		||||
                     backup->has_speed, backup->speed,
 | 
			
		||||
                     backup->has_bitmap, backup->bitmap,
 | 
			
		||||
                     backup->has_on_source_error, backup->on_source_error,
 | 
			
		||||
                     backup->has_on_target_error, backup->on_target_error,
 | 
			
		||||
                     &local_err);
 | 
			
		||||
@@ -1855,8 +1777,7 @@ void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
 | 
			
		||||
 | 
			
		||||
    blk = blk_by_name(device);
 | 
			
		||||
    if (!blk) {
 | 
			
		||||
        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
 | 
			
		||||
                  "Device '%s' not found", device);
 | 
			
		||||
        error_set(errp, QERR_DEVICE_NOT_FOUND, device);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -1916,8 +1837,7 @@ void qmp_change_blockdev(const char *device, const char *filename,
 | 
			
		||||
 | 
			
		||||
    blk = blk_by_name(device);
 | 
			
		||||
    if (!blk) {
 | 
			
		||||
        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
 | 
			
		||||
                  "Device '%s' not found", device);
 | 
			
		||||
        error_set(errp, QERR_DEVICE_NOT_FOUND, device);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    bs = blk_bs(blk);
 | 
			
		||||
@@ -1928,7 +1848,7 @@ void qmp_change_blockdev(const char *device, const char *filename,
 | 
			
		||||
    if (format) {
 | 
			
		||||
        drv = bdrv_find_whitelisted_format(format, bs->read_only);
 | 
			
		||||
        if (!drv) {
 | 
			
		||||
            error_setg(errp, QERR_INVALID_BLOCK_FORMAT, format);
 | 
			
		||||
            error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
 | 
			
		||||
            goto out;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
@@ -1967,9 +1887,7 @@ void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
 | 
			
		||||
                               bool has_iops_wr_max,
 | 
			
		||||
                               int64_t iops_wr_max,
 | 
			
		||||
                               bool has_iops_size,
 | 
			
		||||
                               int64_t iops_size,
 | 
			
		||||
                               bool has_group,
 | 
			
		||||
                               const char *group, Error **errp)
 | 
			
		||||
                               int64_t iops_size, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    ThrottleConfig cfg;
 | 
			
		||||
    BlockDriverState *bs;
 | 
			
		||||
@@ -1978,8 +1896,7 @@ void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
 | 
			
		||||
 | 
			
		||||
    blk = blk_by_name(device);
 | 
			
		||||
    if (!blk) {
 | 
			
		||||
        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
 | 
			
		||||
                  "Device '%s' not found", device);
 | 
			
		||||
        error_set(errp, QERR_DEVICE_NOT_FOUND, device);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    bs = blk_bs(blk);
 | 
			
		||||
@@ -2023,121 +1940,20 @@ void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
 | 
			
		||||
    aio_context = bdrv_get_aio_context(bs);
 | 
			
		||||
    aio_context_acquire(aio_context);
 | 
			
		||||
 | 
			
		||||
    if (throttle_enabled(&cfg)) {
 | 
			
		||||
        /* Enable I/O limits if they're not enabled yet, otherwise
 | 
			
		||||
         * just update the throttling group. */
 | 
			
		||||
        if (!bs->io_limits_enabled) {
 | 
			
		||||
            bdrv_io_limits_enable(bs, has_group ? group : device);
 | 
			
		||||
        } else if (has_group) {
 | 
			
		||||
            bdrv_io_limits_update_group(bs, group);
 | 
			
		||||
        }
 | 
			
		||||
        /* Set the new throttling configuration */
 | 
			
		||||
        bdrv_set_io_limits(bs, &cfg);
 | 
			
		||||
    } else if (bs->io_limits_enabled) {
 | 
			
		||||
        /* If all throttling settings are set to 0, disable I/O limits */
 | 
			
		||||
    if (!bs->io_limits_enabled && throttle_enabled(&cfg)) {
 | 
			
		||||
        bdrv_io_limits_enable(bs);
 | 
			
		||||
    } else if (bs->io_limits_enabled && !throttle_enabled(&cfg)) {
 | 
			
		||||
        bdrv_io_limits_disable(bs);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (bs->io_limits_enabled) {
 | 
			
		||||
        bdrv_set_io_limits(bs, &cfg);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    aio_context_release(aio_context);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void qmp_block_dirty_bitmap_add(const char *node, const char *name,
 | 
			
		||||
                                bool has_granularity, uint32_t granularity,
 | 
			
		||||
                                Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    AioContext *aio_context;
 | 
			
		||||
    BlockDriverState *bs;
 | 
			
		||||
 | 
			
		||||
    if (!name || name[0] == '\0') {
 | 
			
		||||
        error_setg(errp, "Bitmap name cannot be empty");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    bs = bdrv_lookup_bs(node, node, errp);
 | 
			
		||||
    if (!bs) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    aio_context = bdrv_get_aio_context(bs);
 | 
			
		||||
    aio_context_acquire(aio_context);
 | 
			
		||||
 | 
			
		||||
    if (has_granularity) {
 | 
			
		||||
        if (granularity < 512 || !is_power_of_2(granularity)) {
 | 
			
		||||
            error_setg(errp, "Granularity must be power of 2 "
 | 
			
		||||
                             "and at least 512");
 | 
			
		||||
            goto out;
 | 
			
		||||
        }
 | 
			
		||||
    } else {
 | 
			
		||||
        /* Default to cluster size, if available: */
 | 
			
		||||
        granularity = bdrv_get_default_bitmap_granularity(bs);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    bdrv_create_dirty_bitmap(bs, granularity, name, errp);
 | 
			
		||||
 | 
			
		||||
 out:
 | 
			
		||||
    aio_context_release(aio_context);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void qmp_block_dirty_bitmap_remove(const char *node, const char *name,
 | 
			
		||||
                                   Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    AioContext *aio_context;
 | 
			
		||||
    BlockDriverState *bs;
 | 
			
		||||
    BdrvDirtyBitmap *bitmap;
 | 
			
		||||
 | 
			
		||||
    bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
 | 
			
		||||
    if (!bitmap || !bs) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (bdrv_dirty_bitmap_frozen(bitmap)) {
 | 
			
		||||
        error_setg(errp,
 | 
			
		||||
                   "Bitmap '%s' is currently frozen and cannot be removed",
 | 
			
		||||
                   name);
 | 
			
		||||
        goto out;
 | 
			
		||||
    }
 | 
			
		||||
    bdrv_dirty_bitmap_make_anon(bitmap);
 | 
			
		||||
    bdrv_release_dirty_bitmap(bs, bitmap);
 | 
			
		||||
 | 
			
		||||
 out:
 | 
			
		||||
    aio_context_release(aio_context);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
 | 
			
		||||
 * immediately after a full backup operation.
 | 
			
		||||
 */
 | 
			
		||||
void qmp_block_dirty_bitmap_clear(const char *node, const char *name,
 | 
			
		||||
                                  Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    AioContext *aio_context;
 | 
			
		||||
    BdrvDirtyBitmap *bitmap;
 | 
			
		||||
    BlockDriverState *bs;
 | 
			
		||||
 | 
			
		||||
    bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp);
 | 
			
		||||
    if (!bitmap || !bs) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (bdrv_dirty_bitmap_frozen(bitmap)) {
 | 
			
		||||
        error_setg(errp,
 | 
			
		||||
                   "Bitmap '%s' is currently frozen and cannot be modified",
 | 
			
		||||
                   name);
 | 
			
		||||
        goto out;
 | 
			
		||||
    } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
 | 
			
		||||
        error_setg(errp,
 | 
			
		||||
                   "Bitmap '%s' is currently disabled and cannot be cleared",
 | 
			
		||||
                   name);
 | 
			
		||||
        goto out;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    bdrv_clear_dirty_bitmap(bitmap);
 | 
			
		||||
 | 
			
		||||
 out:
 | 
			
		||||
    aio_context_release(aio_context);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void hmp_drive_del(Monitor *mon, const QDict *qdict)
 | 
			
		||||
int hmp_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
 | 
			
		||||
{
 | 
			
		||||
    const char *id = qdict_get_str(qdict, "id");
 | 
			
		||||
    BlockBackend *blk;
 | 
			
		||||
@@ -2148,14 +1964,14 @@ void hmp_drive_del(Monitor *mon, const QDict *qdict)
 | 
			
		||||
    blk = blk_by_name(id);
 | 
			
		||||
    if (!blk) {
 | 
			
		||||
        error_report("Device '%s' not found", id);
 | 
			
		||||
        return;
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
    bs = blk_bs(blk);
 | 
			
		||||
 | 
			
		||||
    if (!blk_legacy_dinfo(blk)) {
 | 
			
		||||
        error_report("Deleting device added with blockdev-add"
 | 
			
		||||
                     " is not supported");
 | 
			
		||||
        return;
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    aio_context = bdrv_get_aio_context(bs);
 | 
			
		||||
@@ -2164,9 +1980,12 @@ void hmp_drive_del(Monitor *mon, const QDict *qdict)
 | 
			
		||||
    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
 | 
			
		||||
        error_report_err(local_err);
 | 
			
		||||
        aio_context_release(aio_context);
 | 
			
		||||
        return;
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* quiesce block driver; prevent further io */
 | 
			
		||||
    bdrv_drain_all();
 | 
			
		||||
    bdrv_flush(bs);
 | 
			
		||||
    bdrv_close(bs);
 | 
			
		||||
 | 
			
		||||
    /* if we have a device attached to this BlockDriverState
 | 
			
		||||
@@ -2184,6 +2003,7 @@ void hmp_drive_del(Monitor *mon, const QDict *qdict)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    aio_context_release(aio_context);
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void qmp_block_resize(bool has_device, const char *device,
 | 
			
		||||
@@ -2207,17 +2027,17 @@ void qmp_block_resize(bool has_device, const char *device,
 | 
			
		||||
    aio_context_acquire(aio_context);
 | 
			
		||||
 | 
			
		||||
    if (!bdrv_is_first_non_filter(bs)) {
 | 
			
		||||
        error_setg(errp, QERR_FEATURE_DISABLED, "resize");
 | 
			
		||||
        error_set(errp, QERR_FEATURE_DISABLED, "resize");
 | 
			
		||||
        goto out;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (size < 0) {
 | 
			
		||||
        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
 | 
			
		||||
        error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
 | 
			
		||||
        goto out;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
 | 
			
		||||
        error_setg(errp, QERR_DEVICE_IN_USE, device);
 | 
			
		||||
        error_set(errp, QERR_DEVICE_IN_USE, device);
 | 
			
		||||
        goto out;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -2229,16 +2049,16 @@ void qmp_block_resize(bool has_device, const char *device,
 | 
			
		||||
    case 0:
 | 
			
		||||
        break;
 | 
			
		||||
    case -ENOMEDIUM:
 | 
			
		||||
        error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
 | 
			
		||||
        error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
 | 
			
		||||
        break;
 | 
			
		||||
    case -ENOTSUP:
 | 
			
		||||
        error_setg(errp, QERR_UNSUPPORTED);
 | 
			
		||||
        error_set(errp, QERR_UNSUPPORTED);
 | 
			
		||||
        break;
 | 
			
		||||
    case -EACCES:
 | 
			
		||||
        error_setg(errp, "Device '%s' is read only", device);
 | 
			
		||||
        error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
 | 
			
		||||
        break;
 | 
			
		||||
    case -EBUSY:
 | 
			
		||||
        error_setg(errp, QERR_DEVICE_IN_USE, device);
 | 
			
		||||
        error_set(errp, QERR_DEVICE_IN_USE, device);
 | 
			
		||||
        break;
 | 
			
		||||
    default:
 | 
			
		||||
        error_setg_errno(errp, -ret, "Could not resize");
 | 
			
		||||
@@ -2296,8 +2116,7 @@ void qmp_block_stream(const char *device,
 | 
			
		||||
 | 
			
		||||
    blk = blk_by_name(device);
 | 
			
		||||
    if (!blk) {
 | 
			
		||||
        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
 | 
			
		||||
                  "Device '%s' not found", device);
 | 
			
		||||
        error_set(errp, QERR_DEVICE_NOT_FOUND, device);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    bs = blk_bs(blk);
 | 
			
		||||
@@ -2312,7 +2131,7 @@ void qmp_block_stream(const char *device,
 | 
			
		||||
    if (has_base) {
 | 
			
		||||
        base_bs = bdrv_find_backing_image(bs, base);
 | 
			
		||||
        if (base_bs == NULL) {
 | 
			
		||||
            error_setg(errp, QERR_BASE_NOT_FOUND, base);
 | 
			
		||||
            error_set(errp, QERR_BASE_NOT_FOUND, base);
 | 
			
		||||
            goto out;
 | 
			
		||||
        }
 | 
			
		||||
        assert(bdrv_get_aio_context(base_bs) == aio_context);
 | 
			
		||||
@@ -2371,8 +2190,7 @@ void qmp_block_commit(const char *device,
 | 
			
		||||
     *  scenario in which all optional arguments are omitted. */
 | 
			
		||||
    blk = blk_by_name(device);
 | 
			
		||||
    if (!blk) {
 | 
			
		||||
        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
 | 
			
		||||
                  "Device '%s' not found", device);
 | 
			
		||||
        error_set(errp, QERR_DEVICE_NOT_FOUND, device);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    bs = blk_bs(blk);
 | 
			
		||||
@@ -2410,7 +2228,7 @@ void qmp_block_commit(const char *device,
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (base_bs == NULL) {
 | 
			
		||||
        error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
 | 
			
		||||
        error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
 | 
			
		||||
        goto out;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -2452,7 +2270,6 @@ void qmp_drive_backup(const char *device, const char *target,
 | 
			
		||||
                      enum MirrorSyncMode sync,
 | 
			
		||||
                      bool has_mode, enum NewImageMode mode,
 | 
			
		||||
                      bool has_speed, int64_t speed,
 | 
			
		||||
                      bool has_bitmap, const char *bitmap,
 | 
			
		||||
                      bool has_on_source_error, BlockdevOnError on_source_error,
 | 
			
		||||
                      bool has_on_target_error, BlockdevOnError on_target_error,
 | 
			
		||||
                      Error **errp)
 | 
			
		||||
@@ -2461,7 +2278,6 @@ void qmp_drive_backup(const char *device, const char *target,
 | 
			
		||||
    BlockDriverState *bs;
 | 
			
		||||
    BlockDriverState *target_bs;
 | 
			
		||||
    BlockDriverState *source = NULL;
 | 
			
		||||
    BdrvDirtyBitmap *bmap = NULL;
 | 
			
		||||
    AioContext *aio_context;
 | 
			
		||||
    BlockDriver *drv = NULL;
 | 
			
		||||
    Error *local_err = NULL;
 | 
			
		||||
@@ -2484,8 +2300,7 @@ void qmp_drive_backup(const char *device, const char *target,
 | 
			
		||||
 | 
			
		||||
    blk = blk_by_name(device);
 | 
			
		||||
    if (!blk) {
 | 
			
		||||
        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
 | 
			
		||||
                  "Device '%s' not found", device);
 | 
			
		||||
        error_set(errp, QERR_DEVICE_NOT_FOUND, device);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    bs = blk_bs(blk);
 | 
			
		||||
@@ -2496,7 +2311,7 @@ void qmp_drive_backup(const char *device, const char *target,
 | 
			
		||||
    /* Although backup_run has this check too, we need to use bs->drv below, so
 | 
			
		||||
     * do an early check redundantly. */
 | 
			
		||||
    if (!bdrv_is_inserted(bs)) {
 | 
			
		||||
        error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
 | 
			
		||||
        error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
 | 
			
		||||
        goto out;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -2506,7 +2321,7 @@ void qmp_drive_backup(const char *device, const char *target,
 | 
			
		||||
    if (format) {
 | 
			
		||||
        drv = bdrv_find_format(format);
 | 
			
		||||
        if (!drv) {
 | 
			
		||||
            error_setg(errp, QERR_INVALID_BLOCK_FORMAT, format);
 | 
			
		||||
            error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
 | 
			
		||||
            goto out;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
@@ -2562,16 +2377,7 @@ void qmp_drive_backup(const char *device, const char *target,
 | 
			
		||||
 | 
			
		||||
    bdrv_set_aio_context(target_bs, aio_context);
 | 
			
		||||
 | 
			
		||||
    if (has_bitmap) {
 | 
			
		||||
        bmap = bdrv_find_dirty_bitmap(bs, bitmap);
 | 
			
		||||
        if (!bmap) {
 | 
			
		||||
            error_setg(errp, "Bitmap '%s' could not be found", bitmap);
 | 
			
		||||
            goto out;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    backup_start(bs, target_bs, speed, sync, bmap,
 | 
			
		||||
                 on_source_error, on_target_error,
 | 
			
		||||
    backup_start(bs, target_bs, speed, sync, on_source_error, on_target_error,
 | 
			
		||||
                 block_job_cb, bs, &local_err);
 | 
			
		||||
    if (local_err != NULL) {
 | 
			
		||||
        bdrv_unref(target_bs);
 | 
			
		||||
@@ -2585,7 +2391,7 @@ out:
 | 
			
		||||
 | 
			
		||||
BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    return bdrv_named_nodes_list(errp);
 | 
			
		||||
    return bdrv_named_nodes_list();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void qmp_blockdev_backup(const char *device, const char *target,
 | 
			
		||||
@@ -2632,8 +2438,8 @@ void qmp_blockdev_backup(const char *device, const char *target,
 | 
			
		||||
 | 
			
		||||
    bdrv_ref(target_bs);
 | 
			
		||||
    bdrv_set_aio_context(target_bs, aio_context);
 | 
			
		||||
    backup_start(bs, target_bs, speed, sync, NULL, on_source_error,
 | 
			
		||||
                 on_target_error, block_job_cb, bs, &local_err);
 | 
			
		||||
    backup_start(bs, target_bs, speed, sync, on_source_error, on_target_error,
 | 
			
		||||
                 block_job_cb, bs, &local_err);
 | 
			
		||||
    if (local_err != NULL) {
 | 
			
		||||
        bdrv_unref(target_bs);
 | 
			
		||||
        error_propagate(errp, local_err);
 | 
			
		||||
@@ -2692,20 +2498,18 @@ void qmp_drive_mirror(const char *device, const char *target,
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
 | 
			
		||||
        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
 | 
			
		||||
                   "a value in range [512B, 64MB]");
 | 
			
		||||
        error_set(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
 | 
			
		||||
                  "a value in range [512B, 64MB]");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    if (granularity & (granularity - 1)) {
 | 
			
		||||
        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
 | 
			
		||||
                   "power of 2");
 | 
			
		||||
        error_set(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", "power of 2");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    blk = blk_by_name(device);
 | 
			
		||||
    if (!blk) {
 | 
			
		||||
        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
 | 
			
		||||
                  "Device '%s' not found", device);
 | 
			
		||||
        error_set(errp, QERR_DEVICE_NOT_FOUND, device);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    bs = blk_bs(blk);
 | 
			
		||||
@@ -2714,7 +2518,7 @@ void qmp_drive_mirror(const char *device, const char *target,
 | 
			
		||||
    aio_context_acquire(aio_context);
 | 
			
		||||
 | 
			
		||||
    if (!bdrv_is_inserted(bs)) {
 | 
			
		||||
        error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
 | 
			
		||||
        error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
 | 
			
		||||
        goto out;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -2724,7 +2528,7 @@ void qmp_drive_mirror(const char *device, const char *target,
 | 
			
		||||
    if (format) {
 | 
			
		||||
        drv = bdrv_find_format(format);
 | 
			
		||||
        if (!drv) {
 | 
			
		||||
            error_setg(errp, QERR_INVALID_BLOCK_FORMAT, format);
 | 
			
		||||
            error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
 | 
			
		||||
            goto out;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
@@ -2900,7 +2704,7 @@ void qmp_block_job_cancel(const char *device,
 | 
			
		||||
        force = false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (job->user_paused && !force) {
 | 
			
		||||
    if (job->paused && !force) {
 | 
			
		||||
        error_setg(errp, "The block job for device '%s' is currently paused",
 | 
			
		||||
                   device);
 | 
			
		||||
        goto out;
 | 
			
		||||
@@ -2917,11 +2721,10 @@ void qmp_block_job_pause(const char *device, Error **errp)
 | 
			
		||||
    AioContext *aio_context;
 | 
			
		||||
    BlockJob *job = find_block_job(device, &aio_context, errp);
 | 
			
		||||
 | 
			
		||||
    if (!job || job->user_paused) {
 | 
			
		||||
    if (!job) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    job->user_paused = true;
 | 
			
		||||
    trace_qmp_block_job_pause(job);
 | 
			
		||||
    block_job_pause(job);
 | 
			
		||||
    aio_context_release(aio_context);
 | 
			
		||||
@@ -2932,11 +2735,10 @@ void qmp_block_job_resume(const char *device, Error **errp)
 | 
			
		||||
    AioContext *aio_context;
 | 
			
		||||
    BlockJob *job = find_block_job(device, &aio_context, errp);
 | 
			
		||||
 | 
			
		||||
    if (!job || !job->user_paused) {
 | 
			
		||||
    if (!job) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    job->user_paused = false;
 | 
			
		||||
    trace_qmp_block_job_resume(job);
 | 
			
		||||
    block_job_resume(job);
 | 
			
		||||
    aio_context_release(aio_context);
 | 
			
		||||
@@ -2972,8 +2774,7 @@ void qmp_change_backing_file(const char *device,
 | 
			
		||||
 | 
			
		||||
    blk = blk_by_name(device);
 | 
			
		||||
    if (!blk) {
 | 
			
		||||
        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
 | 
			
		||||
                  "Device '%s' not found", device);
 | 
			
		||||
        error_set(errp, QERR_DEVICE_NOT_FOUND, device);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    bs = blk_bs(blk);
 | 
			
		||||
@@ -3137,15 +2938,15 @@ QemuOptsList qemu_common_drive_opts = {
 | 
			
		||||
            .type = QEMU_OPT_STRING,
 | 
			
		||||
            .help = "discard operation (ignore/off, unmap/on)",
 | 
			
		||||
        },{
 | 
			
		||||
            .name = BDRV_OPT_CACHE_WB,
 | 
			
		||||
            .name = "cache.writeback",
 | 
			
		||||
            .type = QEMU_OPT_BOOL,
 | 
			
		||||
            .help = "enables writeback mode for any caches",
 | 
			
		||||
        },{
 | 
			
		||||
            .name = BDRV_OPT_CACHE_DIRECT,
 | 
			
		||||
            .name = "cache.direct",
 | 
			
		||||
            .type = QEMU_OPT_BOOL,
 | 
			
		||||
            .help = "enables use of O_DIRECT (bypass the host page cache)",
 | 
			
		||||
        },{
 | 
			
		||||
            .name = BDRV_OPT_CACHE_NO_FLUSH,
 | 
			
		||||
            .name = "cache.no-flush",
 | 
			
		||||
            .type = QEMU_OPT_BOOL,
 | 
			
		||||
            .help = "ignore any flush requests for the device",
 | 
			
		||||
        },{
 | 
			
		||||
@@ -3220,10 +3021,6 @@ QemuOptsList qemu_common_drive_opts = {
 | 
			
		||||
            .name = "throttling.iops-size",
 | 
			
		||||
            .type = QEMU_OPT_NUMBER,
 | 
			
		||||
            .help = "when limiting by iops max size of an I/O in bytes",
 | 
			
		||||
        },{
 | 
			
		||||
            .name = "throttling.group",
 | 
			
		||||
            .type = QEMU_OPT_STRING,
 | 
			
		||||
            .help = "name of the block throttling group",
 | 
			
		||||
        },{
 | 
			
		||||
            .name = "copy-on-read",
 | 
			
		||||
            .type = QEMU_OPT_BOOL,
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										32
									
								
								blockjob.c
									
									
									
									
									
								
							
							
						
						
									
										32
									
								
								blockjob.c
									
									
									
									
									
								
							@@ -29,7 +29,6 @@
 | 
			
		||||
#include "block/block.h"
 | 
			
		||||
#include "block/blockjob.h"
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
#include "qapi/qmp/qerror.h"
 | 
			
		||||
#include "qapi/qmp/qjson.h"
 | 
			
		||||
#include "block/coroutine.h"
 | 
			
		||||
#include "qmp-commands.h"
 | 
			
		||||
@@ -43,7 +42,7 @@ void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs,
 | 
			
		||||
    BlockJob *job;
 | 
			
		||||
 | 
			
		||||
    if (bs->job) {
 | 
			
		||||
        error_setg(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
 | 
			
		||||
        error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
    bdrv_ref(bs);
 | 
			
		||||
@@ -94,7 +93,7 @@ void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
 | 
			
		||||
    Error *local_err = NULL;
 | 
			
		||||
 | 
			
		||||
    if (!job->driver->set_speed) {
 | 
			
		||||
        error_setg(errp, QERR_UNSUPPORTED);
 | 
			
		||||
        error_set(errp, QERR_UNSUPPORTED);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    job->driver->set_speed(job, speed, &local_err);
 | 
			
		||||
@@ -108,9 +107,9 @@ void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
 | 
			
		||||
 | 
			
		||||
void block_job_complete(BlockJob *job, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    if (job->pause_count || job->cancelled || !job->driver->complete) {
 | 
			
		||||
        error_setg(errp, QERR_BLOCK_JOB_NOT_READY,
 | 
			
		||||
                   bdrv_get_device_name(job->bs));
 | 
			
		||||
    if (job->paused || job->cancelled || !job->driver->complete) {
 | 
			
		||||
        error_set(errp, QERR_BLOCK_JOB_NOT_READY,
 | 
			
		||||
                  bdrv_get_device_name(job->bs));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -119,26 +118,17 @@ void block_job_complete(BlockJob *job, Error **errp)
 | 
			
		||||
 | 
			
		||||
void block_job_pause(BlockJob *job)
 | 
			
		||||
{
 | 
			
		||||
    job->pause_count++;
 | 
			
		||||
    job->paused = true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool block_job_is_paused(BlockJob *job)
 | 
			
		||||
{
 | 
			
		||||
    return job->pause_count > 0;
 | 
			
		||||
    return job->paused;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void block_job_resume(BlockJob *job)
 | 
			
		||||
{
 | 
			
		||||
    assert(job->pause_count > 0);
 | 
			
		||||
    job->pause_count--;
 | 
			
		||||
    if (job->pause_count) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    block_job_enter(job);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void block_job_enter(BlockJob *job)
 | 
			
		||||
{
 | 
			
		||||
    job->paused = false;
 | 
			
		||||
    block_job_iostatus_reset(job);
 | 
			
		||||
    if (job->co && !job->busy) {
 | 
			
		||||
        qemu_coroutine_enter(job->co, NULL);
 | 
			
		||||
@@ -148,7 +138,7 @@ void block_job_enter(BlockJob *job)
 | 
			
		||||
void block_job_cancel(BlockJob *job)
 | 
			
		||||
{
 | 
			
		||||
    job->cancelled = true;
 | 
			
		||||
    block_job_enter(job);
 | 
			
		||||
    block_job_resume(job);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool block_job_is_cancelled(BlockJob *job)
 | 
			
		||||
@@ -268,7 +258,7 @@ BlockJobInfo *block_job_query(BlockJob *job)
 | 
			
		||||
    info->device    = g_strdup(bdrv_get_device_name(job->bs));
 | 
			
		||||
    info->len       = job->len;
 | 
			
		||||
    info->busy      = job->busy;
 | 
			
		||||
    info->paused    = job->pause_count > 0;
 | 
			
		||||
    info->paused    = job->paused;
 | 
			
		||||
    info->offset    = job->offset;
 | 
			
		||||
    info->speed     = job->speed;
 | 
			
		||||
    info->io_status = job->iostatus;
 | 
			
		||||
@@ -345,8 +335,6 @@ BlockErrorAction block_job_error_action(BlockJob *job, BlockDriverState *bs,
 | 
			
		||||
                                    IO_OPERATION_TYPE_WRITE,
 | 
			
		||||
                                    action, &error_abort);
 | 
			
		||||
    if (action == BLOCK_ERROR_ACTION_STOP) {
 | 
			
		||||
        /* make the pause user visible, which will be resumed from QMP. */
 | 
			
		||||
        job->user_paused = true;
 | 
			
		||||
        block_job_pause(job);
 | 
			
		||||
        block_job_iostatus_set_err(job, error);
 | 
			
		||||
        if (bs != job->bs) {
 | 
			
		||||
 
 | 
			
		||||
@@ -108,6 +108,10 @@ void cpu_list_unlock(void)
 | 
			
		||||
/***********************************************************/
 | 
			
		||||
/* CPUX86 core interface */
 | 
			
		||||
 | 
			
		||||
void cpu_smm_update(CPUX86State *env)
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint64_t cpu_get_tsc(CPUX86State *env)
 | 
			
		||||
{
 | 
			
		||||
    return cpu_get_real_ticks();
 | 
			
		||||
@@ -901,6 +905,7 @@ int main(int argc, char **argv)
 | 
			
		||||
#endif
 | 
			
		||||
    }
 | 
			
		||||
    tcg_exec_init(0);
 | 
			
		||||
    cpu_exec_init_all();
 | 
			
		||||
    /* NOTE: we need to init the CPU at this stage to get
 | 
			
		||||
       qemu_host_page_size */
 | 
			
		||||
    cpu = cpu_init(cpu_model);
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										478
									
								
								configure
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										478
									
								
								configure
									
									
									
									
										vendored
									
									
								
							@@ -103,8 +103,7 @@ update_cxxflags() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
compile_object() {
 | 
			
		||||
  local_cflags="$1"
 | 
			
		||||
  do_cc $QEMU_CFLAGS $local_cflags -c -o $TMPO $TMPC
 | 
			
		||||
  do_cc $QEMU_CFLAGS -c -o $TMPO $TMPC
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
compile_prog() {
 | 
			
		||||
@@ -285,6 +284,8 @@ sysconfdir="\${prefix}/etc"
 | 
			
		||||
local_statedir="\${prefix}/var"
 | 
			
		||||
confsuffix="/qemu"
 | 
			
		||||
slirp="yes"
 | 
			
		||||
fmod_lib=""
 | 
			
		||||
fmod_inc=""
 | 
			
		||||
oss_lib=""
 | 
			
		||||
bsd="no"
 | 
			
		||||
linux="no"
 | 
			
		||||
@@ -315,7 +316,6 @@ snappy=""
 | 
			
		||||
bzip2=""
 | 
			
		||||
guest_agent=""
 | 
			
		||||
guest_agent_with_vss="no"
 | 
			
		||||
guest_agent_msi=""
 | 
			
		||||
vss_win32_sdk=""
 | 
			
		||||
win_sdk="no"
 | 
			
		||||
want_tools="yes"
 | 
			
		||||
@@ -336,7 +336,6 @@ libssh2=""
 | 
			
		||||
vhdx=""
 | 
			
		||||
quorum=""
 | 
			
		||||
numa=""
 | 
			
		||||
tcmalloc="no"
 | 
			
		||||
 | 
			
		||||
# parse CC options first
 | 
			
		||||
for opt do
 | 
			
		||||
@@ -352,10 +351,10 @@ for opt do
 | 
			
		||||
  ;;
 | 
			
		||||
  --cpu=*) cpu="$optarg"
 | 
			
		||||
  ;;
 | 
			
		||||
  --extra-cflags=*) QEMU_CFLAGS="$QEMU_CFLAGS $optarg"
 | 
			
		||||
  --extra-cflags=*) QEMU_CFLAGS="$optarg $QEMU_CFLAGS"
 | 
			
		||||
                    EXTRA_CFLAGS="$optarg"
 | 
			
		||||
  ;;
 | 
			
		||||
  --extra-ldflags=*) LDFLAGS="$LDFLAGS $optarg"
 | 
			
		||||
  --extra-ldflags=*) LDFLAGS="$optarg $LDFLAGS"
 | 
			
		||||
                     EXTRA_LDFLAGS="$optarg"
 | 
			
		||||
  ;;
 | 
			
		||||
  --enable-debug-info) debug_info="yes"
 | 
			
		||||
@@ -436,20 +435,6 @@ EOF
 | 
			
		||||
  compile_object
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
check_include() {
 | 
			
		||||
cat > $TMPC <<EOF
 | 
			
		||||
#include <$1>
 | 
			
		||||
int main(void) { return 0; }
 | 
			
		||||
EOF
 | 
			
		||||
  compile_object
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
write_c_skeleton() {
 | 
			
		||||
    cat > $TMPC <<EOF
 | 
			
		||||
int main(void) { return 0; }
 | 
			
		||||
EOF
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
if check_define __linux__ ; then
 | 
			
		||||
  targetos="Linux"
 | 
			
		||||
elif check_define _WIN32 ; then
 | 
			
		||||
@@ -571,28 +556,24 @@ case $targetos in
 | 
			
		||||
CYGWIN*)
 | 
			
		||||
  mingw32="yes"
 | 
			
		||||
  QEMU_CFLAGS="-mno-cygwin $QEMU_CFLAGS"
 | 
			
		||||
  audio_possible_drivers="sdl"
 | 
			
		||||
  audio_drv_list="sdl"
 | 
			
		||||
  audio_possible_drivers="winwave sdl"
 | 
			
		||||
  audio_drv_list="winwave"
 | 
			
		||||
;;
 | 
			
		||||
MINGW32*)
 | 
			
		||||
  mingw32="yes"
 | 
			
		||||
  audio_possible_drivers="dsound sdl"
 | 
			
		||||
  if check_include dsound.h; then
 | 
			
		||||
    audio_drv_list="dsound"
 | 
			
		||||
  else
 | 
			
		||||
    audio_drv_list=""
 | 
			
		||||
  fi
 | 
			
		||||
  audio_possible_drivers="winwave dsound sdl fmod"
 | 
			
		||||
  audio_drv_list="winwave"
 | 
			
		||||
;;
 | 
			
		||||
GNU/kFreeBSD)
 | 
			
		||||
  bsd="yes"
 | 
			
		||||
  audio_drv_list="oss"
 | 
			
		||||
  audio_possible_drivers="oss sdl pa"
 | 
			
		||||
  audio_possible_drivers="oss sdl esd pa"
 | 
			
		||||
;;
 | 
			
		||||
FreeBSD)
 | 
			
		||||
  bsd="yes"
 | 
			
		||||
  make="${MAKE-gmake}"
 | 
			
		||||
  audio_drv_list="oss"
 | 
			
		||||
  audio_possible_drivers="oss sdl pa"
 | 
			
		||||
  audio_possible_drivers="oss sdl esd pa"
 | 
			
		||||
  # needed for kinfo_getvmmap(3) in libutil.h
 | 
			
		||||
  LIBS="-lutil $LIBS"
 | 
			
		||||
  netmap=""  # enable netmap autodetect
 | 
			
		||||
@@ -602,14 +583,14 @@ DragonFly)
 | 
			
		||||
  bsd="yes"
 | 
			
		||||
  make="${MAKE-gmake}"
 | 
			
		||||
  audio_drv_list="oss"
 | 
			
		||||
  audio_possible_drivers="oss sdl pa"
 | 
			
		||||
  audio_possible_drivers="oss sdl esd pa"
 | 
			
		||||
  HOST_VARIANT_DIR="dragonfly"
 | 
			
		||||
;;
 | 
			
		||||
NetBSD)
 | 
			
		||||
  bsd="yes"
 | 
			
		||||
  make="${MAKE-gmake}"
 | 
			
		||||
  audio_drv_list="oss"
 | 
			
		||||
  audio_possible_drivers="oss sdl"
 | 
			
		||||
  audio_possible_drivers="oss sdl esd"
 | 
			
		||||
  oss_lib="-lossaudio"
 | 
			
		||||
  HOST_VARIANT_DIR="netbsd"
 | 
			
		||||
;;
 | 
			
		||||
@@ -617,7 +598,7 @@ OpenBSD)
 | 
			
		||||
  bsd="yes"
 | 
			
		||||
  make="${MAKE-gmake}"
 | 
			
		||||
  audio_drv_list="sdl"
 | 
			
		||||
  audio_possible_drivers="sdl"
 | 
			
		||||
  audio_possible_drivers="sdl esd"
 | 
			
		||||
  HOST_VARIANT_DIR="openbsd"
 | 
			
		||||
;;
 | 
			
		||||
Darwin)
 | 
			
		||||
@@ -630,7 +611,7 @@ Darwin)
 | 
			
		||||
  fi
 | 
			
		||||
  cocoa="yes"
 | 
			
		||||
  audio_drv_list="coreaudio"
 | 
			
		||||
  audio_possible_drivers="coreaudio sdl"
 | 
			
		||||
  audio_possible_drivers="coreaudio sdl fmod"
 | 
			
		||||
  LDFLAGS="-framework CoreFoundation -framework IOKit $LDFLAGS"
 | 
			
		||||
  libs_softmmu="-F/System/Library/Frameworks -framework Cocoa -framework IOKit $libs_softmmu"
 | 
			
		||||
  # Disable attempts to use ObjectiveC features in os/object.h since they
 | 
			
		||||
@@ -685,12 +666,15 @@ Haiku)
 | 
			
		||||
;;
 | 
			
		||||
*)
 | 
			
		||||
  audio_drv_list="oss"
 | 
			
		||||
  audio_possible_drivers="oss alsa sdl pa"
 | 
			
		||||
  audio_possible_drivers="oss alsa sdl esd pa"
 | 
			
		||||
  linux="yes"
 | 
			
		||||
  linux_user="yes"
 | 
			
		||||
  kvm="yes"
 | 
			
		||||
  vhost_net="yes"
 | 
			
		||||
  vhost_scsi="yes"
 | 
			
		||||
  if [ "$cpu" = "i386" -o "$cpu" = "x86_64" -o "$cpu" = "x32" ] ; then
 | 
			
		||||
    audio_possible_drivers="$audio_possible_drivers fmod"
 | 
			
		||||
  fi
 | 
			
		||||
  QEMU_INCLUDES="-I\$(SRC_PATH)/linux-headers -I$(pwd)/linux-headers $QEMU_INCLUDES"
 | 
			
		||||
;;
 | 
			
		||||
esac
 | 
			
		||||
@@ -720,7 +704,9 @@ if test "$mingw32" = "yes" ; then
 | 
			
		||||
  # enable C99/POSIX format strings (needs mingw32-runtime 3.15 or later)
 | 
			
		||||
  QEMU_CFLAGS="-D__USE_MINGW_ANSI_STDIO=1 $QEMU_CFLAGS"
 | 
			
		||||
  LIBS="-lwinmm -lws2_32 -liphlpapi $LIBS"
 | 
			
		||||
  write_c_skeleton;
 | 
			
		||||
cat > $TMPC << EOF
 | 
			
		||||
int main(void) { return 0; }
 | 
			
		||||
EOF
 | 
			
		||||
  if compile_prog "" "-liberty" ; then
 | 
			
		||||
    LIBS="-liberty $LIBS"
 | 
			
		||||
  fi
 | 
			
		||||
@@ -855,6 +841,10 @@ for opt do
 | 
			
		||||
  ;;
 | 
			
		||||
  --enable-vnc) vnc="yes"
 | 
			
		||||
  ;;
 | 
			
		||||
  --fmod-lib=*) fmod_lib="$optarg"
 | 
			
		||||
  ;;
 | 
			
		||||
  --fmod-inc=*) fmod_inc="$optarg"
 | 
			
		||||
  ;;
 | 
			
		||||
  --oss-lib=*) oss_lib="$optarg"
 | 
			
		||||
  ;;
 | 
			
		||||
  --audio-drv-list=*) audio_drv_list="$optarg"
 | 
			
		||||
@@ -1079,10 +1069,6 @@ for opt do
 | 
			
		||||
  ;;
 | 
			
		||||
  --disable-guest-agent) guest_agent="no"
 | 
			
		||||
  ;;
 | 
			
		||||
  --enable-guest-agent-msi) guest_agent_msi="yes"
 | 
			
		||||
  ;;
 | 
			
		||||
  --disable-guest-agent-msi) guest_agent_msi="no"
 | 
			
		||||
  ;;
 | 
			
		||||
  --with-vss-sdk) vss_win32_sdk=""
 | 
			
		||||
  ;;
 | 
			
		||||
  --with-vss-sdk=*) vss_win32_sdk="$optarg"
 | 
			
		||||
@@ -1148,10 +1134,6 @@ for opt do
 | 
			
		||||
  ;;
 | 
			
		||||
  --enable-numa) numa="yes"
 | 
			
		||||
  ;;
 | 
			
		||||
  --disable-tcmalloc) tcmalloc="no"
 | 
			
		||||
  ;;
 | 
			
		||||
  --enable-tcmalloc) tcmalloc="yes"
 | 
			
		||||
  ;;
 | 
			
		||||
  *)
 | 
			
		||||
      echo "ERROR: unknown option $opt"
 | 
			
		||||
      echo "Try '$0 --help' for more information"
 | 
			
		||||
@@ -1282,10 +1264,29 @@ Advanced options (experts only):
 | 
			
		||||
  --sysconfdir=PATH        install config in PATH$confsuffix
 | 
			
		||||
  --localstatedir=PATH     install local state in PATH (set at runtime on win32)
 | 
			
		||||
  --with-confsuffix=SUFFIX suffix for QEMU data inside datadir/libdir/sysconfdir [$confsuffix]
 | 
			
		||||
  --enable-modules         enable modules support
 | 
			
		||||
  --enable-debug-tcg       enable TCG debugging
 | 
			
		||||
  --disable-debug-tcg      disable TCG debugging (default)
 | 
			
		||||
  --enable-debug-info      enable debugging information (default)
 | 
			
		||||
  --disable-debug-info     disable debugging information
 | 
			
		||||
  --enable-debug           enable common debug build options
 | 
			
		||||
  --enable-sparse          enable sparse checker
 | 
			
		||||
  --disable-sparse         disable sparse checker (default)
 | 
			
		||||
  --disable-strip          disable stripping binaries
 | 
			
		||||
  --disable-werror         disable compilation abort on warning
 | 
			
		||||
  --disable-stack-protector disable compiler-provided stack protection
 | 
			
		||||
  --disable-sdl            disable SDL
 | 
			
		||||
  --enable-sdl             enable SDL
 | 
			
		||||
  --with-sdlabi            select preferred SDL ABI 1.2 or 2.0
 | 
			
		||||
  --disable-gtk            disable gtk UI
 | 
			
		||||
  --enable-gtk             enable gtk UI
 | 
			
		||||
  --with-gtkabi            select preferred GTK ABI 2.0 or 3.0
 | 
			
		||||
  --disable-virtfs         disable VirtFS
 | 
			
		||||
  --enable-virtfs          enable VirtFS
 | 
			
		||||
  --disable-vnc            disable VNC
 | 
			
		||||
  --enable-vnc             enable VNC
 | 
			
		||||
  --disable-cocoa          disable Cocoa (Mac OS X only)
 | 
			
		||||
  --enable-cocoa           enable Cocoa (default on Mac OS X)
 | 
			
		||||
  --audio-drv-list=LIST    set audio drivers list:
 | 
			
		||||
                           Available drivers: $audio_possible_drivers
 | 
			
		||||
  --block-drv-whitelist=L  Same as --block-drv-rw-whitelist=L
 | 
			
		||||
@@ -1295,89 +1296,117 @@ Advanced options (experts only):
 | 
			
		||||
  --block-drv-ro-whitelist=L
 | 
			
		||||
                           set block driver read-only whitelist
 | 
			
		||||
                           (affects only QEMU, not qemu-img)
 | 
			
		||||
  --disable-xen            disable xen backend driver support
 | 
			
		||||
  --enable-xen             enable xen backend driver support
 | 
			
		||||
  --disable-xen-pci-passthrough
 | 
			
		||||
  --enable-xen-pci-passthrough
 | 
			
		||||
  --disable-brlapi         disable BrlAPI
 | 
			
		||||
  --enable-brlapi          enable BrlAPI
 | 
			
		||||
  --disable-vnc-tls        disable TLS encryption for VNC server
 | 
			
		||||
  --enable-vnc-tls         enable TLS encryption for VNC server
 | 
			
		||||
  --disable-vnc-sasl       disable SASL encryption for VNC server
 | 
			
		||||
  --enable-vnc-sasl        enable SASL encryption for VNC server
 | 
			
		||||
  --disable-vnc-jpeg       disable JPEG lossy compression for VNC server
 | 
			
		||||
  --enable-vnc-jpeg        enable JPEG lossy compression for VNC server
 | 
			
		||||
  --disable-vnc-png        disable PNG compression for VNC server (default)
 | 
			
		||||
  --enable-vnc-png         enable PNG compression for VNC server
 | 
			
		||||
  --disable-vnc-ws         disable Websockets support for VNC server
 | 
			
		||||
  --enable-vnc-ws          enable Websockets support for VNC server
 | 
			
		||||
  --disable-curses         disable curses output
 | 
			
		||||
  --enable-curses          enable curses output
 | 
			
		||||
  --disable-curl           disable curl connectivity
 | 
			
		||||
  --enable-curl            enable curl connectivity
 | 
			
		||||
  --disable-fdt            disable fdt device tree
 | 
			
		||||
  --enable-fdt             enable fdt device tree
 | 
			
		||||
  --disable-bluez          disable bluez stack connectivity
 | 
			
		||||
  --enable-bluez           enable bluez stack connectivity
 | 
			
		||||
  --disable-slirp          disable SLIRP userspace network connectivity
 | 
			
		||||
  --disable-kvm            disable KVM acceleration support
 | 
			
		||||
  --enable-kvm             enable KVM acceleration support
 | 
			
		||||
  --disable-rdma           disable RDMA-based migration support
 | 
			
		||||
  --enable-rdma            enable RDMA-based migration support
 | 
			
		||||
  --enable-tcg-interpreter enable TCG with bytecode interpreter (TCI)
 | 
			
		||||
  --enable-system          enable all system emulation targets
 | 
			
		||||
  --disable-system         disable all system emulation targets
 | 
			
		||||
  --enable-user            enable supported user emulation targets
 | 
			
		||||
  --disable-user           disable all user emulation targets
 | 
			
		||||
  --enable-linux-user      enable all linux usermode emulation targets
 | 
			
		||||
  --disable-linux-user     disable all linux usermode emulation targets
 | 
			
		||||
  --enable-bsd-user        enable all BSD usermode emulation targets
 | 
			
		||||
  --disable-bsd-user       disable all BSD usermode emulation targets
 | 
			
		||||
  --enable-guest-base      enable GUEST_BASE support for usermode
 | 
			
		||||
                           emulation targets
 | 
			
		||||
  --disable-guest-base     disable GUEST_BASE support
 | 
			
		||||
  --enable-pie             build Position Independent Executables
 | 
			
		||||
  --disable-pie            do not build Position Independent Executables
 | 
			
		||||
  --fmod-lib               path to FMOD library
 | 
			
		||||
  --fmod-inc               path to FMOD includes
 | 
			
		||||
  --oss-lib                path to OSS library
 | 
			
		||||
  --cpu=CPU                Build for host CPU [$cpu]
 | 
			
		||||
  --disable-uuid           disable uuid support
 | 
			
		||||
  --enable-uuid            enable uuid support
 | 
			
		||||
  --disable-vde            disable support for vde network
 | 
			
		||||
  --enable-vde             enable support for vde network
 | 
			
		||||
  --disable-netmap         disable support for netmap network
 | 
			
		||||
  --enable-netmap          enable support for netmap network
 | 
			
		||||
  --disable-linux-aio      disable Linux AIO support
 | 
			
		||||
  --enable-linux-aio       enable Linux AIO support
 | 
			
		||||
  --disable-cap-ng         disable libcap-ng support
 | 
			
		||||
  --enable-cap-ng          enable libcap-ng support
 | 
			
		||||
  --disable-attr           disable attr and xattr support
 | 
			
		||||
  --enable-attr            enable attr and xattr support
 | 
			
		||||
  --disable-blobs          disable installing provided firmware blobs
 | 
			
		||||
  --enable-docs            enable documentation build
 | 
			
		||||
  --disable-docs           disable documentation build
 | 
			
		||||
  --disable-vhost-net      disable vhost-net acceleration support
 | 
			
		||||
  --enable-vhost-net       enable vhost-net acceleration support
 | 
			
		||||
  --enable-trace-backends=B Set trace backend
 | 
			
		||||
                           Available backends: $($python $source_path/scripts/tracetool.py --list-backends)
 | 
			
		||||
  --with-trace-file=NAME   Full PATH,NAME of file to store traces
 | 
			
		||||
                           Default:trace-<pid>
 | 
			
		||||
  --disable-slirp          disable SLIRP userspace network connectivity
 | 
			
		||||
  --enable-tcg-interpreter enable TCG with bytecode interpreter (TCI)
 | 
			
		||||
  --oss-lib                path to OSS library
 | 
			
		||||
  --cpu=CPU                Build for host CPU [$cpu]
 | 
			
		||||
  --with-coroutine=BACKEND coroutine backend. Supported options:
 | 
			
		||||
                           gthread, ucontext, sigaltstack, windows
 | 
			
		||||
  --enable-gcov            enable test coverage analysis with gcov
 | 
			
		||||
  --gcov=GCOV              use specified gcov [$gcov_tool]
 | 
			
		||||
  --disable-blobs          disable installing provided firmware blobs
 | 
			
		||||
  --disable-spice          disable spice
 | 
			
		||||
  --enable-spice           enable spice
 | 
			
		||||
  --enable-rbd             enable building the rados block device (rbd)
 | 
			
		||||
  --disable-libiscsi       disable iscsi support
 | 
			
		||||
  --enable-libiscsi        enable iscsi support
 | 
			
		||||
  --disable-libnfs         disable nfs support
 | 
			
		||||
  --enable-libnfs          enable nfs support
 | 
			
		||||
  --disable-smartcard-nss  disable smartcard nss support
 | 
			
		||||
  --enable-smartcard-nss   enable smartcard nss support
 | 
			
		||||
  --disable-libusb         disable libusb (for usb passthrough)
 | 
			
		||||
  --enable-libusb          enable libusb (for usb passthrough)
 | 
			
		||||
  --disable-usb-redir      disable usb network redirection support
 | 
			
		||||
  --enable-usb-redir       enable usb network redirection support
 | 
			
		||||
  --enable-lzo             enable the support of lzo compression library
 | 
			
		||||
  --enable-snappy          enable the support of snappy compression library
 | 
			
		||||
  --enable-bzip2           enable the support of bzip2 compression library (for
 | 
			
		||||
                           reading bzip2-compressed dmg images)
 | 
			
		||||
  --disable-guest-agent    disable building of the QEMU Guest Agent
 | 
			
		||||
  --enable-guest-agent     enable building of the QEMU Guest Agent
 | 
			
		||||
  --with-vss-sdk=SDK-path  enable Windows VSS support in QEMU Guest Agent
 | 
			
		||||
  --with-win-sdk=SDK-path  path to Windows Platform SDK (to build VSS .tlb)
 | 
			
		||||
 | 
			
		||||
Optional features, enabled with --enable-FEATURE and
 | 
			
		||||
disabled with --disable-FEATURE, default is enabled if available:
 | 
			
		||||
 | 
			
		||||
  system          all system emulation targets
 | 
			
		||||
  user            supported user emulation targets
 | 
			
		||||
  linux-user      all linux usermode emulation targets
 | 
			
		||||
  bsd-user        all BSD usermode emulation targets
 | 
			
		||||
  guest-base      GUEST_BASE support for usermode emulation targets
 | 
			
		||||
  docs            build documentation
 | 
			
		||||
  guest-agent     build the QEMU Guest Agent
 | 
			
		||||
  guest-agent-msi build guest agent Windows MSI installation package
 | 
			
		||||
  pie             Position Independent Executables
 | 
			
		||||
  modules         modules support
 | 
			
		||||
  debug-tcg       TCG debugging (default is disabled)
 | 
			
		||||
  debug-info      debugging information
 | 
			
		||||
  sparse          sparse checker
 | 
			
		||||
 | 
			
		||||
  sdl             SDL UI
 | 
			
		||||
  --with-sdlabi     select preferred SDL ABI 1.2 or 2.0
 | 
			
		||||
  gtk             gtk UI
 | 
			
		||||
  --with-gtkabi     select preferred GTK ABI 2.0 or 3.0
 | 
			
		||||
  vte             vte support for the gtk UI
 | 
			
		||||
  curses          curses UI
 | 
			
		||||
  vnc             VNC UI support
 | 
			
		||||
  vnc-tls         TLS encryption for VNC server
 | 
			
		||||
  vnc-sasl        SASL encryption for VNC server
 | 
			
		||||
  vnc-jpeg        JPEG lossy compression for VNC server
 | 
			
		||||
  vnc-png         PNG compression for VNC server
 | 
			
		||||
  vnc-ws          Websockets support for VNC server
 | 
			
		||||
  cocoa           Cocoa UI (Mac OS X only)
 | 
			
		||||
  virtfs          VirtFS
 | 
			
		||||
  xen             xen backend driver support
 | 
			
		||||
  xen-pci-passthrough
 | 
			
		||||
  brlapi          BrlAPI (Braile)
 | 
			
		||||
  curl            curl connectivity
 | 
			
		||||
  fdt             fdt device tree
 | 
			
		||||
  bluez           bluez stack connectivity
 | 
			
		||||
  kvm             KVM acceleration support
 | 
			
		||||
  rdma            RDMA-based migration support
 | 
			
		||||
  uuid            uuid support
 | 
			
		||||
  vde             support for vde network
 | 
			
		||||
  netmap          support for netmap network
 | 
			
		||||
  linux-aio       Linux AIO support
 | 
			
		||||
  cap-ng          libcap-ng support
 | 
			
		||||
  attr            attr and xattr support
 | 
			
		||||
  vhost-net       vhost-net acceleration support
 | 
			
		||||
  spice           spice
 | 
			
		||||
  rbd             rados block device (rbd)
 | 
			
		||||
  libiscsi        iscsi support
 | 
			
		||||
  libnfs          nfs support
 | 
			
		||||
  smartcard-nss   smartcard nss support
 | 
			
		||||
  libusb          libusb (for usb passthrough)
 | 
			
		||||
  usb-redir       usb network redirection support
 | 
			
		||||
  lzo             support of lzo compression library
 | 
			
		||||
  snappy          support of snappy compression library
 | 
			
		||||
  bzip2           support of bzip2 compression library
 | 
			
		||||
                  (for reading bzip2-compressed dmg images)
 | 
			
		||||
  seccomp         seccomp support
 | 
			
		||||
  coroutine-pool  coroutine freelist (better performance)
 | 
			
		||||
  glusterfs       GlusterFS backend
 | 
			
		||||
  archipelago     Archipelago backend
 | 
			
		||||
  tpm             TPM support
 | 
			
		||||
  libssh2         ssh block device support
 | 
			
		||||
  vhdx            support for the Microsoft VHDX image format
 | 
			
		||||
  quorum          quorum block filter support
 | 
			
		||||
  numa            libnuma support
 | 
			
		||||
  tcmalloc        tcmalloc support
 | 
			
		||||
  --disable-seccomp        disable seccomp support
 | 
			
		||||
  --enable-seccomp         enable seccomp support
 | 
			
		||||
  --with-coroutine=BACKEND coroutine backend. Supported options:
 | 
			
		||||
                           gthread, ucontext, sigaltstack, windows
 | 
			
		||||
  --disable-coroutine-pool disable coroutine freelist (worse performance)
 | 
			
		||||
  --enable-coroutine-pool  enable coroutine freelist (better performance)
 | 
			
		||||
  --enable-glusterfs       enable GlusterFS backend
 | 
			
		||||
  --disable-glusterfs      disable GlusterFS backend
 | 
			
		||||
  --enable-archipelago     enable Archipelago backend
 | 
			
		||||
  --disable-archipelago    disable Archipelago backend
 | 
			
		||||
  --enable-gcov            enable test coverage analysis with gcov
 | 
			
		||||
  --gcov=GCOV              use specified gcov [$gcov_tool]
 | 
			
		||||
  --disable-tpm            disable TPM support
 | 
			
		||||
  --enable-tpm             enable TPM support
 | 
			
		||||
  --disable-libssh2        disable ssh block device support
 | 
			
		||||
  --enable-libssh2         enable ssh block device support
 | 
			
		||||
  --disable-vhdx           disable support for the Microsoft VHDX image format
 | 
			
		||||
  --enable-vhdx            enable support for the Microsoft VHDX image format
 | 
			
		||||
  --disable-quorum         disable quorum block filter support
 | 
			
		||||
  --enable-quorum          enable quorum block filter support
 | 
			
		||||
  --disable-numa           disable libnuma support
 | 
			
		||||
  --enable-numa            enable libnuma support
 | 
			
		||||
 | 
			
		||||
NOTE: The object files are built at the place where configure is launched
 | 
			
		||||
EOF
 | 
			
		||||
@@ -1409,7 +1438,10 @@ if test -z "$werror" ; then
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# check that the C compiler works.
 | 
			
		||||
write_c_skeleton;
 | 
			
		||||
cat > $TMPC <<EOF
 | 
			
		||||
int main(void) { return 0; }
 | 
			
		||||
EOF
 | 
			
		||||
 | 
			
		||||
if compile_object ; then
 | 
			
		||||
  : C compiler works ok
 | 
			
		||||
else
 | 
			
		||||
@@ -1457,20 +1489,16 @@ gcc_flags="-Wno-string-plus-int $gcc_flags"
 | 
			
		||||
# enable it for all configure tests. If a configure test failed due
 | 
			
		||||
# to -Werror this would just silently disable some features,
 | 
			
		||||
# so it's too error prone.
 | 
			
		||||
 | 
			
		||||
cc_has_warning_flag() {
 | 
			
		||||
    write_c_skeleton;
 | 
			
		||||
 | 
			
		||||
cat > $TMPC << EOF
 | 
			
		||||
int main(void) { return 0; }
 | 
			
		||||
EOF
 | 
			
		||||
for flag in $gcc_flags; do
 | 
			
		||||
    # Use the positive sense of the flag when testing for -Wno-wombat
 | 
			
		||||
    # support (gcc will happily accept the -Wno- form of unknown
 | 
			
		||||
    # warning options).
 | 
			
		||||
    optflag="$(echo $1 | sed -e 's/^-Wno-/-W/')"
 | 
			
		||||
    compile_prog "-Werror $optflag" ""
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
for flag in $gcc_flags; do
 | 
			
		||||
    if cc_has_warning_flag $flag ; then
 | 
			
		||||
        QEMU_CFLAGS="$QEMU_CFLAGS $flag"
 | 
			
		||||
    optflag="$(echo $flag | sed -e 's/^-Wno-/-W/')"
 | 
			
		||||
    if compile_prog "-Werror $optflag" "" ; then
 | 
			
		||||
	QEMU_CFLAGS="$QEMU_CFLAGS $flag"
 | 
			
		||||
    fi
 | 
			
		||||
done
 | 
			
		||||
 | 
			
		||||
@@ -1521,20 +1549,9 @@ if test "$static" = "yes" ; then
 | 
			
		||||
  fi
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# Unconditional check for compiler __thread support
 | 
			
		||||
  cat > $TMPC << EOF
 | 
			
		||||
static __thread int tls_var;
 | 
			
		||||
int main(void) { return tls_var; }
 | 
			
		||||
EOF
 | 
			
		||||
 | 
			
		||||
if ! compile_prog "-Werror" "" ; then
 | 
			
		||||
    error_exit "Your compiler does not support the __thread specifier for " \
 | 
			
		||||
	"Thread-Local Storage (TLS). Please upgrade to a version that does."
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
if test "$pie" = ""; then
 | 
			
		||||
  case "$cpu-$targetos" in
 | 
			
		||||
    i386-Linux|x86_64-Linux|x32-Linux|i386-OpenBSD|x86_64-OpenBSD)
 | 
			
		||||
    i386-Linux|x86_64-Linux|x32-Linux|ppc*-Linux|i386-OpenBSD|x86_64-OpenBSD)
 | 
			
		||||
      ;;
 | 
			
		||||
    *)
 | 
			
		||||
      pie="no"
 | 
			
		||||
@@ -1572,7 +1589,7 @@ EOF
 | 
			
		||||
    fi
 | 
			
		||||
  fi
 | 
			
		||||
 | 
			
		||||
  if compile_prog "-Werror -fno-pie" "-nopie"; then
 | 
			
		||||
  if compile_prog "-fno-pie" "-nopie"; then
 | 
			
		||||
    CFLAGS_NOPIE="-fno-pie"
 | 
			
		||||
    LDFLAGS_NOPIE="-nopie"
 | 
			
		||||
  fi
 | 
			
		||||
@@ -1832,7 +1849,7 @@ fi
 | 
			
		||||
 | 
			
		||||
if test "$seccomp" != "no" ; then
 | 
			
		||||
    if test "$cpu" = "i386" || test "$cpu" = "x86_64" &&
 | 
			
		||||
        $pkg_config --atleast-version=2.1.1 libseccomp; then
 | 
			
		||||
        $pkg_config --atleast-version=2.1.0 libseccomp; then
 | 
			
		||||
        libs_softmmu="$libs_softmmu `$pkg_config --libs libseccomp`"
 | 
			
		||||
        QEMU_CFLAGS="$QEMU_CFLAGS `$pkg_config --cflags libseccomp`"
 | 
			
		||||
	seccomp="yes"
 | 
			
		||||
@@ -2580,6 +2597,21 @@ for drv in $audio_drv_list; do
 | 
			
		||||
    libs_softmmu="-lasound $libs_softmmu"
 | 
			
		||||
    ;;
 | 
			
		||||
 | 
			
		||||
    fmod)
 | 
			
		||||
    if test -z $fmod_lib || test -z $fmod_inc; then
 | 
			
		||||
        error_exit "You must specify path to FMOD library and headers" \
 | 
			
		||||
            "Example: --fmod-inc=/path/include/fmod --fmod-lib=/path/lib/libfmod-3.74.so"
 | 
			
		||||
    fi
 | 
			
		||||
    audio_drv_probe $drv fmod.h $fmod_lib "return FSOUND_GetVersion();" "-I $fmod_inc"
 | 
			
		||||
    libs_softmmu="$fmod_lib $libs_softmmu"
 | 
			
		||||
    ;;
 | 
			
		||||
 | 
			
		||||
    esd)
 | 
			
		||||
    audio_drv_probe $drv esd.h -lesd 'return esd_play_stream(0, 0, "", 0);'
 | 
			
		||||
    libs_softmmu="-lesd $libs_softmmu"
 | 
			
		||||
    audio_pt_int="yes"
 | 
			
		||||
    ;;
 | 
			
		||||
 | 
			
		||||
    pa)
 | 
			
		||||
    audio_drv_probe $drv pulse/mainloop.h "-lpulse" \
 | 
			
		||||
        "pa_mainloop *m = 0; pa_mainloop_free (m); return 0;"
 | 
			
		||||
@@ -2604,6 +2636,11 @@ for drv in $audio_drv_list; do
 | 
			
		||||
    # XXX: Probes for CoreAudio, DirectSound, SDL(?)
 | 
			
		||||
    ;;
 | 
			
		||||
 | 
			
		||||
    winwave)
 | 
			
		||||
      libs_softmmu="-lwinmm $libs_softmmu"
 | 
			
		||||
      audio_win_int="yes"
 | 
			
		||||
    ;;
 | 
			
		||||
 | 
			
		||||
    *)
 | 
			
		||||
    echo "$audio_possible_drivers" | grep -q "\<$drv\>" || {
 | 
			
		||||
        error_exit "Unknown driver '$drv' selected" \
 | 
			
		||||
@@ -2718,7 +2755,12 @@ fi
 | 
			
		||||
##########################################
 | 
			
		||||
# glib support probe
 | 
			
		||||
 | 
			
		||||
glib_req_ver=2.22
 | 
			
		||||
if test "$mingw32" = yes; then
 | 
			
		||||
    # g_poll is required in order to integrate with the glib main loop.
 | 
			
		||||
    glib_req_ver=2.20
 | 
			
		||||
else
 | 
			
		||||
    glib_req_ver=2.12
 | 
			
		||||
fi
 | 
			
		||||
glib_modules=gthread-2.0
 | 
			
		||||
if test "$modules" = yes; then
 | 
			
		||||
    glib_modules="$glib_modules gmodule-2.0"
 | 
			
		||||
@@ -2742,18 +2784,6 @@ if ! $pkg_config --atleast-version=2.38 glib-2.0; then
 | 
			
		||||
    glib_subprocess=no
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# Silence clang 3.5.0 warnings about glib attribute __alloc_size__ usage
 | 
			
		||||
cat > $TMPC << EOF
 | 
			
		||||
#include <glib.h>
 | 
			
		||||
int main(void) { return 0; }
 | 
			
		||||
EOF
 | 
			
		||||
if ! compile_prog "$glib_cflags -Werror" "$glib_libs" ; then
 | 
			
		||||
    if cc_has_warning_flag "-Wno-unknown-attributes"; then
 | 
			
		||||
        glib_cflags="-Wno-unknown-attributes $glib_cflags"
 | 
			
		||||
        CFLAGS="-Wno-unknown-attributes $CFLAGS"
 | 
			
		||||
    fi
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
##########################################
 | 
			
		||||
# SHA command probe for modules
 | 
			
		||||
if test "$modules" = yes; then
 | 
			
		||||
@@ -3054,11 +3084,9 @@ fi
 | 
			
		||||
if test "$fdt" != "no" ; then
 | 
			
		||||
  fdt_libs="-lfdt"
 | 
			
		||||
  # explicitly check for libfdt_env.h as it is missing in some stable installs
 | 
			
		||||
  # and test for required functions to make sure we are on a version >= 1.4.0
 | 
			
		||||
  cat > $TMPC << EOF
 | 
			
		||||
#include <libfdt.h>
 | 
			
		||||
#include <libfdt_env.h>
 | 
			
		||||
int main(void) { fdt_get_property_by_offset(0, 0, 0); return 0; }
 | 
			
		||||
int main(void) { return 0; }
 | 
			
		||||
EOF
 | 
			
		||||
  if compile_prog "" "$fdt_libs" ; then
 | 
			
		||||
    # system DTC is good - use it
 | 
			
		||||
@@ -3076,7 +3104,7 @@ EOF
 | 
			
		||||
    fdt_libs="-L\$(BUILD_DIR)/dtc/libfdt $fdt_libs"
 | 
			
		||||
  elif test "$fdt" = "yes" ; then
 | 
			
		||||
    # have neither and want - prompt for system/submodule install
 | 
			
		||||
    error_exit "DTC (libfdt) version >= 1.4.0 not present. Your options:" \
 | 
			
		||||
    error_exit "DTC (libfdt) not present. Your options:" \
 | 
			
		||||
        "  (1) Preferred: Install the DTC (libfdt) devel package" \
 | 
			
		||||
        "  (2) Fetch the DTC submodule, using:" \
 | 
			
		||||
        "      git submodule update --init dtc"
 | 
			
		||||
@@ -3107,14 +3135,14 @@ else
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
if test "$opengl" != "no" ; then
 | 
			
		||||
  opengl_pkgs="gl glesv2 epoxy egl"
 | 
			
		||||
  opengl_pkgs="gl"
 | 
			
		||||
  if $pkg_config $opengl_pkgs x11 && test "$have_glx" = "yes"; then
 | 
			
		||||
    opengl_cflags="$($pkg_config --cflags $opengl_pkgs) $x11_cflags"
 | 
			
		||||
    opengl_libs="$($pkg_config --libs $opengl_pkgs) $x11_libs"
 | 
			
		||||
    opengl=yes
 | 
			
		||||
  else
 | 
			
		||||
    if test "$opengl" = "yes" ; then
 | 
			
		||||
      feature_not_found "opengl" "Please install opengl (mesa) devel pkgs: $opengl_pkgs"
 | 
			
		||||
      feature_not_found "opengl" "Install GL devel (e.g. MESA)"
 | 
			
		||||
    fi
 | 
			
		||||
    opengl_cflags=""
 | 
			
		||||
    opengl_libs=""
 | 
			
		||||
@@ -3302,22 +3330,6 @@ EOF
 | 
			
		||||
  fi
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
##########################################
 | 
			
		||||
# tcmalloc probe
 | 
			
		||||
 | 
			
		||||
if test "$tcmalloc" = "yes" ; then
 | 
			
		||||
  cat > $TMPC << EOF
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
int main(void) { malloc(1); return 0; }
 | 
			
		||||
EOF
 | 
			
		||||
 | 
			
		||||
  if compile_prog "" "-ltcmalloc" ; then
 | 
			
		||||
    LIBS="-ltcmalloc $LIBS"
 | 
			
		||||
  else
 | 
			
		||||
    feature_not_found "tcmalloc" "install gperftools devel"
 | 
			
		||||
  fi
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
##########################################
 | 
			
		||||
# signalfd probe
 | 
			
		||||
signalfd="no"
 | 
			
		||||
@@ -3820,56 +3832,6 @@ if test "$mingw32" = "yes" -a "$guest_agent" != "no" -a "$guest_agent_with_vss"
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
##########################################
 | 
			
		||||
# Guest agent Window MSI  package
 | 
			
		||||
 | 
			
		||||
if test "$guest_agent" != yes; then
 | 
			
		||||
  if test "$guest_agent_msi" = yes; then
 | 
			
		||||
    error_exit "MSI guest agent package requires guest agent enabled"
 | 
			
		||||
  fi
 | 
			
		||||
  guest_agent_msi=no
 | 
			
		||||
elif test "$mingw32" != "yes"; then
 | 
			
		||||
  if test "$guest_agent_msi" = "yes"; then
 | 
			
		||||
    error_exit "MSI guest agent package is available only for MinGW Windows cross-compilation"
 | 
			
		||||
  fi
 | 
			
		||||
  guest_agent_msi=no
 | 
			
		||||
elif ! has wixl; then
 | 
			
		||||
  if test "$guest_agent_msi" = "yes"; then
 | 
			
		||||
    error_exit "MSI guest agent package requires wixl tool installed ( usually from msitools package )"
 | 
			
		||||
  fi
 | 
			
		||||
  guest_agent_msi=no
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
if test "$guest_agent_msi" != "no"; then
 | 
			
		||||
  if test "$guest_agent_with_vss" = "yes"; then
 | 
			
		||||
    QEMU_GA_MSI_WITH_VSS="-D InstallVss"
 | 
			
		||||
  fi
 | 
			
		||||
 | 
			
		||||
  if test "$QEMU_GA_MANUFACTURER" = ""; then
 | 
			
		||||
    QEMU_GA_MANUFACTURER=QEMU
 | 
			
		||||
  fi
 | 
			
		||||
 | 
			
		||||
  if test "$QEMU_GA_DISTRO" = ""; then
 | 
			
		||||
    QEMU_GA_DISTRO=Linux
 | 
			
		||||
  fi
 | 
			
		||||
 | 
			
		||||
  if test "$QEMU_GA_VERSION" = ""; then
 | 
			
		||||
      QEMU_GA_VERSION=`cat $source_path/VERSION`
 | 
			
		||||
  fi
 | 
			
		||||
 | 
			
		||||
  QEMU_GA_MSI_MINGW_DLL_PATH="-D Mingw_dlls=`$pkg_config --variable=prefix glib-2.0`/bin"
 | 
			
		||||
  
 | 
			
		||||
  case "$cpu" in
 | 
			
		||||
  x86_64)
 | 
			
		||||
    QEMU_GA_MSI_ARCH="-a x64 -D Arch=64"
 | 
			
		||||
    ;;
 | 
			
		||||
  i386)
 | 
			
		||||
    QEMU_GA_MSI_ARCH="-D Arch=32"
 | 
			
		||||
    ;;
 | 
			
		||||
  *)
 | 
			
		||||
    error_exit "CPU $cpu not supported for building installation package"
 | 
			
		||||
    ;;
 | 
			
		||||
  esac
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
##########################################
 | 
			
		||||
# check if we have fdatasync
 | 
			
		||||
@@ -4196,33 +4158,6 @@ if compile_prog "" "" ; then
 | 
			
		||||
    getauxval=yes
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
########################################
 | 
			
		||||
# check if ccache is interfering with
 | 
			
		||||
# semantic analysis of macros
 | 
			
		||||
 | 
			
		||||
ccache_cpp2=no
 | 
			
		||||
cat > $TMPC << EOF
 | 
			
		||||
static const int Z = 1;
 | 
			
		||||
#define fn() ({ Z; })
 | 
			
		||||
#define TAUT(X) ((X) == Z)
 | 
			
		||||
#define PAREN(X, Y) (X == Y)
 | 
			
		||||
#define ID(X) (X)
 | 
			
		||||
int main(int argc, char *argv[])
 | 
			
		||||
{
 | 
			
		||||
    int x = 0, y = 0;
 | 
			
		||||
    x = ID(x);
 | 
			
		||||
    x = fn();
 | 
			
		||||
    fn();
 | 
			
		||||
    if (PAREN(x, y)) return 0;
 | 
			
		||||
    if (TAUT(Z)) return 0;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
EOF
 | 
			
		||||
 | 
			
		||||
if ! compile_object "-Werror"; then
 | 
			
		||||
    ccache_cpp2=yes
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
##########################################
 | 
			
		||||
# End of CC checks
 | 
			
		||||
# After here, no more $cc or $ld runs
 | 
			
		||||
@@ -4506,7 +4441,6 @@ echo "lzo support       $lzo"
 | 
			
		||||
echo "snappy support    $snappy"
 | 
			
		||||
echo "bzip2 support     $bzip2"
 | 
			
		||||
echo "NUMA host support $numa"
 | 
			
		||||
echo "tcmalloc support  $tcmalloc"
 | 
			
		||||
 | 
			
		||||
if test "$sdl_too_old" = "yes"; then
 | 
			
		||||
echo "-> Your SDL version is too old - please upgrade to have SDL support"
 | 
			
		||||
@@ -4566,15 +4500,6 @@ if test "$mingw32" = "yes" ; then
 | 
			
		||||
    echo "CONFIG_QGA_VSS=y" >> $config_host_mak
 | 
			
		||||
    echo "WIN_SDK=\"$win_sdk\"" >> $config_host_mak
 | 
			
		||||
  fi
 | 
			
		||||
  if test "$guest_agent_msi" != "no"; then
 | 
			
		||||
    echo "QEMU_GA_MSI_ENABLED=yes" >> $config_host_mak  
 | 
			
		||||
    echo "QEMU_GA_MSI_MINGW_DLL_PATH=${QEMU_GA_MSI_MINGW_DLL_PATH}" >> $config_host_mak
 | 
			
		||||
    echo "QEMU_GA_MSI_WITH_VSS=${QEMU_GA_MSI_WITH_VSS}" >> $config_host_mak
 | 
			
		||||
    echo "QEMU_GA_MSI_ARCH=${QEMU_GA_MSI_ARCH}" >> $config_host_mak
 | 
			
		||||
    echo "QEMU_GA_MANUFACTURER=${QEMU_GA_MANUFACTURER}" >> $config_host_mak
 | 
			
		||||
    echo "QEMU_GA_DISTRO=${QEMU_GA_DISTRO}" >> $config_host_mak
 | 
			
		||||
    echo "QEMU_GA_VERSION=${QEMU_GA_VERSION}" >> $config_host_mak
 | 
			
		||||
  fi
 | 
			
		||||
else
 | 
			
		||||
  echo "CONFIG_POSIX=y" >> $config_host_mak
 | 
			
		||||
fi
 | 
			
		||||
@@ -4627,6 +4552,9 @@ echo "CONFIG_AUDIO_DRIVERS=$audio_drv_list" >> $config_host_mak
 | 
			
		||||
for drv in $audio_drv_list; do
 | 
			
		||||
    def=CONFIG_`echo $drv | LC_ALL=C tr '[a-z]' '[A-Z]'`
 | 
			
		||||
    echo "$def=y" >> $config_host_mak
 | 
			
		||||
    if test "$drv" = "fmod"; then
 | 
			
		||||
        echo "FMOD_CFLAGS=-I$fmod_inc" >> $config_host_mak
 | 
			
		||||
    fi
 | 
			
		||||
done
 | 
			
		||||
if test "$audio_pt_int" = "yes" ; then
 | 
			
		||||
  echo "CONFIG_AUDIO_PT_INT=y" >> $config_host_mak
 | 
			
		||||
@@ -4773,7 +4701,7 @@ if test "$bluez" = "yes" ; then
 | 
			
		||||
  echo "CONFIG_BLUEZ=y" >> $config_host_mak
 | 
			
		||||
  echo "BLUEZ_CFLAGS=$bluez_cflags" >> $config_host_mak
 | 
			
		||||
fi
 | 
			
		||||
if test "$glib_subprocess" = "yes" ; then
 | 
			
		||||
if test "glib_subprocess" = "yes" ; then
 | 
			
		||||
  echo "CONFIG_HAS_GLIB_SUBPROCESS_TESTS=y" >> $config_host_mak
 | 
			
		||||
fi
 | 
			
		||||
echo "GLIB_CFLAGS=$glib_cflags" >> $config_host_mak
 | 
			
		||||
@@ -5241,6 +5169,8 @@ case "$target_name" in
 | 
			
		||||
    TARGET_BASE_ARCH=mips
 | 
			
		||||
    echo "TARGET_ABI_MIPSN64=y" >> $config_target_mak
 | 
			
		||||
  ;;
 | 
			
		||||
  tricore)
 | 
			
		||||
  ;;
 | 
			
		||||
  moxie)
 | 
			
		||||
  ;;
 | 
			
		||||
  or32)
 | 
			
		||||
@@ -5289,9 +5219,7 @@ case "$target_name" in
 | 
			
		||||
    echo "TARGET_ABI32=y" >> $config_target_mak
 | 
			
		||||
  ;;
 | 
			
		||||
  s390x)
 | 
			
		||||
    gdb_xml_files="s390x-core64.xml s390-acr.xml s390-fpr.xml s390-vx.xml"
 | 
			
		||||
  ;;
 | 
			
		||||
  tricore)
 | 
			
		||||
    gdb_xml_files="s390x-core64.xml s390-acr.xml s390-fpr.xml"
 | 
			
		||||
  ;;
 | 
			
		||||
  unicore32)
 | 
			
		||||
  ;;
 | 
			
		||||
@@ -5522,10 +5450,6 @@ if test "$numa" = "yes"; then
 | 
			
		||||
  echo "CONFIG_NUMA=y" >> $config_host_mak
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
if test "$ccache_cpp2" = "yes"; then
 | 
			
		||||
  echo "export CCACHE_CPP2=y" >> $config_host_mak
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# build tree in object directory in case the source is not in the current directory
 | 
			
		||||
DIRS="tests tests/tcg tests/tcg/cris tests/tcg/lm32 tests/libqos tests/qapi-schema tests/tcg/xtensa tests/qemu-iotests"
 | 
			
		||||
DIRS="$DIRS fsdev"
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										107
									
								
								cpus.c
									
									
									
									
									
								
							
							
						
						
									
										107
									
								
								cpus.c
									
									
									
									
									
								
							@@ -27,7 +27,6 @@
 | 
			
		||||
 | 
			
		||||
#include "monitor/monitor.h"
 | 
			
		||||
#include "qapi/qmp/qerror.h"
 | 
			
		||||
#include "qemu/error-report.h"
 | 
			
		||||
#include "sysemu/sysemu.h"
 | 
			
		||||
#include "exec/gdbstub.h"
 | 
			
		||||
#include "sysemu/dma.h"
 | 
			
		||||
@@ -106,7 +105,6 @@ static bool all_cpu_threads_idle(void)
 | 
			
		||||
 | 
			
		||||
/* Protected by TimersState seqlock */
 | 
			
		||||
 | 
			
		||||
static bool icount_sleep = true;
 | 
			
		||||
static int64_t vm_clock_warp_start = -1;
 | 
			
		||||
/* Conversion factor from emulated instructions to virtual clock ticks.  */
 | 
			
		||||
static int icount_time_shift;
 | 
			
		||||
@@ -395,18 +393,15 @@ void qemu_clock_warp(QEMUClockType type)
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (icount_sleep) {
 | 
			
		||||
        /*
 | 
			
		||||
         * If the CPUs have been sleeping, advance QEMU_CLOCK_VIRTUAL timer now.
 | 
			
		||||
         * This ensures that the deadline for the timer is computed correctly
 | 
			
		||||
         * below.
 | 
			
		||||
         * This also makes sure that the insn counter is synchronized before
 | 
			
		||||
         * the CPU starts running, in case the CPU is woken by an event other
 | 
			
		||||
         * than the earliest QEMU_CLOCK_VIRTUAL timer.
 | 
			
		||||
         */
 | 
			
		||||
        icount_warp_rt(NULL);
 | 
			
		||||
        timer_del(icount_warp_timer);
 | 
			
		||||
    }
 | 
			
		||||
    /*
 | 
			
		||||
     * If the CPUs have been sleeping, advance QEMU_CLOCK_VIRTUAL timer now.
 | 
			
		||||
     * This ensures that the deadline for the timer is computed correctly below.
 | 
			
		||||
     * This also makes sure that the insn counter is synchronized before the
 | 
			
		||||
     * CPU starts running, in case the CPU is woken by an event other than
 | 
			
		||||
     * the earliest QEMU_CLOCK_VIRTUAL timer.
 | 
			
		||||
     */
 | 
			
		||||
    icount_warp_rt(NULL);
 | 
			
		||||
    timer_del(icount_warp_timer);
 | 
			
		||||
    if (!all_cpu_threads_idle()) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
@@ -420,11 +415,6 @@ void qemu_clock_warp(QEMUClockType type)
 | 
			
		||||
    clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
 | 
			
		||||
    deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
 | 
			
		||||
    if (deadline < 0) {
 | 
			
		||||
        static bool notified;
 | 
			
		||||
        if (!icount_sleep && !notified) {
 | 
			
		||||
            error_report("WARNING: icount sleep disabled and no active timers");
 | 
			
		||||
            notified = true;
 | 
			
		||||
        }
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -435,35 +425,23 @@ void qemu_clock_warp(QEMUClockType type)
 | 
			
		||||
         * interrupt to wake it up, but the interrupt never comes because
 | 
			
		||||
         * the vCPU isn't running any insns and thus doesn't advance the
 | 
			
		||||
         * QEMU_CLOCK_VIRTUAL.
 | 
			
		||||
         *
 | 
			
		||||
         * An extreme solution for this problem would be to never let VCPUs
 | 
			
		||||
         * sleep in icount mode if there is a pending QEMU_CLOCK_VIRTUAL
 | 
			
		||||
         * timer; rather time could just advance to the next QEMU_CLOCK_VIRTUAL
 | 
			
		||||
         * event.  Instead, we do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL
 | 
			
		||||
         * after some "real" time, (related to the time left until the next
 | 
			
		||||
         * event) has passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
 | 
			
		||||
         * This avoids that the warps are visible externally; for example,
 | 
			
		||||
         * you will not be sending network packets continuously instead of
 | 
			
		||||
         * every 100ms.
 | 
			
		||||
         */
 | 
			
		||||
        if (!icount_sleep) {
 | 
			
		||||
            /*
 | 
			
		||||
             * We never let VCPUs sleep in no sleep icount mode.
 | 
			
		||||
             * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
 | 
			
		||||
             * to the next QEMU_CLOCK_VIRTUAL event and notify it.
 | 
			
		||||
             * It is useful when we want a deterministic execution time,
 | 
			
		||||
             * isolated from host latencies.
 | 
			
		||||
             */
 | 
			
		||||
            seqlock_write_lock(&timers_state.vm_clock_seqlock);
 | 
			
		||||
            timers_state.qemu_icount_bias += deadline;
 | 
			
		||||
            seqlock_write_unlock(&timers_state.vm_clock_seqlock);
 | 
			
		||||
            qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
 | 
			
		||||
        } else {
 | 
			
		||||
            /*
 | 
			
		||||
             * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
 | 
			
		||||
             * "real" time, (related to the time left until the next event) has
 | 
			
		||||
             * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
 | 
			
		||||
             * This avoids that the warps are visible externally; for example,
 | 
			
		||||
             * you will not be sending network packets continuously instead of
 | 
			
		||||
             * every 100ms.
 | 
			
		||||
             */
 | 
			
		||||
            seqlock_write_lock(&timers_state.vm_clock_seqlock);
 | 
			
		||||
            if (vm_clock_warp_start == -1 || vm_clock_warp_start > clock) {
 | 
			
		||||
                vm_clock_warp_start = clock;
 | 
			
		||||
            }
 | 
			
		||||
            seqlock_write_unlock(&timers_state.vm_clock_seqlock);
 | 
			
		||||
            timer_mod_anticipate(icount_warp_timer, clock + deadline);
 | 
			
		||||
        seqlock_write_lock(&timers_state.vm_clock_seqlock);
 | 
			
		||||
        if (vm_clock_warp_start == -1 || vm_clock_warp_start > clock) {
 | 
			
		||||
            vm_clock_warp_start = clock;
 | 
			
		||||
        }
 | 
			
		||||
        seqlock_write_unlock(&timers_state.vm_clock_seqlock);
 | 
			
		||||
        timer_mod_anticipate(icount_warp_timer, clock + deadline);
 | 
			
		||||
    } else if (deadline == 0) {
 | 
			
		||||
        qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
 | 
			
		||||
    }
 | 
			
		||||
@@ -481,7 +459,6 @@ static const VMStateDescription icount_vmstate_timers = {
 | 
			
		||||
    .name = "timer/icount",
 | 
			
		||||
    .version_id = 1,
 | 
			
		||||
    .minimum_version_id = 1,
 | 
			
		||||
    .needed = icount_state_needed,
 | 
			
		||||
    .fields = (VMStateField[]) {
 | 
			
		||||
        VMSTATE_INT64(qemu_icount_bias, TimersState),
 | 
			
		||||
        VMSTATE_INT64(qemu_icount, TimersState),
 | 
			
		||||
@@ -499,9 +476,13 @@ static const VMStateDescription vmstate_timers = {
 | 
			
		||||
        VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
 | 
			
		||||
        VMSTATE_END_OF_LIST()
 | 
			
		||||
    },
 | 
			
		||||
    .subsections = (const VMStateDescription*[]) {
 | 
			
		||||
        &icount_vmstate_timers,
 | 
			
		||||
        NULL
 | 
			
		||||
    .subsections = (VMStateSubsection[]) {
 | 
			
		||||
        {
 | 
			
		||||
            .vmsd = &icount_vmstate_timers,
 | 
			
		||||
            .needed = icount_state_needed,
 | 
			
		||||
        }, {
 | 
			
		||||
            /* empty */
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
@@ -523,18 +504,9 @@ void configure_icount(QemuOpts *opts, Error **errp)
 | 
			
		||||
        }
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    icount_sleep = qemu_opt_get_bool(opts, "sleep", true);
 | 
			
		||||
    if (icount_sleep) {
 | 
			
		||||
        icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
 | 
			
		||||
                                         icount_warp_rt, NULL);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    icount_align_option = qemu_opt_get_bool(opts, "align", false);
 | 
			
		||||
 | 
			
		||||
    if (icount_align_option && !icount_sleep) {
 | 
			
		||||
        error_setg(errp, "align=on and sleep=no are incompatible");
 | 
			
		||||
    }
 | 
			
		||||
    icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
 | 
			
		||||
                                     icount_warp_rt, NULL);
 | 
			
		||||
    if (strcmp(option, "auto") != 0) {
 | 
			
		||||
        errno = 0;
 | 
			
		||||
        icount_time_shift = strtol(option, &rem_str, 0);
 | 
			
		||||
@@ -545,8 +517,6 @@ void configure_icount(QemuOpts *opts, Error **errp)
 | 
			
		||||
        return;
 | 
			
		||||
    } else if (icount_align_option) {
 | 
			
		||||
        error_setg(errp, "shift=auto and align=on are incompatible");
 | 
			
		||||
    } else if (!icount_sleep) {
 | 
			
		||||
        error_setg(errp, "shift=auto and sleep=no are incompatible");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    use_icount = 2;
 | 
			
		||||
@@ -1046,7 +1016,7 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)
 | 
			
		||||
    qemu_cond_signal(&qemu_cpu_cond);
 | 
			
		||||
 | 
			
		||||
    /* wait for initial kick-off after machine start */
 | 
			
		||||
    while (first_cpu->stopped) {
 | 
			
		||||
    while (QTAILQ_FIRST(&cpus)->stopped) {
 | 
			
		||||
        qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
 | 
			
		||||
 | 
			
		||||
        /* process any pending work */
 | 
			
		||||
@@ -1465,7 +1435,6 @@ CpuInfoList *qmp_query_cpus(Error **errp)
 | 
			
		||||
        info->value->CPU = cpu->cpu_index;
 | 
			
		||||
        info->value->current = (cpu == first_cpu);
 | 
			
		||||
        info->value->halted = cpu->halted;
 | 
			
		||||
        info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
 | 
			
		||||
        info->value->thread_id = cpu->thread_id;
 | 
			
		||||
#if defined(TARGET_I386)
 | 
			
		||||
        info->value->has_pc = true;
 | 
			
		||||
@@ -1513,8 +1482,8 @@ void qmp_memsave(int64_t addr, int64_t size, const char *filename,
 | 
			
		||||
 | 
			
		||||
    cpu = qemu_get_cpu(cpu_index);
 | 
			
		||||
    if (cpu == NULL) {
 | 
			
		||||
        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
 | 
			
		||||
                   "a CPU number");
 | 
			
		||||
        error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
 | 
			
		||||
                  "a CPU number");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -1534,7 +1503,7 @@ void qmp_memsave(int64_t addr, int64_t size, const char *filename,
 | 
			
		||||
            goto exit;
 | 
			
		||||
        }
 | 
			
		||||
        if (fwrite(buf, 1, l, f) != l) {
 | 
			
		||||
            error_setg(errp, QERR_IO_ERROR);
 | 
			
		||||
            error_set(errp, QERR_IO_ERROR);
 | 
			
		||||
            goto exit;
 | 
			
		||||
        }
 | 
			
		||||
        addr += l;
 | 
			
		||||
@@ -1564,7 +1533,7 @@ void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
 | 
			
		||||
            l = size;
 | 
			
		||||
        cpu_physical_memory_read(addr, buf, l);
 | 
			
		||||
        if (fwrite(buf, 1, l, f) != l) {
 | 
			
		||||
            error_setg(errp, QERR_IO_ERROR);
 | 
			
		||||
            error_set(errp, QERR_IO_ERROR);
 | 
			
		||||
            goto exit;
 | 
			
		||||
        }
 | 
			
		||||
        addr += l;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										29
									
								
								cputlb.c
									
									
									
									
									
								
							
							
						
						
									
										29
									
								
								cputlb.c
									
									
									
									
									
								
							@@ -125,13 +125,14 @@ void tlb_flush_page(CPUState *cpu, target_ulong addr)
 | 
			
		||||
   can be detected */
 | 
			
		||||
void tlb_protect_code(ram_addr_t ram_addr)
 | 
			
		||||
{
 | 
			
		||||
    cpu_physical_memory_test_and_clear_dirty(ram_addr, TARGET_PAGE_SIZE,
 | 
			
		||||
                                             DIRTY_MEMORY_CODE);
 | 
			
		||||
    cpu_physical_memory_reset_dirty(ram_addr, TARGET_PAGE_SIZE,
 | 
			
		||||
                                    DIRTY_MEMORY_CODE);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* update the TLB so that writes in physical page 'phys_addr' are no longer
 | 
			
		||||
   tested for self modifying code */
 | 
			
		||||
void tlb_unprotect_code(ram_addr_t ram_addr)
 | 
			
		||||
void tlb_unprotect_code_phys(CPUState *cpu, ram_addr_t ram_addr,
 | 
			
		||||
                             target_ulong vaddr)
 | 
			
		||||
{
 | 
			
		||||
    cpu_physical_memory_set_dirty_flag(ram_addr, DIRTY_MEMORY_CODE);
 | 
			
		||||
}
 | 
			
		||||
@@ -248,9 +249,9 @@ static void tlb_add_large_page(CPUArchState *env, target_ulong vaddr,
 | 
			
		||||
 * Called from TCG-generated code, which is under an RCU read-side
 | 
			
		||||
 * critical section.
 | 
			
		||||
 */
 | 
			
		||||
void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,
 | 
			
		||||
                             hwaddr paddr, MemTxAttrs attrs, int prot,
 | 
			
		||||
                             int mmu_idx, target_ulong size)
 | 
			
		||||
void tlb_set_page(CPUState *cpu, target_ulong vaddr,
 | 
			
		||||
                  hwaddr paddr, int prot,
 | 
			
		||||
                  int mmu_idx, target_ulong size)
 | 
			
		||||
{
 | 
			
		||||
    CPUArchState *env = cpu->env_ptr;
 | 
			
		||||
    MemoryRegionSection *section;
 | 
			
		||||
@@ -300,8 +301,7 @@ void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,
 | 
			
		||||
    env->iotlb_v[mmu_idx][vidx] = env->iotlb[mmu_idx][index];
 | 
			
		||||
 | 
			
		||||
    /* refill the tlb */
 | 
			
		||||
    env->iotlb[mmu_idx][index].addr = iotlb - vaddr;
 | 
			
		||||
    env->iotlb[mmu_idx][index].attrs = attrs;
 | 
			
		||||
    env->iotlb[mmu_idx][index] = iotlb - vaddr;
 | 
			
		||||
    te->addend = addend - vaddr;
 | 
			
		||||
    if (prot & PAGE_READ) {
 | 
			
		||||
        te->addr_read = address;
 | 
			
		||||
@@ -331,17 +331,6 @@ void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Add a new TLB entry, but without specifying the memory
 | 
			
		||||
 * transaction attributes to be used.
 | 
			
		||||
 */
 | 
			
		||||
void tlb_set_page(CPUState *cpu, target_ulong vaddr,
 | 
			
		||||
                  hwaddr paddr, int prot,
 | 
			
		||||
                  int mmu_idx, target_ulong size)
 | 
			
		||||
{
 | 
			
		||||
    tlb_set_page_with_attrs(cpu, vaddr, paddr, MEMTXATTRS_UNSPECIFIED,
 | 
			
		||||
                            prot, mmu_idx, size);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* NOTE: this function can trigger an exception */
 | 
			
		||||
/* NOTE2: the returned address is not exactly the physical address: it
 | 
			
		||||
 * is actually a ram_addr_t (in system mode; the user mode emulation
 | 
			
		||||
@@ -360,7 +349,7 @@ tb_page_addr_t get_page_addr_code(CPUArchState *env1, target_ulong addr)
 | 
			
		||||
                 (addr & TARGET_PAGE_MASK))) {
 | 
			
		||||
        cpu_ldub_code(env1, addr);
 | 
			
		||||
    }
 | 
			
		||||
    pd = env1->iotlb[mmu_idx][page_index].addr & ~TARGET_PAGE_MASK;
 | 
			
		||||
    pd = env1->iotlb[mmu_idx][page_index] & ~TARGET_PAGE_MASK;
 | 
			
		||||
    mr = iotlb_to_region(cpu, pd);
 | 
			
		||||
    if (memory_region_is_unassigned(mr)) {
 | 
			
		||||
        CPUClass *cc = CPU_GET_CLASS(cpu);
 | 
			
		||||
 
 | 
			
		||||
@@ -3,4 +3,4 @@
 | 
			
		||||
# We support all the 32 bit boards so need all their config
 | 
			
		||||
include arm-softmmu.mak
 | 
			
		||||
 | 
			
		||||
CONFIG_XLNX_ZYNQMP=y
 | 
			
		||||
# Currently no 64-bit specific config requirements
 | 
			
		||||
 
 | 
			
		||||
@@ -101,4 +101,3 @@ CONFIG_ALLWINNER_A10=y
 | 
			
		||||
CONFIG_XIO3130=y
 | 
			
		||||
CONFIG_IOH3420=y
 | 
			
		||||
CONFIG_I82801B11=y
 | 
			
		||||
CONFIG_ACPI=y
 | 
			
		||||
 
 | 
			
		||||
@@ -15,9 +15,6 @@ CONFIG_PCSPK=y
 | 
			
		||||
CONFIG_PCKBD=y
 | 
			
		||||
CONFIG_FDC=y
 | 
			
		||||
CONFIG_ACPI=y
 | 
			
		||||
CONFIG_ACPI_X86=y
 | 
			
		||||
CONFIG_ACPI_MEMORY_HOTPLUG=y
 | 
			
		||||
CONFIG_ACPI_CPU_HOTPLUG=y
 | 
			
		||||
CONFIG_APM=y
 | 
			
		||||
CONFIG_I8257=y
 | 
			
		||||
CONFIG_IDE_ISA=y
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,11 @@
 | 
			
		||||
# Default configuration for microblazeel-softmmu
 | 
			
		||||
 | 
			
		||||
include microblaze-softmmu.mak
 | 
			
		||||
CONFIG_PTIMER=y
 | 
			
		||||
CONFIG_PFLASH_CFI01=y
 | 
			
		||||
CONFIG_SERIAL=y
 | 
			
		||||
CONFIG_XILINX=y
 | 
			
		||||
CONFIG_XILINX_AXI=y
 | 
			
		||||
CONFIG_XILINX_SPI=y
 | 
			
		||||
CONFIG_XILINX_ETHLITE=y
 | 
			
		||||
CONFIG_SSI=y
 | 
			
		||||
CONFIG_SSI_M25P80=y
 | 
			
		||||
 
 | 
			
		||||
@@ -15,18 +15,20 @@ CONFIG_PCSPK=y
 | 
			
		||||
CONFIG_PCKBD=y
 | 
			
		||||
CONFIG_FDC=y
 | 
			
		||||
CONFIG_ACPI=y
 | 
			
		||||
CONFIG_ACPI_X86=y
 | 
			
		||||
CONFIG_ACPI_MEMORY_HOTPLUG=y
 | 
			
		||||
CONFIG_ACPI_CPU_HOTPLUG=y
 | 
			
		||||
CONFIG_APM=y
 | 
			
		||||
CONFIG_I8257=y
 | 
			
		||||
CONFIG_PIIX4=y
 | 
			
		||||
CONFIG_IDE_ISA=y
 | 
			
		||||
CONFIG_IDE_PIIX=y
 | 
			
		||||
CONFIG_NE2000_ISA=y
 | 
			
		||||
CONFIG_RC4030=y
 | 
			
		||||
CONFIG_DP8393X=y
 | 
			
		||||
CONFIG_DS1225Y=y
 | 
			
		||||
CONFIG_MIPSNET=y
 | 
			
		||||
CONFIG_PFLASH_CFI01=y
 | 
			
		||||
CONFIG_G364FB=y
 | 
			
		||||
CONFIG_I8259=y
 | 
			
		||||
CONFIG_JAZZ_LED=y
 | 
			
		||||
CONFIG_MC146818RTC=y
 | 
			
		||||
CONFIG_ISA_TESTDEV=y
 | 
			
		||||
CONFIG_EMPTY_SLOT=y
 | 
			
		||||
 
 | 
			
		||||
@@ -15,9 +15,6 @@ CONFIG_PCSPK=y
 | 
			
		||||
CONFIG_PCKBD=y
 | 
			
		||||
CONFIG_FDC=y
 | 
			
		||||
CONFIG_ACPI=y
 | 
			
		||||
CONFIG_ACPI_X86=y
 | 
			
		||||
CONFIG_ACPI_MEMORY_HOTPLUG=y
 | 
			
		||||
CONFIG_ACPI_CPU_HOTPLUG=y
 | 
			
		||||
CONFIG_APM=y
 | 
			
		||||
CONFIG_I8257=y
 | 
			
		||||
CONFIG_PIIX4=y
 | 
			
		||||
@@ -29,7 +26,6 @@ CONFIG_DP8393X=y
 | 
			
		||||
CONFIG_DS1225Y=y
 | 
			
		||||
CONFIG_MIPSNET=y
 | 
			
		||||
CONFIG_PFLASH_CFI01=y
 | 
			
		||||
CONFIG_JAZZ=y
 | 
			
		||||
CONFIG_G364FB=y
 | 
			
		||||
CONFIG_I8259=y
 | 
			
		||||
CONFIG_JAZZ_LED=y
 | 
			
		||||
 
 | 
			
		||||
@@ -15,9 +15,6 @@ CONFIG_PCSPK=y
 | 
			
		||||
CONFIG_PCKBD=y
 | 
			
		||||
CONFIG_FDC=y
 | 
			
		||||
CONFIG_ACPI=y
 | 
			
		||||
CONFIG_ACPI_X86=y
 | 
			
		||||
CONFIG_ACPI_MEMORY_HOTPLUG=y
 | 
			
		||||
CONFIG_ACPI_CPU_HOTPLUG=y
 | 
			
		||||
CONFIG_APM=y
 | 
			
		||||
CONFIG_I8257=y
 | 
			
		||||
CONFIG_PIIX4=y
 | 
			
		||||
@@ -31,7 +28,6 @@ CONFIG_DS1225Y=y
 | 
			
		||||
CONFIG_MIPSNET=y
 | 
			
		||||
CONFIG_PFLASH_CFI01=y
 | 
			
		||||
CONFIG_FULONG=y
 | 
			
		||||
CONFIG_JAZZ=y
 | 
			
		||||
CONFIG_G364FB=y
 | 
			
		||||
CONFIG_I8259=y
 | 
			
		||||
CONFIG_JAZZ_LED=y
 | 
			
		||||
 
 | 
			
		||||
@@ -15,18 +15,20 @@ CONFIG_PCSPK=y
 | 
			
		||||
CONFIG_PCKBD=y
 | 
			
		||||
CONFIG_FDC=y
 | 
			
		||||
CONFIG_ACPI=y
 | 
			
		||||
CONFIG_ACPI_X86=y
 | 
			
		||||
CONFIG_ACPI_MEMORY_HOTPLUG=y
 | 
			
		||||
CONFIG_ACPI_CPU_HOTPLUG=y
 | 
			
		||||
CONFIG_APM=y
 | 
			
		||||
CONFIG_I8257=y
 | 
			
		||||
CONFIG_PIIX4=y
 | 
			
		||||
CONFIG_IDE_ISA=y
 | 
			
		||||
CONFIG_IDE_PIIX=y
 | 
			
		||||
CONFIG_NE2000_ISA=y
 | 
			
		||||
CONFIG_RC4030=y
 | 
			
		||||
CONFIG_DP8393X=y
 | 
			
		||||
CONFIG_DS1225Y=y
 | 
			
		||||
CONFIG_MIPSNET=y
 | 
			
		||||
CONFIG_PFLASH_CFI01=y
 | 
			
		||||
CONFIG_G364FB=y
 | 
			
		||||
CONFIG_I8259=y
 | 
			
		||||
CONFIG_JAZZ_LED=y
 | 
			
		||||
CONFIG_MC146818RTC=y
 | 
			
		||||
CONFIG_ISA_TESTDEV=y
 | 
			
		||||
CONFIG_EMPTY_SLOT=y
 | 
			
		||||
 
 | 
			
		||||
@@ -36,4 +36,3 @@ CONFIG_EDU=y
 | 
			
		||||
CONFIG_VGA=y
 | 
			
		||||
CONFIG_VGA_PCI=y
 | 
			
		||||
CONFIG_IVSHMEM=$(CONFIG_KVM)
 | 
			
		||||
CONFIG_ROCKER=y
 | 
			
		||||
 
 | 
			
		||||
@@ -4,4 +4,3 @@ CONFIG_VIRTIO=y
 | 
			
		||||
CONFIG_SCLPCONSOLE=y
 | 
			
		||||
CONFIG_S390_FLIC=y
 | 
			
		||||
CONFIG_S390_FLIC_KVM=$(CONFIG_KVM)
 | 
			
		||||
CONFIG_WDT_DIAG288=y
 | 
			
		||||
 
 | 
			
		||||
@@ -7,7 +7,6 @@ CONFIG_QXL=$(CONFIG_SPICE)
 | 
			
		||||
CONFIG_VGA_ISA=y
 | 
			
		||||
CONFIG_VGA_CIRRUS=y
 | 
			
		||||
CONFIG_VMWARE_VGA=y
 | 
			
		||||
CONFIG_VIRTIO_VGA=y
 | 
			
		||||
CONFIG_VMMOUSE=y
 | 
			
		||||
CONFIG_SERIAL=y
 | 
			
		||||
CONFIG_PARALLEL=y
 | 
			
		||||
@@ -16,9 +15,6 @@ CONFIG_PCSPK=y
 | 
			
		||||
CONFIG_PCKBD=y
 | 
			
		||||
CONFIG_FDC=y
 | 
			
		||||
CONFIG_ACPI=y
 | 
			
		||||
CONFIG_ACPI_X86=y
 | 
			
		||||
CONFIG_ACPI_MEMORY_HOTPLUG=y
 | 
			
		||||
CONFIG_ACPI_CPU_HOTPLUG=y
 | 
			
		||||
CONFIG_APM=y
 | 
			
		||||
CONFIG_I8257=y
 | 
			
		||||
CONFIG_IDE_ISA=y
 | 
			
		||||
 
 | 
			
		||||
@@ -18,6 +18,7 @@
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
 | 
			
		||||
#include "config.h"
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "qemu/error-report.h"
 | 
			
		||||
#include "sysemu/device_tree.h"
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										14
									
								
								disas.c
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								disas.c
									
									
									
									
									
								
							@@ -9,7 +9,7 @@
 | 
			
		||||
 | 
			
		||||
typedef struct CPUDebug {
 | 
			
		||||
    struct disassemble_info info;
 | 
			
		||||
    CPUState *cpu;
 | 
			
		||||
    CPUArchState *env;
 | 
			
		||||
} CPUDebug;
 | 
			
		||||
 | 
			
		||||
/* Filled in by elfload.c.  Simplistic, but will do for now. */
 | 
			
		||||
@@ -39,7 +39,7 @@ target_read_memory (bfd_vma memaddr,
 | 
			
		||||
{
 | 
			
		||||
    CPUDebug *s = container_of(info, CPUDebug, info);
 | 
			
		||||
 | 
			
		||||
    cpu_memory_rw_debug(s->cpu, memaddr, myaddr, length, 0);
 | 
			
		||||
    cpu_memory_rw_debug(ENV_GET_CPU(s->env), memaddr, myaddr, length, 0);
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -195,7 +195,7 @@ static int print_insn_od_target(bfd_vma pc, disassemble_info *info)
 | 
			
		||||
           bit 16 indicates little endian.
 | 
			
		||||
    other targets - unused
 | 
			
		||||
 */
 | 
			
		||||
void target_disas(FILE *out, CPUState *cpu, target_ulong code,
 | 
			
		||||
void target_disas(FILE *out, CPUArchState *env, target_ulong code,
 | 
			
		||||
                  target_ulong size, int flags)
 | 
			
		||||
{
 | 
			
		||||
    target_ulong pc;
 | 
			
		||||
@@ -205,7 +205,7 @@ void target_disas(FILE *out, CPUState *cpu, target_ulong code,
 | 
			
		||||
 | 
			
		||||
    INIT_DISASSEMBLE_INFO(s.info, out, fprintf);
 | 
			
		||||
 | 
			
		||||
    s.cpu = cpu;
 | 
			
		||||
    s.env = env;
 | 
			
		||||
    s.info.read_memory_func = target_read_memory;
 | 
			
		||||
    s.info.buffer_vma = code;
 | 
			
		||||
    s.info.buffer_length = size;
 | 
			
		||||
@@ -430,7 +430,7 @@ monitor_read_memory (bfd_vma memaddr, bfd_byte *myaddr, int length,
 | 
			
		||||
    if (monitor_disas_is_physical) {
 | 
			
		||||
        cpu_physical_memory_read(memaddr, myaddr, length);
 | 
			
		||||
    } else {
 | 
			
		||||
        cpu_memory_rw_debug(s->cpu, memaddr, myaddr, length, 0);
 | 
			
		||||
        cpu_memory_rw_debug(ENV_GET_CPU(s->env), memaddr, myaddr, length, 0);
 | 
			
		||||
    }
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
@@ -447,7 +447,7 @@ monitor_fprintf(FILE *stream, const char *fmt, ...)
 | 
			
		||||
 | 
			
		||||
/* Disassembler for the monitor.
 | 
			
		||||
   See target_disas for a description of flags. */
 | 
			
		||||
void monitor_disas(Monitor *mon, CPUState *cpu,
 | 
			
		||||
void monitor_disas(Monitor *mon, CPUArchState *env,
 | 
			
		||||
                   target_ulong pc, int nb_insn, int is_physical, int flags)
 | 
			
		||||
{
 | 
			
		||||
    int count, i;
 | 
			
		||||
@@ -456,7 +456,7 @@ void monitor_disas(Monitor *mon, CPUState *cpu,
 | 
			
		||||
 | 
			
		||||
    INIT_DISASSEMBLE_INFO(s.info, (FILE *)mon, monitor_fprintf);
 | 
			
		||||
 | 
			
		||||
    s.cpu = cpu;
 | 
			
		||||
    s.env = env;
 | 
			
		||||
    monitor_disas_is_physical = is_physical;
 | 
			
		||||
    s.info.read_memory_func = monitor_read_memory;
 | 
			
		||||
    s.info.print_address_func = generic_print_target_address;
 | 
			
		||||
 
 | 
			
		||||
@@ -2238,8 +2238,6 @@ const struct mips_opcode mips_builtin_opcodes[] =
 | 
			
		||||
{"ceil.l.s", "D,S",	0x4600000a, 0xffff003f, WR_D|RD_S|FP_S|FP_D,	0,		I3|I33	},
 | 
			
		||||
{"ceil.w.d", "D,S",	0x4620000e, 0xffff003f, WR_D|RD_S|FP_S|FP_D,	0,		I2	},
 | 
			
		||||
{"ceil.w.s", "D,S",	0x4600000e, 0xffff003f, WR_D|RD_S|FP_S,		0,		I2	},
 | 
			
		||||
{"mfhc0",   "t,G,H",    0x40400000, 0xffe007f8, LCD|WR_t|RD_C0,       0, I33},
 | 
			
		||||
{"mthc0",   "t,G,H",    0x40c00000, 0xffe007f8, COD|RD_t|WR_C0|WR_CC, 0, I33},
 | 
			
		||||
{"cfc0",    "t,G",	0x40400000, 0xffe007ff,	LCD|WR_t|RD_C0,		0,		I1	},
 | 
			
		||||
{"cfc1",    "t,G",	0x44400000, 0xffe007ff,	LCD|WR_t|RD_C1|FP_S,	0,		I1	},
 | 
			
		||||
{"cfc1",    "t,S",	0x44400000, 0xffe007ff,	LCD|WR_t|RD_C1|FP_S,	0,		I1	},
 | 
			
		||||
@@ -2409,7 +2407,6 @@ const struct mips_opcode mips_builtin_opcodes[] =
 | 
			
		||||
{"emt",     "",		0x41600be1, 0xffffffff, TRAP,			0,		MT32	},
 | 
			
		||||
{"emt",     "t",	0x41600be1, 0xffe0ffff, TRAP|WR_t,		0,		MT32	},
 | 
			
		||||
{"eret",    "",         0x42000018, 0xffffffff, 0,      		0,		I3|I32	},
 | 
			
		||||
{"eretnc",  "",         0x42000058, 0xffffffff, 0,                    0, I33},
 | 
			
		||||
{"evpe",    "",		0x41600021, 0xffffffff, TRAP,			0,		MT32	},
 | 
			
		||||
{"evpe",    "t",	0x41600021, 0xffe0ffff, TRAP|WR_t,		0,		MT32	},
 | 
			
		||||
{"ext",     "t,r,+A,+C", 0x7c000000, 0xfc00003f, WR_t|RD_s,    		0,		I33	},
 | 
			
		||||
 
 | 
			
		||||
@@ -10,6 +10,7 @@
 | 
			
		||||
#include "sysemu/block-backend.h"
 | 
			
		||||
#include "sysemu/dma.h"
 | 
			
		||||
#include "trace.h"
 | 
			
		||||
#include "qemu/range.h"
 | 
			
		||||
#include "qemu/thread.h"
 | 
			
		||||
#include "qemu/main-loop.h"
 | 
			
		||||
 | 
			
		||||
@@ -27,8 +28,7 @@ int dma_memory_set(AddressSpace *as, dma_addr_t addr, uint8_t c, dma_addr_t len)
 | 
			
		||||
    memset(fillbuf, c, FILLBUF_SIZE);
 | 
			
		||||
    while (len > 0) {
 | 
			
		||||
        l = len < FILLBUF_SIZE ? len : FILLBUF_SIZE;
 | 
			
		||||
        error |= address_space_rw(as, addr, MEMTXATTRS_UNSPECIFIED,
 | 
			
		||||
                                  fillbuf, l, true);
 | 
			
		||||
        error |= address_space_rw(as, addr, fillbuf, l, true);
 | 
			
		||||
        len -= l;
 | 
			
		||||
        addr += l;
 | 
			
		||||
    }
 | 
			
		||||
@@ -92,6 +92,14 @@ static void reschedule_dma(void *opaque)
 | 
			
		||||
    dma_blk_cb(dbs, 0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void continue_after_map_failure(void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    DMAAIOCB *dbs = (DMAAIOCB *)opaque;
 | 
			
		||||
 | 
			
		||||
    dbs->bh = qemu_bh_new(reschedule_dma, dbs);
 | 
			
		||||
    qemu_bh_schedule(dbs->bh);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void dma_blk_unmap(DMAAIOCB *dbs)
 | 
			
		||||
{
 | 
			
		||||
    int i;
 | 
			
		||||
@@ -153,9 +161,7 @@ static void dma_blk_cb(void *opaque, int ret)
 | 
			
		||||
 | 
			
		||||
    if (dbs->iov.size == 0) {
 | 
			
		||||
        trace_dma_map_wait(dbs);
 | 
			
		||||
        dbs->bh = aio_bh_new(blk_get_aio_context(dbs->blk),
 | 
			
		||||
                             reschedule_dma, dbs);
 | 
			
		||||
        cpu_register_map_client(dbs->bh);
 | 
			
		||||
        cpu_register_map_client(dbs, continue_after_map_failure);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -177,11 +183,6 @@ static void dma_aio_cancel(BlockAIOCB *acb)
 | 
			
		||||
    if (dbs->acb) {
 | 
			
		||||
        blk_aio_cancel_async(dbs->acb);
 | 
			
		||||
    }
 | 
			
		||||
    if (dbs->bh) {
 | 
			
		||||
        cpu_unregister_map_client(dbs->bh);
 | 
			
		||||
        qemu_bh_delete(dbs->bh);
 | 
			
		||||
        dbs->bh = NULL;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -281,7 +281,7 @@ note that the other barrier may actually be in a driver that runs in
 | 
			
		||||
the guest!
 | 
			
		||||
 | 
			
		||||
For the purposes of pairing, smp_read_barrier_depends() and smp_rmb()
 | 
			
		||||
both count as read barriers.  A read barrier shall pair with a write
 | 
			
		||||
both count as read barriers.  A read barriers shall pair with a write
 | 
			
		||||
barrier or a full barrier; a write barrier shall pair with a read
 | 
			
		||||
barrier or a full barrier.  A full barrier can pair with anything.
 | 
			
		||||
For example:
 | 
			
		||||
@@ -294,7 +294,7 @@ For example:
 | 
			
		||||
                             smp_rmb();
 | 
			
		||||
                             y = a;
 | 
			
		||||
 | 
			
		||||
Note that the "writing" thread is accessing the variables in the
 | 
			
		||||
Note that the "writing" thread are accessing the variables in the
 | 
			
		||||
opposite order as the "reading" thread.  This is expected: stores
 | 
			
		||||
before the write barrier will normally match the loads after the
 | 
			
		||||
read barrier, and vice versa.  The same is true for more than 2
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										352
									
								
								docs/bitmaps.md
									
									
									
									
									
								
							
							
						
						
									
										352
									
								
								docs/bitmaps.md
									
									
									
									
									
								
							@@ -1,352 +0,0 @@
 | 
			
		||||
<!--
 | 
			
		||||
Copyright 2015 John Snow <jsnow@redhat.com> and Red Hat, Inc.
 | 
			
		||||
All rights reserved.
 | 
			
		||||
 | 
			
		||||
This file is licensed via The FreeBSD Documentation License, the full text of
 | 
			
		||||
which is included at the end of this document.
 | 
			
		||||
-->
 | 
			
		||||
 | 
			
		||||
# Dirty Bitmaps and Incremental Backup
 | 
			
		||||
 | 
			
		||||
* Dirty Bitmaps are objects that track which data needs to be backed up for the
 | 
			
		||||
  next incremental backup.
 | 
			
		||||
 | 
			
		||||
* Dirty bitmaps can be created at any time and attached to any node
 | 
			
		||||
  (not just complete drives.)
 | 
			
		||||
 | 
			
		||||
## Dirty Bitmap Names
 | 
			
		||||
 | 
			
		||||
* A dirty bitmap's name is unique to the node, but bitmaps attached to different
 | 
			
		||||
  nodes can share the same name.
 | 
			
		||||
 | 
			
		||||
## Bitmap Modes
 | 
			
		||||
 | 
			
		||||
* A Bitmap can be "frozen," which means that it is currently in-use by a backup
 | 
			
		||||
  operation and cannot be deleted, renamed, written to, reset,
 | 
			
		||||
  etc.
 | 
			
		||||
 | 
			
		||||
## Basic QMP Usage
 | 
			
		||||
 | 
			
		||||
### Supported Commands ###
 | 
			
		||||
 | 
			
		||||
* block-dirty-bitmap-add
 | 
			
		||||
* block-dirty-bitmap-remove
 | 
			
		||||
* block-dirty-bitmap-clear
 | 
			
		||||
 | 
			
		||||
### Creation
 | 
			
		||||
 | 
			
		||||
* To create a new bitmap, enabled, on the drive with id=drive0:
 | 
			
		||||
 | 
			
		||||
```json
 | 
			
		||||
{ "execute": "block-dirty-bitmap-add",
 | 
			
		||||
  "arguments": {
 | 
			
		||||
    "node": "drive0",
 | 
			
		||||
    "name": "bitmap0"
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
* This bitmap will have a default granularity that matches the cluster size of
 | 
			
		||||
  its associated drive, if available, clamped to between [4KiB, 64KiB].
 | 
			
		||||
  The current default for qcow2 is 64KiB.
 | 
			
		||||
 | 
			
		||||
* To create a new bitmap that tracks changes in 32KiB segments:
 | 
			
		||||
 | 
			
		||||
```json
 | 
			
		||||
{ "execute": "block-dirty-bitmap-add",
 | 
			
		||||
  "arguments": {
 | 
			
		||||
    "node": "drive0",
 | 
			
		||||
    "name": "bitmap0",
 | 
			
		||||
    "granularity": 32768
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
### Deletion
 | 
			
		||||
 | 
			
		||||
* Bitmaps that are frozen cannot be deleted.
 | 
			
		||||
 | 
			
		||||
* Deleting the bitmap does not impact any other bitmaps attached to the same
 | 
			
		||||
  node, nor does it affect any backups already created from this node.
 | 
			
		||||
 | 
			
		||||
* Because bitmaps are only unique to the node to which they are attached,
 | 
			
		||||
  you must specify the node/drive name here, too.
 | 
			
		||||
 | 
			
		||||
```json
 | 
			
		||||
{ "execute": "block-dirty-bitmap-remove",
 | 
			
		||||
  "arguments": {
 | 
			
		||||
    "node": "drive0",
 | 
			
		||||
    "name": "bitmap0"
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
### Resetting
 | 
			
		||||
 | 
			
		||||
* Resetting a bitmap will clear all information it holds.
 | 
			
		||||
 | 
			
		||||
* An incremental backup created from an empty bitmap will copy no data,
 | 
			
		||||
  as if nothing has changed.
 | 
			
		||||
 | 
			
		||||
```json
 | 
			
		||||
{ "execute": "block-dirty-bitmap-clear",
 | 
			
		||||
  "arguments": {
 | 
			
		||||
    "node": "drive0",
 | 
			
		||||
    "name": "bitmap0"
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## Transactions (Not yet implemented)
 | 
			
		||||
 | 
			
		||||
* Transactional commands are forthcoming in a future version,
 | 
			
		||||
  and are not yet available for use. This section serves as
 | 
			
		||||
  documentation of intent for their design and usage.
 | 
			
		||||
 | 
			
		||||
### Justification
 | 
			
		||||
 | 
			
		||||
Bitmaps can be safely modified when the VM is paused or halted by using
 | 
			
		||||
the basic QMP commands. For instance, you might perform the following actions:
 | 
			
		||||
 | 
			
		||||
1. Boot the VM in a paused state.
 | 
			
		||||
2. Create a full drive backup of drive0.
 | 
			
		||||
3. Create a new bitmap attached to drive0.
 | 
			
		||||
4. Resume execution of the VM.
 | 
			
		||||
5. Incremental backups are ready to be created.
 | 
			
		||||
 | 
			
		||||
At this point, the bitmap and drive backup would be correctly in sync,
 | 
			
		||||
and incremental backups made from this point forward would be correctly aligned
 | 
			
		||||
to the full drive backup.
 | 
			
		||||
 | 
			
		||||
This is not particularly useful if we decide we want to start incremental
 | 
			
		||||
backups after the VM has been running for a while, for which we will need to
 | 
			
		||||
perform actions such as the following:
 | 
			
		||||
 | 
			
		||||
1. Boot the VM and begin execution.
 | 
			
		||||
2. Using a single transaction, perform the following operations:
 | 
			
		||||
    * Create bitmap0.
 | 
			
		||||
    * Create a full drive backup of drive0.
 | 
			
		||||
3. Incremental backups are now ready to be created.
 | 
			
		||||
 | 
			
		||||
### Supported Bitmap Transactions
 | 
			
		||||
 | 
			
		||||
* block-dirty-bitmap-add
 | 
			
		||||
* block-dirty-bitmap-clear
 | 
			
		||||
 | 
			
		||||
The usages are identical to their respective QMP commands, but see below
 | 
			
		||||
for examples.
 | 
			
		||||
 | 
			
		||||
### Example: New Incremental Backup
 | 
			
		||||
 | 
			
		||||
As outlined in the justification, perhaps we want to create a new incremental
 | 
			
		||||
backup chain attached to a drive.
 | 
			
		||||
 | 
			
		||||
```json
 | 
			
		||||
{ "execute": "transaction",
 | 
			
		||||
  "arguments": {
 | 
			
		||||
    "actions": [
 | 
			
		||||
      {"type": "block-dirty-bitmap-add",
 | 
			
		||||
       "data": {"node": "drive0", "name": "bitmap0"} },
 | 
			
		||||
      {"type": "drive-backup",
 | 
			
		||||
       "data": {"device": "drive0", "target": "/path/to/full_backup.img",
 | 
			
		||||
                "sync": "full", "format": "qcow2"} }
 | 
			
		||||
    ]
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
### Example: New Incremental Backup Anchor Point
 | 
			
		||||
 | 
			
		||||
Maybe we just want to create a new full backup with an existing bitmap and
 | 
			
		||||
want to reset the bitmap to track the new chain.
 | 
			
		||||
 | 
			
		||||
```json
 | 
			
		||||
{ "execute": "transaction",
 | 
			
		||||
  "arguments": {
 | 
			
		||||
    "actions": [
 | 
			
		||||
      {"type": "block-dirty-bitmap-clear",
 | 
			
		||||
       "data": {"node": "drive0", "name": "bitmap0"} },
 | 
			
		||||
      {"type": "drive-backup",
 | 
			
		||||
       "data": {"device": "drive0", "target": "/path/to/new_full_backup.img",
 | 
			
		||||
                "sync": "full", "format": "qcow2"} }
 | 
			
		||||
    ]
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## Incremental Backups
 | 
			
		||||
 | 
			
		||||
The star of the show.
 | 
			
		||||
 | 
			
		||||
**Nota Bene!** Only incremental backups of entire drives are supported for now.
 | 
			
		||||
So despite the fact that you can attach a bitmap to any arbitrary node, they are
 | 
			
		||||
only currently useful when attached to the root node. This is because
 | 
			
		||||
drive-backup only supports drives/devices instead of arbitrary nodes.
 | 
			
		||||
 | 
			
		||||
### Example: First Incremental Backup
 | 
			
		||||
 | 
			
		||||
1. Create a full backup and sync it to the dirty bitmap, as in the transactional
 | 
			
		||||
examples above; or with the VM offline, manually create a full copy and then
 | 
			
		||||
create a new bitmap before the VM begins execution.
 | 
			
		||||
 | 
			
		||||
    * Let's assume the full backup is named 'full_backup.img'.
 | 
			
		||||
    * Let's assume the bitmap you created is 'bitmap0' attached to 'drive0'.
 | 
			
		||||
 | 
			
		||||
2. Create a destination image for the incremental backup that utilizes the
 | 
			
		||||
full backup as a backing image.
 | 
			
		||||
 | 
			
		||||
    * Let's assume it is named 'incremental.0.img'.
 | 
			
		||||
 | 
			
		||||
    ```sh
 | 
			
		||||
    # qemu-img create -f qcow2 incremental.0.img -b full_backup.img -F qcow2
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
3. Issue the incremental backup command:
 | 
			
		||||
 | 
			
		||||
    ```json
 | 
			
		||||
    { "execute": "drive-backup",
 | 
			
		||||
      "arguments": {
 | 
			
		||||
        "device": "drive0",
 | 
			
		||||
        "bitmap": "bitmap0",
 | 
			
		||||
        "target": "incremental.0.img",
 | 
			
		||||
        "format": "qcow2",
 | 
			
		||||
        "sync": "incremental",
 | 
			
		||||
        "mode": "existing"
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
### Example: Second Incremental Backup
 | 
			
		||||
 | 
			
		||||
1. Create a new destination image for the incremental backup that points to the
 | 
			
		||||
   previous one, e.g.: 'incremental.1.img'
 | 
			
		||||
 | 
			
		||||
    ```sh
 | 
			
		||||
    # qemu-img create -f qcow2 incremental.1.img -b incremental.0.img -F qcow2
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
2. Issue a new incremental backup command. The only difference here is that we
 | 
			
		||||
   have changed the target image below.
 | 
			
		||||
 | 
			
		||||
    ```json
 | 
			
		||||
    { "execute": "drive-backup",
 | 
			
		||||
      "arguments": {
 | 
			
		||||
        "device": "drive0",
 | 
			
		||||
        "bitmap": "bitmap0",
 | 
			
		||||
        "target": "incremental.1.img",
 | 
			
		||||
        "format": "qcow2",
 | 
			
		||||
        "sync": "incremental",
 | 
			
		||||
        "mode": "existing"
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
## Errors
 | 
			
		||||
 | 
			
		||||
* In the event of an error that occurs after a backup job is successfully
 | 
			
		||||
  launched, either by a direct QMP command or a QMP transaction, the user
 | 
			
		||||
  will receive a BLOCK_JOB_COMPLETE event with a failure message, accompanied
 | 
			
		||||
  by a BLOCK_JOB_ERROR event.
 | 
			
		||||
 | 
			
		||||
* In the case of an event being cancelled, the user will receive a
 | 
			
		||||
  BLOCK_JOB_CANCELLED event instead of a pair of COMPLETE and ERROR events.
 | 
			
		||||
 | 
			
		||||
* In either case, the incremental backup data contained within the bitmap is
 | 
			
		||||
  safely rolled back, and the data within the bitmap is not lost. The image
 | 
			
		||||
  file created for the failed attempt can be safely deleted.
 | 
			
		||||
 | 
			
		||||
* Once the underlying problem is fixed (e.g. more storage space is freed up),
 | 
			
		||||
  you can simply retry the incremental backup command with the same bitmap.
 | 
			
		||||
 | 
			
		||||
### Example
 | 
			
		||||
 | 
			
		||||
1. Create a target image:
 | 
			
		||||
 | 
			
		||||
    ```sh
 | 
			
		||||
    # qemu-img create -f qcow2 incremental.0.img -b full_backup.img -F qcow2
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
2. Attempt to create an incremental backup via QMP:
 | 
			
		||||
 | 
			
		||||
    ```json
 | 
			
		||||
    { "execute": "drive-backup",
 | 
			
		||||
      "arguments": {
 | 
			
		||||
        "device": "drive0",
 | 
			
		||||
        "bitmap": "bitmap0",
 | 
			
		||||
        "target": "incremental.0.img",
 | 
			
		||||
        "format": "qcow2",
 | 
			
		||||
        "sync": "incremental",
 | 
			
		||||
        "mode": "existing"
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
3. Receive an event notifying us of failure:
 | 
			
		||||
 | 
			
		||||
    ```json
 | 
			
		||||
    { "timestamp": { "seconds": 1424709442, "microseconds": 844524 },
 | 
			
		||||
      "data": { "speed": 0, "offset": 0, "len": 67108864,
 | 
			
		||||
                "error": "No space left on device",
 | 
			
		||||
                "device": "drive1", "type": "backup" },
 | 
			
		||||
      "event": "BLOCK_JOB_COMPLETED" }
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
4. Delete the failed incremental, and re-create the image.
 | 
			
		||||
 | 
			
		||||
    ```sh
 | 
			
		||||
    # rm incremental.0.img
 | 
			
		||||
    # qemu-img create -f qcow2 incremental.0.img -b full_backup.img -F qcow2
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
5. Retry the command after fixing the underlying problem,
 | 
			
		||||
   such as freeing up space on the backup volume:
 | 
			
		||||
 | 
			
		||||
    ```json
 | 
			
		||||
    { "execute": "drive-backup",
 | 
			
		||||
      "arguments": {
 | 
			
		||||
        "device": "drive0",
 | 
			
		||||
        "bitmap": "bitmap0",
 | 
			
		||||
        "target": "incremental.0.img",
 | 
			
		||||
        "format": "qcow2",
 | 
			
		||||
        "sync": "incremental",
 | 
			
		||||
        "mode": "existing"
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
6. Receive confirmation that the job completed successfully:
 | 
			
		||||
 | 
			
		||||
    ```json
 | 
			
		||||
    { "timestamp": { "seconds": 1424709668, "microseconds": 526525 },
 | 
			
		||||
      "data": { "device": "drive1", "type": "backup",
 | 
			
		||||
                "speed": 0, "len": 67108864, "offset": 67108864},
 | 
			
		||||
      "event": "BLOCK_JOB_COMPLETED" }
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
<!--
 | 
			
		||||
The FreeBSD Documentation License
 | 
			
		||||
 | 
			
		||||
Redistribution and use in source (Markdown) and 'compiled' forms (SGML, HTML,
 | 
			
		||||
PDF, PostScript, RTF and so forth) with or without modification, are permitted
 | 
			
		||||
provided that the following conditions are met:
 | 
			
		||||
 | 
			
		||||
Redistributions of source code (Markdown) must retain the above copyright
 | 
			
		||||
notice, this list of conditions and the following disclaimer of this file
 | 
			
		||||
unmodified.
 | 
			
		||||
 | 
			
		||||
Redistributions in compiled form (transformed to other DTDs, converted to PDF,
 | 
			
		||||
PostScript, RTF and other formats) must reproduce the above copyright notice,
 | 
			
		||||
this list of conditions and the following disclaimer in the documentation and/or
 | 
			
		||||
other materials provided with the distribution.
 | 
			
		||||
 | 
			
		||||
THIS DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 | 
			
		||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 | 
			
		||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR  PURPOSE ARE
 | 
			
		||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS  BE LIABLE
 | 
			
		||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 | 
			
		||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 | 
			
		||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 | 
			
		||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 | 
			
		||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
			
		||||
THIS DOCUMENTATION, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
			
		||||
-->
 | 
			
		||||
@@ -4,7 +4,9 @@ QEMU memory hotplug
 | 
			
		||||
This document explains how to use the memory hotplug feature in QEMU,
 | 
			
		||||
which is present since v2.1.0.
 | 
			
		||||
 | 
			
		||||
Guest support is required for memory hotplug to work.
 | 
			
		||||
Please, note that memory hotunplug is not supported yet. This means
 | 
			
		||||
that you're able to add memory, but you're not able to remove it.
 | 
			
		||||
Also, proper guest support is required for memory hotplug to work.
 | 
			
		||||
 | 
			
		||||
Basic RAM hotplug
 | 
			
		||||
-----------------
 | 
			
		||||
@@ -72,22 +74,3 @@ comes from regular RAM, 1GB is a 1GB hugepage page and 256MB is from
 | 
			
		||||
   -device pc-dimm,id=dimm1,memdev=mem1 \
 | 
			
		||||
   -object memory-backend-file,id=mem2,size=256M,mem-path=/mnt/hugepages-2MB \
 | 
			
		||||
   -device pc-dimm,id=dimm2,memdev=mem2
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
RAM hot-unplug
 | 
			
		||||
---------------
 | 
			
		||||
 | 
			
		||||
In order to be able to hot unplug pc-dimm device, QEMU has to be told the ids
 | 
			
		||||
of pc-dimm device and memory backend object. The ids were assigned when you hot
 | 
			
		||||
plugged memory.
 | 
			
		||||
 | 
			
		||||
Two monitor commands are used to hot unplug memory:
 | 
			
		||||
 | 
			
		||||
 - "device_del": deletes a front-end pc-dimm device
 | 
			
		||||
 - "object_del": deletes a memory backend object
 | 
			
		||||
 | 
			
		||||
For example, assuming that the pc-dimm device with id "dimm1" exists, and its memory
 | 
			
		||||
backend is "mem1", the following commands tries to remove it.
 | 
			
		||||
 | 
			
		||||
  (qemu) device_del dimm1
 | 
			
		||||
  (qemu) object_del mem1
 | 
			
		||||
 
 | 
			
		||||
@@ -257,7 +257,6 @@ const VMStateDescription vmstate_ide_drive_pio_state = {
 | 
			
		||||
    .minimum_version_id = 1,
 | 
			
		||||
    .pre_save = ide_drive_pio_pre_save,
 | 
			
		||||
    .post_load = ide_drive_pio_post_load,
 | 
			
		||||
    .needed = ide_drive_pio_state_needed,
 | 
			
		||||
    .fields = (VMStateField[]) {
 | 
			
		||||
        VMSTATE_INT32(req_nb_sectors, IDEState),
 | 
			
		||||
        VMSTATE_VARRAY_INT32(io_buffer, IDEState, io_buffer_total_len, 1,
 | 
			
		||||
@@ -280,9 +279,13 @@ const VMStateDescription vmstate_ide_drive = {
 | 
			
		||||
        .... several fields ....
 | 
			
		||||
        VMSTATE_END_OF_LIST()
 | 
			
		||||
    },
 | 
			
		||||
    .subsections = (const VMStateDescription*[]) {
 | 
			
		||||
        &vmstate_ide_drive_pio_state,
 | 
			
		||||
        NULL
 | 
			
		||||
    .subsections = (VMStateSubsection []) {
 | 
			
		||||
        {
 | 
			
		||||
            .vmsd = &vmstate_ide_drive_pio_state,
 | 
			
		||||
            .needed = ide_drive_pio_state_needed,
 | 
			
		||||
        }, {
 | 
			
		||||
            /* empty */
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
Some files were not shown because too many files have changed in this diff Show More
		Reference in New Issue
	
	Block a user