Compare commits
269 Commits
pull-ui-20
...
SLE11-SP4-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1aff087cee | ||
|
|
b4f36774b7 | ||
|
|
2531d7ee4d | ||
|
|
6855c034e7 | ||
|
|
51e5a6007e | ||
|
|
e3806f5d57 | ||
|
|
b8c2ea9776 | ||
| 0398a1b258 | |||
|
|
4bf75ff6d6 | ||
|
|
0447550926 | ||
|
|
b2bdec338d | ||
|
|
a586d7d202 | ||
|
|
85dadfc305 | ||
|
|
7b2dfb5d35 | ||
|
|
686fab80bd | ||
|
|
abfdc2e6ff | ||
|
|
d05ce95406 | ||
|
|
2c2261cfcc | ||
|
|
c6616fd654 | ||
|
|
efe2752601 | ||
|
|
6e543d2c12 | ||
|
|
de04929dd2 | ||
|
|
670d5d7fee | ||
|
|
94107cdfae | ||
|
|
394084d080 | ||
|
|
551fc996b5 | ||
|
|
ee23dcb6c1 | ||
|
|
b42aedacc9 | ||
| 215b18c9fc | |||
|
|
402d0d1f0c | ||
|
|
58b487a2bc | ||
|
|
6d9a092479 | ||
|
|
e87058cc54 | ||
|
|
3b75987950 | ||
|
|
37f7413027 | ||
|
|
44a0871d54 | ||
|
|
cb74d9ee43 | ||
|
|
59cdffd5f1 | ||
|
|
18f2150f92 | ||
|
|
0625626e51 | ||
|
|
1a66a3ca79 | ||
|
|
158326e199 | ||
|
|
fd67499caa | ||
|
|
71149b3e14 | ||
|
|
5f5aa07d16 | ||
|
|
3a34ab453f | ||
|
|
80ce3a7403 | ||
|
|
7af0df9343 | ||
|
|
094e9d9a91 | ||
|
|
a819068104 | ||
|
|
2ca9b4d153 | ||
|
|
ff80ec1aab | ||
|
|
4bf7b7da45 | ||
|
|
5a6e91a399 | ||
|
|
e8363b7738 | ||
|
|
dd9169bc43 | ||
|
|
877b642be0 | ||
|
|
c181a409d4 | ||
|
|
5626edc3f9 | ||
|
|
2a57bae0d1 | ||
|
|
cca58015c0 | ||
|
|
a958839822 | ||
|
|
e8a8f9f1c4 | ||
|
|
0f4f9527d0 | ||
|
|
408dc94b92 | ||
|
|
d9593d1734 | ||
|
|
301feb072e | ||
|
|
9848148c9c | ||
|
|
924eda5c4a | ||
|
|
8c9ef11d8a | ||
|
|
d5685a80ce | ||
|
|
5e783ed780 | ||
|
|
b27b5c305e | ||
|
|
87559bfe5a | ||
|
|
211bbf522c | ||
|
|
7bae3c9587 | ||
|
|
949fab98f8 | ||
|
|
ab00d35ba6 | ||
|
|
ccb16b84cd | ||
|
|
7acfa7e9eb | ||
|
|
0f55cd19aa | ||
|
|
05fc570638 | ||
|
|
a9041a3d9c | ||
|
|
9639415b85 | ||
|
|
7028f2bc09 | ||
|
|
573bea06b3 | ||
|
|
b0f69fb75c | ||
|
|
3f5672ec57 | ||
|
|
7eb2402026 | ||
|
|
176d3f3351 | ||
|
|
da96690d12 | ||
|
|
e3bd9029dc | ||
|
|
b4c60b7142 | ||
|
|
57c36784f0 | ||
|
|
3211c90e16 | ||
|
|
ca59c611d6 | ||
|
|
278ffa97d6 | ||
|
|
617ae61fa9 | ||
|
|
593f5a543b | ||
|
|
8118864031 | ||
|
|
b1fe4e34b1 | ||
|
|
a3d9060d83 | ||
|
|
44309c1775 | ||
|
|
f5a19fb649 | ||
|
|
1b65531708 | ||
|
|
cf8285b5f4 | ||
|
|
2509270b3b | ||
|
|
9571fdcc96 | ||
|
|
f01d1c4975 | ||
|
|
33fc1224b0 | ||
|
|
355d1697da | ||
|
|
721dcef81a | ||
|
|
68bdfae5e5 | ||
|
|
698c02a4f7 | ||
|
|
a39e5bb368 | ||
|
|
3d62fd2ba0 | ||
|
|
342e94e056 | ||
|
|
4b1e5f667f | ||
|
|
639373b494 | ||
|
|
5116278b67 | ||
|
|
bf2d7690fa | ||
|
|
c6a1d6e329 | ||
|
|
b9263558d9 | ||
|
|
669fb73c5f | ||
|
|
6ec86bb006 | ||
|
|
90e32d9444 | ||
|
|
63a4d51a78 | ||
|
|
ddd66af037 | ||
|
|
603c7238ff | ||
|
|
2b87f79326 | ||
|
|
3e812e702d | ||
|
|
05061c843c | ||
|
|
3dc60f68ae | ||
|
|
8cfd98d3ae | ||
|
|
a681202d4e | ||
|
|
e3401412dc | ||
|
|
fd5534832f | ||
|
|
4c739e0d6d | ||
|
|
300a4123d8 | ||
|
|
1ec1ef8f31 | ||
|
|
9a716057cd | ||
|
|
3fc0198478 | ||
|
|
7ece92f464 | ||
|
|
d0ccd870a6 | ||
|
|
46fbe7a783 | ||
|
|
383f92e67d | ||
|
|
383017b9bd | ||
|
|
4bbf4e9d8e | ||
|
|
e56ac60924 | ||
|
|
57e3bf66c2 | ||
|
|
50c6b143ff | ||
|
|
61c8e3c532 | ||
|
|
1380543027 | ||
|
|
53b7a865ee | ||
|
|
ed32292df6 | ||
|
|
a9692822d8 | ||
|
|
c173bbff64 | ||
|
|
230c029d16 | ||
|
|
1e5e5a10c3 | ||
|
|
e57cc5a2f9 | ||
|
|
f9cea35a81 | ||
|
|
0ae2b92b29 | ||
|
|
a4d378b1ce | ||
|
|
de8526eefe | ||
|
|
3ab66401c3 | ||
|
|
f6af7357df | ||
|
|
c8af02494d | ||
|
|
1bb261f222 | ||
|
|
f0922ef574 | ||
|
|
a6aa2f9c54 | ||
|
|
bf5b9c24c5 | ||
|
|
63d9acbe99 | ||
|
|
632f4958a0 | ||
|
|
66db770b81 | ||
|
|
363b2fc4f0 | ||
|
|
287b7249b6 | ||
|
|
ea55d53b1b | ||
|
|
8e4eb52196 | ||
|
|
5efb9ade7e | ||
|
|
73aab0ebf2 | ||
|
|
bad6f6bc3f | ||
|
|
e536419507 | ||
|
|
1bb6f0527b | ||
|
|
a45b1c7069 | ||
|
|
827326be7b | ||
|
|
89400a80f5 | ||
|
|
e85b521519 | ||
|
|
f890185392 | ||
|
|
745f6c0ef7 | ||
|
|
0182df5ae5 | ||
|
|
7f28f0f1f6 | ||
|
|
45bbe1fa89 | ||
|
|
06efdc4f4d | ||
|
|
0c70b5ad59 | ||
|
|
b90fd157f7 | ||
|
|
7322cb17fa | ||
|
|
1d7723ffc7 | ||
|
|
67b460a404 | ||
|
|
84247bbe28 | ||
|
|
2ebcc590c9 | ||
|
|
69001b3145 | ||
|
|
3accab7365 | ||
|
|
60259539ee | ||
|
|
93399d0827 | ||
|
|
074dd56a01 | ||
|
|
d10d2510b9 | ||
|
|
5613bda4ac | ||
|
|
c5675a98bb | ||
|
|
e355efd962 | ||
|
|
4d7f4556fc | ||
|
|
0486c27a36 | ||
|
|
57105f7480 | ||
|
|
6e8865313f | ||
|
|
6d0b135a98 | ||
|
|
d89f9ba43b | ||
|
|
46f9071a23 | ||
|
|
f85e082a36 | ||
|
|
da78a1bc7a | ||
|
|
2b92aa36d1 | ||
|
|
e4cce2d3e9 | ||
|
|
d15b1aa30c | ||
|
|
65fe29ec00 | ||
|
|
888e036eb4 | ||
|
|
d019dd928c | ||
|
|
dac077f0e6 | ||
|
|
b09a673164 | ||
|
|
79a4dd4085 | ||
|
|
57e929c19c | ||
|
|
27c71355fb | ||
|
|
283b7de6a5 | ||
|
|
a1cb89f3fe | ||
|
|
68f9df5990 | ||
|
|
0135796271 | ||
|
|
799a34a48b | ||
|
|
8378910554 | ||
|
|
7a238b9fbd | ||
|
|
02493ee490 | ||
|
|
7d47b243d6 | ||
|
|
02ea844746 | ||
|
|
0fcf00b55c | ||
|
|
5610ef5863 | ||
|
|
7a687aed28 | ||
|
|
b91aee5810 | ||
|
|
e09b99b54f | ||
|
|
611c7f2c3a | ||
|
|
4e4566ce78 | ||
|
|
43e00611bc | ||
|
|
3c3de7c6b4 | ||
|
|
b0da310a69 | ||
|
|
d26efd2d39 | ||
|
|
f305d504ab | ||
|
|
d3652a1b28 | ||
|
|
51943504d5 | ||
|
|
4d1cdb9efd | ||
|
|
c3b81e01b8 | ||
|
|
99b1f39bd2 | ||
|
|
f23ab037c7 | ||
|
|
0c918dd600 | ||
|
|
a8b090ef08 | ||
|
|
4a38944326 | ||
|
|
b7ff1a7a00 | ||
|
|
d49fed4c55 | ||
|
|
cebb8ebe41 | ||
|
|
3b39a11cde | ||
|
|
ec9f828341 | ||
|
|
332e93417a | ||
|
|
e6b795f34e | ||
|
|
51968b8503 | ||
|
|
80d8b5da48 |
@@ -1,2 +0,0 @@
|
|||||||
((c-mode . ((c-file-style . "stroustrup")
|
|
||||||
(indent-tabs-mode . nil))))
|
|
||||||
154
.gitignore
vendored
154
.gitignore
vendored
@@ -1,75 +1,67 @@
|
|||||||
/config-devices.*
|
config-devices.*
|
||||||
/config-all-devices.*
|
config-all-devices.*
|
||||||
/config-all-disas.*
|
config-all-disas.*
|
||||||
/config-host.*
|
config-host.*
|
||||||
/config-target.*
|
config-target.*
|
||||||
/config.status
|
trace/generated-tracers.h
|
||||||
/config-temp
|
trace/generated-tracers.c
|
||||||
/trace/generated-tracers.h
|
trace/generated-tracers-dtrace.h
|
||||||
/trace/generated-tracers.c
|
trace/generated-tracers-dtrace.dtrace
|
||||||
/trace/generated-tracers-dtrace.h
|
libcacard/trace/generated-tracers.c
|
||||||
/trace/generated-tracers.dtrace
|
|
||||||
/trace/generated-events.h
|
|
||||||
/trace/generated-events.c
|
|
||||||
/trace/generated-helpers-wrappers.h
|
|
||||||
/trace/generated-helpers.h
|
|
||||||
/trace/generated-helpers.c
|
|
||||||
/trace/generated-tcg-tracers.h
|
|
||||||
/trace/generated-ust-provider.h
|
|
||||||
/trace/generated-ust.c
|
|
||||||
/ui/shader/texture-blit-frag.h
|
|
||||||
/ui/shader/texture-blit-vert.h
|
|
||||||
*-timestamp
|
*-timestamp
|
||||||
/*-softmmu
|
*-softmmu
|
||||||
/*-darwin-user
|
*-darwin-user
|
||||||
/*-linux-user
|
*-linux-user
|
||||||
/*-bsd-user
|
*-bsd-user
|
||||||
/ivshmem-client
|
libdis*
|
||||||
/ivshmem-server
|
libuser
|
||||||
/libdis*
|
linux-headers/asm
|
||||||
/libuser
|
qapi-generated
|
||||||
/linux-headers/asm
|
qapi-types.[ch]
|
||||||
/qga/qapi-generated
|
qapi-visit.[ch]
|
||||||
/qapi-generated
|
qmp-commands.h
|
||||||
/qapi-types.[ch]
|
qmp-marshal.c
|
||||||
/qapi-visit.[ch]
|
qemu-doc.html
|
||||||
/qapi-event.[ch]
|
qemu-tech.html
|
||||||
/qmp-commands.h
|
qemu-doc.info
|
||||||
/qmp-introspect.[ch]
|
qemu-tech.info
|
||||||
/qmp-marshal.c
|
qemu.1
|
||||||
/qemu-doc.html
|
qemu.pod
|
||||||
/qemu-tech.html
|
qemu-img.1
|
||||||
/qemu-doc.info
|
qemu-img.pod
|
||||||
/qemu-tech.info
|
qemu-img
|
||||||
/qemu-img
|
qemu-nbd
|
||||||
/qemu-nbd
|
qemu-nbd.8
|
||||||
/qemu-options.def
|
qemu-nbd.pod
|
||||||
/qemu-options.texi
|
qemu-options.def
|
||||||
/qemu-img-cmds.texi
|
qemu-options.texi
|
||||||
/qemu-img-cmds.h
|
qemu-img-cmds.texi
|
||||||
/qemu-io
|
qemu-img-cmds.h
|
||||||
/qemu-ga
|
qemu-io
|
||||||
/qemu-bridge-helper
|
qemu-ga
|
||||||
/qemu-monitor.texi
|
qemu-bridge-helper
|
||||||
/qemu-monitor-info.texi
|
qemu-monitor.texi
|
||||||
/qmp-commands.txt
|
vscclient
|
||||||
/vscclient
|
QMP/qmp-commands.txt
|
||||||
/fsdev/virtfs-proxy-helper
|
test-coroutine
|
||||||
*.[1-9]
|
test-qmp-input-visitor
|
||||||
|
test-qmp-output-visitor
|
||||||
|
test-string-input-visitor
|
||||||
|
test-string-output-visitor
|
||||||
|
test-visitor-serialization
|
||||||
|
fsdev/virtfs-proxy-helper
|
||||||
|
fsdev/virtfs-proxy-helper.1
|
||||||
|
fsdev/virtfs-proxy-helper.pod
|
||||||
|
.gdbinit
|
||||||
*.a
|
*.a
|
||||||
*.aux
|
*.aux
|
||||||
*.cp
|
*.cp
|
||||||
*.dvi
|
*.dvi
|
||||||
*.exe
|
*.exe
|
||||||
*.msi
|
|
||||||
*.dll
|
|
||||||
*.so
|
|
||||||
*.mo
|
|
||||||
*.fn
|
*.fn
|
||||||
*.ky
|
*.ky
|
||||||
*.log
|
*.log
|
||||||
*.pdf
|
*.pdf
|
||||||
*.pod
|
|
||||||
*.cps
|
*.cps
|
||||||
*.fns
|
*.fns
|
||||||
*.kys
|
*.kys
|
||||||
@@ -79,31 +71,29 @@
|
|||||||
*.tp
|
*.tp
|
||||||
*.vr
|
*.vr
|
||||||
*.d
|
*.d
|
||||||
!/scripts/qemu-guest-agent/fsfreeze-hook.d
|
!scripts/qemu-guest-agent/fsfreeze-hook.d
|
||||||
*.o
|
*.o
|
||||||
*.lo
|
*.lo
|
||||||
*.la
|
*.la
|
||||||
*.pc
|
*.pc
|
||||||
.libs
|
.libs
|
||||||
.sdk
|
*.swp
|
||||||
*.gcda
|
*.orig
|
||||||
*.gcno
|
.pc
|
||||||
/pc-bios/bios-pq/status
|
patches
|
||||||
/pc-bios/vgabios-pq/status
|
pc-bios/bios-pq/status
|
||||||
/pc-bios/optionrom/linuxboot.asm
|
pc-bios/vgabios-pq/status
|
||||||
/pc-bios/optionrom/linuxboot.bin
|
pc-bios/optionrom/linuxboot.bin
|
||||||
/pc-bios/optionrom/linuxboot.raw
|
pc-bios/optionrom/linuxboot.raw
|
||||||
/pc-bios/optionrom/linuxboot.img
|
pc-bios/optionrom/linuxboot.img
|
||||||
/pc-bios/optionrom/multiboot.asm
|
pc-bios/optionrom/multiboot.bin
|
||||||
/pc-bios/optionrom/multiboot.bin
|
pc-bios/optionrom/multiboot.raw
|
||||||
/pc-bios/optionrom/multiboot.raw
|
pc-bios/optionrom/multiboot.img
|
||||||
/pc-bios/optionrom/multiboot.img
|
pc-bios/optionrom/kvmvapic.bin
|
||||||
/pc-bios/optionrom/kvmvapic.asm
|
pc-bios/optionrom/kvmvapic.raw
|
||||||
/pc-bios/optionrom/kvmvapic.bin
|
pc-bios/optionrom/kvmvapic.img
|
||||||
/pc-bios/optionrom/kvmvapic.raw
|
pc-bios/s390-ccw/s390-ccw.elf
|
||||||
/pc-bios/optionrom/kvmvapic.img
|
pc-bios/s390-ccw/s390-ccw.img
|
||||||
/pc-bios/s390-ccw/s390-ccw.elf
|
|
||||||
/pc-bios/s390-ccw/s390-ccw.img
|
|
||||||
.stgit-*
|
.stgit-*
|
||||||
cscope.*
|
cscope.*
|
||||||
tags
|
tags
|
||||||
|
|||||||
23
.gitmodules
vendored
23
.gitmodules
vendored
@@ -1,33 +1,24 @@
|
|||||||
[submodule "roms/vgabios"]
|
[submodule "roms/vgabios"]
|
||||||
path = roms/vgabios
|
path = roms/vgabios
|
||||||
url = git://git.qemu-project.org/vgabios.git/
|
url = git://git.qemu.org/vgabios.git/
|
||||||
[submodule "roms/seabios"]
|
[submodule "roms/seabios"]
|
||||||
path = roms/seabios
|
path = roms/seabios
|
||||||
url = git://git.qemu-project.org/seabios.git/
|
url = git://git.qemu.org/seabios.git/
|
||||||
[submodule "roms/SLOF"]
|
[submodule "roms/SLOF"]
|
||||||
path = roms/SLOF
|
path = roms/SLOF
|
||||||
url = git://git.qemu-project.org/SLOF.git
|
url = git://git.qemu.org/SLOF.git
|
||||||
[submodule "roms/ipxe"]
|
[submodule "roms/ipxe"]
|
||||||
path = roms/ipxe
|
path = roms/ipxe
|
||||||
url = git://git.qemu-project.org/ipxe.git
|
url = git://git.qemu.org/ipxe.git
|
||||||
[submodule "roms/openbios"]
|
[submodule "roms/openbios"]
|
||||||
path = roms/openbios
|
path = roms/openbios
|
||||||
url = git://git.qemu-project.org/openbios.git
|
url = git://git.qemu.org/openbios.git
|
||||||
[submodule "roms/openhackware"]
|
|
||||||
path = roms/openhackware
|
|
||||||
url = git://git.qemu-project.org/openhackware.git
|
|
||||||
[submodule "roms/qemu-palcode"]
|
[submodule "roms/qemu-palcode"]
|
||||||
path = roms/qemu-palcode
|
path = roms/qemu-palcode
|
||||||
url = git://github.com/rth7680/qemu-palcode.git
|
url = git://repo.or.cz/qemu-palcode.git
|
||||||
[submodule "roms/sgabios"]
|
[submodule "roms/sgabios"]
|
||||||
path = roms/sgabios
|
path = roms/sgabios
|
||||||
url = git://git.qemu-project.org/sgabios.git
|
url = git://git.qemu.org/sgabios.git
|
||||||
[submodule "pixman"]
|
[submodule "pixman"]
|
||||||
path = pixman
|
path = pixman
|
||||||
url = git://anongit.freedesktop.org/pixman
|
url = git://anongit.freedesktop.org/pixman
|
||||||
[submodule "dtc"]
|
|
||||||
path = dtc
|
|
||||||
url = git://git.qemu-project.org/dtc.git
|
|
||||||
[submodule "roms/u-boot"]
|
|
||||||
path = roms/u-boot
|
|
||||||
url = git://git.qemu-project.org/u-boot.git
|
|
||||||
|
|||||||
3
.mailmap
3
.mailmap
@@ -2,8 +2,7 @@
|
|||||||
# into proper addresses so that they are counted properly in git shortlog output.
|
# into proper addresses so that they are counted properly in git shortlog output.
|
||||||
#
|
#
|
||||||
Andrzej Zaborowski <balrogg@gmail.com> balrog <balrog@c046a42c-6fe2-441c-8c8c-71466251a162>
|
Andrzej Zaborowski <balrogg@gmail.com> balrog <balrog@c046a42c-6fe2-441c-8c8c-71466251a162>
|
||||||
Anthony Liguori <anthony@codemonkey.ws> aliguori <aliguori@c046a42c-6fe2-441c-8c8c-71466251a162>
|
Anthony Liguori <aliguori@us.ibm.com> aliguori <aliguori@c046a42c-6fe2-441c-8c8c-71466251a162>
|
||||||
Anthony Liguori <anthony@codemonkey.ws> Anthony Liguori <aliguori@us.ibm.com>
|
|
||||||
Aurelien Jarno <aurelien@aurel32.net> aurel32 <aurel32@c046a42c-6fe2-441c-8c8c-71466251a162>
|
Aurelien Jarno <aurelien@aurel32.net> aurel32 <aurel32@c046a42c-6fe2-441c-8c8c-71466251a162>
|
||||||
Blue Swirl <blauwirbel@gmail.com> blueswir1 <blueswir1@c046a42c-6fe2-441c-8c8c-71466251a162>
|
Blue Swirl <blauwirbel@gmail.com> blueswir1 <blueswir1@c046a42c-6fe2-441c-8c8c-71466251a162>
|
||||||
Edgar E. Iglesias <edgar.iglesias@gmail.com> edgar_igl <edgar_igl@c046a42c-6fe2-441c-8c8c-71466251a162>
|
Edgar E. Iglesias <edgar.iglesias@gmail.com> edgar_igl <edgar_igl@c046a42c-6fe2-441c-8c8c-71466251a162>
|
||||||
|
|||||||
95
.travis.yml
95
.travis.yml
@@ -1,95 +0,0 @@
|
|||||||
language: c
|
|
||||||
python:
|
|
||||||
- "2.4"
|
|
||||||
compiler:
|
|
||||||
- gcc
|
|
||||||
- clang
|
|
||||||
notifications:
|
|
||||||
irc:
|
|
||||||
channels:
|
|
||||||
- "irc.oftc.net#qemu"
|
|
||||||
on_success: change
|
|
||||||
on_failure: always
|
|
||||||
env:
|
|
||||||
global:
|
|
||||||
- TEST_CMD=""
|
|
||||||
- EXTRA_CONFIG=""
|
|
||||||
# Development packages, EXTRA_PKGS saved for additional builds
|
|
||||||
- CORE_PKGS="libusb-1.0-0-dev libiscsi-dev librados-dev libncurses5-dev"
|
|
||||||
- NET_PKGS="libseccomp-dev libgnutls-dev libssh2-1-dev libspice-server-dev libspice-protocol-dev libnss3-dev"
|
|
||||||
- GUI_PKGS="libgtk-3-dev libvte-2.90-dev libsdl1.2-dev libpng12-dev libpixman-1-dev"
|
|
||||||
- EXTRA_PKGS=""
|
|
||||||
matrix:
|
|
||||||
# Group major targets together with their linux-user counterparts
|
|
||||||
- TARGETS=alpha-softmmu,alpha-linux-user
|
|
||||||
- TARGETS=arm-softmmu,arm-linux-user,armeb-linux-user,aarch64-softmmu,aarch64-linux-user
|
|
||||||
- TARGETS=cris-softmmu,cris-linux-user
|
|
||||||
- TARGETS=i386-softmmu,i386-linux-user,x86_64-softmmu,x86_64-linux-user
|
|
||||||
- TARGETS=m68k-softmmu,m68k-linux-user
|
|
||||||
- TARGETS=microblaze-softmmu,microblazeel-softmmu,microblaze-linux-user,microblazeel-linux-user
|
|
||||||
- TARGETS=mips-softmmu,mips64-softmmu,mips64el-softmmu,mipsel-softmmu
|
|
||||||
- TARGETS=mips-linux-user,mips64-linux-user,mips64el-linux-user,mipsel-linux-user,mipsn32-linux-user,mipsn32el-linux-user
|
|
||||||
- TARGETS=or32-softmmu,or32-linux-user
|
|
||||||
- TARGETS=ppc-softmmu,ppc64-softmmu,ppcemb-softmmu,ppc-linux-user,ppc64-linux-user,ppc64abi32-linux-user,ppc64le-linux-user
|
|
||||||
- TARGETS=s390x-softmmu,s390x-linux-user
|
|
||||||
- TARGETS=sh4-softmmu,sh4eb-softmmu,sh4-linux-user sh4eb-linux-user
|
|
||||||
- TARGETS=sparc-softmmu,sparc64-softmmu,sparc-linux-user,sparc32plus-linux-user,sparc64-linux-user
|
|
||||||
- TARGETS=unicore32-softmmu,unicore32-linux-user
|
|
||||||
# Group remaining softmmu only targets into one build
|
|
||||||
- TARGETS=lm32-softmmu,moxie-softmmu,tricore-softmmu,xtensa-softmmu,xtensaeb-softmmu
|
|
||||||
git:
|
|
||||||
# we want to do this ourselves
|
|
||||||
submodules: false
|
|
||||||
before_install:
|
|
||||||
- wget -O - http://people.linaro.org/~alex.bennee/qemu-submodule-git-seed.tar.xz | tar -xvJ
|
|
||||||
- git submodule update --init --recursive
|
|
||||||
- sudo apt-get update -qq
|
|
||||||
- sudo apt-get install -qq ${CORE_PKGS} ${NET_PKGS} ${GUI_PKGS} ${EXTRA_PKGS}
|
|
||||||
before_script:
|
|
||||||
- ./configure --target-list=${TARGETS} --enable-debug-tcg ${EXTRA_CONFIG}
|
|
||||||
script:
|
|
||||||
- make -j2 && ${TEST_CMD}
|
|
||||||
matrix:
|
|
||||||
# We manually include a number of additional build for non-standard bits
|
|
||||||
include:
|
|
||||||
# Make check target (we only do this once)
|
|
||||||
- env:
|
|
||||||
- TARGETS=alpha-softmmu,arm-softmmu,aarch64-softmmu,cris-softmmu,i386-softmmu,x86_64-softmmu,m68k-softmmu,microblaze-softmmu,microblazeel-softmmu,mips-softmmu,mips64-softmmu,mips64el-softmmu,mipsel-softmmu,or32-softmmu,ppc-softmmu,ppc64-softmmu,ppcemb-softmmu,s390x-softmmu,sh4-softmmu,sh4eb-softmmu,sparc-softmmu,sparc64-softmmu,unicore32-softmmu,unicore32-linux-user,lm32-softmmu,moxie-softmmu,tricore-softmmu,xtensa-softmmu,xtensaeb-softmmu
|
|
||||||
TEST_CMD="make check"
|
|
||||||
compiler: gcc
|
|
||||||
# Debug related options
|
|
||||||
- env: TARGETS=i386-softmmu,x86_64-softmmu
|
|
||||||
EXTRA_CONFIG="--enable-debug"
|
|
||||||
compiler: gcc
|
|
||||||
- env: TARGETS=i386-softmmu,x86_64-softmmu
|
|
||||||
EXTRA_CONFIG="--enable-debug --enable-tcg-interpreter"
|
|
||||||
compiler: gcc
|
|
||||||
# All the extra -dev packages
|
|
||||||
- env: TARGETS=i386-softmmu,x86_64-softmmu
|
|
||||||
EXTRA_PKGS="libaio-dev libcap-ng-dev libattr1-dev libbrlapi-dev uuid-dev libusb-1.0.0-dev"
|
|
||||||
compiler: gcc
|
|
||||||
# Currently configure doesn't force --disable-pie
|
|
||||||
- env: TARGETS=i386-softmmu,x86_64-softmmu
|
|
||||||
EXTRA_CONFIG="--enable-gprof --enable-gcov --disable-pie"
|
|
||||||
compiler: gcc
|
|
||||||
- env: TARGETS=i386-softmmu,x86_64-softmmu
|
|
||||||
EXTRA_PKGS="sparse"
|
|
||||||
EXTRA_CONFIG="--enable-sparse"
|
|
||||||
compiler: gcc
|
|
||||||
# All the trace backends (apart from dtrace)
|
|
||||||
- env: TARGETS=i386-softmmu,x86_64-softmmu
|
|
||||||
EXTRA_CONFIG="--enable-trace-backends=stderr"
|
|
||||||
compiler: gcc
|
|
||||||
- env: TARGETS=i386-softmmu,x86_64-softmmu
|
|
||||||
EXTRA_CONFIG="--enable-trace-backends=simple"
|
|
||||||
compiler: gcc
|
|
||||||
- env: TARGETS=i386-softmmu,x86_64-softmmu
|
|
||||||
EXTRA_CONFIG="--enable-trace-backends=ftrace"
|
|
||||||
compiler: gcc
|
|
||||||
- env: TARGETS=i386-softmmu,x86_64-softmmu
|
|
||||||
EXTRA_PKGS="liblttng-ust-dev liburcu-dev"
|
|
||||||
EXTRA_CONFIG="--enable-trace-backends=ust"
|
|
||||||
compiler: gcc
|
|
||||||
- env: TARGETS=i386-softmmu,x86_64-softmmu
|
|
||||||
EXTRA_CONFIG="--enable-modules"
|
|
||||||
compiler: gcc
|
|
||||||
26
CODING_STYLE
26
CODING_STYLE
@@ -84,29 +84,3 @@ and clarity it comes on a line by itself:
|
|||||||
Rationale: a consistent (except for functions...) bracing style reduces
|
Rationale: a consistent (except for functions...) bracing style reduces
|
||||||
ambiguity and avoids needless churn when lines are added or removed.
|
ambiguity and avoids needless churn when lines are added or removed.
|
||||||
Furthermore, it is the QEMU coding style.
|
Furthermore, it is the QEMU coding style.
|
||||||
|
|
||||||
5. Declarations
|
|
||||||
|
|
||||||
Mixed declarations (interleaving statements and declarations within
|
|
||||||
blocks) are generally not allowed; declarations should be at the beginning
|
|
||||||
of blocks.
|
|
||||||
|
|
||||||
Every now and then, an exception is made for declarations inside a
|
|
||||||
#ifdef or #ifndef block: if the code looks nicer, such declarations can
|
|
||||||
be placed at the top of the block even if there are statements above.
|
|
||||||
On the other hand, however, it's often best to move that #ifdef/#ifndef
|
|
||||||
block to a separate function altogether.
|
|
||||||
|
|
||||||
6. Conditional statements
|
|
||||||
|
|
||||||
When comparing a variable for (in)equality with a constant, list the
|
|
||||||
constant on the right, as in:
|
|
||||||
|
|
||||||
if (a == 1) {
|
|
||||||
/* Reads like: "If a equals 1" */
|
|
||||||
do_something();
|
|
||||||
}
|
|
||||||
|
|
||||||
Rationale: Yoda conditions (as in 'if (1 == a)') are awkward to read.
|
|
||||||
Besides, good compilers already warn users when '==' is mis-typed as '=',
|
|
||||||
even when the constant is on the right.
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
This file documents changes for QEMU releases 0.12 and earlier.
|
This file documents changes for QEMU releases 0.12 and earlier.
|
||||||
For changelog information for later releases, see
|
For changelog information for later releases, see
|
||||||
http://wiki.qemu-project.org/ChangeLog or look at the git history for
|
http://wiki.qemu.org/ChangeLog or look at the git history for
|
||||||
more detailed information.
|
more detailed information.
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
26
HACKING
26
HACKING
@@ -40,23 +40,8 @@ speaking, the size of guest memory can always fit into ram_addr_t but
|
|||||||
it would not be correct to store an actual guest physical address in a
|
it would not be correct to store an actual guest physical address in a
|
||||||
ram_addr_t.
|
ram_addr_t.
|
||||||
|
|
||||||
For CPU virtual addresses there are several possible types.
|
Use target_ulong (or abi_ulong) for CPU virtual addresses, however
|
||||||
vaddr is the best type to use to hold a CPU virtual address in
|
devices should not need to use target_ulong.
|
||||||
target-independent code. It is guaranteed to be large enough to hold a
|
|
||||||
virtual address for any target, and it does not change size from target
|
|
||||||
to target. It is always unsigned.
|
|
||||||
target_ulong is a type the size of a virtual address on the CPU; this means
|
|
||||||
it may be 32 or 64 bits depending on which target is being built. It should
|
|
||||||
therefore be used only in target-specific code, and in some
|
|
||||||
performance-critical built-per-target core code such as the TLB code.
|
|
||||||
There is also a signed version, target_long.
|
|
||||||
abi_ulong is for the *-user targets, and represents a type the size of
|
|
||||||
'void *' in that target's ABI. (This may not be the same as the size of a
|
|
||||||
full CPU virtual address in the case of target ABIs which use 32 bit pointers
|
|
||||||
on 64 bit CPUs, like sparc32plus.) Definitions of structures that must match
|
|
||||||
the target's ABI must use this type for anything that on the target is defined
|
|
||||||
to be an 'unsigned long' or a pointer type.
|
|
||||||
There is also a signed version, abi_long.
|
|
||||||
|
|
||||||
Of course, take all of the above with a grain of salt. If you're about
|
Of course, take all of the above with a grain of salt. If you're about
|
||||||
to use some system interface that requires a type like size_t, pid_t or
|
to use some system interface that requires a type like size_t, pid_t or
|
||||||
@@ -93,15 +78,16 @@ avoided.
|
|||||||
Use of the malloc/free/realloc/calloc/valloc/memalign/posix_memalign
|
Use of the malloc/free/realloc/calloc/valloc/memalign/posix_memalign
|
||||||
APIs is not allowed in the QEMU codebase. Instead of these routines,
|
APIs is not allowed in the QEMU codebase. Instead of these routines,
|
||||||
use the GLib memory allocation routines g_malloc/g_malloc0/g_new/
|
use the GLib memory allocation routines g_malloc/g_malloc0/g_new/
|
||||||
g_new0/g_realloc/g_free or QEMU's qemu_memalign/qemu_blockalign/qemu_vfree
|
g_new0/g_realloc/g_free or QEMU's qemu_vmalloc/qemu_memalign/qemu_vfree
|
||||||
APIs.
|
APIs.
|
||||||
|
|
||||||
Please note that g_malloc will exit on allocation failure, so there
|
Please note that g_malloc will exit on allocation failure, so there
|
||||||
is no need to test for failure (as you would have to with malloc).
|
is no need to test for failure (as you would have to with malloc).
|
||||||
Calling g_malloc with a zero size is valid and will return NULL.
|
Calling g_malloc with a zero size is valid and will return NULL.
|
||||||
|
|
||||||
Memory allocated by qemu_memalign or qemu_blockalign must be freed with
|
Memory allocated by qemu_vmalloc or qemu_memalign must be freed with
|
||||||
qemu_vfree, since breaking this will cause problems on Win32.
|
qemu_vfree, since breaking this will cause problems on Win32 and user
|
||||||
|
emulators.
|
||||||
|
|
||||||
4. String manipulation
|
4. String manipulation
|
||||||
|
|
||||||
|
|||||||
15
LICENSE
15
LICENSE
@@ -1,21 +1,16 @@
|
|||||||
The following points clarify the QEMU license:
|
The following points clarify the QEMU license:
|
||||||
|
|
||||||
1) QEMU as a whole is released under the GNU General Public License,
|
1) QEMU as a whole is released under the GNU General Public License
|
||||||
version 2.
|
|
||||||
|
|
||||||
2) Parts of QEMU have specific licenses which are compatible with the
|
2) Parts of QEMU have specific licenses which are compatible with the
|
||||||
GNU General Public License, version 2. Hence each source file contains
|
GNU General Public License. Hence each source file contains its own
|
||||||
its own licensing information. Source files with no licensing information
|
licensing information.
|
||||||
are released under the GNU General Public License, version 2 or (at your
|
|
||||||
option) any later version.
|
|
||||||
|
|
||||||
As of July 2013, contributions under version 2 of the GNU General Public
|
Many hardware device emulation sources are released under the BSD license.
|
||||||
License (and no later version) are only accepted for the following files
|
|
||||||
or directories: bsd-user/, linux-user/, hw/vfio/, hw/xen/xen_pt*.
|
|
||||||
|
|
||||||
3) The Tiny Code Generator (TCG) is released under the BSD license
|
3) The Tiny Code Generator (TCG) is released under the BSD license
|
||||||
(see license headers in files).
|
(see license headers in files).
|
||||||
|
|
||||||
4) QEMU is a trademark of Fabrice Bellard.
|
4) QEMU is a trademark of Fabrice Bellard.
|
||||||
|
|
||||||
Fabrice Bellard and the QEMU team
|
Fabrice Bellard.
|
||||||
|
|||||||
1118
MAINTAINERS
1118
MAINTAINERS
File diff suppressed because it is too large
Load Diff
395
Makefile
395
Makefile
@@ -3,11 +3,6 @@
|
|||||||
# Always point to the root of the build tree (needs GNU make).
|
# Always point to the root of the build tree (needs GNU make).
|
||||||
BUILD_DIR=$(CURDIR)
|
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
|
# All following code might depend on configuration variables
|
||||||
ifneq ($(wildcard config-host.mak),)
|
ifneq ($(wildcard config-host.mak),)
|
||||||
# Put the all: rule here so that config-host.mak can contain dependencies.
|
# Put the all: rule here so that config-host.mak can contain dependencies.
|
||||||
@@ -24,64 +19,35 @@ seems to have been used for an in-tree build. You can fix this by running \
|
|||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
CONFIG_SOFTMMU := $(if $(filter %-softmmu,$(TARGET_DIRS)),y)
|
|
||||||
CONFIG_USER_ONLY := $(if $(filter %-user,$(TARGET_DIRS)),y)
|
|
||||||
CONFIG_ALL=y
|
|
||||||
-include config-all-devices.mak
|
|
||||||
-include config-all-disas.mak
|
|
||||||
|
|
||||||
include $(SRC_PATH)/rules.mak
|
include $(SRC_PATH)/rules.mak
|
||||||
config-host.mak: $(SRC_PATH)/configure
|
config-host.mak: $(SRC_PATH)/configure
|
||||||
@echo $@ is out-of-date, running configure
|
@echo $@ is out-of-date, running configure
|
||||||
@# TODO: The next lines include code which supports a smooth
|
@sed -n "/.*Configured with/s/[^:]*: //p" $@ | sh
|
||||||
@# transition from old configurations without config.status.
|
|
||||||
@# This code can be removed after QEMU 1.7.
|
|
||||||
@if test -x config.status; then \
|
|
||||||
./config.status; \
|
|
||||||
else \
|
|
||||||
sed -n "/.*Configured with/s/[^:]*: //p" $@ | sh; \
|
|
||||||
fi
|
|
||||||
else
|
else
|
||||||
config-host.mak:
|
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!"
|
@echo "Please call configure before running make!"
|
||||||
@exit 1
|
@exit 1
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
GENERATED_HEADERS = config-host.h qemu-options.def
|
GENERATED_HEADERS = config-host.h qemu-options.def
|
||||||
GENERATED_HEADERS += qmp-commands.h qapi-types.h qapi-visit.h qapi-event.h
|
GENERATED_HEADERS += qmp-commands.h qapi-types.h qapi-visit.h
|
||||||
GENERATED_SOURCES += qmp-marshal.c qapi-types.c qapi-visit.c qapi-event.c
|
GENERATED_SOURCES += qmp-marshal.c qapi-types.c qapi-visit.c
|
||||||
GENERATED_HEADERS += qmp-introspect.h
|
|
||||||
GENERATED_SOURCES += qmp-introspect.c
|
|
||||||
|
|
||||||
GENERATED_HEADERS += trace/generated-events.h
|
|
||||||
GENERATED_SOURCES += trace/generated-events.c
|
|
||||||
|
|
||||||
GENERATED_HEADERS += trace/generated-tracers.h
|
GENERATED_HEADERS += trace/generated-tracers.h
|
||||||
ifeq ($(findstring dtrace,$(TRACE_BACKENDS)),dtrace)
|
ifeq ($(TRACE_BACKEND),dtrace)
|
||||||
GENERATED_HEADERS += trace/generated-tracers-dtrace.h
|
GENERATED_HEADERS += trace/generated-tracers-dtrace.h
|
||||||
endif
|
endif
|
||||||
GENERATED_SOURCES += trace/generated-tracers.c
|
GENERATED_SOURCES += trace/generated-tracers.c
|
||||||
|
|
||||||
GENERATED_HEADERS += trace/generated-tcg-tracers.h
|
|
||||||
|
|
||||||
GENERATED_HEADERS += trace/generated-helpers-wrappers.h
|
|
||||||
GENERATED_HEADERS += trace/generated-helpers.h
|
|
||||||
GENERATED_SOURCES += trace/generated-helpers.c
|
|
||||||
|
|
||||||
ifeq ($(findstring ust,$(TRACE_BACKENDS)),ust)
|
|
||||||
GENERATED_HEADERS += trace/generated-ust-provider.h
|
|
||||||
GENERATED_SOURCES += trace/generated-ust.c
|
|
||||||
endif
|
|
||||||
|
|
||||||
# Don't try to regenerate Makefile or configure
|
# Don't try to regenerate Makefile or configure
|
||||||
# We don't generate any of them
|
# We don't generate any of them
|
||||||
Makefile: ;
|
Makefile: ;
|
||||||
configure: ;
|
configure: ;
|
||||||
|
|
||||||
.PHONY: all clean cscope distclean dvi html info install install-doc \
|
.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))
|
$(call set-vpath, $(SRC_PATH))
|
||||||
|
|
||||||
@@ -90,11 +56,7 @@ LIBS+=-lz $(LIBS_TOOLS)
|
|||||||
HELPERS-$(CONFIG_LINUX) = qemu-bridge-helper$(EXESUF)
|
HELPERS-$(CONFIG_LINUX) = qemu-bridge-helper$(EXESUF)
|
||||||
|
|
||||||
ifdef BUILD_DOCS
|
ifdef BUILD_DOCS
|
||||||
DOCS=qemu-doc.html qemu-tech.html qemu.1 qemu-img.1 qemu-nbd.8 qemu-ga.8
|
DOCS=qemu-doc.html qemu-tech.html qemu.1 qemu-img.1 qemu-nbd.8 QMP/qmp-commands.txt
|
||||||
DOCS+=qmp-commands.txt
|
|
||||||
ifdef CONFIG_LINUX
|
|
||||||
DOCS+=kvm_stat.1
|
|
||||||
endif
|
|
||||||
ifdef CONFIG_VIRTFS
|
ifdef CONFIG_VIRTFS
|
||||||
DOCS+=fsdev/virtfs-proxy-helper.1
|
DOCS+=fsdev/virtfs-proxy-helper.1
|
||||||
endif
|
endif
|
||||||
@@ -104,25 +66,21 @@ endif
|
|||||||
|
|
||||||
SUBDIR_MAKEFLAGS=$(if $(V),,--no-print-directory) BUILD_DIR=$(BUILD_DIR)
|
SUBDIR_MAKEFLAGS=$(if $(V),,--no-print-directory) BUILD_DIR=$(BUILD_DIR)
|
||||||
SUBDIR_DEVICES_MAK=$(patsubst %, %/config-devices.mak, $(TARGET_DIRS))
|
SUBDIR_DEVICES_MAK=$(patsubst %, %/config-devices.mak, $(TARGET_DIRS))
|
||||||
SUBDIR_DEVICES_MAK_DEP=$(patsubst %, %-config-devices.mak.d, $(TARGET_DIRS))
|
SUBDIR_DEVICES_MAK_DEP=$(patsubst %, %/config-devices.mak.d, $(TARGET_DIRS))
|
||||||
|
|
||||||
ifeq ($(SUBDIR_DEVICES_MAK),)
|
ifeq ($(SUBDIR_DEVICES_MAK),)
|
||||||
config-all-devices.mak:
|
config-all-devices.mak:
|
||||||
$(call quiet-command,echo '# no devices' > $@," GEN $@")
|
$(call quiet-command,echo '# no devices' > $@," GEN $@")
|
||||||
else
|
else
|
||||||
config-all-devices.mak: $(SUBDIR_DEVICES_MAK)
|
config-all-devices.mak: $(SUBDIR_DEVICES_MAK)
|
||||||
$(call quiet-command, sed -n \
|
$(call quiet-command,cat $(SUBDIR_DEVICES_MAK) | grep =y | sort -u > $@," GEN $@")
|
||||||
's|^\([^=]*\)=\(.*\)$$|\1:=$$(findstring y,$$(\1)\2)|p' \
|
|
||||||
$(SUBDIR_DEVICES_MAK) | sort -u > $@, \
|
|
||||||
" GEN $@")
|
|
||||||
endif
|
endif
|
||||||
|
|
||||||
-include $(SUBDIR_DEVICES_MAK_DEP)
|
-include $(SUBDIR_DEVICES_MAK_DEP)
|
||||||
|
|
||||||
%/config-devices.mak: default-configs/%.mak
|
%/config-devices.mak: default-configs/%.mak
|
||||||
$(call quiet-command, \
|
$(call quiet-command,$(SHELL) $(SRC_PATH)/scripts/make_device_config.sh $@ $<, " GEN $@")
|
||||||
$(SHELL) $(SRC_PATH)/scripts/make_device_config.sh $< $*-config-devices.mak.d $@ > $@.tmp, " GEN $@.tmp")
|
@if test -f $@; then \
|
||||||
$(call quiet-command, if test -f $@; then \
|
|
||||||
if cmp -s $@.old $@; then \
|
if cmp -s $@.old $@; then \
|
||||||
mv $@.tmp $@; \
|
mv $@.tmp $@; \
|
||||||
cp -p $@ $@.old; \
|
cp -p $@ $@.old; \
|
||||||
@@ -138,36 +96,26 @@ endif
|
|||||||
else \
|
else \
|
||||||
mv $@.tmp $@; \
|
mv $@.tmp $@; \
|
||||||
cp -p $@ $@.old; \
|
cp -p $@ $@.old; \
|
||||||
fi, " GEN $@");
|
fi
|
||||||
|
|
||||||
defconfig:
|
defconfig:
|
||||||
rm -f config-all-devices.mak $(SUBDIR_DEVICES_MAK)
|
rm -f config-all-devices.mak $(SUBDIR_DEVICES_MAK)
|
||||||
|
|
||||||
|
-include config-all-devices.mak
|
||||||
|
-include config-all-disas.mak
|
||||||
|
CONFIG_SOFTMMU := $(if $(filter %-softmmu,$(TARGET_DIRS)),y)
|
||||||
|
CONFIG_USER_ONLY := $(if $(filter %-user,$(TARGET_DIRS)),y)
|
||||||
|
CONFIG_ALL=y
|
||||||
|
|
||||||
ifneq ($(wildcard config-host.mak),)
|
ifneq ($(wildcard config-host.mak),)
|
||||||
include $(SRC_PATH)/Makefile.objs
|
include $(SRC_PATH)/Makefile.objs
|
||||||
endif
|
|
||||||
|
|
||||||
dummy := $(call unnest-vars,, \
|
|
||||||
stub-obj-y \
|
|
||||||
util-obj-y \
|
|
||||||
qga-obj-y \
|
|
||||||
ivshmem-client-obj-y \
|
|
||||||
ivshmem-server-obj-y \
|
|
||||||
qga-vss-dll-obj-y \
|
|
||||||
block-obj-y \
|
|
||||||
block-obj-m \
|
|
||||||
crypto-obj-y \
|
|
||||||
crypto-aes-obj-y \
|
|
||||||
qom-obj-y \
|
|
||||||
io-obj-y \
|
|
||||||
common-obj-y \
|
|
||||||
common-obj-m)
|
|
||||||
|
|
||||||
ifneq ($(wildcard config-host.mak),)
|
|
||||||
include $(SRC_PATH)/tests/Makefile
|
include $(SRC_PATH)/tests/Makefile
|
||||||
endif
|
endif
|
||||||
|
ifeq ($(CONFIG_SMARTCARD_NSS),y)
|
||||||
|
include $(SRC_PATH)/libcacard/Makefile
|
||||||
|
endif
|
||||||
|
|
||||||
all: $(DOCS) $(TOOLS) $(HELPERS-y) recurse-all modules
|
all: $(DOCS) $(TOOLS) $(HELPERS-y) recurse-all
|
||||||
|
|
||||||
config-host.h: config-host.h-timestamp
|
config-host.h: config-host.h-timestamp
|
||||||
config-host.h-timestamp: config-host.mak
|
config-host.h-timestamp: config-host.mak
|
||||||
@@ -175,12 +123,6 @@ qemu-options.def: $(SRC_PATH)/qemu-options.hx
|
|||||||
$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@," GEN $@")
|
$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@," GEN $@")
|
||||||
|
|
||||||
SUBDIR_RULES=$(patsubst %,subdir-%, $(TARGET_DIRS))
|
SUBDIR_RULES=$(patsubst %,subdir-%, $(TARGET_DIRS))
|
||||||
SOFTMMU_SUBDIR_RULES=$(filter %-softmmu,$(SUBDIR_RULES))
|
|
||||||
|
|
||||||
$(SOFTMMU_SUBDIR_RULES): $(block-obj-y)
|
|
||||||
$(SOFTMMU_SUBDIR_RULES): $(crypto-obj-y)
|
|
||||||
$(SOFTMMU_SUBDIR_RULES): $(io-obj-y)
|
|
||||||
$(SOFTMMU_SUBDIR_RULES): config-all-devices.mak
|
|
||||||
|
|
||||||
subdir-%:
|
subdir-%:
|
||||||
$(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C $* V="$(V)" TARGET_DIR="$*/" all,)
|
$(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C $* V="$(V)" TARGET_DIR="$*/" all,)
|
||||||
@@ -194,17 +136,7 @@ pixman/Makefile: $(SRC_PATH)/pixman/configure
|
|||||||
$(SRC_PATH)/pixman/configure:
|
$(SRC_PATH)/pixman/configure:
|
||||||
(cd $(SRC_PATH)/pixman; autoreconf -v --install)
|
(cd $(SRC_PATH)/pixman; autoreconf -v --install)
|
||||||
|
|
||||||
DTC_MAKE_ARGS=-I$(SRC_PATH)/dtc VPATH=$(SRC_PATH)/dtc -C dtc V="$(V)" LIBFDT_srcdir=$(SRC_PATH)/dtc/libfdt
|
$(SUBDIR_RULES): libqemuutil.a libqemustub.a $(common-obj-y)
|
||||||
DTC_CFLAGS=$(CFLAGS) $(QEMU_CFLAGS)
|
|
||||||
DTC_CPPFLAGS=-I$(BUILD_DIR)/dtc -I$(SRC_PATH)/dtc -I$(SRC_PATH)/dtc/libfdt
|
|
||||||
|
|
||||||
subdir-dtc:dtc/libfdt dtc/tests
|
|
||||||
$(call quiet-command,$(MAKE) $(DTC_MAKE_ARGS) CPPFLAGS="$(DTC_CPPFLAGS)" CFLAGS="$(DTC_CFLAGS)" LDFLAGS="$(LDFLAGS)" ARFLAGS="$(ARFLAGS)" CC="$(CC)" AR="$(AR)" LD="$(LD)" $(SUBDIR_MAKEFLAGS) libfdt/libfdt.a,)
|
|
||||||
|
|
||||||
dtc/%:
|
|
||||||
mkdir -p $@
|
|
||||||
|
|
||||||
$(SUBDIR_RULES): libqemuutil.a libqemustub.a $(common-obj-y) $(qom-obj-y) $(crypto-aes-obj-$(CONFIG_USER_ONLY))
|
|
||||||
|
|
||||||
ROMSUBDIR_RULES=$(patsubst %,romsubdir-%, $(ROMS))
|
ROMSUBDIR_RULES=$(patsubst %,romsubdir-%, $(ROMS))
|
||||||
romsubdir-%:
|
romsubdir-%:
|
||||||
@@ -214,12 +146,13 @@ ALL_SUBDIRS=$(TARGET_DIRS) $(patsubst %,pc-bios/%, $(ROMS))
|
|||||||
|
|
||||||
recurse-all: $(SUBDIR_RULES) $(ROMSUBDIR_RULES)
|
recurse-all: $(SUBDIR_RULES) $(ROMSUBDIR_RULES)
|
||||||
|
|
||||||
$(BUILD_DIR)/version.o: $(SRC_PATH)/version.rc config-host.h | $(BUILD_DIR)/version.lo
|
bt-host.o: QEMU_CFLAGS += $(BLUEZ_CFLAGS)
|
||||||
$(call quiet-command,$(WINDRES) -I$(BUILD_DIR) -o $@ $<," RC version.o")
|
|
||||||
$(BUILD_DIR)/version.lo: $(SRC_PATH)/version.rc config-host.h
|
|
||||||
$(call quiet-command,$(WINDRES) -I$(BUILD_DIR) -o $@ $<," RC version.lo")
|
|
||||||
|
|
||||||
Makefile: $(version-obj-y) $(version-lobj-y)
|
version.o: $(SRC_PATH)/version.rc config-host.h
|
||||||
|
$(call quiet-command,$(WINDRES) -I. -o $@ $<," RC $(TARGET_DIR)$@")
|
||||||
|
|
||||||
|
version-obj-$(CONFIG_WIN32) += version.o
|
||||||
|
Makefile: $(version-obj-y)
|
||||||
|
|
||||||
######################################################################
|
######################################################################
|
||||||
# Build libraries
|
# Build libraries
|
||||||
@@ -227,16 +160,13 @@ Makefile: $(version-obj-y) $(version-lobj-y)
|
|||||||
libqemustub.a: $(stub-obj-y)
|
libqemustub.a: $(stub-obj-y)
|
||||||
libqemuutil.a: $(util-obj-y)
|
libqemuutil.a: $(util-obj-y)
|
||||||
|
|
||||||
block-modules = $(foreach o,$(block-obj-m),"$(basename $(subst /,-,$o))",) NULL
|
|
||||||
util/module.o-cflags = -D'CONFIG_BLOCK_MODULES=$(block-modules)'
|
|
||||||
|
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
qemu-img.o: qemu-img-cmds.h
|
qemu-img.o: qemu-img-cmds.h
|
||||||
|
|
||||||
qemu-img$(EXESUF): qemu-img.o $(block-obj-y) $(crypto-obj-y) $(qom-obj-y) libqemuutil.a libqemustub.a
|
qemu-img$(EXESUF): qemu-img.o $(block-obj-y) libqemuutil.a libqemustub.a
|
||||||
qemu-nbd$(EXESUF): qemu-nbd.o $(block-obj-y) $(crypto-obj-y) $(qom-obj-y) libqemuutil.a libqemustub.a
|
qemu-nbd$(EXESUF): qemu-nbd.o $(block-obj-y) libqemuutil.a libqemustub.a
|
||||||
qemu-io$(EXESUF): qemu-io.o $(block-obj-y) $(crypto-obj-y) $(qom-obj-y) libqemuutil.a libqemustub.a
|
qemu-io$(EXESUF): qemu-io.o cmd.o $(block-obj-y) libqemuutil.a libqemustub.a
|
||||||
|
|
||||||
qemu-bridge-helper$(EXESUF): qemu-bridge-helper.o
|
qemu-bridge-helper$(EXESUF): qemu-bridge-helper.o
|
||||||
|
|
||||||
@@ -255,50 +185,23 @@ 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 :\
|
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)
|
$(SRC_PATH)/qga/qapi-schema.json $(SRC_PATH)/scripts/qapi-types.py $(qapi-py)
|
||||||
$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-types.py \
|
$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-types.py $(gen-out-type) -o qga/qapi-generated -p "qga-" < $<, " GEN $@")
|
||||||
$(gen-out-type) -o qga/qapi-generated -p "qga-" $<, \
|
|
||||||
" GEN $@")
|
|
||||||
qga/qapi-generated/qga-qapi-visit.c qga/qapi-generated/qga-qapi-visit.h :\
|
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)
|
$(SRC_PATH)/qga/qapi-schema.json $(SRC_PATH)/scripts/qapi-visit.py $(qapi-py)
|
||||||
$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-visit.py \
|
$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-visit.py $(gen-out-type) -o qga/qapi-generated -p "qga-" < $<, " GEN $@")
|
||||||
$(gen-out-type) -o qga/qapi-generated -p "qga-" $<, \
|
|
||||||
" GEN $@")
|
|
||||||
qga/qapi-generated/qga-qmp-commands.h qga/qapi-generated/qga-qmp-marshal.c :\
|
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)
|
$(SRC_PATH)/qga/qapi-schema.json $(SRC_PATH)/scripts/qapi-commands.py $(qapi-py)
|
||||||
$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-commands.py \
|
$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-commands.py $(gen-out-type) -o qga/qapi-generated -p "qga-" < $<, " GEN $@")
|
||||||
$(gen-out-type) -o qga/qapi-generated -p "qga-" $<, \
|
|
||||||
" GEN $@")
|
|
||||||
|
|
||||||
qapi-modules = $(SRC_PATH)/qapi-schema.json $(SRC_PATH)/qapi/common.json \
|
|
||||||
$(SRC_PATH)/qapi/block.json $(SRC_PATH)/qapi/block-core.json \
|
|
||||||
$(SRC_PATH)/qapi/event.json $(SRC_PATH)/qapi/introspect.json \
|
|
||||||
$(SRC_PATH)/qapi/crypto.json
|
|
||||||
|
|
||||||
qapi-types.c qapi-types.h :\
|
qapi-types.c qapi-types.h :\
|
||||||
$(qapi-modules) $(SRC_PATH)/scripts/qapi-types.py $(qapi-py)
|
$(SRC_PATH)/qapi-schema.json $(SRC_PATH)/scripts/qapi-types.py $(qapi-py)
|
||||||
$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-types.py \
|
$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-types.py $(gen-out-type) -o "." < $<, " GEN $@")
|
||||||
$(gen-out-type) -o "." -b $<, \
|
|
||||||
" GEN $@")
|
|
||||||
qapi-visit.c qapi-visit.h :\
|
qapi-visit.c qapi-visit.h :\
|
||||||
$(qapi-modules) $(SRC_PATH)/scripts/qapi-visit.py $(qapi-py)
|
$(SRC_PATH)/qapi-schema.json $(SRC_PATH)/scripts/qapi-visit.py $(qapi-py)
|
||||||
$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-visit.py \
|
$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-visit.py $(gen-out-type) -o "." < $<, " GEN $@")
|
||||||
$(gen-out-type) -o "." -b $<, \
|
|
||||||
" 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 $@")
|
|
||||||
qmp-commands.h qmp-marshal.c :\
|
qmp-commands.h qmp-marshal.c :\
|
||||||
$(qapi-modules) $(SRC_PATH)/scripts/qapi-commands.py $(qapi-py)
|
$(SRC_PATH)/qapi-schema.json $(SRC_PATH)/scripts/qapi-commands.py $(qapi-py)
|
||||||
$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-commands.py \
|
$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-commands.py $(gen-out-type) -m -o "." < $<, " GEN $@")
|
||||||
$(gen-out-type) -o "." -m $<, \
|
|
||||||
" GEN $@")
|
|
||||||
qmp-introspect.h qmp-introspect.c :\
|
|
||||||
$(qapi-modules) $(SRC_PATH)/scripts/qapi-introspect.py $(qapi-py)
|
|
||||||
$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-introspect.py \
|
|
||||||
$(gen-out-type) -o "." $<, \
|
|
||||||
" GEN $@")
|
|
||||||
|
|
||||||
QGALIB_GEN=$(addprefix qga/qapi-generated/, qga-qapi-types.h qga-qapi-visit.h qga-qmp-commands.h)
|
QGALIB_GEN=$(addprefix qga/qapi-generated/, qga-qapi-types.h qga-qapi-visit.h qga-qmp-commands.h)
|
||||||
$(qga-obj-y) qemu-ga.o: $(QGALIB_GEN)
|
$(qga-obj-y) qemu-ga.o: $(QGALIB_GEN)
|
||||||
@@ -306,44 +209,15 @@ $(qga-obj-y) qemu-ga.o: $(QGALIB_GEN)
|
|||||||
qemu-ga$(EXESUF): $(qga-obj-y) libqemuutil.a libqemustub.a
|
qemu-ga$(EXESUF): $(qga-obj-y) libqemuutil.a libqemustub.a
|
||||||
$(call LINK, $^)
|
$(call LINK, $^)
|
||||||
|
|
||||||
ifdef QEMU_GA_MSI_ENABLED
|
|
||||||
QEMU_GA_MSI=qemu-ga-$(ARCH).msi
|
|
||||||
|
|
||||||
msi: $(QEMU_GA_MSI)
|
|
||||||
|
|
||||||
$(QEMU_GA_MSI): qemu-ga.exe $(QGA_VSS_PROVIDER)
|
|
||||||
|
|
||||||
$(QEMU_GA_MSI): config-host.mak
|
|
||||||
|
|
||||||
$(QEMU_GA_MSI): $(SRC_PATH)/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)" BUILD_DIR="$(BUILD_DIR)" \
|
|
||||||
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
|
|
||||||
|
|
||||||
ifneq ($(EXESUF),)
|
|
||||||
.PHONY: qemu-ga
|
|
||||||
qemu-ga: qemu-ga$(EXESUF) $(QGA_VSS_PROVIDER) $(QEMU_GA_MSI)
|
|
||||||
endif
|
|
||||||
|
|
||||||
ivshmem-client$(EXESUF): $(ivshmem-client-obj-y)
|
|
||||||
$(call LINK, $^)
|
|
||||||
ivshmem-server$(EXESUF): $(ivshmem-server-obj-y) libqemuutil.a libqemustub.a
|
|
||||||
$(call LINK, $^)
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
# avoid old build problems by removing potentially incorrect old files
|
# 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 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 qemu-options.def
|
||||||
rm -f *.msi
|
find . -name '*.[oda]' -type f -exec rm -f {} +
|
||||||
find . \( -name '*.l[oa]' -o -name '*.so' -o -name '*.dll' -o -name '*.mo' -o -name '*.[oda]' \) -type f -exec rm {} +
|
find . -name '*.l[oa]' -type f -exec rm -f {} +
|
||||||
rm -f $(filter-out %.tlb,$(TOOLS)) $(HELPERS-y) qemu-ga TAGS cscope.* *.pod *~ */*~
|
rm -f $(TOOLS) $(HELPERS-y) qemu-ga TAGS cscope.* *.pod *~ */*~
|
||||||
rm -f fsdev/*.pod
|
rm -Rf .libs
|
||||||
rm -rf .libs */.libs
|
|
||||||
rm -f qemu-img-cmds.h
|
rm -f qemu-img-cmds.h
|
||||||
rm -f ui/shader/*-vert.h ui/shader/*-frag.h
|
|
||||||
@# May not be present in GENERATED_HEADERS
|
@# May not be present in GENERATED_HEADERS
|
||||||
rm -f trace/generated-tracers-dtrace.dtrace*
|
rm -f trace/generated-tracers-dtrace.dtrace*
|
||||||
rm -f trace/generated-tracers-dtrace.h*
|
rm -f trace/generated-tracers-dtrace.h*
|
||||||
@@ -351,6 +225,7 @@ clean:
|
|||||||
rm -f $(foreach f,$(GENERATED_SOURCES),$(f) $(f)-timestamp)
|
rm -f $(foreach f,$(GENERATED_SOURCES),$(f) $(f)-timestamp)
|
||||||
rm -rf qapi-generated
|
rm -rf qapi-generated
|
||||||
rm -rf qga/qapi-generated
|
rm -rf qga/qapi-generated
|
||||||
|
$(MAKE) -C tests/tcg clean
|
||||||
for d in $(ALL_SUBDIRS); do \
|
for d in $(ALL_SUBDIRS); do \
|
||||||
if test -d $$d; then $(MAKE) -C $$d $@ || exit 1; fi; \
|
if test -d $$d; then $(MAKE) -C $$d $@ || exit 1; fi; \
|
||||||
rm -f $$d/qemu-options.def; \
|
rm -f $$d/qemu-options.def; \
|
||||||
@@ -364,9 +239,8 @@ qemu-%.tar.bz2:
|
|||||||
$(SRC_PATH)/scripts/make-release "$(SRC_PATH)" "$(patsubst qemu-%.tar.bz2,%,$@)"
|
$(SRC_PATH)/scripts/make-release "$(SRC_PATH)" "$(patsubst qemu-%.tar.bz2,%,$@)"
|
||||||
|
|
||||||
distclean: clean
|
distclean: clean
|
||||||
rm -f config-host.mak config-host.h* config-host.ld $(DOCS) qemu-options.texi qemu-img-cmds.texi qemu-monitor.texi qemu-monitor-info.texi
|
rm -f config-host.mak config-host.h* config-host.ld $(DOCS) qemu-options.texi qemu-img-cmds.texi qemu-monitor.texi
|
||||||
rm -f config-all-devices.mak config-all-disas.mak config.status
|
rm -f config-all-devices.mak config-all-disas.mak
|
||||||
rm -f po/*.mo tests/qemu-iotests/common.env
|
|
||||||
rm -f roms/seabios/config.mak roms/vgabios/config.mak
|
rm -f roms/seabios/config.mak roms/vgabios/config.mak
|
||||||
rm -f qemu-doc.info qemu-doc.aux qemu-doc.cp qemu-doc.cps qemu-doc.dvi
|
rm -f qemu-doc.info qemu-doc.aux qemu-doc.cp qemu-doc.cps qemu-doc.dvi
|
||||||
rm -f qemu-doc.fn qemu-doc.fns qemu-doc.info qemu-doc.ky qemu-doc.kys
|
rm -f qemu-doc.fn qemu-doc.fns qemu-doc.info qemu-doc.ky qemu-doc.kys
|
||||||
@@ -378,32 +252,26 @@ distclean: clean
|
|||||||
for d in $(TARGET_DIRS); do \
|
for d in $(TARGET_DIRS); do \
|
||||||
rm -rf $$d || exit 1 ; \
|
rm -rf $$d || exit 1 ; \
|
||||||
done
|
done
|
||||||
rm -Rf .sdk
|
if test -f pixman/config.log; then make -C pixman distclean; fi
|
||||||
if test -f pixman/config.log; then $(MAKE) -C pixman distclean; fi
|
|
||||||
if test -f dtc/version_gen.h; then $(MAKE) $(DTC_MAKE_ARGS) clean; fi
|
|
||||||
|
|
||||||
KEYMAPS=da en-gb et fr fr-ch is lt modifiers no pt-br sv \
|
KEYMAPS=da en-gb et fr fr-ch is lt modifiers no pt-br sv \
|
||||||
ar de en-us fi fr-be hr it lv nl pl ru th \
|
ar de en-us fi fr-be hr it lv nl pl ru th \
|
||||||
common de-ch es fo fr-ca hu ja mk nl-be pt sl tr \
|
common de-ch es fo fr-ca hu ja mk nl-be pt sl tr \
|
||||||
bepo cz
|
bepo
|
||||||
|
|
||||||
ifdef INSTALL_BLOBS
|
ifdef INSTALL_BLOBS
|
||||||
BLOBS=bios.bin bios-256k.bin sgabios.bin vgabios.bin vgabios-cirrus.bin \
|
BLOBS=bios.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 \
|
acpi-dsdt.aml q35-acpi-dsdt.aml \
|
||||||
ppc_rom.bin openbios-sparc32 openbios-sparc64 openbios-ppc QEMU,tcx.bin QEMU,cgthree.bin \
|
ppc_rom.bin openbios-sparc32 openbios-sparc64 openbios-ppc \
|
||||||
pxe-e1000.rom pxe-eepro100.rom pxe-ne2k_pci.rom \
|
pxe-e1000.rom pxe-eepro100.rom pxe-ne2k_pci.rom \
|
||||||
pxe-pcnet.rom pxe-rtl8139.rom pxe-virtio.rom \
|
pxe-pcnet.rom pxe-rtl8139.rom pxe-virtio.rom \
|
||||||
efi-e1000.rom efi-eepro100.rom efi-ne2k_pci.rom \
|
qemu-icon.bmp \
|
||||||
efi-pcnet.rom efi-rtl8139.rom efi-virtio.rom \
|
|
||||||
qemu-icon.bmp qemu_logo_no_text.svg \
|
|
||||||
bamboo.dtb petalogix-s3adsp1800.dtb petalogix-ml605.dtb \
|
bamboo.dtb petalogix-s3adsp1800.dtb petalogix-ml605.dtb \
|
||||||
multiboot.bin linuxboot.bin kvmvapic.bin \
|
multiboot.bin linuxboot.bin kvmvapic.bin \
|
||||||
s390-zipl.rom \
|
s390-zipl.rom \
|
||||||
s390-ccw.img \
|
|
||||||
spapr-rtas.bin slof.bin \
|
spapr-rtas.bin slof.bin \
|
||||||
palcode-clipper \
|
palcode-clipper
|
||||||
u-boot.e500
|
|
||||||
else
|
else
|
||||||
BLOBS=
|
BLOBS=
|
||||||
endif
|
endif
|
||||||
@@ -411,19 +279,13 @@ endif
|
|||||||
install-doc: $(DOCS)
|
install-doc: $(DOCS)
|
||||||
$(INSTALL_DIR) "$(DESTDIR)$(qemu_docdir)"
|
$(INSTALL_DIR) "$(DESTDIR)$(qemu_docdir)"
|
||||||
$(INSTALL_DATA) qemu-doc.html qemu-tech.html "$(DESTDIR)$(qemu_docdir)"
|
$(INSTALL_DATA) qemu-doc.html qemu-tech.html "$(DESTDIR)$(qemu_docdir)"
|
||||||
$(INSTALL_DATA) qmp-commands.txt "$(DESTDIR)$(qemu_docdir)"
|
$(INSTALL_DATA) QMP/qmp-commands.txt "$(DESTDIR)$(qemu_docdir)"
|
||||||
ifdef CONFIG_POSIX
|
ifdef CONFIG_POSIX
|
||||||
$(INSTALL_DIR) "$(DESTDIR)$(mandir)/man1"
|
$(INSTALL_DIR) "$(DESTDIR)$(mandir)/man1"
|
||||||
$(INSTALL_DATA) qemu.1 "$(DESTDIR)$(mandir)/man1"
|
$(INSTALL_DATA) qemu.1 qemu-img.1 "$(DESTDIR)$(mandir)/man1"
|
||||||
ifneq ($(TOOLS),)
|
|
||||||
$(INSTALL_DATA) qemu-img.1 "$(DESTDIR)$(mandir)/man1"
|
|
||||||
$(INSTALL_DIR) "$(DESTDIR)$(mandir)/man8"
|
$(INSTALL_DIR) "$(DESTDIR)$(mandir)/man8"
|
||||||
$(INSTALL_DATA) qemu-nbd.8 "$(DESTDIR)$(mandir)/man8"
|
$(INSTALL_DATA) qemu-nbd.8 "$(DESTDIR)$(mandir)/man8"
|
||||||
endif
|
endif
|
||||||
ifneq (,$(findstring qemu-ga,$(TOOLS)))
|
|
||||||
$(INSTALL_DATA) qemu-ga.8 "$(DESTDIR)$(mandir)/man8"
|
|
||||||
endif
|
|
||||||
endif
|
|
||||||
ifdef CONFIG_VIRTFS
|
ifdef CONFIG_VIRTFS
|
||||||
$(INSTALL_DIR) "$(DESTDIR)$(mandir)/man1"
|
$(INSTALL_DIR) "$(DESTDIR)$(mandir)/man1"
|
||||||
$(INSTALL_DATA) fsdev/virtfs-proxy-helper.1 "$(DESTDIR)$(mandir)/man1"
|
$(INSTALL_DATA) fsdev/virtfs-proxy-helper.1 "$(DESTDIR)$(mandir)/man1"
|
||||||
@@ -432,81 +294,46 @@ endif
|
|||||||
install-datadir:
|
install-datadir:
|
||||||
$(INSTALL_DIR) "$(DESTDIR)$(qemu_datadir)"
|
$(INSTALL_DIR) "$(DESTDIR)$(qemu_datadir)"
|
||||||
|
|
||||||
install-localstatedir:
|
install-confdir:
|
||||||
ifdef CONFIG_POSIX
|
$(INSTALL_DIR) "$(DESTDIR)$(qemu_confdir)"
|
||||||
ifneq (,$(findstring qemu-ga,$(TOOLS)))
|
|
||||||
$(INSTALL_DIR) "$(DESTDIR)$(qemu_localstatedir)"/run
|
|
||||||
endif
|
|
||||||
endif
|
|
||||||
|
|
||||||
|
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: all $(if $(BUILD_DOCS),install-doc) install-sysconfig install-datadir
|
||||||
install-datadir install-localstatedir
|
$(INSTALL_DIR) "$(DESTDIR)$(bindir)"
|
||||||
ifneq ($(TOOLS),)
|
ifneq ($(TOOLS),)
|
||||||
$(call install-prog,$(subst qemu-ga,qemu-ga$(EXESUF),$(TOOLS)),$(DESTDIR)$(bindir))
|
$(INSTALL_PROG) $(STRIP_OPT) $(TOOLS) "$(DESTDIR)$(bindir)"
|
||||||
endif
|
|
||||||
ifneq ($(CONFIG_MODULES),)
|
|
||||||
$(INSTALL_DIR) "$(DESTDIR)$(qemu_moddir)"
|
|
||||||
for s in $(modules-m:.mo=$(DSOSUF)); do \
|
|
||||||
t="$(DESTDIR)$(qemu_moddir)/$$(echo $$s | tr / -)"; \
|
|
||||||
$(INSTALL_LIB) $$s "$$t"; \
|
|
||||||
test -z "$(STRIP)" || $(STRIP) "$$t"; \
|
|
||||||
done
|
|
||||||
endif
|
endif
|
||||||
ifneq ($(HELPERS-y),)
|
ifneq ($(HELPERS-y),)
|
||||||
$(call install-prog,$(HELPERS-y),$(DESTDIR)$(libexecdir))
|
$(INSTALL_DIR) "$(DESTDIR)$(libexecdir)"
|
||||||
|
$(INSTALL_PROG) $(STRIP_OPT) $(HELPERS-y) "$(DESTDIR)$(libexecdir)"
|
||||||
endif
|
endif
|
||||||
ifneq ($(BLOBS),)
|
ifneq ($(BLOBS),)
|
||||||
set -e; for x in $(BLOBS); do \
|
set -e; for x in $(BLOBS); do \
|
||||||
$(INSTALL_DATA) $(SRC_PATH)/pc-bios/$$x "$(DESTDIR)$(qemu_datadir)"; \
|
$(INSTALL_DATA) $(SRC_PATH)/pc-bios/$$x "$(DESTDIR)$(qemu_datadir)"; \
|
||||||
done
|
done
|
||||||
endif
|
|
||||||
ifeq ($(CONFIG_GTK),y)
|
|
||||||
$(MAKE) -C po $@
|
|
||||||
endif
|
endif
|
||||||
$(INSTALL_DIR) "$(DESTDIR)$(qemu_datadir)/keymaps"
|
$(INSTALL_DIR) "$(DESTDIR)$(qemu_datadir)/keymaps"
|
||||||
set -e; for x in $(KEYMAPS); do \
|
set -e; for x in $(KEYMAPS); do \
|
||||||
$(INSTALL_DATA) $(SRC_PATH)/pc-bios/keymaps/$$x "$(DESTDIR)$(qemu_datadir)/keymaps"; \
|
$(INSTALL_DATA) $(SRC_PATH)/pc-bios/keymaps/$$x "$(DESTDIR)$(qemu_datadir)/keymaps"; \
|
||||||
done
|
done
|
||||||
$(INSTALL_DATA) $(SRC_PATH)/trace-events "$(DESTDIR)$(qemu_datadir)/trace-events"
|
|
||||||
for d in $(TARGET_DIRS); do \
|
for d in $(TARGET_DIRS); do \
|
||||||
$(MAKE) $(SUBDIR_MAKEFLAGS) TARGET_DIR=$$d/ -C $$d $@ || exit 1 ; \
|
$(MAKE) -C $$d $@ || exit 1 ; \
|
||||||
done
|
done
|
||||||
|
|
||||||
# various test targets
|
# various test targets
|
||||||
test speed: all
|
test speed: all
|
||||||
$(MAKE) -C tests/tcg $@
|
$(MAKE) -C tests/tcg $@
|
||||||
|
|
||||||
.PHONY: ctags
|
|
||||||
ctags:
|
|
||||||
rm -f $@
|
|
||||||
find "$(SRC_PATH)" -name '*.[hc]' -exec ctags --append {} +
|
|
||||||
|
|
||||||
.PHONY: TAGS
|
.PHONY: TAGS
|
||||||
TAGS:
|
TAGS:
|
||||||
rm -f $@
|
find "$(SRC_PATH)" -name '*.[hc]' -print0 | xargs -0 etags
|
||||||
find "$(SRC_PATH)" -name '*.[hc]' -exec etags --append {} +
|
|
||||||
|
|
||||||
cscope:
|
cscope:
|
||||||
rm -f "$(SRC_PATH)"/cscope.*
|
rm -f ./cscope.*
|
||||||
find "$(SRC_PATH)/" -name "*.[chsS]" -print | sed 's,^\./,,' > "$(SRC_PATH)/cscope.files"
|
find "$(SRC_PATH)" -name "*.[chsS]" -print | sed 's,^\./,,' > ./cscope.files
|
||||||
cscope -b -i"$(SRC_PATH)/cscope.files"
|
cscope -b
|
||||||
|
|
||||||
# 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
|
|
||||||
|
|
||||||
# documentation
|
# documentation
|
||||||
MAKEINFO=makeinfo
|
MAKEINFO=makeinfo
|
||||||
@@ -531,16 +358,13 @@ qemu-options.texi: $(SRC_PATH)/qemu-options.hx
|
|||||||
qemu-monitor.texi: $(SRC_PATH)/hmp-commands.hx
|
qemu-monitor.texi: $(SRC_PATH)/hmp-commands.hx
|
||||||
$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -t < $< > $@," GEN $@")
|
$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -t < $< > $@," GEN $@")
|
||||||
|
|
||||||
qemu-monitor-info.texi: $(SRC_PATH)/hmp-commands-info.hx
|
QMP/qmp-commands.txt: $(SRC_PATH)/qmp-commands.hx
|
||||||
$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -t < $< > $@," GEN $@")
|
|
||||||
|
|
||||||
qmp-commands.txt: $(SRC_PATH)/qmp-commands.hx
|
|
||||||
$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -q < $< > $@," GEN $@")
|
$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -q < $< > $@," GEN $@")
|
||||||
|
|
||||||
qemu-img-cmds.texi: $(SRC_PATH)/qemu-img-cmds.hx
|
qemu-img-cmds.texi: $(SRC_PATH)/qemu-img-cmds.hx
|
||||||
$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -t < $< > $@," GEN $@")
|
$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -t < $< > $@," GEN $@")
|
||||||
|
|
||||||
qemu.1: qemu-doc.texi qemu-options.texi qemu-monitor.texi qemu-monitor-info.texi
|
qemu.1: qemu-doc.texi qemu-options.texi qemu-monitor.texi
|
||||||
$(call quiet-command, \
|
$(call quiet-command, \
|
||||||
perl -Ww -- $(SRC_PATH)/scripts/texi2pod.pl $< qemu.pod && \
|
perl -Ww -- $(SRC_PATH)/scripts/texi2pod.pl $< qemu.pod && \
|
||||||
$(POD2MAN) --section=1 --center=" " --release=" " qemu.pod > $@, \
|
$(POD2MAN) --section=1 --center=" " --release=" " qemu.pod > $@, \
|
||||||
@@ -564,18 +388,6 @@ qemu-nbd.8: qemu-nbd.texi
|
|||||||
$(POD2MAN) --section=8 --center=" " --release=" " qemu-nbd.pod > $@, \
|
$(POD2MAN) --section=8 --center=" " --release=" " qemu-nbd.pod > $@, \
|
||||||
" GEN $@")
|
" GEN $@")
|
||||||
|
|
||||||
qemu-ga.8: qemu-ga.texi
|
|
||||||
$(call quiet-command, \
|
|
||||||
perl -Ww -- $(SRC_PATH)/scripts/texi2pod.pl $< qemu-ga.pod && \
|
|
||||||
$(POD2MAN) --section=8 --center=" " --release=" " qemu-ga.pod > $@, \
|
|
||||||
" GEN $@")
|
|
||||||
|
|
||||||
kvm_stat.1: scripts/kvm/kvm_stat.texi
|
|
||||||
$(call quiet-command, \
|
|
||||||
perl -Ww -- $(SRC_PATH)/scripts/texi2pod.pl $< kvm_stat.pod && \
|
|
||||||
$(POD2MAN) --section=1 --center=" " --release=" " kvm_stat.pod > $@, \
|
|
||||||
" GEN $@")
|
|
||||||
|
|
||||||
dvi: qemu-doc.dvi qemu-tech.dvi
|
dvi: qemu-doc.dvi qemu-tech.dvi
|
||||||
html: qemu-doc.html qemu-tech.html
|
html: qemu-doc.html qemu-tech.html
|
||||||
info: qemu-doc.info qemu-tech.info
|
info: qemu-doc.info qemu-tech.info
|
||||||
@@ -583,68 +395,11 @@ pdf: qemu-doc.pdf qemu-tech.pdf
|
|||||||
|
|
||||||
qemu-doc.dvi qemu-doc.html qemu-doc.info qemu-doc.pdf: \
|
qemu-doc.dvi qemu-doc.html qemu-doc.info qemu-doc.pdf: \
|
||||||
qemu-img.texi qemu-nbd.texi qemu-options.texi \
|
qemu-img.texi qemu-nbd.texi qemu-options.texi \
|
||||||
qemu-monitor.texi qemu-img-cmds.texi qemu-ga.texi \
|
qemu-monitor.texi qemu-img-cmds.texi
|
||||||
qemu-monitor-info.texi
|
|
||||||
|
|
||||||
ifdef CONFIG_WIN32
|
|
||||||
|
|
||||||
INSTALLER = qemu-setup-$(VERSION)$(EXESUF)
|
|
||||||
|
|
||||||
nsisflags = -V2 -NOCD
|
|
||||||
|
|
||||||
ifneq ($(wildcard $(SRC_PATH)/dll),)
|
|
||||||
ifeq ($(ARCH),x86_64)
|
|
||||||
# 64 bit executables
|
|
||||||
DLL_PATH = $(SRC_PATH)/dll/w64
|
|
||||||
nsisflags += -DW64
|
|
||||||
else
|
|
||||||
# 32 bit executables
|
|
||||||
DLL_PATH = $(SRC_PATH)/dll/w32
|
|
||||||
endif
|
|
||||||
endif
|
|
||||||
|
|
||||||
.PHONY: installer
|
|
||||||
installer: $(INSTALLER)
|
|
||||||
|
|
||||||
INSTDIR=/tmp/qemu-nsis
|
|
||||||
|
|
||||||
$(INSTALLER): $(SRC_PATH)/qemu.nsi
|
|
||||||
$(MAKE) install prefix=${INSTDIR}
|
|
||||||
ifdef SIGNCODE
|
|
||||||
(cd ${INSTDIR}; \
|
|
||||||
for i in *.exe; do \
|
|
||||||
$(SIGNCODE) $${i}; \
|
|
||||||
done \
|
|
||||||
)
|
|
||||||
endif # SIGNCODE
|
|
||||||
(cd ${INSTDIR}; \
|
|
||||||
for i in qemu-system-*.exe; do \
|
|
||||||
arch=$${i%.exe}; \
|
|
||||||
arch=$${arch#qemu-system-}; \
|
|
||||||
echo Section \"$$arch\" Section_$$arch; \
|
|
||||||
echo SetOutPath \"\$$INSTDIR\"; \
|
|
||||||
echo File \"\$${BINDIR}\\$$i\"; \
|
|
||||||
echo SectionEnd; \
|
|
||||||
done \
|
|
||||||
) >${INSTDIR}/system-emulations.nsh
|
|
||||||
makensis $(nsisflags) \
|
|
||||||
$(if $(BUILD_DOCS),-DCONFIG_DOCUMENTATION="y") \
|
|
||||||
$(if $(CONFIG_GTK),-DCONFIG_GTK="y") \
|
|
||||||
-DBINDIR="${INSTDIR}" \
|
|
||||||
$(if $(DLL_PATH),-DDLLDIR="$(DLL_PATH)") \
|
|
||||||
-DSRCDIR="$(SRC_PATH)" \
|
|
||||||
-DOUTFILE="$(INSTALLER)" \
|
|
||||||
-DDISPLAYVERSION="$(VERSION)" \
|
|
||||||
$(SRC_PATH)/qemu.nsi
|
|
||||||
rm -r ${INSTDIR}
|
|
||||||
ifdef SIGNCODE
|
|
||||||
$(SIGNCODE) $(INSTALLER)
|
|
||||||
endif # SIGNCODE
|
|
||||||
endif # CONFIG_WIN
|
|
||||||
|
|
||||||
# Add a dependency on the generated files, so that they are always
|
# Add a dependency on the generated files, so that they are always
|
||||||
# rebuilt before other object files
|
# 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)
|
Makefile: $(GENERATED_HEADERS)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
#######################################################################
|
#######################################################################
|
||||||
# Common libraries for tools and emulators
|
# Common libraries for tools and emulators
|
||||||
stub-obj-y = stubs/
|
stub-obj-y = stubs/
|
||||||
util-obj-y = util/ qobject/ qapi/
|
util-obj-y = util/ qobject/ qapi/ trace/
|
||||||
util-obj-y += qmp-introspect.o qapi-types.o qapi-visit.o qapi-event.o
|
|
||||||
|
|
||||||
#######################################################################
|
#######################################################################
|
||||||
# block-obj-y is code used by both qemu system emulation and qemu-img
|
# block-obj-y is code used by both qemu system emulation and qemu-img
|
||||||
@@ -13,25 +12,26 @@ block-obj-y += main-loop.o iohandler.o qemu-timer.o
|
|||||||
block-obj-$(CONFIG_POSIX) += aio-posix.o
|
block-obj-$(CONFIG_POSIX) += aio-posix.o
|
||||||
block-obj-$(CONFIG_WIN32) += aio-win32.o
|
block-obj-$(CONFIG_WIN32) += aio-win32.o
|
||||||
block-obj-y += block/
|
block-obj-y += block/
|
||||||
block-obj-y += qemu-io-cmds.o
|
block-obj-y += qapi-types.o qapi-visit.o
|
||||||
|
|
||||||
block-obj-m = block/
|
block-obj-y += qemu-coroutine.o qemu-coroutine-lock.o qemu-coroutine-io.o
|
||||||
|
block-obj-y += qemu-coroutine-sleep.o
|
||||||
|
block-obj-y += coroutine-$(CONFIG_COROUTINE_BACKEND).o
|
||||||
|
|
||||||
#######################################################################
|
ifeq ($(CONFIG_VIRTIO)$(CONFIG_VIRTFS)$(CONFIG_PCI),yyy)
|
||||||
# crypto-obj-y is code used by both qemu system emulation and qemu-img
|
# Lots of the fsdev/9pcode is pulled in by vl.c via qemu_fsdev_add.
|
||||||
|
# only pull in the actual virtio-9p device if we also enabled virtio.
|
||||||
|
CONFIG_REALLY_VIRTFS=y
|
||||||
|
endif
|
||||||
|
|
||||||
crypto-obj-y = crypto/
|
######################################################################
|
||||||
crypto-aes-obj-y = crypto/
|
# smartcard
|
||||||
|
|
||||||
#######################################################################
|
libcacard-y += libcacard/cac.o libcacard/event.o
|
||||||
# qom-obj-y is code used by both qemu system emulation and qemu-img
|
libcacard-y += libcacard/vcard.o libcacard/vreader.o
|
||||||
|
libcacard-y += libcacard/vcard_emul_nss.o
|
||||||
qom-obj-y = qom/
|
libcacard-y += libcacard/vcard_emul_type.o
|
||||||
|
libcacard-y += libcacard/card_7816.o
|
||||||
#######################################################################
|
|
||||||
# io-obj-y is code used by both qemu system emulation and qemu-img
|
|
||||||
|
|
||||||
io-obj-y = io/
|
|
||||||
|
|
||||||
######################################################################
|
######################################################################
|
||||||
# Target independent part of system emulation. The long term path is to
|
# Target independent part of system emulation. The long term path is to
|
||||||
@@ -39,36 +39,32 @@ io-obj-y = io/
|
|||||||
# single QEMU executable should support all CPUs and machines.
|
# single QEMU executable should support all CPUs and machines.
|
||||||
|
|
||||||
ifeq ($(CONFIG_SOFTMMU),y)
|
ifeq ($(CONFIG_SOFTMMU),y)
|
||||||
common-obj-y = blockdev.o blockdev-nbd.o block/
|
common-obj-y = $(block-obj-y) blockdev.o blockdev-nbd.o block/
|
||||||
common-obj-y += iothread.o
|
|
||||||
common-obj-y += net/
|
common-obj-y += net/
|
||||||
common-obj-y += qdev-monitor.o device-hotplug.o
|
common-obj-y += readline.o
|
||||||
common-obj-$(CONFIG_WIN32) += os-win32.o
|
common-obj-$(CONFIG_WIN32) += os-win32.o
|
||||||
common-obj-$(CONFIG_POSIX) += os-posix.o
|
common-obj-$(CONFIG_POSIX) += os-posix.o
|
||||||
|
|
||||||
common-obj-$(CONFIG_LINUX) += fsdev/
|
common-obj-$(CONFIG_LINUX) += fsdev/
|
||||||
|
|
||||||
common-obj-y += migration/
|
common-obj-y += migration.o migration-tcp.o
|
||||||
common-obj-y += qemu-char.o #aio.o
|
common-obj-y += qemu-char.o #aio.o
|
||||||
common-obj-y += page_cache.o
|
common-obj-y += block-migration.o
|
||||||
common-obj-y += qjson.o
|
common-obj-y += page_cache.o xbzrle.o
|
||||||
|
|
||||||
|
common-obj-$(CONFIG_POSIX) += migration-exec.o migration-unix.o migration-fd.o
|
||||||
|
|
||||||
common-obj-$(CONFIG_SPICE) += spice-qemu-char.o
|
common-obj-$(CONFIG_SPICE) += spice-qemu-char.o
|
||||||
|
|
||||||
common-obj-y += audio/
|
common-obj-y += audio/
|
||||||
common-obj-y += hw/
|
common-obj-y += hw/
|
||||||
common-obj-y += accel.o
|
|
||||||
|
|
||||||
common-obj-y += replay/
|
|
||||||
|
|
||||||
common-obj-y += ui/
|
common-obj-y += ui/
|
||||||
common-obj-y += bt-host.o bt-vhci.o
|
common-obj-y += bt-host.o bt-vhci.o
|
||||||
bt-host.o-cflags := $(BLUEZ_CFLAGS)
|
|
||||||
|
|
||||||
common-obj-y += dma-helpers.o
|
common-obj-y += dma-helpers.o
|
||||||
|
common-obj-y += qtest.o
|
||||||
common-obj-y += vl.o
|
common-obj-y += vl.o
|
||||||
vl.o-cflags := $(GPROF_CFLAGS) $(SDL_CFLAGS)
|
|
||||||
common-obj-y += tpm.o
|
|
||||||
|
|
||||||
common-obj-$(CONFIG_SLIRP) += slirp/
|
common-obj-$(CONFIG_SLIRP) += slirp/
|
||||||
|
|
||||||
@@ -76,13 +72,12 @@ common-obj-y += backends/
|
|||||||
|
|
||||||
common-obj-$(CONFIG_SECCOMP) += qemu-seccomp.o
|
common-obj-$(CONFIG_SECCOMP) += qemu-seccomp.o
|
||||||
|
|
||||||
common-obj-$(CONFIG_FDT) += device_tree.o
|
common-obj-$(CONFIG_SMARTCARD_NSS) += $(libcacard-y)
|
||||||
|
|
||||||
######################################################################
|
######################################################################
|
||||||
# qapi
|
# qapi
|
||||||
|
|
||||||
common-obj-y += qmp-marshal.o
|
common-obj-y += qmp-marshal.o qapi-visit.o qapi-types.o
|
||||||
common-obj-y += qmp-introspect.o
|
|
||||||
common-obj-y += qmp.o hmp.o
|
common-obj-y += qmp.o hmp.o
|
||||||
endif
|
endif
|
||||||
|
|
||||||
@@ -94,25 +89,23 @@ common-obj-y += hw/
|
|||||||
common-obj-y += qom/
|
common-obj-y += qom/
|
||||||
common-obj-y += disas/
|
common-obj-y += disas/
|
||||||
|
|
||||||
######################################################################
|
|
||||||
# Resource file for Windows executables
|
|
||||||
version-obj-$(CONFIG_WIN32) += $(BUILD_DIR)/version.o
|
|
||||||
version-lobj-$(CONFIG_WIN32) += $(BUILD_DIR)/version.lo
|
|
||||||
|
|
||||||
######################################################################
|
|
||||||
# tracing
|
|
||||||
util-obj-y += trace/
|
|
||||||
target-obj-y += trace/
|
|
||||||
|
|
||||||
######################################################################
|
######################################################################
|
||||||
# guest agent
|
# guest agent
|
||||||
|
|
||||||
# FIXME: a few definitions from qapi-types.o/qapi-visit.o are needed
|
# FIXME: a few definitions from qapi-types.o/qapi-visit.o are needed
|
||||||
# by libqemuutil.a. These should be moved to a separate .json schema.
|
# by libqemuutil.a. These should be moved to a separate .json schema.
|
||||||
qga-obj-y = qga/
|
qga-obj-y = qga/ qapi-types.o qapi-visit.o
|
||||||
qga-vss-dll-obj-y = qga/
|
|
||||||
|
|
||||||
######################################################################
|
vl.o: QEMU_CFLAGS+=$(GPROF_CFLAGS)
|
||||||
# contrib
|
|
||||||
ivshmem-client-obj-y = contrib/ivshmem-client/
|
vl.o: QEMU_CFLAGS+=$(SDL_CFLAGS)
|
||||||
ivshmem-server-obj-y = contrib/ivshmem-server/
|
|
||||||
|
QEMU_CFLAGS+=$(GLIB_CFLAGS)
|
||||||
|
|
||||||
|
nested-vars += \
|
||||||
|
stub-obj-y \
|
||||||
|
util-obj-y \
|
||||||
|
qga-obj-y \
|
||||||
|
block-obj-y \
|
||||||
|
common-obj-y
|
||||||
|
dummy := $(call unnest-vars)
|
||||||
|
|||||||
161
Makefile.target
161
Makefile.target
@@ -1,13 +1,11 @@
|
|||||||
# -*- Mode: makefile -*-
|
# -*- Mode: makefile -*-
|
||||||
|
|
||||||
BUILD_DIR?=$(CURDIR)/..
|
|
||||||
|
|
||||||
include ../config-host.mak
|
include ../config-host.mak
|
||||||
include config-target.mak
|
|
||||||
include config-devices.mak
|
include config-devices.mak
|
||||||
|
include config-target.mak
|
||||||
include $(SRC_PATH)/rules.mak
|
include $(SRC_PATH)/rules.mak
|
||||||
|
|
||||||
$(call set-vpath, $(SRC_PATH):$(BUILD_DIR))
|
$(call set-vpath, $(SRC_PATH))
|
||||||
ifdef CONFIG_LINUX
|
ifdef CONFIG_LINUX
|
||||||
QEMU_CFLAGS += -I../linux-headers
|
QEMU_CFLAGS += -I../linux-headers
|
||||||
endif
|
endif
|
||||||
@@ -17,30 +15,31 @@ QEMU_CFLAGS+=-I$(SRC_PATH)/include
|
|||||||
|
|
||||||
ifdef CONFIG_USER_ONLY
|
ifdef CONFIG_USER_ONLY
|
||||||
# user emulator name
|
# user emulator name
|
||||||
QEMU_PROG=qemu-$(TARGET_NAME)
|
QEMU_PROG=qemu-$(TARGET_ARCH2)
|
||||||
QEMU_PROG_BUILD = $(QEMU_PROG)
|
|
||||||
else
|
else
|
||||||
# system emulator name
|
# system emulator name
|
||||||
QEMU_PROG=qemu-system-$(TARGET_NAME)$(EXESUF)
|
ifneq (,$(findstring -mwindows,$(LIBS)))
|
||||||
ifneq (,$(findstring -mwindows,$(libs_softmmu)))
|
|
||||||
# Terminate program name with a 'w' because the linker builds a windows executable.
|
# Terminate program name with a 'w' because the linker builds a windows executable.
|
||||||
QEMU_PROGW=qemu-system-$(TARGET_NAME)w$(EXESUF)
|
QEMU_PROGW=qemu-system-$(TARGET_ARCH2)w$(EXESUF)
|
||||||
$(QEMU_PROG): $(QEMU_PROGW)
|
endif # windows executable
|
||||||
$(call quiet-command,$(OBJCOPY) --subsystem console $(QEMU_PROGW) $(QEMU_PROG)," GEN $(TARGET_DIR)$(QEMU_PROG)")
|
QEMU_PROG=qemu-system-$(TARGET_ARCH2)$(EXESUF)
|
||||||
QEMU_PROG_BUILD = $(QEMU_PROGW)
|
|
||||||
else
|
|
||||||
QEMU_PROG_BUILD = $(QEMU_PROG)
|
|
||||||
endif
|
|
||||||
endif
|
endif
|
||||||
|
|
||||||
PROGS=$(QEMU_PROG) $(QEMU_PROGW)
|
PROGS=$(QEMU_PROG)
|
||||||
|
ifdef QEMU_PROGW
|
||||||
|
PROGS+=$(QEMU_PROGW)
|
||||||
|
endif
|
||||||
STPFILES=
|
STPFILES=
|
||||||
|
|
||||||
|
ifndef CONFIG_HAIKU
|
||||||
|
LIBS+=-lm
|
||||||
|
endif
|
||||||
|
|
||||||
config-target.h: config-target.h-timestamp
|
config-target.h: config-target.h-timestamp
|
||||||
config-target.h-timestamp: config-target.mak
|
config-target.h-timestamp: config-target.mak
|
||||||
|
|
||||||
ifdef CONFIG_TRACE_SYSTEMTAP
|
ifdef CONFIG_TRACE_SYSTEMTAP
|
||||||
stap: $(QEMU_PROG).stp-installed $(QEMU_PROG).stp $(QEMU_PROG)-simpletrace.stp
|
stap: $(QEMU_PROG).stp
|
||||||
|
|
||||||
ifdef CONFIG_USER_ONLY
|
ifdef CONFIG_USER_ONLY
|
||||||
TARGET_TYPE=user
|
TARGET_TYPE=user
|
||||||
@@ -48,31 +47,14 @@ else
|
|||||||
TARGET_TYPE=system
|
TARGET_TYPE=system
|
||||||
endif
|
endif
|
||||||
|
|
||||||
$(QEMU_PROG).stp-installed: $(SRC_PATH)/trace-events
|
|
||||||
$(call quiet-command,$(TRACETOOL) \
|
|
||||||
--format=stap \
|
|
||||||
--backends=$(TRACE_BACKENDS) \
|
|
||||||
--binary=$(bindir)/$(QEMU_PROG) \
|
|
||||||
--target-name=$(TARGET_NAME) \
|
|
||||||
--target-type=$(TARGET_TYPE) \
|
|
||||||
< $< > $@," GEN $(TARGET_DIR)$(QEMU_PROG).stp-installed")
|
|
||||||
|
|
||||||
$(QEMU_PROG).stp: $(SRC_PATH)/trace-events
|
$(QEMU_PROG).stp: $(SRC_PATH)/trace-events
|
||||||
$(call quiet-command,$(TRACETOOL) \
|
$(call quiet-command,$(TRACETOOL) \
|
||||||
--format=stap \
|
--format=stap \
|
||||||
--backends=$(TRACE_BACKENDS) \
|
--backend=$(TRACE_BACKEND) \
|
||||||
--binary=$(realpath .)/$(QEMU_PROG) \
|
--binary=$(bindir)/$(QEMU_PROG) \
|
||||||
--target-name=$(TARGET_NAME) \
|
--target-arch=$(TARGET_ARCH) \
|
||||||
--target-type=$(TARGET_TYPE) \
|
--target-type=$(TARGET_TYPE) \
|
||||||
< $< > $@," GEN $(TARGET_DIR)$(QEMU_PROG).stp")
|
< $< > $@," GEN $(TARGET_DIR)$(QEMU_PROG).stp")
|
||||||
|
|
||||||
$(QEMU_PROG)-simpletrace.stp: $(SRC_PATH)/trace-events
|
|
||||||
$(call quiet-command,$(TRACETOOL) \
|
|
||||||
--format=simpletrace-stap \
|
|
||||||
--backends=$(TRACE_BACKENDS) \
|
|
||||||
--probe-prefix=qemu.$(TARGET_TYPE).$(TARGET_NAME) \
|
|
||||||
< $< > $@," GEN $(TARGET_DIR)$(QEMU_PROG)-simpletrace.stp")
|
|
||||||
|
|
||||||
else
|
else
|
||||||
stap:
|
stap:
|
||||||
endif
|
endif
|
||||||
@@ -85,23 +67,13 @@ all: $(PROGS) stap
|
|||||||
#########################################################
|
#########################################################
|
||||||
# cpu emulator library
|
# cpu emulator library
|
||||||
obj-y = exec.o translate-all.o cpu-exec.o
|
obj-y = exec.o translate-all.o cpu-exec.o
|
||||||
obj-y += translate-common.o
|
obj-y += tcg/tcg.o tcg/optimize.o
|
||||||
obj-y += cpu-exec-common.o
|
|
||||||
obj-y += tcg/tcg.o tcg/tcg-op.o tcg/optimize.o
|
|
||||||
obj-$(CONFIG_TCG_INTERPRETER) += tci.o
|
obj-$(CONFIG_TCG_INTERPRETER) += tci.o
|
||||||
obj-y += tcg/tcg-common.o
|
|
||||||
obj-$(CONFIG_TCG_INTERPRETER) += disas/tci.o
|
obj-$(CONFIG_TCG_INTERPRETER) += disas/tci.o
|
||||||
obj-y += fpu/softfloat.o
|
obj-y += fpu/softfloat.o
|
||||||
obj-y += target-$(TARGET_BASE_ARCH)/
|
obj-y += target-$(TARGET_BASE_ARCH)/
|
||||||
obj-y += disas.o
|
obj-y += disas.o
|
||||||
obj-$(call notempty,$(TARGET_XML_FILES)) += gdbstub-xml.o
|
obj-$(CONFIG_GDBSTUB_XML) += gdbstub-xml.o
|
||||||
obj-$(call lnot,$(CONFIG_KVM)) += kvm-stub.o
|
|
||||||
|
|
||||||
obj-$(CONFIG_LIBDECNUMBER) += libdecnumber/decContext.o
|
|
||||||
obj-$(CONFIG_LIBDECNUMBER) += libdecnumber/decNumber.o
|
|
||||||
obj-$(CONFIG_LIBDECNUMBER) += libdecnumber/dpd/decimal32.o
|
|
||||||
obj-$(CONFIG_LIBDECNUMBER) += libdecnumber/dpd/decimal64.o
|
|
||||||
obj-$(CONFIG_LIBDECNUMBER) += libdecnumber/dpd/decimal128.o
|
|
||||||
|
|
||||||
#########################################################
|
#########################################################
|
||||||
# Linux user emulator target
|
# Linux user emulator target
|
||||||
@@ -120,8 +92,7 @@ endif #CONFIG_LINUX_USER
|
|||||||
|
|
||||||
ifdef CONFIG_BSD_USER
|
ifdef CONFIG_BSD_USER
|
||||||
|
|
||||||
QEMU_CFLAGS+=-I$(SRC_PATH)/bsd-user -I$(SRC_PATH)/bsd-user/$(TARGET_ABI_DIR) \
|
QEMU_CFLAGS+=-I$(SRC_PATH)/bsd-user -I$(SRC_PATH)/bsd-user/$(TARGET_ARCH)
|
||||||
-I$(SRC_PATH)/bsd-user/$(HOST_VARIANT_DIR)
|
|
||||||
|
|
||||||
obj-y += bsd-user/
|
obj-y += bsd-user/
|
||||||
obj-y += gdbstub.o user-exec.o
|
obj-y += gdbstub.o user-exec.o
|
||||||
@@ -131,71 +102,60 @@ endif #CONFIG_BSD_USER
|
|||||||
#########################################################
|
#########################################################
|
||||||
# System emulator target
|
# System emulator target
|
||||||
ifdef CONFIG_SOFTMMU
|
ifdef CONFIG_SOFTMMU
|
||||||
obj-y += arch_init.o cpus.o monitor.o gdbstub.o balloon.o ioport.o numa.o
|
CONFIG_NO_PCI = $(if $(subst n,,$(CONFIG_PCI)),n,y)
|
||||||
obj-y += qtest.o bootdevice.o
|
CONFIG_NO_KVM = $(if $(subst n,,$(CONFIG_KVM)),n,y)
|
||||||
|
CONFIG_NO_XEN = $(if $(subst n,,$(CONFIG_XEN)),n,y)
|
||||||
|
CONFIG_NO_GET_MEMORY_MAPPING = $(if $(subst n,,$(CONFIG_HAVE_GET_MEMORY_MAPPING)),n,y)
|
||||||
|
CONFIG_NO_CORE_DUMP = $(if $(subst n,,$(CONFIG_HAVE_CORE_DUMP)),n,y)
|
||||||
|
|
||||||
|
obj-y += arch_init.o cpus.o monitor.o gdbstub.o balloon.o ioport.o
|
||||||
obj-y += hw/
|
obj-y += hw/
|
||||||
obj-$(CONFIG_KVM) += kvm-all.o
|
obj-$(CONFIG_KVM) += kvm-all.o
|
||||||
obj-y += memory.o cputlb.o
|
obj-$(CONFIG_NO_KVM) += kvm-stub.o
|
||||||
obj-y += memory_mapping.o
|
obj-y += memory.o savevm.o cputlb.o
|
||||||
obj-y += dump.o
|
obj-$(CONFIG_HAVE_GET_MEMORY_MAPPING) += memory_mapping.o
|
||||||
obj-y += migration/ram.o migration/savevm.o
|
obj-$(CONFIG_HAVE_CORE_DUMP) += dump.o
|
||||||
LIBS := $(libs_softmmu) $(LIBS)
|
obj-$(CONFIG_NO_GET_MEMORY_MAPPING) += memory_mapping-stub.o
|
||||||
|
obj-$(CONFIG_NO_CORE_DUMP) += dump-stub.o
|
||||||
|
LIBS+=-lz
|
||||||
|
|
||||||
# xen support
|
# xen support
|
||||||
obj-$(CONFIG_XEN) += xen-common.o
|
obj-$(CONFIG_XEN) += xen-all.o xen-mapcache.o
|
||||||
obj-$(CONFIG_XEN_I386) += xen-hvm.o xen-mapcache.o
|
obj-$(CONFIG_NO_XEN) += xen-stub.o
|
||||||
obj-$(call lnot,$(CONFIG_XEN)) += xen-common-stub.o
|
|
||||||
obj-$(call lnot,$(CONFIG_XEN_I386)) += xen-hvm-stub.o
|
|
||||||
|
|
||||||
# Hardware support
|
# Hardware support
|
||||||
ifeq ($(TARGET_NAME), sparc64)
|
ifeq ($(TARGET_ARCH), sparc64)
|
||||||
obj-y += hw/sparc64/
|
obj-y += hw/sparc64/
|
||||||
else
|
else
|
||||||
obj-y += hw/$(TARGET_BASE_ARCH)/
|
obj-y += hw/$(TARGET_BASE_ARCH)/
|
||||||
endif
|
endif
|
||||||
|
|
||||||
GENERATED_HEADERS += hmp-commands.h hmp-commands-info.h qmp-commands-old.h
|
main.o: QEMU_CFLAGS+=$(GPROF_CFLAGS)
|
||||||
|
|
||||||
|
GENERATED_HEADERS += hmp-commands.h qmp-commands-old.h
|
||||||
|
|
||||||
endif # CONFIG_SOFTMMU
|
endif # CONFIG_SOFTMMU
|
||||||
|
|
||||||
# Workaround for http://gcc.gnu.org/PR55489, see configure.
|
# Workaround for http://gcc.gnu.org/PR55489, see configure.
|
||||||
%/translate.o: QEMU_CFLAGS += $(TRANSLATE_OPT_CFLAGS)
|
%/translate.o: QEMU_CFLAGS += $(TRANSLATE_OPT_CFLAGS)
|
||||||
|
|
||||||
dummy := $(call unnest-vars,,obj-y)
|
nested-vars += obj-y
|
||||||
all-obj-y := $(obj-y)
|
|
||||||
|
|
||||||
target-obj-y :=
|
# This resolves all nested paths, so it must come last
|
||||||
block-obj-y :=
|
|
||||||
common-obj-y :=
|
|
||||||
include $(SRC_PATH)/Makefile.objs
|
include $(SRC_PATH)/Makefile.objs
|
||||||
dummy := $(call unnest-vars,,target-obj-y)
|
|
||||||
target-obj-y-save := $(target-obj-y)
|
|
||||||
dummy := $(call unnest-vars,.., \
|
|
||||||
block-obj-y \
|
|
||||||
block-obj-m \
|
|
||||||
crypto-obj-y \
|
|
||||||
crypto-aes-obj-y \
|
|
||||||
qom-obj-y \
|
|
||||||
io-obj-y \
|
|
||||||
common-obj-y \
|
|
||||||
common-obj-m)
|
|
||||||
target-obj-y := $(target-obj-y-save)
|
|
||||||
all-obj-y += $(common-obj-y)
|
|
||||||
all-obj-y += $(target-obj-y)
|
|
||||||
all-obj-y += $(qom-obj-y)
|
|
||||||
all-obj-$(CONFIG_SOFTMMU) += $(block-obj-y)
|
|
||||||
all-obj-$(CONFIG_USER_ONLY) += $(crypto-aes-obj-y)
|
|
||||||
all-obj-$(CONFIG_SOFTMMU) += $(crypto-obj-y)
|
|
||||||
all-obj-$(CONFIG_SOFTMMU) += $(io-obj-y)
|
|
||||||
|
|
||||||
$(QEMU_PROG_BUILD): config-devices.mak
|
all-obj-y = $(obj-y)
|
||||||
|
all-obj-y += $(addprefix ../, $(common-obj-y))
|
||||||
|
|
||||||
# build either PROG or PROGW
|
ifdef QEMU_PROGW
|
||||||
$(QEMU_PROG_BUILD): $(all-obj-y) ../libqemuutil.a ../libqemustub.a
|
# The linker builds a windows executable. Make also a console executable.
|
||||||
$(call LINK, $(filter-out %.mak, $^))
|
$(QEMU_PROGW): $(all-obj-y) ../libqemuutil.a ../libqemustub.a
|
||||||
ifdef CONFIG_DARWIN
|
$(call LINK,$^)
|
||||||
$(call quiet-command,Rez -append $(SRC_PATH)/pc-bios/qemu.rsrc -o $@," REZ $(TARGET_DIR)$@")
|
$(QEMU_PROG): $(QEMU_PROGW)
|
||||||
$(call quiet-command,SetFile -a C $@," SETFILE $(TARGET_DIR)$@")
|
$(call quiet-command,$(OBJCOPY) --subsystem console $(QEMU_PROGW) $(QEMU_PROG)," GEN $(TARGET_DIR)$(QEMU_PROG)")
|
||||||
|
else
|
||||||
|
$(QEMU_PROG): $(all-obj-y) ../libqemuutil.a ../libqemustub.a
|
||||||
|
$(call LINK,$^)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
gdbstub-xml.c: $(TARGET_XML_FILES) $(SRC_PATH)/scripts/feature_to_c.sh
|
gdbstub-xml.c: $(TARGET_XML_FILES) $(SRC_PATH)/scripts/feature_to_c.sh
|
||||||
@@ -204,9 +164,6 @@ gdbstub-xml.c: $(TARGET_XML_FILES) $(SRC_PATH)/scripts/feature_to_c.sh
|
|||||||
hmp-commands.h: $(SRC_PATH)/hmp-commands.hx
|
hmp-commands.h: $(SRC_PATH)/hmp-commands.hx
|
||||||
$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@," GEN $(TARGET_DIR)$@")
|
$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@," GEN $(TARGET_DIR)$@")
|
||||||
|
|
||||||
hmp-commands-info.h: $(SRC_PATH)/hmp-commands-info.hx
|
|
||||||
$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@," GEN $(TARGET_DIR)$@")
|
|
||||||
|
|
||||||
qmp-commands-old.h: $(SRC_PATH)/qmp-commands.hx
|
qmp-commands-old.h: $(SRC_PATH)/qmp-commands.hx
|
||||||
$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@," GEN $(TARGET_DIR)$@")
|
$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@," GEN $(TARGET_DIR)$@")
|
||||||
|
|
||||||
@@ -220,12 +177,14 @@ endif
|
|||||||
|
|
||||||
install: all
|
install: all
|
||||||
ifneq ($(PROGS),)
|
ifneq ($(PROGS),)
|
||||||
$(call install-prog,$(PROGS),$(DESTDIR)$(bindir))
|
$(INSTALL) -m 755 $(PROGS) "$(DESTDIR)$(bindir)"
|
||||||
|
ifneq ($(STRIP),)
|
||||||
|
$(STRIP) $(patsubst %,"$(DESTDIR)$(bindir)/%",$(PROGS))
|
||||||
|
endif
|
||||||
endif
|
endif
|
||||||
ifdef CONFIG_TRACE_SYSTEMTAP
|
ifdef CONFIG_TRACE_SYSTEMTAP
|
||||||
$(INSTALL_DIR) "$(DESTDIR)$(qemu_datadir)/../systemtap/tapset"
|
$(INSTALL_DIR) "$(DESTDIR)$(qemu_datadir)/../systemtap/tapset"
|
||||||
$(INSTALL_DATA) $(QEMU_PROG).stp-installed "$(DESTDIR)$(qemu_datadir)/../systemtap/tapset/$(QEMU_PROG).stp"
|
$(INSTALL_DATA) $(QEMU_PROG).stp "$(DESTDIR)$(qemu_datadir)/../systemtap/tapset"
|
||||||
$(INSTALL_DATA) $(QEMU_PROG)-simpletrace.stp "$(DESTDIR)$(qemu_datadir)/../systemtap/tapset/$(QEMU_PROG)-simpletrace.stp"
|
|
||||||
endif
|
endif
|
||||||
|
|
||||||
GENERATED_HEADERS += config-target.h
|
GENERATED_HEADERS += config-target.h
|
||||||
|
|||||||
88
QMP/README
Normal file
88
QMP/README
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
QEMU Monitor Protocol
|
||||||
|
=====================
|
||||||
|
|
||||||
|
Introduction
|
||||||
|
-------------
|
||||||
|
|
||||||
|
The QEMU Monitor Protocol (QMP) allows applications to communicate with
|
||||||
|
QEMU's Monitor.
|
||||||
|
|
||||||
|
QMP is JSON[1] based and currently has the following features:
|
||||||
|
|
||||||
|
- Lightweight, text-based, easy to parse data format
|
||||||
|
- Asynchronous messages support (ie. events)
|
||||||
|
- Capabilities Negotiation
|
||||||
|
|
||||||
|
For detailed information on QMP's usage, please, refer to the following files:
|
||||||
|
|
||||||
|
o qmp-spec.txt QEMU Monitor Protocol current specification
|
||||||
|
o qmp-commands.txt QMP supported commands (auto-generated at build-time)
|
||||||
|
o qmp-events.txt List of available asynchronous events
|
||||||
|
|
||||||
|
There is also a simple Python script called 'qmp-shell' available.
|
||||||
|
|
||||||
|
IMPORTANT: It's strongly recommended to read the 'Stability Considerations'
|
||||||
|
section in the qmp-commands.txt file before making any serious use of QMP.
|
||||||
|
|
||||||
|
|
||||||
|
[1] http://www.json.org
|
||||||
|
|
||||||
|
Usage
|
||||||
|
-----
|
||||||
|
|
||||||
|
To enable QMP, you need a QEMU monitor instance in "control mode". There are
|
||||||
|
two ways of doing this.
|
||||||
|
|
||||||
|
The simplest one is using the '-qmp' command-line option. The following
|
||||||
|
example makes QMP available on localhost port 4444:
|
||||||
|
|
||||||
|
$ qemu [...] -qmp tcp:localhost:4444,server
|
||||||
|
|
||||||
|
However, in order to have more complex combinations, like multiple monitors,
|
||||||
|
the '-mon' command-line option should be used along with the '-chardev' one.
|
||||||
|
For instance, the following example creates one user monitor on stdio and one
|
||||||
|
QMP monitor on localhost port 4444.
|
||||||
|
|
||||||
|
$ qemu [...] -chardev stdio,id=mon0 -mon chardev=mon0,mode=readline \
|
||||||
|
-chardev socket,id=mon1,host=localhost,port=4444,server \
|
||||||
|
-mon chardev=mon1,mode=control
|
||||||
|
|
||||||
|
Please, refer to QEMU's manpage for more information.
|
||||||
|
|
||||||
|
Simple Testing
|
||||||
|
--------------
|
||||||
|
|
||||||
|
To manually test QMP one can connect with telnet and issue commands by hand:
|
||||||
|
|
||||||
|
$ telnet localhost 4444
|
||||||
|
Trying 127.0.0.1...
|
||||||
|
Connected to localhost.
|
||||||
|
Escape character is '^]'.
|
||||||
|
{"QMP": {"version": {"qemu": {"micro": 50, "minor": 13, "major": 0}, "package": ""}, "capabilities": []}}
|
||||||
|
{ "execute": "qmp_capabilities" }
|
||||||
|
{"return": {}}
|
||||||
|
{ "execute": "query-version" }
|
||||||
|
{"return": {"qemu": {"micro": 50, "minor": 13, "major": 0}, "package": ""}}
|
||||||
|
|
||||||
|
Development Process
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
When changing QMP's interface (by adding new commands, events or modifying
|
||||||
|
existing ones) it's mandatory to update the relevant documentation, which is
|
||||||
|
one (or more) of the files listed in the 'Introduction' section*.
|
||||||
|
|
||||||
|
Also, it's strongly recommended to send the documentation patch first, before
|
||||||
|
doing any code change. This is so because:
|
||||||
|
|
||||||
|
1. Avoids the code dictating the interface
|
||||||
|
|
||||||
|
2. Review can improve your interface. Letting that happen before
|
||||||
|
you implement it can save you work.
|
||||||
|
|
||||||
|
* The qmp-commands.txt file is generated from the qmp-commands.hx one, which
|
||||||
|
is the file that should be edited.
|
||||||
|
|
||||||
|
Homepage
|
||||||
|
--------
|
||||||
|
|
||||||
|
http://wiki.qemu.org/QMP
|
||||||
@@ -33,7 +33,7 @@
|
|||||||
# $ qemu-ga-client fsfreeze freeze
|
# $ qemu-ga-client fsfreeze freeze
|
||||||
# 2 filesystems frozen
|
# 2 filesystems frozen
|
||||||
#
|
#
|
||||||
# See also: http://wiki.qemu-project.org/Features/QAPI/GuestAgent
|
# See also: http://wiki.qemu.org/Features/QAPI/GuestAgent
|
||||||
#
|
#
|
||||||
|
|
||||||
import base64
|
import base64
|
||||||
@@ -267,9 +267,7 @@ def main(address, cmd, args):
|
|||||||
print('Hint: qemu is not running?')
|
print('Hint: qemu is not running?')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if cmd == 'fsfreeze' and args[0] == 'freeze':
|
if cmd != 'ping':
|
||||||
client.sync(60)
|
|
||||||
elif cmd != 'ping':
|
|
||||||
client.sync()
|
client.sync()
|
||||||
|
|
||||||
globals()['_cmd_' + cmd](client, args)
|
globals()['_cmd_' + cmd](client, args)
|
||||||
@@ -1,16 +1,6 @@
|
|||||||
QEMU Machine Protocol Events
|
QEMU Monitor Protocol Events
|
||||||
============================
|
============================
|
||||||
|
|
||||||
ACPI_DEVICE_OST
|
|
||||||
---------------
|
|
||||||
|
|
||||||
Emitted when guest executes ACPI _OST method.
|
|
||||||
|
|
||||||
- data: ACPIOSTInfo type as described in qapi-schema.json
|
|
||||||
|
|
||||||
{ "event": "ACPI_DEVICE_OST",
|
|
||||||
"data": { "device": "d1", "slot": "0", "slot-type": "DIMM", "source": 1, "status": 0 } }
|
|
||||||
|
|
||||||
BALLOON_CHANGE
|
BALLOON_CHANGE
|
||||||
--------------
|
--------------
|
||||||
|
|
||||||
@@ -28,36 +18,6 @@ Example:
|
|||||||
"data": { "actual": 944766976 },
|
"data": { "actual": 944766976 },
|
||||||
"timestamp": { "seconds": 1267020223, "microseconds": 435656 } }
|
"timestamp": { "seconds": 1267020223, "microseconds": 435656 } }
|
||||||
|
|
||||||
Note: this event is rate-limited.
|
|
||||||
|
|
||||||
BLOCK_IMAGE_CORRUPTED
|
|
||||||
---------------------
|
|
||||||
|
|
||||||
Emitted when a disk image is being marked corrupt. The image can be
|
|
||||||
identified by its device or node name. The 'device' field is always
|
|
||||||
present for compatibility reasons, but it can be empty ("") if the
|
|
||||||
image does not have a device name associated.
|
|
||||||
|
|
||||||
Data:
|
|
||||||
|
|
||||||
- "device": Device name (json-string)
|
|
||||||
- "node-name": Node name (json-string, optional)
|
|
||||||
- "msg": Informative message (e.g., reason for the corruption)
|
|
||||||
(json-string)
|
|
||||||
- "offset": If the corruption resulted from an image access, this
|
|
||||||
is the host's access offset into the image
|
|
||||||
(json-int, optional)
|
|
||||||
- "size": If the corruption resulted from an image access, this
|
|
||||||
is the access size (json-int, optional)
|
|
||||||
|
|
||||||
Example:
|
|
||||||
|
|
||||||
{ "event": "BLOCK_IMAGE_CORRUPTED",
|
|
||||||
"data": { "device": "ide0-hd0", "node-name": "node0",
|
|
||||||
"msg": "Prevented active L1 table overwrite", "offset": 196608,
|
|
||||||
"size": 65536 },
|
|
||||||
"timestamp": { "seconds": 1378126126, "microseconds": 966463 } }
|
|
||||||
|
|
||||||
BLOCK_IO_ERROR
|
BLOCK_IO_ERROR
|
||||||
--------------
|
--------------
|
||||||
|
|
||||||
@@ -70,7 +30,7 @@ Data:
|
|||||||
- "action": action that has been taken, it's one of the following (json-string):
|
- "action": action that has been taken, it's one of the following (json-string):
|
||||||
"ignore": error has been ignored
|
"ignore": error has been ignored
|
||||||
"report": error has been reported to the device
|
"report": error has been reported to the device
|
||||||
"stop": the VM is going to stop because of the error
|
"stop": error caused VM to be stopped
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
@@ -165,43 +125,17 @@ Emitted when a block job is ready to complete.
|
|||||||
|
|
||||||
Data:
|
Data:
|
||||||
|
|
||||||
- "type": Job type (json-string; "stream" for image streaming
|
- "device": device name (json-string)
|
||||||
"commit" for block commit)
|
|
||||||
- "device": Device name (json-string)
|
|
||||||
- "len": Maximum progress value (json-int)
|
|
||||||
- "offset": Current progress value (json-int)
|
|
||||||
On success this is equal to len.
|
|
||||||
On failure this is less than len.
|
|
||||||
- "speed": Rate limit, bytes per second (json-int)
|
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
{ "event": "BLOCK_JOB_READY",
|
{ "event": "BLOCK_JOB_READY",
|
||||||
"data": { "device": "drive0", "type": "mirror", "speed": 0,
|
"data": { "device": "ide0-hd1" },
|
||||||
"len": 2097152, "offset": 2097152 }
|
|
||||||
"timestamp": { "seconds": 1265044230, "microseconds": 450486 } }
|
"timestamp": { "seconds": 1265044230, "microseconds": 450486 } }
|
||||||
|
|
||||||
Note: The "ready to complete" status is always reset by a BLOCK_JOB_ERROR
|
Note: The "ready to complete" status is always reset by a BLOCK_JOB_ERROR
|
||||||
event.
|
event.
|
||||||
|
|
||||||
DEVICE_DELETED
|
|
||||||
--------------
|
|
||||||
|
|
||||||
Emitted whenever the device removal completion is acknowledged
|
|
||||||
by the guest.
|
|
||||||
At this point, it's safe to reuse the specified device ID.
|
|
||||||
Device removal can be initiated by the guest or by HMP/QMP commands.
|
|
||||||
|
|
||||||
Data:
|
|
||||||
|
|
||||||
- "device": device name (json-string, optional)
|
|
||||||
- "path": device path (json-string)
|
|
||||||
|
|
||||||
{ "event": "DEVICE_DELETED",
|
|
||||||
"data": { "device": "virtio-net-pci-0",
|
|
||||||
"path": "/machine/peripheral/virtio-net-pci-0" },
|
|
||||||
"timestamp": { "seconds": 1265044230, "microseconds": 450486 } }
|
|
||||||
|
|
||||||
DEVICE_TRAY_MOVED
|
DEVICE_TRAY_MOVED
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
@@ -220,114 +154,10 @@ Data:
|
|||||||
},
|
},
|
||||||
"timestamp": { "seconds": 1265044230, "microseconds": 450486 } }
|
"timestamp": { "seconds": 1265044230, "microseconds": 450486 } }
|
||||||
|
|
||||||
GUEST_PANICKED
|
|
||||||
--------------
|
|
||||||
|
|
||||||
Emitted when guest OS panic is detected.
|
|
||||||
|
|
||||||
Data:
|
|
||||||
|
|
||||||
- "action": Action that has been taken (json-string, currently always "pause").
|
|
||||||
|
|
||||||
Example:
|
|
||||||
|
|
||||||
{ "event": "GUEST_PANICKED",
|
|
||||||
"data": { "action": "pause" } }
|
|
||||||
|
|
||||||
MEM_UNPLUG_ERROR
|
|
||||||
--------------------
|
|
||||||
Emitted when memory hot unplug error occurs.
|
|
||||||
|
|
||||||
Data:
|
|
||||||
|
|
||||||
- "device": device name (json-string)
|
|
||||||
- "msg": Informative message (e.g., reason for the error) (json-string)
|
|
||||||
|
|
||||||
Example:
|
|
||||||
|
|
||||||
{ "event": "MEM_UNPLUG_ERROR"
|
|
||||||
"data": { "device": "dimm1",
|
|
||||||
"msg": "acpi: device unplug for unsupported device"
|
|
||||||
},
|
|
||||||
"timestamp": { "seconds": 1265044230, "microseconds": 450486 } }
|
|
||||||
|
|
||||||
NIC_RX_FILTER_CHANGED
|
|
||||||
---------------------
|
|
||||||
|
|
||||||
The event is emitted once until the query command is executed,
|
|
||||||
the first event will always be emitted.
|
|
||||||
|
|
||||||
Data:
|
|
||||||
|
|
||||||
- "name": net client name (json-string)
|
|
||||||
- "path": device path (json-string)
|
|
||||||
|
|
||||||
{ "event": "NIC_RX_FILTER_CHANGED",
|
|
||||||
"data": { "name": "vnet0",
|
|
||||||
"path": "/machine/peripheral/vnet0/virtio-backend" },
|
|
||||||
"timestamp": { "seconds": 1368697518, "microseconds": 326866 } }
|
|
||||||
}
|
|
||||||
|
|
||||||
POWERDOWN
|
|
||||||
---------
|
|
||||||
|
|
||||||
Emitted when the Virtual Machine is powered down through the power
|
|
||||||
control system, such as via ACPI.
|
|
||||||
|
|
||||||
Data: None.
|
|
||||||
|
|
||||||
Example:
|
|
||||||
|
|
||||||
{ "event": "POWERDOWN",
|
|
||||||
"timestamp": { "seconds": 1267040730, "microseconds": 682951 } }
|
|
||||||
|
|
||||||
QUORUM_FAILURE
|
|
||||||
--------------
|
|
||||||
|
|
||||||
Emitted by the Quorum block driver if it fails to establish a quorum.
|
|
||||||
|
|
||||||
Data:
|
|
||||||
|
|
||||||
- "reference": device name if defined else node name.
|
|
||||||
- "sector-num": Number of the first sector of the failed read operation.
|
|
||||||
- "sectors-count": Failed read operation sector count.
|
|
||||||
|
|
||||||
Example:
|
|
||||||
|
|
||||||
{ "event": "QUORUM_FAILURE",
|
|
||||||
"data": { "reference": "usr1", "sector-num": 345435, "sectors-count": 5 },
|
|
||||||
"timestamp": { "seconds": 1344522075, "microseconds": 745528 } }
|
|
||||||
|
|
||||||
Note: this event is rate-limited.
|
|
||||||
|
|
||||||
QUORUM_REPORT_BAD
|
|
||||||
-----------------
|
|
||||||
|
|
||||||
Emitted to report a corruption of a Quorum file.
|
|
||||||
|
|
||||||
Data:
|
|
||||||
|
|
||||||
- "error": Error message (json-string, optional)
|
|
||||||
Only present on failure. This field contains a human-readable
|
|
||||||
error message. There are no semantics other than that the
|
|
||||||
block layer reported an error and clients should not try to
|
|
||||||
interpret the error string.
|
|
||||||
- "node-name": The graph node name of the block driver state.
|
|
||||||
- "sector-num": Number of the first sector of the failed read operation.
|
|
||||||
- "sectors-count": Failed read operation sector count.
|
|
||||||
|
|
||||||
Example:
|
|
||||||
|
|
||||||
{ "event": "QUORUM_REPORT_BAD",
|
|
||||||
"data": { "node-name": "1.raw", "sector-num": 345435, "sectors-count": 5 },
|
|
||||||
"timestamp": { "seconds": 1344522075, "microseconds": 745528 } }
|
|
||||||
|
|
||||||
Note: this event is rate-limited.
|
|
||||||
|
|
||||||
RESET
|
RESET
|
||||||
-----
|
-----
|
||||||
|
|
||||||
Emitted when the Virtual Machine is reset.
|
Emitted when the Virtual Machine is reseted.
|
||||||
|
|
||||||
Data: None.
|
Data: None.
|
||||||
|
|
||||||
@@ -355,8 +185,7 @@ Emitted when the guest changes the RTC time.
|
|||||||
|
|
||||||
Data:
|
Data:
|
||||||
|
|
||||||
- "offset": Offset between base RTC clock (as specified by -rtc base), and
|
- "offset": delta against the host UTC in seconds (json-number)
|
||||||
new RTC clock value (json-number)
|
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
@@ -364,13 +193,10 @@ Example:
|
|||||||
"data": { "offset": 78 },
|
"data": { "offset": 78 },
|
||||||
"timestamp": { "seconds": 1267020223, "microseconds": 435656 } }
|
"timestamp": { "seconds": 1267020223, "microseconds": 435656 } }
|
||||||
|
|
||||||
Note: this event is rate-limited.
|
|
||||||
|
|
||||||
SHUTDOWN
|
SHUTDOWN
|
||||||
--------
|
--------
|
||||||
|
|
||||||
Emitted when the Virtual Machine has shut down, indicating that qemu
|
Emitted when the Virtual Machine is powered down.
|
||||||
is about to exit.
|
|
||||||
|
|
||||||
Data: None.
|
Data: None.
|
||||||
|
|
||||||
@@ -382,10 +208,10 @@ Example:
|
|||||||
Note: If the command-line option "-no-shutdown" has been specified, a STOP
|
Note: If the command-line option "-no-shutdown" has been specified, a STOP
|
||||||
event will eventually follow the SHUTDOWN event.
|
event will eventually follow the SHUTDOWN event.
|
||||||
|
|
||||||
SPICE_CONNECTED
|
SPICE_CONNECTED, SPICE_DISCONNECTED
|
||||||
---------------
|
-----------------------------------
|
||||||
|
|
||||||
Emitted when a SPICE client connects.
|
Emitted when a SPICE client connects or disconnects.
|
||||||
|
|
||||||
Data:
|
Data:
|
||||||
|
|
||||||
@@ -407,36 +233,11 @@ Example:
|
|||||||
"client": {"port": "52873", "family": "ipv4", "host": "127.0.0.1"}
|
"client": {"port": "52873", "family": "ipv4", "host": "127.0.0.1"}
|
||||||
}}
|
}}
|
||||||
|
|
||||||
SPICE_DISCONNECTED
|
|
||||||
------------------
|
|
||||||
|
|
||||||
Emitted when a SPICE client disconnects.
|
|
||||||
|
|
||||||
Data:
|
|
||||||
|
|
||||||
- "server": Server information (json-object)
|
|
||||||
- "host": IP address (json-string)
|
|
||||||
- "port": port number (json-string)
|
|
||||||
- "family": address family (json-string, "ipv4" or "ipv6")
|
|
||||||
- "client": Client information (json-object)
|
|
||||||
- "host": IP address (json-string)
|
|
||||||
- "port": port number (json-string)
|
|
||||||
- "family": address family (json-string, "ipv4" or "ipv6")
|
|
||||||
|
|
||||||
Example:
|
|
||||||
|
|
||||||
{ "timestamp": {"seconds": 1290688046, "microseconds": 388707},
|
|
||||||
"event": "SPICE_DISCONNECTED",
|
|
||||||
"data": {
|
|
||||||
"server": { "port": "5920", "family": "ipv4", "host": "127.0.0.1"},
|
|
||||||
"client": {"port": "52873", "family": "ipv4", "host": "127.0.0.1"}
|
|
||||||
}}
|
|
||||||
|
|
||||||
SPICE_INITIALIZED
|
SPICE_INITIALIZED
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
Emitted after initial handshake and authentication takes place (if any)
|
Emitted after initial handshake and authentication takes place (if any)
|
||||||
and the SPICE channel is up and running
|
and the SPICE channel is up'n'running
|
||||||
|
|
||||||
Data:
|
Data:
|
||||||
|
|
||||||
@@ -469,33 +270,6 @@ Example:
|
|||||||
"channel-id": 0, "tls": true}
|
"channel-id": 0, "tls": true}
|
||||||
}}
|
}}
|
||||||
|
|
||||||
SPICE_MIGRATE_COMPLETED
|
|
||||||
-----------------------
|
|
||||||
|
|
||||||
Emitted when SPICE migration has completed
|
|
||||||
|
|
||||||
Data: None.
|
|
||||||
|
|
||||||
Example:
|
|
||||||
|
|
||||||
{ "timestamp": {"seconds": 1290688046, "microseconds": 417172},
|
|
||||||
"event": "SPICE_MIGRATE_COMPLETED" }
|
|
||||||
|
|
||||||
MIGRATION
|
|
||||||
---------
|
|
||||||
|
|
||||||
Emitted when a migration event happens
|
|
||||||
|
|
||||||
Data: None.
|
|
||||||
|
|
||||||
- "status": migration status
|
|
||||||
See MigrationStatus in ~/qapi-schema.json for possible values
|
|
||||||
|
|
||||||
Example:
|
|
||||||
|
|
||||||
{"timestamp": {"seconds": 1432121972, "microseconds": 744001},
|
|
||||||
"event": "MIGRATION", "data": {"status": "completed"}}
|
|
||||||
|
|
||||||
STOP
|
STOP
|
||||||
----
|
----
|
||||||
|
|
||||||
@@ -624,24 +398,6 @@ Example:
|
|||||||
"host": "127.0.0.1", "sasl_username": "luiz" } },
|
"host": "127.0.0.1", "sasl_username": "luiz" } },
|
||||||
"timestamp": { "seconds": 1263475302, "microseconds": 150772 } }
|
"timestamp": { "seconds": 1263475302, "microseconds": 150772 } }
|
||||||
|
|
||||||
VSERPORT_CHANGE
|
|
||||||
---------------
|
|
||||||
|
|
||||||
Emitted when the guest opens or closes a virtio-serial port.
|
|
||||||
|
|
||||||
Data:
|
|
||||||
|
|
||||||
- "id": device identifier of the virtio-serial port (json-string)
|
|
||||||
- "open": true if the guest has opened the virtio-serial port (json-bool)
|
|
||||||
|
|
||||||
Example:
|
|
||||||
|
|
||||||
{ "event": "VSERPORT_CHANGE",
|
|
||||||
"data": { "id": "channel0", "open": true },
|
|
||||||
"timestamp": { "seconds": 1401385907, "microseconds": 422329 } }
|
|
||||||
|
|
||||||
Note: this event is rate-limited separately for each "id".
|
|
||||||
|
|
||||||
WAKEUP
|
WAKEUP
|
||||||
------
|
------
|
||||||
|
|
||||||
@@ -651,7 +407,7 @@ Data: None.
|
|||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
{ "event": "WAKEUP",
|
{ "event": "WATCHDOG",
|
||||||
"timestamp": { "seconds": 1344522075, "microseconds": 745528 } }
|
"timestamp": { "seconds": 1344522075, "microseconds": 745528 } }
|
||||||
|
|
||||||
WATCHDOG
|
WATCHDOG
|
||||||
@@ -672,5 +428,3 @@ Example:
|
|||||||
|
|
||||||
Note: If action is "reset", "shutdown", or "pause" the WATCHDOG event is
|
Note: If action is "reset", "shutdown", or "pause" the WATCHDOG event is
|
||||||
followed respectively by the RESET, SHUTDOWN, or STOP events.
|
followed respectively by the RESET, SHUTDOWN, or STOP events.
|
||||||
|
|
||||||
Note: this event is rate-limited.
|
|
||||||
@@ -29,45 +29,8 @@
|
|||||||
# (QEMU) device_add driver=e1000 id=net1
|
# (QEMU) device_add driver=e1000 id=net1
|
||||||
# {u'return': {}}
|
# {u'return': {}}
|
||||||
# (QEMU)
|
# (QEMU)
|
||||||
#
|
|
||||||
# key=value pairs also support Python or JSON object literal subset notations,
|
|
||||||
# without spaces. Dictionaries/objects {} are supported as are arrays [].
|
|
||||||
#
|
|
||||||
# example-command arg-name1={'key':'value','obj'={'prop':"value"}}
|
|
||||||
#
|
|
||||||
# Both JSON and Python formatting should work, including both styles of
|
|
||||||
# string literal quotes. Both paradigms of literal values should work,
|
|
||||||
# including null/true/false for JSON and None/True/False for Python.
|
|
||||||
#
|
|
||||||
#
|
|
||||||
# Transactions have the following multi-line format:
|
|
||||||
#
|
|
||||||
# transaction(
|
|
||||||
# action-name1 [ arg-name1=arg1 ] ... [arg-nameN=argN ]
|
|
||||||
# ...
|
|
||||||
# action-nameN [ arg-name1=arg1 ] ... [arg-nameN=argN ]
|
|
||||||
# )
|
|
||||||
#
|
|
||||||
# One line transactions are also supported:
|
|
||||||
#
|
|
||||||
# transaction( action-name1 ... )
|
|
||||||
#
|
|
||||||
# For example:
|
|
||||||
#
|
|
||||||
# (QEMU) transaction(
|
|
||||||
# TRANS> block-dirty-bitmap-add node=drive0 name=bitmap1
|
|
||||||
# TRANS> block-dirty-bitmap-clear node=drive0 name=bitmap0
|
|
||||||
# TRANS> )
|
|
||||||
# {"return": {}}
|
|
||||||
# (QEMU)
|
|
||||||
#
|
|
||||||
# Use the -v and -p options to activate the verbose and pretty-print options,
|
|
||||||
# which will echo back the properly formatted JSON-compliant QMP that is being
|
|
||||||
# sent to QEMU, which is useful for debugging and documentation generation.
|
|
||||||
|
|
||||||
import qmp
|
import qmp
|
||||||
import json
|
|
||||||
import ast
|
|
||||||
import readline
|
import readline
|
||||||
import sys
|
import sys
|
||||||
import pprint
|
import pprint
|
||||||
@@ -87,19 +50,6 @@ class QMPShellError(Exception):
|
|||||||
class QMPShellBadPort(QMPShellError):
|
class QMPShellBadPort(QMPShellError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class FuzzyJSON(ast.NodeTransformer):
|
|
||||||
'''This extension of ast.NodeTransformer filters literal "true/false/null"
|
|
||||||
values in an AST and replaces them by proper "True/False/None" values that
|
|
||||||
Python can properly evaluate.'''
|
|
||||||
def visit_Name(self, node):
|
|
||||||
if node.id == 'true':
|
|
||||||
node.id = 'True'
|
|
||||||
if node.id == 'false':
|
|
||||||
node.id = 'False'
|
|
||||||
if node.id == 'null':
|
|
||||||
node.id = 'None'
|
|
||||||
return node
|
|
||||||
|
|
||||||
# TODO: QMPShell's interface is a bit ugly (eg. _fill_completion() and
|
# TODO: QMPShell's interface is a bit ugly (eg. _fill_completion() and
|
||||||
# _execute_cmd()). Let's design a better one.
|
# _execute_cmd()). Let's design a better one.
|
||||||
class QMPShell(qmp.QEMUMonitorProtocol):
|
class QMPShell(qmp.QEMUMonitorProtocol):
|
||||||
@@ -108,8 +58,6 @@ class QMPShell(qmp.QEMUMonitorProtocol):
|
|||||||
self._greeting = None
|
self._greeting = None
|
||||||
self._completer = None
|
self._completer = None
|
||||||
self._pp = pp
|
self._pp = pp
|
||||||
self._transmode = False
|
|
||||||
self._actions = list()
|
|
||||||
|
|
||||||
def __get_address(self, arg):
|
def __get_address(self, arg):
|
||||||
"""
|
"""
|
||||||
@@ -139,55 +87,6 @@ class QMPShell(qmp.QEMUMonitorProtocol):
|
|||||||
# clearing everything as it doesn't seem to matter
|
# clearing everything as it doesn't seem to matter
|
||||||
readline.set_completer_delims('')
|
readline.set_completer_delims('')
|
||||||
|
|
||||||
def __parse_value(self, val):
|
|
||||||
try:
|
|
||||||
return int(val)
|
|
||||||
except ValueError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
if val.lower() == 'true':
|
|
||||||
return True
|
|
||||||
if val.lower() == 'false':
|
|
||||||
return False
|
|
||||||
if val.startswith(('{', '[')):
|
|
||||||
# Try first as pure JSON:
|
|
||||||
try:
|
|
||||||
return json.loads(val)
|
|
||||||
except ValueError:
|
|
||||||
pass
|
|
||||||
# Try once again as FuzzyJSON:
|
|
||||||
try:
|
|
||||||
st = ast.parse(val, mode='eval')
|
|
||||||
return ast.literal_eval(FuzzyJSON().visit(st))
|
|
||||||
except SyntaxError:
|
|
||||||
pass
|
|
||||||
except ValueError:
|
|
||||||
pass
|
|
||||||
return val
|
|
||||||
|
|
||||||
def __cli_expr(self, tokens, parent):
|
|
||||||
for arg in tokens:
|
|
||||||
(key, _, val) = arg.partition('=')
|
|
||||||
if not val:
|
|
||||||
raise QMPShellError("Expected a key=value pair, got '%s'" % arg)
|
|
||||||
|
|
||||||
value = self.__parse_value(val)
|
|
||||||
optpath = key.split('.')
|
|
||||||
curpath = []
|
|
||||||
for p in optpath[:-1]:
|
|
||||||
curpath.append(p)
|
|
||||||
d = parent.get(p, {})
|
|
||||||
if type(d) is not dict:
|
|
||||||
raise QMPShellError('Cannot use "%s" as both leaf and non-leaf key' % '.'.join(curpath))
|
|
||||||
parent[p] = d
|
|
||||||
parent = d
|
|
||||||
if optpath[-1] in parent:
|
|
||||||
if type(parent[optpath[-1]]) is dict:
|
|
||||||
raise QMPShellError('Cannot use "%s" as both leaf and non-leaf key' % '.'.join(curpath))
|
|
||||||
else:
|
|
||||||
raise QMPShellError('Cannot set "%s" multiple times' % key)
|
|
||||||
parent[optpath[-1]] = value
|
|
||||||
|
|
||||||
def __build_cmd(self, cmdline):
|
def __build_cmd(self, cmdline):
|
||||||
"""
|
"""
|
||||||
Build a QMP input object from a user provided command-line in the
|
Build a QMP input object from a user provided command-line in the
|
||||||
@@ -196,65 +95,32 @@ class QMPShell(qmp.QEMUMonitorProtocol):
|
|||||||
< command-name > [ arg-name1=arg1 ] ... [ arg-nameN=argN ]
|
< command-name > [ arg-name1=arg1 ] ... [ arg-nameN=argN ]
|
||||||
"""
|
"""
|
||||||
cmdargs = cmdline.split()
|
cmdargs = cmdline.split()
|
||||||
|
|
||||||
# Transactional CLI entry/exit:
|
|
||||||
if cmdargs[0] == 'transaction(':
|
|
||||||
self._transmode = True
|
|
||||||
cmdargs.pop(0)
|
|
||||||
elif cmdargs[0] == ')' and self._transmode:
|
|
||||||
self._transmode = False
|
|
||||||
if len(cmdargs) > 1:
|
|
||||||
raise QMPShellError("Unexpected input after close of Transaction sub-shell")
|
|
||||||
qmpcmd = { 'execute': 'transaction',
|
|
||||||
'arguments': { 'actions': self._actions } }
|
|
||||||
self._actions = list()
|
|
||||||
return qmpcmd
|
|
||||||
|
|
||||||
# Nothing to process?
|
|
||||||
if not cmdargs:
|
|
||||||
return None
|
|
||||||
|
|
||||||
# Parse and then cache this Transactional Action
|
|
||||||
if self._transmode:
|
|
||||||
finalize = False
|
|
||||||
action = { 'type': cmdargs[0], 'data': {} }
|
|
||||||
if cmdargs[-1] == ')':
|
|
||||||
cmdargs.pop(-1)
|
|
||||||
finalize = True
|
|
||||||
self.__cli_expr(cmdargs[1:], action['data'])
|
|
||||||
self._actions.append(action)
|
|
||||||
return self.__build_cmd(')') if finalize else None
|
|
||||||
|
|
||||||
# Standard command: parse and return it to be executed.
|
|
||||||
qmpcmd = { 'execute': cmdargs[0], 'arguments': {} }
|
qmpcmd = { 'execute': cmdargs[0], 'arguments': {} }
|
||||||
self.__cli_expr(cmdargs[1:], qmpcmd['arguments'])
|
for arg in cmdargs[1:]:
|
||||||
|
opt = arg.split('=')
|
||||||
|
try:
|
||||||
|
value = int(opt[1])
|
||||||
|
except ValueError:
|
||||||
|
value = opt[1]
|
||||||
|
qmpcmd['arguments'][opt[0]] = value
|
||||||
return qmpcmd
|
return qmpcmd
|
||||||
|
|
||||||
def _print(self, qmp):
|
|
||||||
jsobj = json.dumps(qmp)
|
|
||||||
if self._pp is not None:
|
|
||||||
self._pp.pprint(jsobj)
|
|
||||||
else:
|
|
||||||
print str(jsobj)
|
|
||||||
|
|
||||||
def _execute_cmd(self, cmdline):
|
def _execute_cmd(self, cmdline):
|
||||||
try:
|
try:
|
||||||
qmpcmd = self.__build_cmd(cmdline)
|
qmpcmd = self.__build_cmd(cmdline)
|
||||||
except Exception, e:
|
except:
|
||||||
print 'Error while parsing command line: %s' % e
|
|
||||||
print 'command format: <command-name> ',
|
print 'command format: <command-name> ',
|
||||||
print '[arg-name1=arg1] ... [arg-nameN=argN]'
|
print '[arg-name1=arg1] ... [arg-nameN=argN]'
|
||||||
return True
|
return True
|
||||||
# For transaction mode, we may have just cached the action:
|
|
||||||
if qmpcmd is None:
|
|
||||||
return True
|
|
||||||
if self._verbose:
|
|
||||||
self._print(qmpcmd)
|
|
||||||
resp = self.cmd_obj(qmpcmd)
|
resp = self.cmd_obj(qmpcmd)
|
||||||
if resp is None:
|
if resp is None:
|
||||||
print 'Disconnected'
|
print 'Disconnected'
|
||||||
return False
|
return False
|
||||||
self._print(resp)
|
|
||||||
|
if self._pp is not None:
|
||||||
|
self._pp.pprint(resp)
|
||||||
|
else:
|
||||||
|
print resp
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def connect(self):
|
def connect(self):
|
||||||
@@ -266,11 +132,6 @@ class QMPShell(qmp.QEMUMonitorProtocol):
|
|||||||
version = self._greeting['QMP']['version']['qemu']
|
version = self._greeting['QMP']['version']['qemu']
|
||||||
print 'Connected to QEMU %d.%d.%d\n' % (version['major'],version['minor'],version['micro'])
|
print 'Connected to QEMU %d.%d.%d\n' % (version['major'],version['minor'],version['micro'])
|
||||||
|
|
||||||
def get_prompt(self):
|
|
||||||
if self._transmode:
|
|
||||||
return "TRANS> "
|
|
||||||
return "(QEMU) "
|
|
||||||
|
|
||||||
def read_exec_command(self, prompt):
|
def read_exec_command(self, prompt):
|
||||||
"""
|
"""
|
||||||
Read and execute a command.
|
Read and execute a command.
|
||||||
@@ -290,9 +151,6 @@ class QMPShell(qmp.QEMUMonitorProtocol):
|
|||||||
else:
|
else:
|
||||||
return self._execute_cmd(cmdline)
|
return self._execute_cmd(cmdline)
|
||||||
|
|
||||||
def set_verbosity(self, verbose):
|
|
||||||
self._verbose = verbose
|
|
||||||
|
|
||||||
class HMPShell(QMPShell):
|
class HMPShell(QMPShell):
|
||||||
def __init__(self, address):
|
def __init__(self, address):
|
||||||
QMPShell.__init__(self, address)
|
QMPShell.__init__(self, address)
|
||||||
@@ -370,7 +228,7 @@ def die(msg):
|
|||||||
def fail_cmdline(option=None):
|
def fail_cmdline(option=None):
|
||||||
if option:
|
if option:
|
||||||
sys.stderr.write('ERROR: bad command-line option \'%s\'\n' % option)
|
sys.stderr.write('ERROR: bad command-line option \'%s\'\n' % option)
|
||||||
sys.stderr.write('qemu-shell [ -v ] [ -p ] [ -H ] < UNIX socket path> | < TCP address:port >\n')
|
sys.stderr.write('qemu-shell [ -p ] [ -H ] < UNIX socket path> | < TCP address:port >\n')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@@ -378,7 +236,6 @@ def main():
|
|||||||
qemu = None
|
qemu = None
|
||||||
hmp = False
|
hmp = False
|
||||||
pp = None
|
pp = None
|
||||||
verbose = False
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
for arg in sys.argv[1:]:
|
for arg in sys.argv[1:]:
|
||||||
@@ -390,8 +247,6 @@ def main():
|
|||||||
if pp is not None:
|
if pp is not None:
|
||||||
fail_cmdline(arg)
|
fail_cmdline(arg)
|
||||||
pp = pprint.PrettyPrinter(indent=4)
|
pp = pprint.PrettyPrinter(indent=4)
|
||||||
elif arg == "-v":
|
|
||||||
verbose = True
|
|
||||||
else:
|
else:
|
||||||
if qemu is not None:
|
if qemu is not None:
|
||||||
fail_cmdline(arg)
|
fail_cmdline(arg)
|
||||||
@@ -416,8 +271,7 @@ def main():
|
|||||||
die('Could not connect to %s' % addr)
|
die('Could not connect to %s' % addr)
|
||||||
|
|
||||||
qemu.show_banner()
|
qemu.show_banner()
|
||||||
qemu.set_verbosity(verbose)
|
while qemu.read_exec_command('(QEMU) '):
|
||||||
while qemu.read_exec_command(qemu.get_prompt()):
|
|
||||||
pass
|
pass
|
||||||
qemu.close()
|
qemu.close()
|
||||||
|
|
||||||
@@ -1,55 +1,35 @@
|
|||||||
QEMU Machine Protocol Specification
|
QEMU Monitor Protocol Specification - Version 0.1
|
||||||
|
|
||||||
0. About This Document
|
|
||||||
======================
|
|
||||||
|
|
||||||
Copyright (C) 2009-2015 Red Hat, Inc.
|
|
||||||
|
|
||||||
This work is licensed under the terms of the GNU GPL, version 2 or
|
|
||||||
later. See the COPYING file in the top-level directory.
|
|
||||||
|
|
||||||
1. Introduction
|
1. Introduction
|
||||||
===============
|
===============
|
||||||
|
|
||||||
This document specifies the QEMU Machine Protocol (QMP), a JSON-based
|
This document specifies the QEMU Monitor Protocol (QMP), a JSON-based protocol
|
||||||
protocol which is available for applications to operate QEMU at the
|
which is available for applications to control QEMU at the machine-level.
|
||||||
machine-level. It is also in use by the QEMU Guest Agent (QGA), which
|
|
||||||
is available for host applications to interact with the guest
|
To enable QMP support, QEMU has to be run in "control mode". This is done by
|
||||||
operating system.
|
starting QEMU with the appropriate command-line options. Please, refer to the
|
||||||
|
QEMU manual page for more information.
|
||||||
|
|
||||||
2. Protocol Specification
|
2. Protocol Specification
|
||||||
=========================
|
=========================
|
||||||
|
|
||||||
This section details the protocol format. For the purpose of this document
|
This section details the protocol format. For the purpose of this document
|
||||||
"Client" is any application which is using QMP to communicate with QEMU and
|
"Client" is any application which is communicating with QEMU in control mode,
|
||||||
"Server" is QEMU itself.
|
and "Server" is QEMU itself.
|
||||||
|
|
||||||
JSON data structures, when mentioned in this document, are always in the
|
JSON data structures, when mentioned in this document, are always in the
|
||||||
following format:
|
following format:
|
||||||
|
|
||||||
json-DATA-STRUCTURE-NAME
|
json-DATA-STRUCTURE-NAME
|
||||||
|
|
||||||
Where DATA-STRUCTURE-NAME is any valid JSON data structure, as defined
|
Where DATA-STRUCTURE-NAME is any valid JSON data structure, as defined by
|
||||||
by the JSON standard:
|
the JSON standard:
|
||||||
|
|
||||||
http://www.ietf.org/rfc/rfc7159.txt
|
http://www.ietf.org/rfc/rfc4627.txt
|
||||||
|
|
||||||
The protocol is always encoded in UTF-8 except for synchronization
|
For convenience, json-object members and json-array elements mentioned in
|
||||||
bytes (documented below); although thanks to json-string escape
|
this document will be in a certain order. However, in real protocol usage
|
||||||
sequences, the server will reply using only the strict ASCII subset.
|
they can be in ANY order, thus no particular order should be assumed.
|
||||||
|
|
||||||
For convenience, json-object members mentioned in this document will
|
|
||||||
be in a certain order. However, in real protocol usage they can be in
|
|
||||||
ANY order, thus no particular order should be assumed. On the other
|
|
||||||
hand, use of json-array elements presumes that preserving order is
|
|
||||||
important unless specifically documented otherwise. Repeating a key
|
|
||||||
within a json-object gives unpredictable results.
|
|
||||||
|
|
||||||
Also for convenience, the server will accept an extension of
|
|
||||||
'single-quoted' strings in place of the usual "double-quoted"
|
|
||||||
json-string, and both input forms of strings understand an additional
|
|
||||||
escape sequence of "\'" for a single quote. The server will only use
|
|
||||||
double quoting on output.
|
|
||||||
|
|
||||||
2.1 General Definitions
|
2.1 General Definitions
|
||||||
-----------------------
|
-----------------------
|
||||||
@@ -67,25 +47,16 @@ that the connection has been successfully established and that the Server is
|
|||||||
ready for capabilities negotiation (for more information refer to section
|
ready for capabilities negotiation (for more information refer to section
|
||||||
'4. Capabilities Negotiation').
|
'4. Capabilities Negotiation').
|
||||||
|
|
||||||
The greeting message format is:
|
The format is:
|
||||||
|
|
||||||
{ "QMP": { "version": json-object, "capabilities": json-array } }
|
{ "QMP": { "version": json-object, "capabilities": json-array } }
|
||||||
|
|
||||||
Where,
|
Where,
|
||||||
|
|
||||||
- The "version" member contains the Server's version information (the format
|
- The "version" member contains the Server's version information (the format
|
||||||
is the same of the query-version command)
|
is the same of the 'query-version' command)
|
||||||
- The "capabilities" member specify the availability of features beyond the
|
- The "capabilities" member specify the availability of features beyond the
|
||||||
baseline specification; the order of elements in this array has no
|
baseline specification
|
||||||
particular significance, so a client must search the entire array
|
|
||||||
when looking for a particular capability
|
|
||||||
|
|
||||||
2.2.1 Capabilities
|
|
||||||
------------------
|
|
||||||
|
|
||||||
As of the date this document was last revised, no server or client
|
|
||||||
capability strings have been defined.
|
|
||||||
|
|
||||||
|
|
||||||
2.3 Issuing Commands
|
2.3 Issuing Commands
|
||||||
--------------------
|
--------------------
|
||||||
@@ -98,14 +69,10 @@ The format for command execution is:
|
|||||||
|
|
||||||
- The "execute" member identifies the command to be executed by the Server
|
- The "execute" member identifies the command to be executed by the Server
|
||||||
- The "arguments" member is used to pass any arguments required for the
|
- The "arguments" member is used to pass any arguments required for the
|
||||||
execution of the command, it is optional when no arguments are
|
execution of the command, it is optional when no arguments are required
|
||||||
required. Each command documents what contents will be considered
|
|
||||||
valid when handling the json-argument
|
|
||||||
- The "id" member is a transaction identification associated with the
|
- The "id" member is a transaction identification associated with the
|
||||||
command execution, it is optional and will be part of the response if
|
command execution, it is optional and will be part of the response if
|
||||||
provided. The "id" member can be any json-value, although most
|
provided
|
||||||
clients merely use a json-number incremented for each successive
|
|
||||||
command
|
|
||||||
|
|
||||||
2.4 Commands Responses
|
2.4 Commands Responses
|
||||||
----------------------
|
----------------------
|
||||||
@@ -116,24 +83,28 @@ of a command execution: success or error.
|
|||||||
2.4.1 success
|
2.4.1 success
|
||||||
-------------
|
-------------
|
||||||
|
|
||||||
The format of a success response is:
|
The success response is issued when the command execution has finished
|
||||||
|
without errors.
|
||||||
|
|
||||||
{ "return": json-value, "id": json-value }
|
The format is:
|
||||||
|
|
||||||
|
{ "return": json-object, "id": json-value }
|
||||||
|
|
||||||
Where,
|
Where,
|
||||||
|
|
||||||
- The "return" member contains the data returned by the command, which
|
- The "return" member contains the command returned data, which is defined
|
||||||
is defined on a per-command basis (usually a json-object or
|
in a per-command basis or an empty json-object if the command does not
|
||||||
json-array of json-objects, but sometimes a json-number, json-string,
|
return data
|
||||||
or json-array of json-strings); it is an empty json-object if the
|
|
||||||
command does not return data
|
|
||||||
- The "id" member contains the transaction identification associated
|
- The "id" member contains the transaction identification associated
|
||||||
with the command execution if issued by the Client
|
with the command execution (if issued by the Client)
|
||||||
|
|
||||||
2.4.2 error
|
2.4.2 error
|
||||||
-----------
|
-----------
|
||||||
|
|
||||||
The format of an error response is:
|
The error response is issued when the command execution could not be
|
||||||
|
completed because of an error condition.
|
||||||
|
|
||||||
|
The format is:
|
||||||
|
|
||||||
{ "error": { "class": json-string, "desc": json-string }, "id": json-value }
|
{ "error": { "class": json-string, "desc": json-string }, "id": json-value }
|
||||||
|
|
||||||
@@ -143,7 +114,7 @@ The format of an error response is:
|
|||||||
- The "desc" member is a human-readable error message. Clients should
|
- The "desc" member is a human-readable error message. Clients should
|
||||||
not attempt to parse this message.
|
not attempt to parse this message.
|
||||||
- The "id" member contains the transaction identification associated with
|
- The "id" member contains the transaction identification associated with
|
||||||
the command execution if issued by the Client
|
the command execution (if issued by the Client)
|
||||||
|
|
||||||
NOTE: Some errors can occur before the Server is able to read the "id" member,
|
NOTE: Some errors can occur before the Server is able to read the "id" member,
|
||||||
in these cases the "id" member will not be part of the error response, even
|
in these cases the "id" member will not be part of the error response, even
|
||||||
@@ -153,10 +124,9 @@ if provided by the client.
|
|||||||
-----------------------
|
-----------------------
|
||||||
|
|
||||||
As a result of state changes, the Server may send messages unilaterally
|
As a result of state changes, the Server may send messages unilaterally
|
||||||
to the Client at any time, when not in the middle of any other
|
to the Client at any time. They are called 'asynchronous events'.
|
||||||
response. They are called "asynchronous events".
|
|
||||||
|
|
||||||
The format of asynchronous events is:
|
The format is:
|
||||||
|
|
||||||
{ "event": json-string, "data": json-object,
|
{ "event": json-string, "data": json-object,
|
||||||
"timestamp": { "seconds": json-number, "microseconds": json-number } }
|
"timestamp": { "seconds": json-number, "microseconds": json-number } }
|
||||||
@@ -166,94 +136,69 @@ The format of asynchronous events is:
|
|||||||
- The "event" member contains the event's name
|
- The "event" member contains the event's name
|
||||||
- The "data" member contains event specific data, which is defined in a
|
- The "data" member contains event specific data, which is defined in a
|
||||||
per-event basis, it is optional
|
per-event basis, it is optional
|
||||||
- The "timestamp" member contains the exact time of when the event
|
- The "timestamp" member contains the exact time of when the event occurred
|
||||||
occurred in the Server. It is a fixed json-object with time in
|
in the Server. It is a fixed json-object with time in seconds and
|
||||||
seconds and microseconds relative to the Unix Epoch (1 Jan 1970); if
|
microseconds
|
||||||
there is a failure to retrieve host time, both members of the
|
|
||||||
timestamp will be set to -1.
|
|
||||||
|
|
||||||
For a listing of supported asynchronous events, please, refer to the
|
For a listing of supported asynchronous events, please, refer to the
|
||||||
qmp-events.txt file.
|
qmp-events.txt file.
|
||||||
|
|
||||||
Some events are rate-limited to at most one per second. If additional
|
|
||||||
"similar" events arrive within one second, all but the last one are
|
|
||||||
dropped, and the last one is delayed. "Similar" normally means same
|
|
||||||
event type. See qmp-events.txt for details.
|
|
||||||
|
|
||||||
2.5 QGA Synchronization
|
|
||||||
-----------------------
|
|
||||||
|
|
||||||
When using QGA, an additional synchronization feature is built into
|
|
||||||
the protocol. If the Client sends a raw 0xFF sentinel byte (not valid
|
|
||||||
JSON), then the Server will reset its state and discard all pending
|
|
||||||
data prior to the sentinel. Conversely, if the Client makes use of
|
|
||||||
the 'guest-sync-delimited' command, the Server will send a raw 0xFF
|
|
||||||
sentinel byte prior to its response, to aid the Client in discarding
|
|
||||||
any data prior to the sentinel.
|
|
||||||
|
|
||||||
|
|
||||||
3. QMP Examples
|
3. QMP Examples
|
||||||
===============
|
===============
|
||||||
|
|
||||||
This section provides some examples of real QMP usage, in all of them
|
This section provides some examples of real QMP usage, in all of them
|
||||||
"C" stands for "Client" and "S" stands for "Server".
|
'C' stands for 'Client' and 'S' stands for 'Server'.
|
||||||
|
|
||||||
3.1 Server greeting
|
3.1 Server greeting
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
S: { "QMP": { "version": { "qemu": { "micro": 50, "minor": 6, "major": 1 },
|
S: {"QMP": {"version": {"qemu": "0.12.50", "package": ""}, "capabilities": []}}
|
||||||
"package": ""}, "capabilities": []}}
|
|
||||||
|
|
||||||
3.2 Client QMP negotiation
|
3.2 Simple 'stop' execution
|
||||||
--------------------------
|
|
||||||
C: { "execute": "qmp_capabilities" }
|
|
||||||
S: { "return": {}}
|
|
||||||
|
|
||||||
3.3 Simple 'stop' execution
|
|
||||||
---------------------------
|
---------------------------
|
||||||
|
|
||||||
C: { "execute": "stop" }
|
C: { "execute": "stop" }
|
||||||
S: {"return": {}}
|
S: {"return": {}}
|
||||||
|
|
||||||
3.4 KVM information
|
3.3 KVM information
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
C: { "execute": "query-kvm", "id": "example" }
|
C: { "execute": "query-kvm", "id": "example" }
|
||||||
S: {"return": {"enabled": true, "present": true}, "id": "example"}
|
S: {"return": {"enabled": true, "present": true}, "id": "example"}
|
||||||
|
|
||||||
3.5 Parsing error
|
3.4 Parsing error
|
||||||
------------------
|
------------------
|
||||||
|
|
||||||
C: { "execute": }
|
C: { "execute": }
|
||||||
S: {"error": {"class": "GenericError", "desc": "Invalid JSON syntax" } }
|
S: {"error": {"class": "GenericError", "desc": "Invalid JSON syntax" } }
|
||||||
|
|
||||||
3.6 Powerdown event
|
3.5 Powerdown event
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
S: { "timestamp": { "seconds": 1258551470, "microseconds": 802384 },
|
S: {"timestamp": {"seconds": 1258551470, "microseconds": 802384}, "event":
|
||||||
"event": "POWERDOWN" }
|
"POWERDOWN"}
|
||||||
|
|
||||||
4. Capabilities Negotiation
|
4. Capabilities Negotiation
|
||||||
===========================
|
----------------------------
|
||||||
|
|
||||||
When a Client successfully establishes a connection, the Server is in
|
When a Client successfully establishes a connection, the Server is in
|
||||||
Capabilities Negotiation mode.
|
Capabilities Negotiation mode.
|
||||||
|
|
||||||
In this mode only the qmp_capabilities command is allowed to run, all
|
In this mode only the 'qmp_capabilities' command is allowed to run, all
|
||||||
other commands will return the CommandNotFound error. Asynchronous
|
other commands will return the CommandNotFound error. Asynchronous messages
|
||||||
messages are not delivered either.
|
are not delivered either.
|
||||||
|
|
||||||
Clients should use the qmp_capabilities command to enable capabilities
|
Clients should use the 'qmp_capabilities' command to enable capabilities
|
||||||
advertised in the Server's greeting (section '2.2 Server Greeting') they
|
advertised in the Server's greeting (section '2.2 Server Greeting') they
|
||||||
support.
|
support.
|
||||||
|
|
||||||
When the qmp_capabilities command is issued, and if it does not return an
|
When the 'qmp_capabilities' command is issued, and if it does not return an
|
||||||
error, the Server enters in Command mode where capabilities changes take
|
error, the Server enters in Command mode where capabilities changes take
|
||||||
effect, all commands (except qmp_capabilities) are allowed and asynchronous
|
effect, all commands (except 'qmp_capabilities') are allowed and asynchronous
|
||||||
messages are delivered.
|
messages are delivered.
|
||||||
|
|
||||||
5 Compatibility Considerations
|
5 Compatibility Considerations
|
||||||
==============================
|
------------------------------
|
||||||
|
|
||||||
All protocol changes or new features which modify the protocol format in an
|
All protocol changes or new features which modify the protocol format in an
|
||||||
incompatible way are disabled by default and will be advertised by the
|
incompatible way are disabled by default and will be advertised by the
|
||||||
@@ -277,16 +222,12 @@ However, Clients must not assume any particular:
|
|||||||
- Amount of errors generated by a command, that is, new errors can be added
|
- Amount of errors generated by a command, that is, new errors can be added
|
||||||
to any existing command in newer versions of the Server
|
to any existing command in newer versions of the Server
|
||||||
|
|
||||||
Any command or field name beginning with "x-" is deemed experimental,
|
|
||||||
and may be withdrawn or changed in an incompatible manner in a future
|
|
||||||
release.
|
|
||||||
|
|
||||||
Of course, the Server does guarantee to send valid JSON. But apart from
|
Of course, the Server does guarantee to send valid JSON. But apart from
|
||||||
this, a Client should be "conservative in what they send, and liberal in
|
this, a Client should be "conservative in what they send, and liberal in
|
||||||
what they accept".
|
what they accept".
|
||||||
|
|
||||||
6. Downstream extension of QMP
|
6. Downstream extension of QMP
|
||||||
==============================
|
------------------------------
|
||||||
|
|
||||||
We recommend that downstream consumers of QEMU do *not* modify QMP.
|
We recommend that downstream consumers of QEMU do *not* modify QMP.
|
||||||
Management tools should be able to support both upstream and downstream
|
Management tools should be able to support both upstream and downstream
|
||||||
@@ -304,7 +245,7 @@ arguments, errors, asynchronous events, and so forth.
|
|||||||
|
|
||||||
Any new names downstream wishes to add must begin with '__'. To
|
Any new names downstream wishes to add must begin with '__'. To
|
||||||
ensure compatibility with other downstreams, it is strongly
|
ensure compatibility with other downstreams, it is strongly
|
||||||
recommended that you prefix your downstream names with '__RFQDN_' where
|
recommended that you prefix your downstram names with '__RFQDN_' where
|
||||||
RFQDN is a valid, reverse fully qualified domain name which you
|
RFQDN is a valid, reverse fully qualified domain name which you
|
||||||
control. For example, a qemu-kvm specific monitor command would be:
|
control. For example, a qemu-kvm specific monitor command would be:
|
||||||
|
|
||||||
@@ -21,9 +21,6 @@ class QMPConnectError(QMPError):
|
|||||||
class QMPCapabilitiesError(QMPError):
|
class QMPCapabilitiesError(QMPError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class QMPTimeoutError(QMPError):
|
|
||||||
pass
|
|
||||||
|
|
||||||
class QEMUMonitorProtocol:
|
class QEMUMonitorProtocol:
|
||||||
def __init__(self, address, server=False):
|
def __init__(self, address, server=False):
|
||||||
"""
|
"""
|
||||||
@@ -75,44 +72,6 @@ class QEMUMonitorProtocol:
|
|||||||
|
|
||||||
error = socket.error
|
error = socket.error
|
||||||
|
|
||||||
def __get_events(self, wait=False):
|
|
||||||
"""
|
|
||||||
Check for new events in the stream and cache them in __events.
|
|
||||||
|
|
||||||
@param wait (bool): block until an event is available.
|
|
||||||
@param wait (float): If wait is a float, treat it as a timeout value.
|
|
||||||
|
|
||||||
@raise QMPTimeoutError: If a timeout float is provided and the timeout
|
|
||||||
period elapses.
|
|
||||||
@raise QMPConnectError: If wait is True but no events could be retrieved
|
|
||||||
or if some other error occurred.
|
|
||||||
"""
|
|
||||||
|
|
||||||
# Check for new events regardless and pull them into the cache:
|
|
||||||
self.__sock.setblocking(0)
|
|
||||||
try:
|
|
||||||
self.__json_read()
|
|
||||||
except socket.error, err:
|
|
||||||
if err[0] == errno.EAGAIN:
|
|
||||||
# No data available
|
|
||||||
pass
|
|
||||||
self.__sock.setblocking(1)
|
|
||||||
|
|
||||||
# Wait for new events, if needed.
|
|
||||||
# if wait is 0.0, this means "no wait" and is also implicitly false.
|
|
||||||
if not self.__events and wait:
|
|
||||||
if isinstance(wait, float):
|
|
||||||
self.__sock.settimeout(wait)
|
|
||||||
try:
|
|
||||||
ret = self.__json_read(only_event=True)
|
|
||||||
except socket.timeout:
|
|
||||||
raise QMPTimeoutError("Timeout waiting for event")
|
|
||||||
except:
|
|
||||||
raise QMPConnectError("Error while reading from socket")
|
|
||||||
if ret is None:
|
|
||||||
raise QMPConnectError("Error while reading from socket")
|
|
||||||
self.__sock.settimeout(None)
|
|
||||||
|
|
||||||
def connect(self, negotiate=True):
|
def connect(self, negotiate=True):
|
||||||
"""
|
"""
|
||||||
Connect to the QMP Monitor and perform capabilities negotiation.
|
Connect to the QMP Monitor and perform capabilities negotiation.
|
||||||
@@ -181,37 +140,38 @@ class QEMUMonitorProtocol:
|
|||||||
"""
|
"""
|
||||||
Get and delete the first available QMP event.
|
Get and delete the first available QMP event.
|
||||||
|
|
||||||
@param wait (bool): block until an event is available.
|
@param wait: block until an event is available (bool)
|
||||||
@param wait (float): If wait is a float, treat it as a timeout value.
|
|
||||||
|
|
||||||
@raise QMPTimeoutError: If a timeout float is provided and the timeout
|
|
||||||
period elapses.
|
|
||||||
@raise QMPConnectError: If wait is True but no events could be retrieved
|
|
||||||
or if some other error occurred.
|
|
||||||
|
|
||||||
@return The first available QMP event, or None.
|
|
||||||
"""
|
"""
|
||||||
self.__get_events(wait)
|
self.__sock.setblocking(0)
|
||||||
|
try:
|
||||||
if self.__events:
|
self.__json_read()
|
||||||
return self.__events.pop(0)
|
except socket.error, err:
|
||||||
return None
|
if err[0] == errno.EAGAIN:
|
||||||
|
# No data available
|
||||||
|
pass
|
||||||
|
self.__sock.setblocking(1)
|
||||||
|
if not self.__events and wait:
|
||||||
|
self.__json_read(only_event=True)
|
||||||
|
event = self.__events[0]
|
||||||
|
del self.__events[0]
|
||||||
|
return event
|
||||||
|
|
||||||
def get_events(self, wait=False):
|
def get_events(self, wait=False):
|
||||||
"""
|
"""
|
||||||
Get a list of available QMP events.
|
Get a list of available QMP events.
|
||||||
|
|
||||||
@param wait (bool): block until an event is available.
|
@param wait: block until an event is available (bool)
|
||||||
@param wait (float): If wait is a float, treat it as a timeout value.
|
|
||||||
|
|
||||||
@raise QMPTimeoutError: If a timeout float is provided and the timeout
|
|
||||||
period elapses.
|
|
||||||
@raise QMPConnectError: If wait is True but no events could be retrieved
|
|
||||||
or if some other error occurred.
|
|
||||||
|
|
||||||
@return The list of available QMP events.
|
|
||||||
"""
|
"""
|
||||||
self.__get_events(wait)
|
self.__sock.setblocking(0)
|
||||||
|
try:
|
||||||
|
self.__json_read()
|
||||||
|
except socket.error, err:
|
||||||
|
if err[0] == errno.EAGAIN:
|
||||||
|
# No data available
|
||||||
|
pass
|
||||||
|
self.__sock.setblocking(1)
|
||||||
|
if not self.__events and wait:
|
||||||
|
self.__json_read(only_event=True)
|
||||||
return self.__events
|
return self.__events
|
||||||
|
|
||||||
def clear_events(self):
|
def clear_events(self):
|
||||||
@@ -228,9 +188,3 @@ class QEMUMonitorProtocol:
|
|||||||
|
|
||||||
def settimeout(self, timeout):
|
def settimeout(self, timeout):
|
||||||
self.__sock.settimeout(timeout)
|
self.__sock.settimeout(timeout)
|
||||||
|
|
||||||
def get_sock_fd(self):
|
|
||||||
return self.__sock.fileno()
|
|
||||||
|
|
||||||
def is_scm_available(self):
|
|
||||||
return self.__sock.family == socket.AF_UNIX
|
|
||||||
108
README
108
README
@@ -1,107 +1,3 @@
|
|||||||
QEMU README
|
Read the documentation in qemu-doc.html or on http://wiki.qemu.org
|
||||||
===========
|
|
||||||
|
|
||||||
QEMU is a generic and open source machine & userspace emulator and
|
- QEMU team
|
||||||
virtualizer.
|
|
||||||
|
|
||||||
QEMU is capable of emulating a complete machine in software without any
|
|
||||||
need for hardware virtualization support. By using dynamic translation,
|
|
||||||
it achieves very good performance. QEMU can also integrate with the Xen
|
|
||||||
and KVM hypervisors to provide emulated hardware while allowing the
|
|
||||||
hypervisor to manage the CPU. With hypervisor support, QEMU can achieve
|
|
||||||
near native performance for CPUs. When QEMU emulates CPUs directly it is
|
|
||||||
capable of running operating systems made for one machine (e.g. an ARMv7
|
|
||||||
board) on a different machine (e.g. an x86_64 PC board).
|
|
||||||
|
|
||||||
QEMU is also capable of providing userspace API virtualization for Linux
|
|
||||||
and BSD kernel interfaces. This allows binaries compiled against one
|
|
||||||
architecture ABI (e.g. the Linux PPC64 ABI) to be run on a host using a
|
|
||||||
different architecture ABI (e.g. the Linux x86_64 ABI). This does not
|
|
||||||
involve any hardware emulation, simply CPU and syscall emulation.
|
|
||||||
|
|
||||||
QEMU aims to fit into a variety of use cases. It can be invoked directly
|
|
||||||
by users wishing to have full control over its behaviour and settings.
|
|
||||||
It also aims to facilitate integration into higher level management
|
|
||||||
layers, by providing a stable command line interface and monitor API.
|
|
||||||
It is commonly invoked indirectly via the libvirt library when using
|
|
||||||
open source applications such as oVirt, OpenStack and virt-manager.
|
|
||||||
|
|
||||||
QEMU as a whole is released under the GNU General Public License,
|
|
||||||
version 2. For full licensing details, consult the LICENSE file.
|
|
||||||
|
|
||||||
|
|
||||||
Building
|
|
||||||
========
|
|
||||||
|
|
||||||
QEMU is multi-platform software intended to be buildable on all modern
|
|
||||||
Linux platforms, OS-X, Win32 (via the Mingw64 toolchain) and a variety
|
|
||||||
of other UNIX targets. The simple steps to build QEMU are:
|
|
||||||
|
|
||||||
mkdir build
|
|
||||||
cd build
|
|
||||||
../configure
|
|
||||||
make
|
|
||||||
|
|
||||||
Complete details of the process for building and configuring QEMU for
|
|
||||||
all supported host platforms can be found in the qemu-tech.html file.
|
|
||||||
Additional information can also be found online via the QEMU website:
|
|
||||||
|
|
||||||
http://qemu-project.org/Hosts/Linux
|
|
||||||
http://qemu-project.org/Hosts/W32
|
|
||||||
|
|
||||||
|
|
||||||
Submitting patches
|
|
||||||
==================
|
|
||||||
|
|
||||||
The QEMU source code is maintained under the GIT version control system.
|
|
||||||
|
|
||||||
git clone git://git.qemu-project.org/qemu.git
|
|
||||||
|
|
||||||
When submitting patches, the preferred approach is to use 'git
|
|
||||||
format-patch' and/or 'git send-email' to format & send the mail to the
|
|
||||||
qemu-devel@nongnu.org mailing list. All patches submitted must contain
|
|
||||||
a 'Signed-off-by' line from the author. Patches should follow the
|
|
||||||
guidelines set out in the HACKING and CODING_STYLE files.
|
|
||||||
|
|
||||||
Additional information on submitting patches can be found online via
|
|
||||||
the QEMU website
|
|
||||||
|
|
||||||
http://qemu-project.org/Contribute/SubmitAPatch
|
|
||||||
http://qemu-project.org/Contribute/TrivialPatches
|
|
||||||
|
|
||||||
|
|
||||||
Bug reporting
|
|
||||||
=============
|
|
||||||
|
|
||||||
The QEMU project uses Launchpad as its primary upstream bug tracker. Bugs
|
|
||||||
found when running code built from QEMU git or upstream released sources
|
|
||||||
should be reported via:
|
|
||||||
|
|
||||||
https://bugs.launchpad.net/qemu/
|
|
||||||
|
|
||||||
If using QEMU via an operating system vendor pre-built binary package, it
|
|
||||||
is preferable to report bugs to the vendor's own bug tracker first. If
|
|
||||||
the bug is also known to affect latest upstream code, it can also be
|
|
||||||
reported via launchpad.
|
|
||||||
|
|
||||||
For additional information on bug reporting consult:
|
|
||||||
|
|
||||||
http://qemu-project.org/Contribute/ReportABug
|
|
||||||
|
|
||||||
|
|
||||||
Contact
|
|
||||||
=======
|
|
||||||
|
|
||||||
The QEMU community can be contacted in a number of ways, with the two
|
|
||||||
main methods being email and IRC
|
|
||||||
|
|
||||||
- qemu-devel@nongnu.org
|
|
||||||
http://lists.nongnu.org/mailman/listinfo/qemu-devel
|
|
||||||
- #qemu on irc.oftc.net
|
|
||||||
|
|
||||||
Information on additional methods of contacting the community can be
|
|
||||||
found online via the QEMU website:
|
|
||||||
|
|
||||||
http://qemu-project.org/Contribute/StartHere
|
|
||||||
|
|
||||||
-- End
|
|
||||||
|
|||||||
37
TODO
Normal file
37
TODO
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
General:
|
||||||
|
-------
|
||||||
|
- cycle counter for all archs
|
||||||
|
- cpu_interrupt() win32/SMP fix
|
||||||
|
- merge PIC spurious interrupt patch
|
||||||
|
- warning for OS/2: must not use 128 MB memory (merge bochs cmos patch ?)
|
||||||
|
- config file (at least for windows/Mac OS X)
|
||||||
|
- update doc: PCI infos.
|
||||||
|
- basic VGA optimizations
|
||||||
|
- better code fetch
|
||||||
|
- do not resize vga if invalid size.
|
||||||
|
- TLB code protection support for PPC
|
||||||
|
- disable SMC handling for ARM/SPARC/PPC (not finished)
|
||||||
|
- see undefined flags for BTx insn
|
||||||
|
- keyboard output buffer filling timing emulation
|
||||||
|
- tests for each target CPU
|
||||||
|
- fix all remaining thread lock issues (must put TBs in a specific invalid
|
||||||
|
state, find a solution for tb_flush()).
|
||||||
|
|
||||||
|
ppc specific:
|
||||||
|
------------
|
||||||
|
- TLB invalidate not needed if msr_pr changes
|
||||||
|
- enable shift optimizations ?
|
||||||
|
|
||||||
|
linux-user specific:
|
||||||
|
-------------------
|
||||||
|
- remove threading support as it cannot work at this point
|
||||||
|
- improve IPC syscalls
|
||||||
|
- more syscalls (in particular all 64 bit ones, IPCs, fix 64 bit
|
||||||
|
issues, fix 16 bit uid issues)
|
||||||
|
- use kernel traps for unaligned accesses on ARM ?
|
||||||
|
|
||||||
|
|
||||||
|
lower priority:
|
||||||
|
--------------
|
||||||
|
- int15 ah=86: use better timing
|
||||||
|
- use -msoft-float on ARM
|
||||||
157
accel.c
157
accel.c
@@ -1,157 +0,0 @@
|
|||||||
/*
|
|
||||||
* QEMU System Emulator, accelerator interfaces
|
|
||||||
*
|
|
||||||
* Copyright (c) 2003-2008 Fabrice Bellard
|
|
||||||
* Copyright (c) 2014 Red Hat Inc.
|
|
||||||
*
|
|
||||||
* 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 "sysemu/accel.h"
|
|
||||||
#include "hw/boards.h"
|
|
||||||
#include "qemu-common.h"
|
|
||||||
#include "sysemu/arch_init.h"
|
|
||||||
#include "sysemu/sysemu.h"
|
|
||||||
#include "sysemu/kvm.h"
|
|
||||||
#include "sysemu/qtest.h"
|
|
||||||
#include "hw/xen/xen.h"
|
|
||||||
#include "qom/object.h"
|
|
||||||
#include "hw/boards.h"
|
|
||||||
|
|
||||||
int tcg_tb_size;
|
|
||||||
static bool tcg_allowed = true;
|
|
||||||
|
|
||||||
static int tcg_init(MachineState *ms)
|
|
||||||
{
|
|
||||||
tcg_exec_init(tcg_tb_size * 1024 * 1024);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const TypeInfo accel_type = {
|
|
||||||
.name = TYPE_ACCEL,
|
|
||||||
.parent = TYPE_OBJECT,
|
|
||||||
.class_size = sizeof(AccelClass),
|
|
||||||
.instance_size = sizeof(AccelState),
|
|
||||||
};
|
|
||||||
|
|
||||||
/* Lookup AccelClass from opt_name. Returns NULL if not found */
|
|
||||||
static AccelClass *accel_find(const char *opt_name)
|
|
||||||
{
|
|
||||||
char *class_name = g_strdup_printf(ACCEL_CLASS_NAME("%s"), opt_name);
|
|
||||||
AccelClass *ac = ACCEL_CLASS(object_class_by_name(class_name));
|
|
||||||
g_free(class_name);
|
|
||||||
return ac;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int accel_init_machine(AccelClass *acc, MachineState *ms)
|
|
||||||
{
|
|
||||||
ObjectClass *oc = OBJECT_CLASS(acc);
|
|
||||||
const char *cname = object_class_get_name(oc);
|
|
||||||
AccelState *accel = ACCEL(object_new(cname));
|
|
||||||
int ret;
|
|
||||||
ms->accelerator = accel;
|
|
||||||
*(acc->allowed) = true;
|
|
||||||
ret = acc->init_machine(ms);
|
|
||||||
if (ret < 0) {
|
|
||||||
ms->accelerator = NULL;
|
|
||||||
*(acc->allowed) = false;
|
|
||||||
object_unref(OBJECT(accel));
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int configure_accelerator(MachineState *ms)
|
|
||||||
{
|
|
||||||
const char *p;
|
|
||||||
char buf[10];
|
|
||||||
int ret;
|
|
||||||
bool accel_initialised = false;
|
|
||||||
bool init_failed = false;
|
|
||||||
AccelClass *acc = NULL;
|
|
||||||
|
|
||||||
p = qemu_opt_get(qemu_get_machine_opts(), "accel");
|
|
||||||
if (p == NULL) {
|
|
||||||
/* Use the default "accelerator", tcg */
|
|
||||||
p = "tcg";
|
|
||||||
}
|
|
||||||
|
|
||||||
while (!accel_initialised && *p != '\0') {
|
|
||||||
if (*p == ':') {
|
|
||||||
p++;
|
|
||||||
}
|
|
||||||
p = get_opt_name(buf, sizeof(buf), p, ':');
|
|
||||||
acc = accel_find(buf);
|
|
||||||
if (!acc) {
|
|
||||||
fprintf(stderr, "\"%s\" accelerator not found.\n", buf);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (acc->available && !acc->available()) {
|
|
||||||
printf("%s not supported for this target\n",
|
|
||||||
acc->name);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
ret = accel_init_machine(acc, ms);
|
|
||||||
if (ret < 0) {
|
|
||||||
init_failed = true;
|
|
||||||
fprintf(stderr, "failed to initialize %s: %s\n",
|
|
||||||
acc->name,
|
|
||||||
strerror(-ret));
|
|
||||||
} else {
|
|
||||||
accel_initialised = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!accel_initialised) {
|
|
||||||
if (!init_failed) {
|
|
||||||
fprintf(stderr, "No accelerator found!\n");
|
|
||||||
}
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (init_failed) {
|
|
||||||
fprintf(stderr, "Back to %s accelerator.\n", acc->name);
|
|
||||||
}
|
|
||||||
|
|
||||||
return !accel_initialised;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void tcg_accel_class_init(ObjectClass *oc, void *data)
|
|
||||||
{
|
|
||||||
AccelClass *ac = ACCEL_CLASS(oc);
|
|
||||||
ac->name = "tcg";
|
|
||||||
ac->init_machine = tcg_init;
|
|
||||||
ac->allowed = &tcg_allowed;
|
|
||||||
}
|
|
||||||
|
|
||||||
#define TYPE_TCG_ACCEL ACCEL_CLASS_NAME("tcg")
|
|
||||||
|
|
||||||
static const TypeInfo tcg_accel_type = {
|
|
||||||
.name = TYPE_TCG_ACCEL,
|
|
||||||
.parent = TYPE_ACCEL,
|
|
||||||
.class_init = tcg_accel_class_init,
|
|
||||||
};
|
|
||||||
|
|
||||||
static void register_accel_types(void)
|
|
||||||
{
|
|
||||||
type_register_static(&accel_type);
|
|
||||||
type_register_static(&tcg_accel_type);
|
|
||||||
}
|
|
||||||
|
|
||||||
type_init(register_accel_types);
|
|
||||||
423
aio-posix.c
423
aio-posix.c
@@ -17,177 +17,18 @@
|
|||||||
#include "block/block.h"
|
#include "block/block.h"
|
||||||
#include "qemu/queue.h"
|
#include "qemu/queue.h"
|
||||||
#include "qemu/sockets.h"
|
#include "qemu/sockets.h"
|
||||||
#ifdef CONFIG_EPOLL
|
|
||||||
#include <sys/epoll.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
struct AioHandler
|
struct AioHandler
|
||||||
{
|
{
|
||||||
GPollFD pfd;
|
GPollFD pfd;
|
||||||
IOHandler *io_read;
|
IOHandler *io_read;
|
||||||
IOHandler *io_write;
|
IOHandler *io_write;
|
||||||
|
AioFlushHandler *io_flush;
|
||||||
int deleted;
|
int deleted;
|
||||||
void *opaque;
|
void *opaque;
|
||||||
bool is_external;
|
|
||||||
QLIST_ENTRY(AioHandler) node;
|
QLIST_ENTRY(AioHandler) node;
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef CONFIG_EPOLL
|
|
||||||
|
|
||||||
/* The fd number threashold to switch to epoll */
|
|
||||||
#define EPOLL_ENABLE_THRESHOLD 64
|
|
||||||
|
|
||||||
static void aio_epoll_disable(AioContext *ctx)
|
|
||||||
{
|
|
||||||
ctx->epoll_available = false;
|
|
||||||
if (!ctx->epoll_enabled) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ctx->epoll_enabled = false;
|
|
||||||
close(ctx->epollfd);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int epoll_events_from_pfd(int pfd_events)
|
|
||||||
{
|
|
||||||
return (pfd_events & G_IO_IN ? EPOLLIN : 0) |
|
|
||||||
(pfd_events & G_IO_OUT ? EPOLLOUT : 0) |
|
|
||||||
(pfd_events & G_IO_HUP ? EPOLLHUP : 0) |
|
|
||||||
(pfd_events & G_IO_ERR ? EPOLLERR : 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool aio_epoll_try_enable(AioContext *ctx)
|
|
||||||
{
|
|
||||||
AioHandler *node;
|
|
||||||
struct epoll_event event;
|
|
||||||
|
|
||||||
QLIST_FOREACH(node, &ctx->aio_handlers, node) {
|
|
||||||
int r;
|
|
||||||
if (node->deleted || !node->pfd.events) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
event.events = epoll_events_from_pfd(node->pfd.events);
|
|
||||||
event.data.ptr = node;
|
|
||||||
r = epoll_ctl(ctx->epollfd, EPOLL_CTL_ADD, node->pfd.fd, &event);
|
|
||||||
if (r) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ctx->epoll_enabled = true;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void aio_epoll_update(AioContext *ctx, AioHandler *node, bool is_new)
|
|
||||||
{
|
|
||||||
struct epoll_event event;
|
|
||||||
int r;
|
|
||||||
|
|
||||||
if (!ctx->epoll_enabled) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!node->pfd.events) {
|
|
||||||
r = epoll_ctl(ctx->epollfd, EPOLL_CTL_DEL, node->pfd.fd, &event);
|
|
||||||
if (r) {
|
|
||||||
aio_epoll_disable(ctx);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
event.data.ptr = node;
|
|
||||||
event.events = epoll_events_from_pfd(node->pfd.events);
|
|
||||||
if (is_new) {
|
|
||||||
r = epoll_ctl(ctx->epollfd, EPOLL_CTL_ADD, node->pfd.fd, &event);
|
|
||||||
if (r) {
|
|
||||||
aio_epoll_disable(ctx);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
r = epoll_ctl(ctx->epollfd, EPOLL_CTL_MOD, node->pfd.fd, &event);
|
|
||||||
if (r) {
|
|
||||||
aio_epoll_disable(ctx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static int aio_epoll(AioContext *ctx, GPollFD *pfds,
|
|
||||||
unsigned npfd, int64_t timeout)
|
|
||||||
{
|
|
||||||
AioHandler *node;
|
|
||||||
int i, ret = 0;
|
|
||||||
struct epoll_event events[128];
|
|
||||||
|
|
||||||
assert(npfd == 1);
|
|
||||||
assert(pfds[0].fd == ctx->epollfd);
|
|
||||||
if (timeout > 0) {
|
|
||||||
ret = qemu_poll_ns(pfds, npfd, timeout);
|
|
||||||
}
|
|
||||||
if (timeout <= 0 || ret > 0) {
|
|
||||||
ret = epoll_wait(ctx->epollfd, events,
|
|
||||||
sizeof(events) / sizeof(events[0]),
|
|
||||||
timeout);
|
|
||||||
if (ret <= 0) {
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
for (i = 0; i < ret; i++) {
|
|
||||||
int ev = events[i].events;
|
|
||||||
node = events[i].data.ptr;
|
|
||||||
node->pfd.revents = (ev & EPOLLIN ? G_IO_IN : 0) |
|
|
||||||
(ev & EPOLLOUT ? G_IO_OUT : 0) |
|
|
||||||
(ev & EPOLLHUP ? G_IO_HUP : 0) |
|
|
||||||
(ev & EPOLLERR ? G_IO_ERR : 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
out:
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool aio_epoll_enabled(AioContext *ctx)
|
|
||||||
{
|
|
||||||
/* Fall back to ppoll when external clients are disabled. */
|
|
||||||
return !aio_external_disabled(ctx) && ctx->epoll_enabled;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool aio_epoll_check_poll(AioContext *ctx, GPollFD *pfds,
|
|
||||||
unsigned npfd, int64_t timeout)
|
|
||||||
{
|
|
||||||
if (!ctx->epoll_available) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (aio_epoll_enabled(ctx)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (npfd >= EPOLL_ENABLE_THRESHOLD) {
|
|
||||||
if (aio_epoll_try_enable(ctx)) {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
aio_epoll_disable(ctx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
static void aio_epoll_update(AioContext *ctx, AioHandler *node, bool is_new)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
static int aio_epoll(AioContext *ctx, GPollFD *pfds,
|
|
||||||
unsigned npfd, int64_t timeout)
|
|
||||||
{
|
|
||||||
assert(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool aio_epoll_enabled(AioContext *ctx)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool aio_epoll_check_poll(AioContext *ctx, GPollFD *pfds,
|
|
||||||
unsigned npfd, int64_t timeout)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static AioHandler *find_aio_handler(AioContext *ctx, int fd)
|
static AioHandler *find_aio_handler(AioContext *ctx, int fd)
|
||||||
{
|
{
|
||||||
AioHandler *node;
|
AioHandler *node;
|
||||||
@@ -203,14 +44,12 @@ static AioHandler *find_aio_handler(AioContext *ctx, int fd)
|
|||||||
|
|
||||||
void aio_set_fd_handler(AioContext *ctx,
|
void aio_set_fd_handler(AioContext *ctx,
|
||||||
int fd,
|
int fd,
|
||||||
bool is_external,
|
|
||||||
IOHandler *io_read,
|
IOHandler *io_read,
|
||||||
IOHandler *io_write,
|
IOHandler *io_write,
|
||||||
|
AioFlushHandler *io_flush,
|
||||||
void *opaque)
|
void *opaque)
|
||||||
{
|
{
|
||||||
AioHandler *node;
|
AioHandler *node;
|
||||||
bool is_new = false;
|
|
||||||
bool deleted = false;
|
|
||||||
|
|
||||||
node = find_aio_handler(ctx, fd);
|
node = find_aio_handler(ctx, fd);
|
||||||
|
|
||||||
@@ -229,48 +68,39 @@ void aio_set_fd_handler(AioContext *ctx,
|
|||||||
* releasing the walking_handlers lock.
|
* releasing the walking_handlers lock.
|
||||||
*/
|
*/
|
||||||
QLIST_REMOVE(node, node);
|
QLIST_REMOVE(node, node);
|
||||||
deleted = true;
|
g_free(node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (node == NULL) {
|
if (node == NULL) {
|
||||||
/* Alloc and insert if it's not already there */
|
/* Alloc and insert if it's not already there */
|
||||||
node = g_new0(AioHandler, 1);
|
node = g_malloc0(sizeof(AioHandler));
|
||||||
node->pfd.fd = fd;
|
node->pfd.fd = fd;
|
||||||
QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
|
QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
|
||||||
|
|
||||||
g_source_add_poll(&ctx->source, &node->pfd);
|
g_source_add_poll(&ctx->source, &node->pfd);
|
||||||
is_new = true;
|
|
||||||
}
|
}
|
||||||
/* Update handler with latest information */
|
/* Update handler with latest information */
|
||||||
node->io_read = io_read;
|
node->io_read = io_read;
|
||||||
node->io_write = io_write;
|
node->io_write = io_write;
|
||||||
|
node->io_flush = io_flush;
|
||||||
node->opaque = opaque;
|
node->opaque = opaque;
|
||||||
node->is_external = is_external;
|
|
||||||
|
|
||||||
node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP | G_IO_ERR : 0);
|
node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP : 0);
|
||||||
node->pfd.events |= (io_write ? G_IO_OUT | G_IO_ERR : 0);
|
node->pfd.events |= (io_write ? G_IO_OUT : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
aio_epoll_update(ctx, node, is_new);
|
|
||||||
aio_notify(ctx);
|
aio_notify(ctx);
|
||||||
if (deleted) {
|
|
||||||
g_free(node);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void aio_set_event_notifier(AioContext *ctx,
|
void aio_set_event_notifier(AioContext *ctx,
|
||||||
EventNotifier *notifier,
|
EventNotifier *notifier,
|
||||||
bool is_external,
|
EventNotifierHandler *io_read,
|
||||||
EventNotifierHandler *io_read)
|
AioFlushEventNotifierHandler *io_flush)
|
||||||
{
|
{
|
||||||
aio_set_fd_handler(ctx, event_notifier_get_fd(notifier),
|
aio_set_fd_handler(ctx, event_notifier_get_fd(notifier),
|
||||||
is_external, (IOHandler *)io_read, NULL, notifier);
|
(IOHandler *)io_read, NULL,
|
||||||
}
|
(AioFlushHandler *)io_flush, notifier);
|
||||||
|
|
||||||
bool aio_prepare(AioContext *ctx)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool aio_pending(AioContext *ctx)
|
bool aio_pending(AioContext *ctx)
|
||||||
@@ -280,6 +110,13 @@ bool aio_pending(AioContext *ctx)
|
|||||||
QLIST_FOREACH(node, &ctx->aio_handlers, node) {
|
QLIST_FOREACH(node, &ctx->aio_handlers, node) {
|
||||||
int revents;
|
int revents;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* FIXME: right now we cannot get G_IO_HUP and G_IO_ERR because
|
||||||
|
* main-loop.c is still select based (due to the slirp legacy).
|
||||||
|
* If main-loop.c ever switches to poll, G_IO_ERR should be
|
||||||
|
* tested too. Dispatching G_IO_ERR to both handlers should be
|
||||||
|
* okay, since handlers need to be ready for spurious wakeups.
|
||||||
|
*/
|
||||||
revents = node->pfd.revents & node->pfd.events;
|
revents = node->pfd.revents & node->pfd.events;
|
||||||
if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read) {
|
if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read) {
|
||||||
return true;
|
return true;
|
||||||
@@ -292,22 +129,31 @@ bool aio_pending(AioContext *ctx)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool aio_dispatch(AioContext *ctx)
|
bool aio_poll(AioContext *ctx, bool blocking)
|
||||||
{
|
{
|
||||||
|
static struct timeval tv0;
|
||||||
AioHandler *node;
|
AioHandler *node;
|
||||||
bool progress = false;
|
fd_set rdfds, wrfds;
|
||||||
|
int max_fd = -1;
|
||||||
|
int ret;
|
||||||
|
bool busy, progress;
|
||||||
|
|
||||||
|
progress = false;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If there are callbacks left that have been queued, we need to call them.
|
* If there are callbacks left that have been queued, we need to call then.
|
||||||
* Do not call select in this case, because it is possible that the caller
|
* Do not call select in this case, because it is possible that the caller
|
||||||
* does not need a complete flush (as is the case for aio_poll loops).
|
* does not need a complete flush (as is the case for qemu_aio_wait loops).
|
||||||
*/
|
*/
|
||||||
if (aio_bh_poll(ctx)) {
|
if (aio_bh_poll(ctx)) {
|
||||||
|
blocking = false;
|
||||||
progress = true;
|
progress = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* We have to walk very carefully in case aio_set_fd_handler is
|
* Then dispatch any pending callbacks from the GSource.
|
||||||
|
*
|
||||||
|
* We have to walk very carefully in case qemu_aio_set_fd_handler is
|
||||||
* called while we're walking.
|
* called while we're walking.
|
||||||
*/
|
*/
|
||||||
node = QLIST_FIRST(&ctx->aio_handlers);
|
node = QLIST_FIRST(&ctx->aio_handlers);
|
||||||
@@ -320,19 +166,12 @@ bool aio_dispatch(AioContext *ctx)
|
|||||||
revents = node->pfd.revents & node->pfd.events;
|
revents = node->pfd.revents & node->pfd.events;
|
||||||
node->pfd.revents = 0;
|
node->pfd.revents = 0;
|
||||||
|
|
||||||
if (!node->deleted &&
|
/* See comment in aio_pending. */
|
||||||
(revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) &&
|
if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read) {
|
||||||
node->io_read) {
|
|
||||||
node->io_read(node->opaque);
|
node->io_read(node->opaque);
|
||||||
|
|
||||||
/* aio_notify() does not count as progress */
|
|
||||||
if (node->opaque != &ctx->notifier) {
|
|
||||||
progress = true;
|
progress = true;
|
||||||
}
|
}
|
||||||
}
|
if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write) {
|
||||||
if (!node->deleted &&
|
|
||||||
(revents & (G_IO_OUT | G_IO_ERR)) &&
|
|
||||||
node->io_write) {
|
|
||||||
node->io_write(node->opaque);
|
node->io_write(node->opaque);
|
||||||
progress = true;
|
progress = true;
|
||||||
}
|
}
|
||||||
@@ -348,147 +187,83 @@ bool aio_dispatch(AioContext *ctx)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Run our timers */
|
if (progress && !blocking) {
|
||||||
progress |= timerlistgroup_run_timers(&ctx->tlg);
|
return true;
|
||||||
|
|
||||||
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;
|
|
||||||
int i, ret;
|
|
||||||
bool progress;
|
|
||||||
int64_t timeout;
|
|
||||||
|
|
||||||
aio_context_acquire(ctx);
|
|
||||||
progress = false;
|
|
||||||
|
|
||||||
/* aio_notify can avoid the expensive event_notifier_set if
|
|
||||||
* everything (file descriptors, bottom halves, timers) will
|
|
||||||
* be re-evaluated before the next blocking poll(). This is
|
|
||||||
* already true when aio_poll is called with blocking == false;
|
|
||||||
* if blocking == true, it is only true after poll() returns,
|
|
||||||
* so disable the optimization now.
|
|
||||||
*/
|
|
||||||
if (blocking) {
|
|
||||||
atomic_add(&ctx->notify_me, 2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx->walking_handlers++;
|
ctx->walking_handlers++;
|
||||||
|
|
||||||
assert(npfd == 0);
|
FD_ZERO(&rdfds);
|
||||||
|
FD_ZERO(&wrfds);
|
||||||
|
|
||||||
/* fill pollfds */
|
/* fill fd sets */
|
||||||
|
busy = false;
|
||||||
QLIST_FOREACH(node, &ctx->aio_handlers, node) {
|
QLIST_FOREACH(node, &ctx->aio_handlers, node) {
|
||||||
if (!node->deleted && node->pfd.events
|
/* If there aren't pending AIO operations, don't invoke callbacks.
|
||||||
&& !aio_epoll_enabled(ctx)
|
* Otherwise, if there are no AIO requests, qemu_aio_wait() would
|
||||||
&& aio_node_check(ctx, node->is_external)) {
|
* wait indefinitely.
|
||||||
add_pollfd(node);
|
*/
|
||||||
|
if (!node->deleted && node->io_flush) {
|
||||||
|
if (node->io_flush(node->opaque) == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
busy = true;
|
||||||
|
}
|
||||||
|
if (!node->deleted && node->io_read) {
|
||||||
|
FD_SET(node->pfd.fd, &rdfds);
|
||||||
|
max_fd = MAX(max_fd, node->pfd.fd + 1);
|
||||||
|
}
|
||||||
|
if (!node->deleted && node->io_write) {
|
||||||
|
FD_SET(node->pfd.fd, &wrfds);
|
||||||
|
max_fd = MAX(max_fd, node->pfd.fd + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
timeout = blocking ? aio_compute_timeout(ctx) : 0;
|
|
||||||
|
|
||||||
/* wait until next event */
|
|
||||||
if (timeout) {
|
|
||||||
aio_context_release(ctx);
|
|
||||||
}
|
|
||||||
if (aio_epoll_check_poll(ctx, pollfds, npfd, timeout)) {
|
|
||||||
AioHandler epoll_handler;
|
|
||||||
|
|
||||||
epoll_handler.pfd.fd = ctx->epollfd;
|
|
||||||
epoll_handler.pfd.events = G_IO_IN | G_IO_OUT | G_IO_HUP | G_IO_ERR;
|
|
||||||
npfd = 0;
|
|
||||||
add_pollfd(&epoll_handler);
|
|
||||||
ret = aio_epoll(ctx, pollfds, npfd, timeout);
|
|
||||||
} else {
|
|
||||||
ret = qemu_poll_ns(pollfds, npfd, timeout);
|
|
||||||
}
|
|
||||||
if (blocking) {
|
|
||||||
atomic_sub(&ctx->notify_me, 2);
|
|
||||||
}
|
|
||||||
if (timeout) {
|
|
||||||
aio_context_acquire(ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
aio_notify_accept(ctx);
|
|
||||||
|
|
||||||
/* if we have any readable fds, dispatch event */
|
|
||||||
if (ret > 0) {
|
|
||||||
for (i = 0; i < npfd; i++) {
|
|
||||||
nodes[i]->pfd.revents = pollfds[i].revents;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
npfd = 0;
|
|
||||||
ctx->walking_handlers--;
|
ctx->walking_handlers--;
|
||||||
|
|
||||||
/* Run dispatch even if there were no readable fds to run timers */
|
/* No AIO operations? Get us out of here */
|
||||||
if (aio_dispatch(ctx)) {
|
if (!busy) {
|
||||||
progress = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
aio_context_release(ctx);
|
|
||||||
|
|
||||||
return progress;
|
return progress;
|
||||||
}
|
}
|
||||||
|
|
||||||
void aio_context_setup(AioContext *ctx, Error **errp)
|
/* wait until next event */
|
||||||
{
|
ret = select(max_fd, &rdfds, &wrfds, NULL, blocking ? NULL : &tv0);
|
||||||
#ifdef CONFIG_EPOLL
|
|
||||||
assert(!ctx->epollfd);
|
/* if we have any readable fds, dispatch event */
|
||||||
ctx->epollfd = epoll_create1(EPOLL_CLOEXEC);
|
if (ret > 0) {
|
||||||
if (ctx->epollfd == -1) {
|
/* we have to walk very carefully in case
|
||||||
ctx->epoll_available = false;
|
* qemu_aio_set_fd_handler is called while we're walking */
|
||||||
} else {
|
node = QLIST_FIRST(&ctx->aio_handlers);
|
||||||
ctx->epoll_available = true;
|
while (node) {
|
||||||
|
AioHandler *tmp;
|
||||||
|
|
||||||
|
ctx->walking_handlers++;
|
||||||
|
|
||||||
|
if (!node->deleted &&
|
||||||
|
FD_ISSET(node->pfd.fd, &rdfds) &&
|
||||||
|
node->io_read) {
|
||||||
|
node->io_read(node->opaque);
|
||||||
|
progress = true;
|
||||||
}
|
}
|
||||||
#endif
|
if (!node->deleted &&
|
||||||
|
FD_ISSET(node->pfd.fd, &wrfds) &&
|
||||||
|
node->io_write) {
|
||||||
|
node->io_write(node->opaque);
|
||||||
|
progress = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp = node;
|
||||||
|
node = QLIST_NEXT(node, node);
|
||||||
|
|
||||||
|
ctx->walking_handlers--;
|
||||||
|
|
||||||
|
if (!ctx->walking_handlers && tmp->deleted) {
|
||||||
|
QLIST_REMOVE(tmp, node);
|
||||||
|
g_free(tmp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(progress || busy);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
326
aio-win32.c
326
aio-win32.c
@@ -22,87 +22,17 @@
|
|||||||
|
|
||||||
struct AioHandler {
|
struct AioHandler {
|
||||||
EventNotifier *e;
|
EventNotifier *e;
|
||||||
IOHandler *io_read;
|
|
||||||
IOHandler *io_write;
|
|
||||||
EventNotifierHandler *io_notify;
|
EventNotifierHandler *io_notify;
|
||||||
|
AioFlushEventNotifierHandler *io_flush;
|
||||||
GPollFD pfd;
|
GPollFD pfd;
|
||||||
int deleted;
|
int deleted;
|
||||||
void *opaque;
|
|
||||||
bool is_external;
|
|
||||||
QLIST_ENTRY(AioHandler) node;
|
QLIST_ENTRY(AioHandler) node;
|
||||||
};
|
};
|
||||||
|
|
||||||
void aio_set_fd_handler(AioContext *ctx,
|
|
||||||
int fd,
|
|
||||||
bool is_external,
|
|
||||||
IOHandler *io_read,
|
|
||||||
IOHandler *io_write,
|
|
||||||
void *opaque)
|
|
||||||
{
|
|
||||||
/* fd is a SOCKET in our case */
|
|
||||||
AioHandler *node;
|
|
||||||
|
|
||||||
QLIST_FOREACH(node, &ctx->aio_handlers, node) {
|
|
||||||
if (node->pfd.fd == fd && !node->deleted) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Are we deleting the fd handler? */
|
|
||||||
if (!io_read && !io_write) {
|
|
||||||
if (node) {
|
|
||||||
/* If the lock is held, just mark the node as deleted */
|
|
||||||
if (ctx->walking_handlers) {
|
|
||||||
node->deleted = 1;
|
|
||||||
node->pfd.revents = 0;
|
|
||||||
} else {
|
|
||||||
/* Otherwise, delete it for real. We can't just mark it as
|
|
||||||
* deleted because deleted nodes are only cleaned up after
|
|
||||||
* releasing the walking_handlers lock.
|
|
||||||
*/
|
|
||||||
QLIST_REMOVE(node, node);
|
|
||||||
g_free(node);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
HANDLE event;
|
|
||||||
|
|
||||||
if (node == NULL) {
|
|
||||||
/* Alloc and insert if it's not already there */
|
|
||||||
node = g_new0(AioHandler, 1);
|
|
||||||
node->pfd.fd = fd;
|
|
||||||
QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
|
|
||||||
}
|
|
||||||
|
|
||||||
node->pfd.events = 0;
|
|
||||||
if (node->io_read) {
|
|
||||||
node->pfd.events |= G_IO_IN;
|
|
||||||
}
|
|
||||||
if (node->io_write) {
|
|
||||||
node->pfd.events |= G_IO_OUT;
|
|
||||||
}
|
|
||||||
|
|
||||||
node->e = &ctx->notifier;
|
|
||||||
|
|
||||||
/* Update handler with latest information */
|
|
||||||
node->opaque = opaque;
|
|
||||||
node->io_read = io_read;
|
|
||||||
node->io_write = io_write;
|
|
||||||
node->is_external = is_external;
|
|
||||||
|
|
||||||
event = event_notifier_get_handle(&ctx->notifier);
|
|
||||||
WSAEventSelect(node->pfd.fd, event,
|
|
||||||
FD_READ | FD_ACCEPT | FD_CLOSE |
|
|
||||||
FD_CONNECT | FD_WRITE | FD_OOB);
|
|
||||||
}
|
|
||||||
|
|
||||||
aio_notify(ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
void aio_set_event_notifier(AioContext *ctx,
|
void aio_set_event_notifier(AioContext *ctx,
|
||||||
EventNotifier *e,
|
EventNotifier *e,
|
||||||
bool is_external,
|
EventNotifierHandler *io_notify,
|
||||||
EventNotifierHandler *io_notify)
|
AioFlushEventNotifierHandler *io_flush)
|
||||||
{
|
{
|
||||||
AioHandler *node;
|
AioHandler *node;
|
||||||
|
|
||||||
@@ -133,59 +63,22 @@ void aio_set_event_notifier(AioContext *ctx,
|
|||||||
} else {
|
} else {
|
||||||
if (node == NULL) {
|
if (node == NULL) {
|
||||||
/* Alloc and insert if it's not already there */
|
/* Alloc and insert if it's not already there */
|
||||||
node = g_new0(AioHandler, 1);
|
node = g_malloc0(sizeof(AioHandler));
|
||||||
node->e = e;
|
node->e = e;
|
||||||
node->pfd.fd = (uintptr_t)event_notifier_get_handle(e);
|
node->pfd.fd = (uintptr_t)event_notifier_get_handle(e);
|
||||||
node->pfd.events = G_IO_IN;
|
node->pfd.events = G_IO_IN;
|
||||||
node->is_external = is_external;
|
|
||||||
QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
|
QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
|
||||||
|
|
||||||
g_source_add_poll(&ctx->source, &node->pfd);
|
g_source_add_poll(&ctx->source, &node->pfd);
|
||||||
}
|
}
|
||||||
/* Update handler with latest information */
|
/* Update handler with latest information */
|
||||||
node->io_notify = io_notify;
|
node->io_notify = io_notify;
|
||||||
|
node->io_flush = io_flush;
|
||||||
}
|
}
|
||||||
|
|
||||||
aio_notify(ctx);
|
aio_notify(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool aio_prepare(AioContext *ctx)
|
|
||||||
{
|
|
||||||
static struct timeval tv0;
|
|
||||||
AioHandler *node;
|
|
||||||
bool have_select_revents = false;
|
|
||||||
fd_set rfds, wfds;
|
|
||||||
|
|
||||||
/* fill fd sets */
|
|
||||||
FD_ZERO(&rfds);
|
|
||||||
FD_ZERO(&wfds);
|
|
||||||
QLIST_FOREACH(node, &ctx->aio_handlers, node) {
|
|
||||||
if (node->io_read) {
|
|
||||||
FD_SET ((SOCKET)node->pfd.fd, &rfds);
|
|
||||||
}
|
|
||||||
if (node->io_write) {
|
|
||||||
FD_SET ((SOCKET)node->pfd.fd, &wfds);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (select(0, &rfds, &wfds, NULL, &tv0) > 0) {
|
|
||||||
QLIST_FOREACH(node, &ctx->aio_handlers, node) {
|
|
||||||
node->pfd.revents = 0;
|
|
||||||
if (FD_ISSET(node->pfd.fd, &rfds)) {
|
|
||||||
node->pfd.revents |= G_IO_IN;
|
|
||||||
have_select_revents = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (FD_ISSET(node->pfd.fd, &wfds)) {
|
|
||||||
node->pfd.revents |= G_IO_OUT;
|
|
||||||
have_select_revents = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return have_select_revents;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool aio_pending(AioContext *ctx)
|
bool aio_pending(AioContext *ctx)
|
||||||
{
|
{
|
||||||
AioHandler *node;
|
AioHandler *node;
|
||||||
@@ -194,67 +87,47 @@ bool aio_pending(AioContext *ctx)
|
|||||||
if (node->pfd.revents && node->io_notify) {
|
if (node->pfd.revents && node->io_notify) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((node->pfd.revents & G_IO_IN) && node->io_read) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if ((node->pfd.revents & G_IO_OUT) && node->io_write) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool aio_dispatch_handlers(AioContext *ctx, HANDLE event)
|
bool aio_poll(AioContext *ctx, bool blocking)
|
||||||
{
|
{
|
||||||
AioHandler *node;
|
AioHandler *node;
|
||||||
bool progress = false;
|
HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
|
||||||
|
bool busy, progress;
|
||||||
|
int count;
|
||||||
|
|
||||||
|
progress = false;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* We have to walk very carefully in case aio_set_fd_handler is
|
* If there are callbacks left that have been queued, we need to call then.
|
||||||
|
* Do not call select in this case, because it is possible that the caller
|
||||||
|
* does not need a complete flush (as is the case for qemu_aio_wait loops).
|
||||||
|
*/
|
||||||
|
if (aio_bh_poll(ctx)) {
|
||||||
|
blocking = false;
|
||||||
|
progress = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Then dispatch any pending callbacks from the GSource.
|
||||||
|
*
|
||||||
|
* We have to walk very carefully in case qemu_aio_set_fd_handler is
|
||||||
* called while we're walking.
|
* called while we're walking.
|
||||||
*/
|
*/
|
||||||
node = QLIST_FIRST(&ctx->aio_handlers);
|
node = QLIST_FIRST(&ctx->aio_handlers);
|
||||||
while (node) {
|
while (node) {
|
||||||
AioHandler *tmp;
|
AioHandler *tmp;
|
||||||
int revents = node->pfd.revents;
|
|
||||||
|
|
||||||
ctx->walking_handlers++;
|
ctx->walking_handlers++;
|
||||||
|
|
||||||
if (!node->deleted &&
|
if (node->pfd.revents && node->io_notify) {
|
||||||
(revents || event_notifier_get_handle(node->e) == event) &&
|
|
||||||
node->io_notify) {
|
|
||||||
node->pfd.revents = 0;
|
node->pfd.revents = 0;
|
||||||
node->io_notify(node->e);
|
node->io_notify(node->e);
|
||||||
|
|
||||||
/* aio_notify() does not count as progress */
|
|
||||||
if (node->e != &ctx->notifier) {
|
|
||||||
progress = true;
|
progress = true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!node->deleted &&
|
|
||||||
(node->io_read || node->io_write)) {
|
|
||||||
node->pfd.revents = 0;
|
|
||||||
if ((revents & G_IO_IN) && node->io_read) {
|
|
||||||
node->io_read(node->opaque);
|
|
||||||
progress = true;
|
|
||||||
}
|
|
||||||
if ((revents & G_IO_OUT) && node->io_write) {
|
|
||||||
node->io_write(node->opaque);
|
|
||||||
progress = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* if the next select() will return an event, we have progressed */
|
|
||||||
if (event == event_notifier_get_handle(&ctx->notifier)) {
|
|
||||||
WSANETWORKEVENTS ev;
|
|
||||||
WSAEnumNetworkEvents(node->pfd.fd, event, &ev);
|
|
||||||
if (ev.lNetworkEvents) {
|
|
||||||
progress = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tmp = node;
|
tmp = node;
|
||||||
node = QLIST_NEXT(node, node);
|
node = QLIST_NEXT(node, node);
|
||||||
@@ -267,109 +140,80 @@ static bool aio_dispatch_handlers(AioContext *ctx, HANDLE event)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return progress;
|
if (progress && !blocking) {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool aio_dispatch(AioContext *ctx)
|
|
||||||
{
|
|
||||||
bool progress;
|
|
||||||
|
|
||||||
progress = aio_bh_poll(ctx);
|
|
||||||
progress |= aio_dispatch_handlers(ctx, INVALID_HANDLE_VALUE);
|
|
||||||
progress |= timerlistgroup_run_timers(&ctx->tlg);
|
|
||||||
return progress;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool aio_poll(AioContext *ctx, bool blocking)
|
|
||||||
{
|
|
||||||
AioHandler *node;
|
|
||||||
HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
|
|
||||||
bool progress, have_select_revents, first;
|
|
||||||
int count;
|
|
||||||
int timeout;
|
|
||||||
|
|
||||||
aio_context_acquire(ctx);
|
|
||||||
progress = false;
|
|
||||||
|
|
||||||
/* aio_notify can avoid the expensive event_notifier_set if
|
|
||||||
* everything (file descriptors, bottom halves, timers) will
|
|
||||||
* be re-evaluated before the next blocking poll(). This is
|
|
||||||
* already true when aio_poll is called with blocking == false;
|
|
||||||
* if blocking == true, it is only true after poll() returns,
|
|
||||||
* so disable the optimization now.
|
|
||||||
*/
|
|
||||||
if (blocking) {
|
|
||||||
atomic_add(&ctx->notify_me, 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
have_select_revents = aio_prepare(ctx);
|
|
||||||
|
|
||||||
ctx->walking_handlers++;
|
ctx->walking_handlers++;
|
||||||
|
|
||||||
/* fill fd sets */
|
/* fill fd sets */
|
||||||
|
busy = false;
|
||||||
count = 0;
|
count = 0;
|
||||||
QLIST_FOREACH(node, &ctx->aio_handlers, node) {
|
QLIST_FOREACH(node, &ctx->aio_handlers, node) {
|
||||||
if (!node->deleted && node->io_notify
|
/* If there aren't pending AIO operations, don't invoke callbacks.
|
||||||
&& aio_node_check(ctx, node->is_external)) {
|
* Otherwise, if there are no AIO requests, qemu_aio_wait() would
|
||||||
|
* wait indefinitely.
|
||||||
|
*/
|
||||||
|
if (!node->deleted && node->io_flush) {
|
||||||
|
if (node->io_flush(node->e) == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
busy = true;
|
||||||
|
}
|
||||||
|
if (!node->deleted && node->io_notify) {
|
||||||
events[count++] = event_notifier_get_handle(node->e);
|
events[count++] = event_notifier_get_handle(node->e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx->walking_handlers--;
|
ctx->walking_handlers--;
|
||||||
first = true;
|
|
||||||
|
|
||||||
/* ctx->notifier is always registered. */
|
/* No AIO operations? Get us out of here */
|
||||||
assert(count > 0);
|
if (!busy) {
|
||||||
|
|
||||||
/* Multiple iterations, all of them non-blocking except the first,
|
|
||||||
* may be necessary to process all pending events. After the first
|
|
||||||
* WaitForMultipleObjects call ctx->notify_me will be decremented.
|
|
||||||
*/
|
|
||||||
do {
|
|
||||||
HANDLE event;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
timeout = blocking && !have_select_revents
|
|
||||||
? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx)) : 0;
|
|
||||||
if (timeout) {
|
|
||||||
aio_context_release(ctx);
|
|
||||||
}
|
|
||||||
ret = WaitForMultipleObjects(count, events, FALSE, timeout);
|
|
||||||
if (blocking) {
|
|
||||||
assert(first);
|
|
||||||
atomic_sub(&ctx->notify_me, 2);
|
|
||||||
}
|
|
||||||
if (timeout) {
|
|
||||||
aio_context_acquire(ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (first) {
|
|
||||||
aio_notify_accept(ctx);
|
|
||||||
progress |= aio_bh_poll(ctx);
|
|
||||||
first = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* if we have any signaled events, dispatch event */
|
|
||||||
event = NULL;
|
|
||||||
if ((DWORD) (ret - WAIT_OBJECT_0) < count) {
|
|
||||||
event = events[ret - WAIT_OBJECT_0];
|
|
||||||
events[ret - WAIT_OBJECT_0] = events[--count];
|
|
||||||
} else if (!have_select_revents) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
have_select_revents = false;
|
|
||||||
blocking = false;
|
|
||||||
|
|
||||||
progress |= aio_dispatch_handlers(ctx, event);
|
|
||||||
} while (count > 0);
|
|
||||||
|
|
||||||
progress |= timerlistgroup_run_timers(&ctx->tlg);
|
|
||||||
|
|
||||||
aio_context_release(ctx);
|
|
||||||
return progress;
|
return progress;
|
||||||
}
|
}
|
||||||
|
|
||||||
void aio_context_setup(AioContext *ctx, Error **errp)
|
/* wait until next event */
|
||||||
{
|
while (count > 0) {
|
||||||
|
int timeout = blocking ? INFINITE : 0;
|
||||||
|
int ret = WaitForMultipleObjects(count, events, FALSE, timeout);
|
||||||
|
|
||||||
|
/* if we have any signaled events, dispatch event */
|
||||||
|
if ((DWORD) (ret - WAIT_OBJECT_0) >= count) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
blocking = false;
|
||||||
|
|
||||||
|
/* we have to walk very carefully in case
|
||||||
|
* qemu_aio_set_fd_handler is called while we're walking */
|
||||||
|
node = QLIST_FIRST(&ctx->aio_handlers);
|
||||||
|
while (node) {
|
||||||
|
AioHandler *tmp;
|
||||||
|
|
||||||
|
ctx->walking_handlers++;
|
||||||
|
|
||||||
|
if (!node->deleted &&
|
||||||
|
event_notifier_get_handle(node->e) == events[ret - WAIT_OBJECT_0] &&
|
||||||
|
node->io_notify) {
|
||||||
|
node->io_notify(node->e);
|
||||||
|
progress = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp = node;
|
||||||
|
node = QLIST_NEXT(node, node);
|
||||||
|
|
||||||
|
ctx->walking_handlers--;
|
||||||
|
|
||||||
|
if (!ctx->walking_handlers && tmp->deleted) {
|
||||||
|
QLIST_REMOVE(tmp, node);
|
||||||
|
g_free(tmp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Try again, but only call each handler once. */
|
||||||
|
events[ret - WAIT_OBJECT_0] = events[--count];
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(progress || busy);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
968
arch_init.c
968
arch_init.c
File diff suppressed because it is too large
Load Diff
218
async.c
218
async.c
@@ -24,9 +24,7 @@
|
|||||||
|
|
||||||
#include "qemu-common.h"
|
#include "qemu-common.h"
|
||||||
#include "block/aio.h"
|
#include "block/aio.h"
|
||||||
#include "block/thread-pool.h"
|
|
||||||
#include "qemu/main-loop.h"
|
#include "qemu/main-loop.h"
|
||||||
#include "qemu/atomic.h"
|
|
||||||
|
|
||||||
/***********************************************************/
|
/***********************************************************/
|
||||||
/* bottom halves (can be seen as timers which expire ASAP) */
|
/* bottom halves (can be seen as timers which expire ASAP) */
|
||||||
@@ -44,27 +42,15 @@ struct QEMUBH {
|
|||||||
QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque)
|
QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque)
|
||||||
{
|
{
|
||||||
QEMUBH *bh;
|
QEMUBH *bh;
|
||||||
bh = g_new(QEMUBH, 1);
|
bh = g_malloc0(sizeof(QEMUBH));
|
||||||
*bh = (QEMUBH){
|
bh->ctx = ctx;
|
||||||
.ctx = ctx,
|
bh->cb = cb;
|
||||||
.cb = cb,
|
bh->opaque = opaque;
|
||||||
.opaque = opaque,
|
|
||||||
};
|
|
||||||
qemu_mutex_lock(&ctx->bh_lock);
|
|
||||||
bh->next = ctx->first_bh;
|
bh->next = ctx->first_bh;
|
||||||
/* Make sure that the members are ready before putting bh into list */
|
|
||||||
smp_wmb();
|
|
||||||
ctx->first_bh = bh;
|
ctx->first_bh = bh;
|
||||||
qemu_mutex_unlock(&ctx->bh_lock);
|
|
||||||
return bh;
|
return bh;
|
||||||
}
|
}
|
||||||
|
|
||||||
void aio_bh_call(QEMUBH *bh)
|
|
||||||
{
|
|
||||||
bh->cb(bh->opaque);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Multiple occurrences of aio_bh_poll cannot be called concurrently */
|
|
||||||
int aio_bh_poll(AioContext *ctx)
|
int aio_bh_poll(AioContext *ctx)
|
||||||
{
|
{
|
||||||
QEMUBH *bh, **bhp, *next;
|
QEMUBH *bh, **bhp, *next;
|
||||||
@@ -74,22 +60,13 @@ int aio_bh_poll(AioContext *ctx)
|
|||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
for (bh = ctx->first_bh; bh; bh = next) {
|
for (bh = ctx->first_bh; bh; bh = next) {
|
||||||
/* Make sure that fetching bh happens before accessing its members */
|
|
||||||
smp_read_barrier_depends();
|
|
||||||
next = bh->next;
|
next = bh->next;
|
||||||
/* The atomic_xchg is paired with the one in qemu_bh_schedule. The
|
if (!bh->deleted && bh->scheduled) {
|
||||||
* implicit memory barrier ensures that the callback sees all writes
|
bh->scheduled = 0;
|
||||||
* done by the scheduling thread. It also ensures that the scheduling
|
if (!bh->idle)
|
||||||
* thread sees the zero before bh->cb has run, and thus will call
|
|
||||||
* aio_notify again if necessary.
|
|
||||||
*/
|
|
||||||
if (!bh->deleted && atomic_xchg(&bh->scheduled, 0)) {
|
|
||||||
/* Idle BHs and the notify BH don't count as progress */
|
|
||||||
if (!bh->idle && bh != ctx->notify_dummy_bh) {
|
|
||||||
ret = 1;
|
ret = 1;
|
||||||
}
|
|
||||||
bh->idle = 0;
|
bh->idle = 0;
|
||||||
aio_bh_call(bh);
|
bh->cb(bh->opaque);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,7 +74,6 @@ int aio_bh_poll(AioContext *ctx)
|
|||||||
|
|
||||||
/* remove deleted bhs */
|
/* remove deleted bhs */
|
||||||
if (!ctx->walking_bh) {
|
if (!ctx->walking_bh) {
|
||||||
qemu_mutex_lock(&ctx->bh_lock);
|
|
||||||
bhp = &ctx->first_bh;
|
bhp = &ctx->first_bh;
|
||||||
while (*bhp) {
|
while (*bhp) {
|
||||||
bh = *bhp;
|
bh = *bhp;
|
||||||
@@ -108,7 +84,6 @@ int aio_bh_poll(AioContext *ctx)
|
|||||||
bhp = &bh->next;
|
bhp = &bh->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
qemu_mutex_unlock(&ctx->bh_lock);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
@@ -116,52 +91,36 @@ int aio_bh_poll(AioContext *ctx)
|
|||||||
|
|
||||||
void qemu_bh_schedule_idle(QEMUBH *bh)
|
void qemu_bh_schedule_idle(QEMUBH *bh)
|
||||||
{
|
{
|
||||||
|
if (bh->scheduled)
|
||||||
|
return;
|
||||||
|
bh->scheduled = 1;
|
||||||
bh->idle = 1;
|
bh->idle = 1;
|
||||||
/* Make sure that idle & any writes needed by the callback are done
|
|
||||||
* before the locations are read in the aio_bh_poll.
|
|
||||||
*/
|
|
||||||
atomic_mb_set(&bh->scheduled, 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void qemu_bh_schedule(QEMUBH *bh)
|
void qemu_bh_schedule(QEMUBH *bh)
|
||||||
{
|
{
|
||||||
AioContext *ctx;
|
if (bh->scheduled)
|
||||||
|
return;
|
||||||
ctx = bh->ctx;
|
bh->scheduled = 1;
|
||||||
bh->idle = 0;
|
bh->idle = 0;
|
||||||
/* The memory barrier implicit in atomic_xchg makes sure that:
|
aio_notify(bh->ctx);
|
||||||
* 1. idle & any writes needed by the callback are done before the
|
|
||||||
* locations are read in the aio_bh_poll.
|
|
||||||
* 2. ctx is loaded before scheduled is set and the callback has a chance
|
|
||||||
* to execute.
|
|
||||||
*/
|
|
||||||
if (atomic_xchg(&bh->scheduled, 1) == 0) {
|
|
||||||
aio_notify(ctx);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* This func is async.
|
|
||||||
*/
|
|
||||||
void qemu_bh_cancel(QEMUBH *bh)
|
void qemu_bh_cancel(QEMUBH *bh)
|
||||||
{
|
{
|
||||||
bh->scheduled = 0;
|
bh->scheduled = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* This func is async.The bottom half will do the delete action at the finial
|
|
||||||
* end.
|
|
||||||
*/
|
|
||||||
void qemu_bh_delete(QEMUBH *bh)
|
void qemu_bh_delete(QEMUBH *bh)
|
||||||
{
|
{
|
||||||
bh->scheduled = 0;
|
bh->scheduled = 0;
|
||||||
bh->deleted = 1;
|
bh->deleted = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t
|
static gboolean
|
||||||
aio_compute_timeout(AioContext *ctx)
|
aio_ctx_prepare(GSource *source, gint *timeout)
|
||||||
{
|
{
|
||||||
int64_t deadline;
|
AioContext *ctx = (AioContext *) source;
|
||||||
int timeout = -1;
|
|
||||||
QEMUBH *bh;
|
QEMUBH *bh;
|
||||||
|
|
||||||
for (bh = ctx->first_bh; bh; bh = bh->next) {
|
for (bh = ctx->first_bh; bh; bh = bh->next) {
|
||||||
@@ -169,38 +128,17 @@ aio_compute_timeout(AioContext *ctx)
|
|||||||
if (bh->idle) {
|
if (bh->idle) {
|
||||||
/* idle bottom halves will be polled at least
|
/* idle bottom halves will be polled at least
|
||||||
* every 10ms */
|
* every 10ms */
|
||||||
timeout = 10000000;
|
*timeout = 10;
|
||||||
} else {
|
} else {
|
||||||
/* non-idle bottom halves will be executed
|
/* non-idle bottom halves will be executed
|
||||||
* immediately */
|
* immediately */
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
deadline = timerlistgroup_deadline_ns(&ctx->tlg);
|
|
||||||
if (deadline == 0) {
|
|
||||||
return 0;
|
|
||||||
} else {
|
|
||||||
return qemu_soonest_timeout(timeout, deadline);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static gboolean
|
|
||||||
aio_ctx_prepare(GSource *source, gint *timeout)
|
|
||||||
{
|
|
||||||
AioContext *ctx = (AioContext *) source;
|
|
||||||
|
|
||||||
atomic_or(&ctx->notify_me, 1);
|
|
||||||
|
|
||||||
/* We assume there is no timeout already supplied */
|
|
||||||
*timeout = qemu_timeout_ns_to_ms(aio_compute_timeout(ctx));
|
|
||||||
|
|
||||||
if (aio_prepare(ctx)) {
|
|
||||||
*timeout = 0;
|
*timeout = 0;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return *timeout == 0;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
@@ -209,15 +147,12 @@ aio_ctx_check(GSource *source)
|
|||||||
AioContext *ctx = (AioContext *) source;
|
AioContext *ctx = (AioContext *) source;
|
||||||
QEMUBH *bh;
|
QEMUBH *bh;
|
||||||
|
|
||||||
atomic_and(&ctx->notify_me, ~1);
|
|
||||||
aio_notify_accept(ctx);
|
|
||||||
|
|
||||||
for (bh = ctx->first_bh; bh; bh = bh->next) {
|
for (bh = ctx->first_bh; bh; bh = bh->next) {
|
||||||
if (!bh->deleted && bh->scheduled) {
|
if (!bh->deleted && bh->scheduled) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return aio_pending(ctx) || (timerlistgroup_deadline_ns(&ctx->tlg) == 0);
|
return aio_pending(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
@@ -228,7 +163,7 @@ aio_ctx_dispatch(GSource *source,
|
|||||||
AioContext *ctx = (AioContext *) source;
|
AioContext *ctx = (AioContext *) source;
|
||||||
|
|
||||||
assert(callback == NULL);
|
assert(callback == NULL);
|
||||||
aio_dispatch(ctx);
|
aio_poll(ctx, false);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,26 +172,8 @@ aio_ctx_finalize(GSource *source)
|
|||||||
{
|
{
|
||||||
AioContext *ctx = (AioContext *) source;
|
AioContext *ctx = (AioContext *) source;
|
||||||
|
|
||||||
qemu_bh_delete(ctx->notify_dummy_bh);
|
aio_set_event_notifier(ctx, &ctx->notifier, NULL, NULL);
|
||||||
thread_pool_free(ctx->thread_pool);
|
|
||||||
|
|
||||||
qemu_mutex_lock(&ctx->bh_lock);
|
|
||||||
while (ctx->first_bh) {
|
|
||||||
QEMUBH *next = ctx->first_bh->next;
|
|
||||||
|
|
||||||
/* qemu_bh_delete() must have been called on BHs in this AioContext */
|
|
||||||
assert(ctx->first_bh->deleted);
|
|
||||||
|
|
||||||
g_free(ctx->first_bh);
|
|
||||||
ctx->first_bh = next;
|
|
||||||
}
|
|
||||||
qemu_mutex_unlock(&ctx->bh_lock);
|
|
||||||
|
|
||||||
aio_set_event_notifier(ctx, &ctx->notifier, false, NULL);
|
|
||||||
event_notifier_cleanup(&ctx->notifier);
|
event_notifier_cleanup(&ctx->notifier);
|
||||||
rfifolock_destroy(&ctx->lock);
|
|
||||||
qemu_mutex_destroy(&ctx->bh_lock);
|
|
||||||
timerlistgroup_deinit(&ctx->tlg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static GSourceFuncs aio_source_funcs = {
|
static GSourceFuncs aio_source_funcs = {
|
||||||
@@ -272,88 +189,21 @@ GSource *aio_get_g_source(AioContext *ctx)
|
|||||||
return &ctx->source;
|
return &ctx->source;
|
||||||
}
|
}
|
||||||
|
|
||||||
ThreadPool *aio_get_thread_pool(AioContext *ctx)
|
|
||||||
{
|
|
||||||
if (!ctx->thread_pool) {
|
|
||||||
ctx->thread_pool = thread_pool_new(ctx);
|
|
||||||
}
|
|
||||||
return ctx->thread_pool;
|
|
||||||
}
|
|
||||||
|
|
||||||
void aio_notify(AioContext *ctx)
|
void aio_notify(AioContext *ctx)
|
||||||
{
|
{
|
||||||
/* Write e.g. bh->scheduled before reading ctx->notify_me. Pairs
|
|
||||||
* with atomic_or in aio_ctx_prepare or atomic_add in aio_poll.
|
|
||||||
*/
|
|
||||||
smp_mb();
|
|
||||||
if (ctx->notify_me) {
|
|
||||||
event_notifier_set(&ctx->notifier);
|
event_notifier_set(&ctx->notifier);
|
||||||
atomic_mb_set(&ctx->notified, true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void aio_notify_accept(AioContext *ctx)
|
AioContext *aio_context_new(void)
|
||||||
{
|
{
|
||||||
if (atomic_xchg(&ctx->notified, false)) {
|
|
||||||
event_notifier_test_and_clear(&ctx->notifier);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void aio_timerlist_notify(void *opaque)
|
|
||||||
{
|
|
||||||
aio_notify(opaque);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void aio_rfifolock_cb(void *opaque)
|
|
||||||
{
|
|
||||||
AioContext *ctx = opaque;
|
|
||||||
|
|
||||||
/* Kick owner thread in case they are blocked in aio_poll() */
|
|
||||||
qemu_bh_schedule(ctx->notify_dummy_bh);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void notify_dummy_bh(void *opaque)
|
|
||||||
{
|
|
||||||
/* Do nothing, we were invoked just to force the event loop to iterate */
|
|
||||||
}
|
|
||||||
|
|
||||||
static void event_notifier_dummy_cb(EventNotifier *e)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
AioContext *aio_context_new(Error **errp)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
AioContext *ctx;
|
AioContext *ctx;
|
||||||
Error *local_err = NULL;
|
|
||||||
|
|
||||||
ctx = (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioContext));
|
ctx = (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioContext));
|
||||||
aio_context_setup(ctx, &local_err);
|
event_notifier_init(&ctx->notifier, false);
|
||||||
if (local_err) {
|
|
||||||
error_propagate(errp, local_err);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
ret = event_notifier_init(&ctx->notifier, false);
|
|
||||||
if (ret < 0) {
|
|
||||||
error_setg_errno(errp, -ret, "Failed to initialize event notifier");
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
g_source_set_can_recurse(&ctx->source, true);
|
|
||||||
aio_set_event_notifier(ctx, &ctx->notifier,
|
aio_set_event_notifier(ctx, &ctx->notifier,
|
||||||
false,
|
|
||||||
(EventNotifierHandler *)
|
(EventNotifierHandler *)
|
||||||
event_notifier_dummy_cb);
|
event_notifier_test_and_clear, NULL);
|
||||||
ctx->thread_pool = NULL;
|
|
||||||
qemu_mutex_init(&ctx->bh_lock);
|
|
||||||
rfifolock_init(&ctx->lock, aio_rfifolock_cb, ctx);
|
|
||||||
timerlistgroup_init(&ctx->tlg, aio_timerlist_notify, ctx);
|
|
||||||
|
|
||||||
ctx->notify_dummy_bh = aio_bh_new(ctx, notify_dummy_bh, NULL);
|
|
||||||
|
|
||||||
return ctx;
|
return ctx;
|
||||||
fail:
|
|
||||||
g_source_destroy(&ctx->source);
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void aio_context_ref(AioContext *ctx)
|
void aio_context_ref(AioContext *ctx)
|
||||||
@@ -365,13 +215,3 @@ void aio_context_unref(AioContext *ctx)
|
|||||||
{
|
{
|
||||||
g_source_unref(&ctx->source);
|
g_source_unref(&ctx->source);
|
||||||
}
|
}
|
||||||
|
|
||||||
void aio_context_acquire(AioContext *ctx)
|
|
||||||
{
|
|
||||||
rfifolock_lock(&ctx->lock);
|
|
||||||
}
|
|
||||||
|
|
||||||
void aio_context_release(AioContext *ctx)
|
|
||||||
{
|
|
||||||
rfifolock_unlock(&ctx->lock);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -5,9 +5,13 @@ common-obj-$(CONFIG_SPICE) += spiceaudio.o
|
|||||||
common-obj-$(CONFIG_COREAUDIO) += coreaudio.o
|
common-obj-$(CONFIG_COREAUDIO) += coreaudio.o
|
||||||
common-obj-$(CONFIG_ALSA) += alsaaudio.o
|
common-obj-$(CONFIG_ALSA) += alsaaudio.o
|
||||||
common-obj-$(CONFIG_DSOUND) += dsoundaudio.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_PA) += paaudio.o
|
||||||
|
common-obj-$(CONFIG_WINWAVE) += winwaveaudio.o
|
||||||
common-obj-$(CONFIG_AUDIO_PT_INT) += audio_pt_int.o
|
common-obj-$(CONFIG_AUDIO_PT_INT) += audio_pt_int.o
|
||||||
common-obj-$(CONFIG_AUDIO_WIN_INT) += audio_win_int.o
|
common-obj-$(CONFIG_AUDIO_WIN_INT) += audio_win_int.o
|
||||||
common-obj-y += wavcapture.o
|
common-obj-y += wavcapture.o
|
||||||
|
|
||||||
sdlaudio.o-cflags := $(SDL_CFLAGS)
|
$(obj)/audio.o $(obj)/fmodaudio.o: QEMU_CFLAGS += $(FMOD_CFLAGS)
|
||||||
|
$(obj)/sdlaudio.o: QEMU_CFLAGS += $(SDL_CFLAGS)
|
||||||
|
|||||||
@@ -25,7 +25,6 @@
|
|||||||
#include "qemu-common.h"
|
#include "qemu-common.h"
|
||||||
#include "qemu/main-loop.h"
|
#include "qemu/main-loop.h"
|
||||||
#include "audio.h"
|
#include "audio.h"
|
||||||
#include "trace.h"
|
|
||||||
|
|
||||||
#if QEMU_GNUC_PREREQ(4, 3)
|
#if QEMU_GNUC_PREREQ(4, 3)
|
||||||
#pragma GCC diagnostic ignored "-Waddress"
|
#pragma GCC diagnostic ignored "-Waddress"
|
||||||
@@ -34,28 +33,9 @@
|
|||||||
#define AUDIO_CAP "alsa"
|
#define AUDIO_CAP "alsa"
|
||||||
#include "audio_int.h"
|
#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 {
|
struct pollhlp {
|
||||||
snd_pcm_t *handle;
|
snd_pcm_t *handle;
|
||||||
struct pollfd *pfds;
|
struct pollfd *pfds;
|
||||||
ALSAConf *conf;
|
|
||||||
int count;
|
int count;
|
||||||
int mask;
|
int mask;
|
||||||
};
|
};
|
||||||
@@ -76,6 +56,30 @@ typedef struct ALSAVoiceIn {
|
|||||||
struct pollhlp pollhlp;
|
struct pollhlp pollhlp;
|
||||||
} ALSAVoiceIn;
|
} 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 {
|
struct alsa_params_req {
|
||||||
int freq;
|
int freq;
|
||||||
snd_pcm_format_t fmt;
|
snd_pcm_format_t fmt;
|
||||||
@@ -201,7 +205,9 @@ static void alsa_poll_handler (void *opaque)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!(revents & hlp->mask)) {
|
if (!(revents & hlp->mask)) {
|
||||||
trace_alsa_revents(revents);
|
if (conf.verbose) {
|
||||||
|
dolog ("revents = %d\n", revents);
|
||||||
|
}
|
||||||
return;
|
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) {
|
for (i = 0; i < count; ++i) {
|
||||||
if (pfds[i].events & POLLIN) {
|
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) {
|
if (pfds[i].events & POLLOUT) {
|
||||||
trace_alsa_pollout(i, pfds[i].fd);
|
if (conf.verbose) {
|
||||||
qemu_set_fd_handler (pfds[i].fd, NULL, alsa_poll_handler, hlp);
|
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->pfds = pfds;
|
||||||
hlp->count = count;
|
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,
|
static int alsa_open (int in, struct alsa_params_req *req,
|
||||||
struct alsa_params_obt *obt, snd_pcm_t **handlep,
|
struct alsa_params_obt *obt, snd_pcm_t **handlep)
|
||||||
ALSAConf *conf)
|
|
||||||
{
|
{
|
||||||
snd_pcm_t *handle;
|
snd_pcm_t *handle;
|
||||||
snd_pcm_hw_params_t *hw_params;
|
snd_pcm_hw_params_t *hw_params;
|
||||||
int err;
|
int err;
|
||||||
int size_in_usec;
|
int size_in_usec;
|
||||||
unsigned int freq, nchannels;
|
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;
|
snd_pcm_uframes_t obt_buffer_size;
|
||||||
const char *typ = in ? "ADC" : "DAC";
|
const char *typ = in ? "ADC" : "DAC";
|
||||||
snd_pcm_format_t obtfmt;
|
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);
|
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);
|
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;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!in && conf->threshold) {
|
if (!in && conf.threshold) {
|
||||||
snd_pcm_uframes_t threshold;
|
snd_pcm_uframes_t threshold;
|
||||||
int bytes_per_sec;
|
int bytes_per_sec;
|
||||||
|
|
||||||
@@ -654,7 +676,7 @@ static int alsa_open (int in, struct alsa_params_req *req,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
threshold = (conf->threshold * bytes_per_sec) / 1000;
|
threshold = (conf.threshold * bytes_per_sec) / 1000;
|
||||||
alsa_set_threshold (handle, threshold);
|
alsa_set_threshold (handle, threshold);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -664,9 +686,10 @@ static int alsa_open (int in, struct alsa_params_req *req,
|
|||||||
|
|
||||||
*handlep = handle;
|
*handlep = handle;
|
||||||
|
|
||||||
if (obtfmt != req->fmt ||
|
if (conf.verbose &&
|
||||||
|
(obtfmt != req->fmt ||
|
||||||
obt->nchannels != req->nchannels ||
|
obt->nchannels != req->nchannels ||
|
||||||
obt->freq != req->freq) {
|
obt->freq != req->freq)) {
|
||||||
dolog ("Audio parameters for %s\n", typ);
|
dolog ("Audio parameters for %s\n", typ);
|
||||||
alsa_dump_info (req, obt, obtfmt);
|
alsa_dump_info (req, obt, obtfmt);
|
||||||
}
|
}
|
||||||
@@ -720,7 +743,9 @@ static void alsa_write_pending (ALSAVoiceOut *alsa)
|
|||||||
if (written <= 0) {
|
if (written <= 0) {
|
||||||
switch (written) {
|
switch (written) {
|
||||||
case 0:
|
case 0:
|
||||||
trace_alsa_wrote_zero(len);
|
if (conf.verbose) {
|
||||||
|
dolog ("Failed to write %d frames (wrote zero)\n", len);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case -EPIPE:
|
case -EPIPE:
|
||||||
@@ -729,7 +754,9 @@ static void alsa_write_pending (ALSAVoiceOut *alsa)
|
|||||||
len);
|
len);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
trace_alsa_xrun_out();
|
if (conf.verbose) {
|
||||||
|
dolog ("Recovering from playback xrun\n");
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
case -ESTRPIPE:
|
case -ESTRPIPE:
|
||||||
@@ -740,7 +767,9 @@ static void alsa_write_pending (ALSAVoiceOut *alsa)
|
|||||||
len);
|
len);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
trace_alsa_resume_out();
|
if (conf.verbose) {
|
||||||
|
dolog ("Resuming suspended output stream\n");
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
case -EAGAIN:
|
case -EAGAIN:
|
||||||
@@ -786,31 +815,31 @@ static void alsa_fini_out (HWVoiceOut *hw)
|
|||||||
ldebug ("alsa_fini\n");
|
ldebug ("alsa_fini\n");
|
||||||
alsa_anal_close (&alsa->handle, &alsa->pollhlp);
|
alsa_anal_close (&alsa->handle, &alsa->pollhlp);
|
||||||
|
|
||||||
|
if (alsa->pcm_buf) {
|
||||||
g_free (alsa->pcm_buf);
|
g_free (alsa->pcm_buf);
|
||||||
alsa->pcm_buf = NULL;
|
alsa->pcm_buf = NULL;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static int alsa_init_out(HWVoiceOut *hw, struct audsettings *as,
|
static int alsa_init_out (HWVoiceOut *hw, struct audsettings *as)
|
||||||
void *drv_opaque)
|
|
||||||
{
|
{
|
||||||
ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
|
ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
|
||||||
struct alsa_params_req req;
|
struct alsa_params_req req;
|
||||||
struct alsa_params_obt obt;
|
struct alsa_params_obt obt;
|
||||||
snd_pcm_t *handle;
|
snd_pcm_t *handle;
|
||||||
struct audsettings obt_as;
|
struct audsettings obt_as;
|
||||||
ALSAConf *conf = drv_opaque;
|
|
||||||
|
|
||||||
req.fmt = aud_to_alsafmt (as->fmt, as->endianness);
|
req.fmt = aud_to_alsafmt (as->fmt, as->endianness);
|
||||||
req.freq = as->freq;
|
req.freq = as->freq;
|
||||||
req.nchannels = as->nchannels;
|
req.nchannels = as->nchannels;
|
||||||
req.period_size = conf->period_size_out;
|
req.period_size = conf.period_size_out;
|
||||||
req.buffer_size = conf->buffer_size_out;
|
req.buffer_size = conf.buffer_size_out;
|
||||||
req.size_in_usec = conf->size_in_usec_out;
|
req.size_in_usec = conf.size_in_usec_out;
|
||||||
req.override_mask =
|
req.override_mask =
|
||||||
(conf->period_size_out_overridden ? 1 : 0) |
|
(conf.period_size_out_overridden ? 1 : 0) |
|
||||||
(conf->buffer_size_out_overridden ? 2 : 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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -831,7 +860,6 @@ static int alsa_init_out(HWVoiceOut *hw, struct audsettings *as,
|
|||||||
}
|
}
|
||||||
|
|
||||||
alsa->handle = handle;
|
alsa->handle = handle;
|
||||||
alsa->pollhlp.conf = conf;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -902,26 +930,25 @@ static int alsa_ctl_out (HWVoiceOut *hw, int cmd, ...)
|
|||||||
return -1;
|
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;
|
ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
|
||||||
struct alsa_params_req req;
|
struct alsa_params_req req;
|
||||||
struct alsa_params_obt obt;
|
struct alsa_params_obt obt;
|
||||||
snd_pcm_t *handle;
|
snd_pcm_t *handle;
|
||||||
struct audsettings obt_as;
|
struct audsettings obt_as;
|
||||||
ALSAConf *conf = drv_opaque;
|
|
||||||
|
|
||||||
req.fmt = aud_to_alsafmt (as->fmt, as->endianness);
|
req.fmt = aud_to_alsafmt (as->fmt, as->endianness);
|
||||||
req.freq = as->freq;
|
req.freq = as->freq;
|
||||||
req.nchannels = as->nchannels;
|
req.nchannels = as->nchannels;
|
||||||
req.period_size = conf->period_size_in;
|
req.period_size = conf.period_size_in;
|
||||||
req.buffer_size = conf->buffer_size_in;
|
req.buffer_size = conf.buffer_size_in;
|
||||||
req.size_in_usec = conf->size_in_usec_in;
|
req.size_in_usec = conf.size_in_usec_in;
|
||||||
req.override_mask =
|
req.override_mask =
|
||||||
(conf->period_size_in_overridden ? 1 : 0) |
|
(conf.period_size_in_overridden ? 1 : 0) |
|
||||||
(conf->buffer_size_in_overridden ? 2 : 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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -942,7 +969,6 @@ static int alsa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
|
|||||||
}
|
}
|
||||||
|
|
||||||
alsa->handle = handle;
|
alsa->handle = handle;
|
||||||
alsa->pollhlp.conf = conf;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -952,9 +978,11 @@ static void alsa_fini_in (HWVoiceIn *hw)
|
|||||||
|
|
||||||
alsa_anal_close (&alsa->handle, &alsa->pollhlp);
|
alsa_anal_close (&alsa->handle, &alsa->pollhlp);
|
||||||
|
|
||||||
|
if (alsa->pcm_buf) {
|
||||||
g_free (alsa->pcm_buf);
|
g_free (alsa->pcm_buf);
|
||||||
alsa->pcm_buf = NULL;
|
alsa->pcm_buf = NULL;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static int alsa_run_in (HWVoiceIn *hw)
|
static int alsa_run_in (HWVoiceIn *hw)
|
||||||
{
|
{
|
||||||
@@ -998,10 +1026,14 @@ static int alsa_run_in (HWVoiceIn *hw)
|
|||||||
dolog ("Failed to resume suspended input stream\n");
|
dolog ("Failed to resume suspended input stream\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
trace_alsa_resume_in();
|
if (conf.verbose) {
|
||||||
|
dolog ("Resuming suspended input stream\n");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
trace_alsa_no_frames(state);
|
if (conf.verbose) {
|
||||||
|
dolog ("No frames available and ALSA state is %d\n", state);
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1036,7 +1068,9 @@ static int alsa_run_in (HWVoiceIn *hw)
|
|||||||
if (nread <= 0) {
|
if (nread <= 0) {
|
||||||
switch (nread) {
|
switch (nread) {
|
||||||
case 0:
|
case 0:
|
||||||
trace_alsa_read_zero(len);
|
if (conf.verbose) {
|
||||||
|
dolog ("Failed to read %ld frames (read zero)\n", len);
|
||||||
|
}
|
||||||
goto exit;
|
goto exit;
|
||||||
|
|
||||||
case -EPIPE:
|
case -EPIPE:
|
||||||
@@ -1044,7 +1078,9 @@ static int alsa_run_in (HWVoiceIn *hw)
|
|||||||
alsa_logerr (nread, "Failed to read %ld frames\n", len);
|
alsa_logerr (nread, "Failed to read %ld frames\n", len);
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
trace_alsa_xrun_in();
|
if (conf.verbose) {
|
||||||
|
dolog ("Recovering from capture xrun\n");
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
case -EAGAIN:
|
case -EAGAIN:
|
||||||
@@ -1116,85 +1152,82 @@ static int alsa_ctl_in (HWVoiceIn *hw, int cmd, ...)
|
|||||||
return -1;
|
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)
|
static void *alsa_audio_init (void)
|
||||||
{
|
{
|
||||||
ALSAConf *conf = g_malloc(sizeof(ALSAConf));
|
return &conf;
|
||||||
*conf = glob_conf;
|
|
||||||
return conf;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void alsa_audio_fini (void *opaque)
|
static void alsa_audio_fini (void *opaque)
|
||||||
{
|
{
|
||||||
g_free(opaque);
|
(void) opaque;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct audio_option alsa_options[] = {
|
static struct audio_option alsa_options[] = {
|
||||||
{
|
{
|
||||||
.name = "DAC_SIZE_IN_USEC",
|
.name = "DAC_SIZE_IN_USEC",
|
||||||
.tag = AUD_OPT_BOOL,
|
.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)"
|
.descr = "DAC period/buffer size in microseconds (otherwise in frames)"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "DAC_PERIOD_SIZE",
|
.name = "DAC_PERIOD_SIZE",
|
||||||
.tag = AUD_OPT_INT,
|
.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)",
|
.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",
|
.name = "DAC_BUFFER_SIZE",
|
||||||
.tag = AUD_OPT_INT,
|
.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)",
|
.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",
|
.name = "ADC_SIZE_IN_USEC",
|
||||||
.tag = AUD_OPT_BOOL,
|
.tag = AUD_OPT_BOOL,
|
||||||
.valp = &glob_conf.size_in_usec_in,
|
.valp = &conf.size_in_usec_in,
|
||||||
.descr =
|
.descr =
|
||||||
"ADC period/buffer size in microseconds (otherwise in frames)"
|
"ADC period/buffer size in microseconds (otherwise in frames)"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "ADC_PERIOD_SIZE",
|
.name = "ADC_PERIOD_SIZE",
|
||||||
.tag = AUD_OPT_INT,
|
.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)",
|
.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",
|
.name = "ADC_BUFFER_SIZE",
|
||||||
.tag = AUD_OPT_INT,
|
.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)",
|
.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",
|
.name = "THRESHOLD",
|
||||||
.tag = AUD_OPT_INT,
|
.tag = AUD_OPT_INT,
|
||||||
.valp = &glob_conf.threshold,
|
.valp = &conf.threshold,
|
||||||
.descr = "(undocumented)"
|
.descr = "(undocumented)"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "DAC_DEV",
|
.name = "DAC_DEV",
|
||||||
.tag = AUD_OPT_STR,
|
.tag = AUD_OPT_STR,
|
||||||
.valp = &glob_conf.pcm_name_out,
|
.valp = &conf.pcm_name_out,
|
||||||
.descr = "DAC device name (for instance dmix)"
|
.descr = "DAC device name (for instance dmix)"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "ADC_DEV",
|
.name = "ADC_DEV",
|
||||||
.tag = AUD_OPT_STR,
|
.tag = AUD_OPT_STR,
|
||||||
.valp = &glob_conf.pcm_name_in,
|
.valp = &conf.pcm_name_in,
|
||||||
.descr = "ADC device name"
|
.descr = "ADC device name"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
.name = "VERBOSE",
|
||||||
|
.tag = AUD_OPT_BOOL,
|
||||||
|
.valp = &conf.verbose,
|
||||||
|
.descr = "Behave in a more verbose way"
|
||||||
|
},
|
||||||
{ /* End of list */ }
|
{ /* End of list */ }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -30,6 +30,7 @@
|
|||||||
#define AUDIO_CAP "audio"
|
#define AUDIO_CAP "audio"
|
||||||
#include "audio_int.h"
|
#include "audio_int.h"
|
||||||
|
|
||||||
|
/* #define DEBUG_PLIVE */
|
||||||
/* #define DEBUG_LIVE */
|
/* #define DEBUG_LIVE */
|
||||||
/* #define DEBUG_OUT */
|
/* #define DEBUG_OUT */
|
||||||
/* #define DEBUG_CAPTURE */
|
/* #define DEBUG_CAPTURE */
|
||||||
@@ -65,6 +66,8 @@ static struct {
|
|||||||
int hertz;
|
int hertz;
|
||||||
int64_t ticks;
|
int64_t ticks;
|
||||||
} period;
|
} period;
|
||||||
|
int plive;
|
||||||
|
int log_to_monitor;
|
||||||
int try_poll_in;
|
int try_poll_in;
|
||||||
int try_poll_out;
|
int try_poll_out;
|
||||||
} conf = {
|
} conf = {
|
||||||
@@ -92,7 +95,9 @@ static struct {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
.period = { .hertz = 100 },
|
.period = { .hertz = 250 },
|
||||||
|
.plive = 0,
|
||||||
|
.log_to_monitor = 0,
|
||||||
.try_poll_in = 1,
|
.try_poll_in = 1,
|
||||||
.try_poll_out = 1,
|
.try_poll_out = 1,
|
||||||
};
|
};
|
||||||
@@ -326,12 +331,21 @@ static const char *audio_get_conf_str (const char *key,
|
|||||||
|
|
||||||
void AUD_vlog (const char *cap, const char *fmt, va_list ap)
|
void AUD_vlog (const char *cap, const char *fmt, va_list ap)
|
||||||
{
|
{
|
||||||
|
if (conf.log_to_monitor) {
|
||||||
|
if (cap) {
|
||||||
|
monitor_printf(default_mon, "%s: ", cap);
|
||||||
|
}
|
||||||
|
|
||||||
|
monitor_vprintf(default_mon, fmt, ap);
|
||||||
|
}
|
||||||
|
else {
|
||||||
if (cap) {
|
if (cap) {
|
||||||
fprintf (stderr, "%s: ", cap);
|
fprintf (stderr, "%s: ", cap);
|
||||||
}
|
}
|
||||||
|
|
||||||
vfprintf (stderr, fmt, ap);
|
vfprintf (stderr, fmt, ap);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void AUD_log (const char *cap, const char *fmt, ...)
|
void AUD_log (const char *cap, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
@@ -1110,11 +1124,10 @@ static int audio_is_timer_needed (void)
|
|||||||
static void audio_reset_timer (AudioState *s)
|
static void audio_reset_timer (AudioState *s)
|
||||||
{
|
{
|
||||||
if (audio_is_timer_needed ()) {
|
if (audio_is_timer_needed ()) {
|
||||||
timer_mod (s->ts,
|
qemu_mod_timer (s->ts, qemu_get_clock_ns (vm_clock) + 1);
|
||||||
qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + conf.period.ticks);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
timer_del (s->ts);
|
qemu_del_timer (s->ts);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1440,6 +1453,9 @@ static void audio_run_out (AudioState *s)
|
|||||||
while (sw) {
|
while (sw) {
|
||||||
sw1 = sw->entries.le_next;
|
sw1 = sw->entries.le_next;
|
||||||
if (!sw->active && !sw->callback.fn) {
|
if (!sw->active && !sw->callback.fn) {
|
||||||
|
#ifdef DEBUG_PLIVE
|
||||||
|
dolog ("Finishing with old voice\n");
|
||||||
|
#endif
|
||||||
audio_close_out (sw);
|
audio_close_out (sw);
|
||||||
}
|
}
|
||||||
sw = sw1;
|
sw = sw1;
|
||||||
@@ -1631,6 +1647,18 @@ static struct audio_option audio_options[] = {
|
|||||||
.valp = &conf.period.hertz,
|
.valp = &conf.period.hertz,
|
||||||
.descr = "Timer period in HZ (0 - use lowest possible)"
|
.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 */ }
|
{ /* End of list */ }
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -1783,6 +1811,7 @@ static const VMStateDescription vmstate_audio = {
|
|||||||
.name = "audio",
|
.name = "audio",
|
||||||
.version_id = 1,
|
.version_id = 1,
|
||||||
.minimum_version_id = 1,
|
.minimum_version_id = 1,
|
||||||
|
.minimum_version_id_old = 1,
|
||||||
.fields = (VMStateField []) {
|
.fields = (VMStateField []) {
|
||||||
VMSTATE_END_OF_LIST()
|
VMSTATE_END_OF_LIST()
|
||||||
}
|
}
|
||||||
@@ -1805,7 +1834,7 @@ static void audio_init (void)
|
|||||||
QLIST_INIT (&s->cap_head);
|
QLIST_INIT (&s->cap_head);
|
||||||
atexit (audio_atexit);
|
atexit (audio_atexit);
|
||||||
|
|
||||||
s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s);
|
s->ts = qemu_new_timer_ns (vm_clock, audio_timer, s);
|
||||||
if (!s->ts) {
|
if (!s->ts) {
|
||||||
hw_error("Could not create audio timer\n");
|
hw_error("Could not create audio timer\n");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -156,13 +156,13 @@ struct audio_driver {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct audio_pcm_ops {
|
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);
|
void (*fini_out)(HWVoiceOut *hw);
|
||||||
int (*run_out) (HWVoiceOut *hw, int live);
|
int (*run_out) (HWVoiceOut *hw, int live);
|
||||||
int (*write) (SWVoiceOut *sw, void *buf, int size);
|
int (*write) (SWVoiceOut *sw, void *buf, int size);
|
||||||
int (*ctl_out) (HWVoiceOut *hw, int cmd, ...);
|
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);
|
void (*fini_in) (HWVoiceIn *hw);
|
||||||
int (*run_in) (HWVoiceIn *hw);
|
int (*run_in) (HWVoiceIn *hw);
|
||||||
int (*read) (SWVoiceIn *sw, void *buf, int size);
|
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 oss_audio_driver;
|
||||||
extern struct audio_driver sdl_audio_driver;
|
extern struct audio_driver sdl_audio_driver;
|
||||||
extern struct audio_driver wav_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 alsa_audio_driver;
|
||||||
extern struct audio_driver coreaudio_audio_driver;
|
extern struct audio_driver coreaudio_audio_driver;
|
||||||
extern struct audio_driver dsound_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 pa_audio_driver;
|
||||||
extern struct audio_driver spice_audio_driver;
|
extern struct audio_driver spice_audio_driver;
|
||||||
|
extern struct audio_driver winwave_audio_driver;
|
||||||
extern const struct mixeng_volume nominal_volume;
|
extern const struct mixeng_volume nominal_volume;
|
||||||
|
|
||||||
void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as);
|
void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as);
|
||||||
@@ -240,13 +243,38 @@ static inline int audio_ring_dist (int dst, int src, int len)
|
|||||||
return (dst >= src) ? (dst - src) : (len - src + dst);
|
return (dst >= src) ? (dst - src) : (len - src + dst);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define dolog(fmt, ...) AUD_log(AUDIO_CAP, fmt, ## __VA_ARGS__)
|
static void GCC_ATTR dolog (const char *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list ap;
|
||||||
|
|
||||||
|
va_start (ap, fmt);
|
||||||
|
AUD_vlog (AUDIO_CAP, fmt, ap);
|
||||||
|
va_end (ap);
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
#define ldebug(fmt, ...) AUD_log(AUDIO_CAP, fmt, ## __VA_ARGS__)
|
static void GCC_ATTR ldebug (const char *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list ap;
|
||||||
|
|
||||||
|
va_start (ap, fmt);
|
||||||
|
AUD_vlog (AUDIO_CAP, fmt, ap);
|
||||||
|
va_end (ap);
|
||||||
|
}
|
||||||
#else
|
#else
|
||||||
#define ldebug(fmt, ...) (void)0
|
#if defined NDEBUG && defined __GNUC__
|
||||||
|
#define ldebug(...)
|
||||||
|
#elif defined NDEBUG && defined _MSC_VER
|
||||||
|
#define ldebug __noop
|
||||||
|
#else
|
||||||
|
static void GCC_ATTR ldebug (const char *fmt, ...)
|
||||||
|
{
|
||||||
|
(void) fmt;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#undef GCC_ATTR
|
||||||
|
|
||||||
#define AUDIO_STRINGIFY_(n) #n
|
#define AUDIO_STRINGIFY_(n) #n
|
||||||
#define AUDIO_STRINGIFY(n) AUDIO_STRINGIFY_(n)
|
#define AUDIO_STRINGIFY(n) AUDIO_STRINGIFY_(n)
|
||||||
|
|||||||
@@ -71,7 +71,10 @@ static void glue (audio_init_nb_voices_, TYPE) (struct audio_driver *drv)
|
|||||||
|
|
||||||
static void glue (audio_pcm_hw_free_resources_, TYPE) (HW *hw)
|
static void glue (audio_pcm_hw_free_resources_, TYPE) (HW *hw)
|
||||||
{
|
{
|
||||||
|
if (HWBUF) {
|
||||||
g_free (HWBUF);
|
g_free (HWBUF);
|
||||||
|
}
|
||||||
|
|
||||||
HWBUF = NULL;
|
HWBUF = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,7 +92,9 @@ static int glue (audio_pcm_hw_alloc_resources_, TYPE) (HW *hw)
|
|||||||
|
|
||||||
static void glue (audio_pcm_sw_free_resources_, TYPE) (SW *sw)
|
static void glue (audio_pcm_sw_free_resources_, TYPE) (SW *sw)
|
||||||
{
|
{
|
||||||
|
if (sw->buf) {
|
||||||
g_free (sw->buf);
|
g_free (sw->buf);
|
||||||
|
}
|
||||||
|
|
||||||
if (sw->rate) {
|
if (sw->rate) {
|
||||||
st_rate_stop (sw->rate);
|
st_rate_stop (sw->rate);
|
||||||
@@ -167,9 +172,11 @@ static int glue (audio_pcm_sw_init_, TYPE) (
|
|||||||
static void glue (audio_pcm_sw_fini_, TYPE) (SW *sw)
|
static void glue (audio_pcm_sw_fini_, TYPE) (SW *sw)
|
||||||
{
|
{
|
||||||
glue (audio_pcm_sw_free_resources_, TYPE) (sw);
|
glue (audio_pcm_sw_free_resources_, TYPE) (sw);
|
||||||
|
if (sw->name) {
|
||||||
g_free (sw->name);
|
g_free (sw->name);
|
||||||
sw->name = NULL;
|
sw->name = NULL;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void glue (audio_pcm_hw_add_sw_, TYPE) (HW *hw, SW *sw)
|
static void glue (audio_pcm_hw_add_sw_, TYPE) (HW *hw, SW *sw)
|
||||||
{
|
{
|
||||||
@@ -191,9 +198,9 @@ static void glue (audio_pcm_hw_gc_, TYPE) (HW **hwp)
|
|||||||
audio_detach_capture (hw);
|
audio_detach_capture (hw);
|
||||||
#endif
|
#endif
|
||||||
QLIST_REMOVE (hw, entries);
|
QLIST_REMOVE (hw, entries);
|
||||||
glue (hw->pcm_ops->fini_, TYPE) (hw);
|
|
||||||
glue (s->nb_hw_voices_, TYPE) += 1;
|
glue (s->nb_hw_voices_, TYPE) += 1;
|
||||||
glue (audio_pcm_hw_free_resources_ ,TYPE) (hw);
|
glue (audio_pcm_hw_free_resources_ ,TYPE) (hw);
|
||||||
|
glue (hw->pcm_ops->fini_, TYPE) (hw);
|
||||||
g_free (hw);
|
g_free (hw);
|
||||||
*hwp = NULL;
|
*hwp = NULL;
|
||||||
}
|
}
|
||||||
@@ -262,7 +269,7 @@ static HW *glue (audio_pcm_hw_add_new_, TYPE) (struct audsettings *as)
|
|||||||
#ifdef DAC
|
#ifdef DAC
|
||||||
QLIST_INIT (&hw->cap_head);
|
QLIST_INIT (&hw->cap_head);
|
||||||
#endif
|
#endif
|
||||||
if (glue (hw->pcm_ops->init_, TYPE) (hw, as, s->drv_opaque)) {
|
if (glue (hw->pcm_ops->init_, TYPE) (hw, as)) {
|
||||||
goto err0;
|
goto err0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -398,6 +405,10 @@ SW *glue (AUD_open_, TYPE) (
|
|||||||
)
|
)
|
||||||
{
|
{
|
||||||
AudioState *s = &glob_audio_state;
|
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)) {
|
if (audio_bug (AUDIO_FUNC, !card || !name || !callback_fn || !as)) {
|
||||||
dolog ("card=%p name=%p callback_fn=%p as=%p\n",
|
dolog ("card=%p name=%p callback_fn=%p as=%p\n",
|
||||||
@@ -422,6 +433,29 @@ SW *glue (AUD_open_, TYPE) (
|
|||||||
return sw;
|
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) {
|
if (!glue (conf.fixed_, TYPE).enabled && sw) {
|
||||||
glue (AUD_close_, TYPE) (card, sw);
|
glue (AUD_close_, TYPE) (card, sw);
|
||||||
sw = NULL;
|
sw = NULL;
|
||||||
@@ -454,6 +488,20 @@ SW *glue (AUD_open_, TYPE) (
|
|||||||
sw->callback.fn = callback_fn;
|
sw->callback.fn = callback_fn;
|
||||||
sw->callback.opaque = callback_opaque;
|
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
|
#ifdef DEBUG_AUDIO
|
||||||
dolog ("%s\n", name);
|
dolog ("%s\n", name);
|
||||||
audio_pcm_print_info ("hw", &sw->hw->info);
|
audio_pcm_print_info ("hw", &sw->hw->info);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
/* public domain */
|
/* public domain */
|
||||||
|
|
||||||
#include "qemu-common.h"
|
#include "qemu-common.h"
|
||||||
|
#include "audio.h"
|
||||||
|
|
||||||
#define AUDIO_CAP "win-int"
|
#define AUDIO_CAP "win-int"
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
|||||||
@@ -32,250 +32,28 @@
|
|||||||
#define AUDIO_CAP "coreaudio"
|
#define AUDIO_CAP "coreaudio"
|
||||||
#include "audio_int.h"
|
#include "audio_int.h"
|
||||||
|
|
||||||
#ifndef MAC_OS_X_VERSION_10_6
|
struct {
|
||||||
#define MAC_OS_X_VERSION_10_6 1060
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static int isAtexit;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
int buffer_frames;
|
int buffer_frames;
|
||||||
int nbuffers;
|
int nbuffers;
|
||||||
} CoreaudioConf;
|
int isAtexit;
|
||||||
|
} conf = {
|
||||||
|
.buffer_frames = 512,
|
||||||
|
.nbuffers = 4,
|
||||||
|
.isAtexit = 0
|
||||||
|
};
|
||||||
|
|
||||||
typedef struct coreaudioVoiceOut {
|
typedef struct coreaudioVoiceOut {
|
||||||
HWVoiceOut hw;
|
HWVoiceOut hw;
|
||||||
pthread_mutex_t mutex;
|
pthread_mutex_t mutex;
|
||||||
|
int isAtexit;
|
||||||
AudioDeviceID outputDeviceID;
|
AudioDeviceID outputDeviceID;
|
||||||
UInt32 audioDevicePropertyBufferFrameSize;
|
UInt32 audioDevicePropertyBufferFrameSize;
|
||||||
AudioStreamBasicDescription outputStreamBasicDescription;
|
AudioStreamBasicDescription outputStreamBasicDescription;
|
||||||
AudioDeviceIOProcID ioprocid;
|
|
||||||
int live;
|
int live;
|
||||||
int decr;
|
int decr;
|
||||||
int rpos;
|
int rpos;
|
||||||
} coreaudioVoiceOut;
|
} coreaudioVoiceOut;
|
||||||
|
|
||||||
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
|
|
||||||
/* The APIs used here only become available from 10.6 */
|
|
||||||
|
|
||||||
static OSStatus coreaudio_get_voice(AudioDeviceID *id)
|
|
||||||
{
|
|
||||||
UInt32 size = sizeof(*id);
|
|
||||||
AudioObjectPropertyAddress addr = {
|
|
||||||
kAudioHardwarePropertyDefaultOutputDevice,
|
|
||||||
kAudioObjectPropertyScopeGlobal,
|
|
||||||
kAudioObjectPropertyElementMaster
|
|
||||||
};
|
|
||||||
|
|
||||||
return AudioObjectGetPropertyData(kAudioObjectSystemObject,
|
|
||||||
&addr,
|
|
||||||
0,
|
|
||||||
NULL,
|
|
||||||
&size,
|
|
||||||
id);
|
|
||||||
}
|
|
||||||
|
|
||||||
static OSStatus coreaudio_get_framesizerange(AudioDeviceID id,
|
|
||||||
AudioValueRange *framerange)
|
|
||||||
{
|
|
||||||
UInt32 size = sizeof(*framerange);
|
|
||||||
AudioObjectPropertyAddress addr = {
|
|
||||||
kAudioDevicePropertyBufferFrameSizeRange,
|
|
||||||
kAudioDevicePropertyScopeOutput,
|
|
||||||
kAudioObjectPropertyElementMaster
|
|
||||||
};
|
|
||||||
|
|
||||||
return AudioObjectGetPropertyData(id,
|
|
||||||
&addr,
|
|
||||||
0,
|
|
||||||
NULL,
|
|
||||||
&size,
|
|
||||||
framerange);
|
|
||||||
}
|
|
||||||
|
|
||||||
static OSStatus coreaudio_get_framesize(AudioDeviceID id, UInt32 *framesize)
|
|
||||||
{
|
|
||||||
UInt32 size = sizeof(*framesize);
|
|
||||||
AudioObjectPropertyAddress addr = {
|
|
||||||
kAudioDevicePropertyBufferFrameSize,
|
|
||||||
kAudioDevicePropertyScopeOutput,
|
|
||||||
kAudioObjectPropertyElementMaster
|
|
||||||
};
|
|
||||||
|
|
||||||
return AudioObjectGetPropertyData(id,
|
|
||||||
&addr,
|
|
||||||
0,
|
|
||||||
NULL,
|
|
||||||
&size,
|
|
||||||
framesize);
|
|
||||||
}
|
|
||||||
|
|
||||||
static OSStatus coreaudio_set_framesize(AudioDeviceID id, UInt32 *framesize)
|
|
||||||
{
|
|
||||||
UInt32 size = sizeof(*framesize);
|
|
||||||
AudioObjectPropertyAddress addr = {
|
|
||||||
kAudioDevicePropertyBufferFrameSize,
|
|
||||||
kAudioDevicePropertyScopeOutput,
|
|
||||||
kAudioObjectPropertyElementMaster
|
|
||||||
};
|
|
||||||
|
|
||||||
return AudioObjectSetPropertyData(id,
|
|
||||||
&addr,
|
|
||||||
0,
|
|
||||||
NULL,
|
|
||||||
size,
|
|
||||||
framesize);
|
|
||||||
}
|
|
||||||
|
|
||||||
static OSStatus coreaudio_get_streamformat(AudioDeviceID id,
|
|
||||||
AudioStreamBasicDescription *d)
|
|
||||||
{
|
|
||||||
UInt32 size = sizeof(*d);
|
|
||||||
AudioObjectPropertyAddress addr = {
|
|
||||||
kAudioDevicePropertyStreamFormat,
|
|
||||||
kAudioDevicePropertyScopeOutput,
|
|
||||||
kAudioObjectPropertyElementMaster
|
|
||||||
};
|
|
||||||
|
|
||||||
return AudioObjectGetPropertyData(id,
|
|
||||||
&addr,
|
|
||||||
0,
|
|
||||||
NULL,
|
|
||||||
&size,
|
|
||||||
d);
|
|
||||||
}
|
|
||||||
|
|
||||||
static OSStatus coreaudio_set_streamformat(AudioDeviceID id,
|
|
||||||
AudioStreamBasicDescription *d)
|
|
||||||
{
|
|
||||||
UInt32 size = sizeof(*d);
|
|
||||||
AudioObjectPropertyAddress addr = {
|
|
||||||
kAudioDevicePropertyStreamFormat,
|
|
||||||
kAudioDevicePropertyScopeOutput,
|
|
||||||
kAudioObjectPropertyElementMaster
|
|
||||||
};
|
|
||||||
|
|
||||||
return AudioObjectSetPropertyData(id,
|
|
||||||
&addr,
|
|
||||||
0,
|
|
||||||
NULL,
|
|
||||||
size,
|
|
||||||
d);
|
|
||||||
}
|
|
||||||
|
|
||||||
static OSStatus coreaudio_get_isrunning(AudioDeviceID id, UInt32 *result)
|
|
||||||
{
|
|
||||||
UInt32 size = sizeof(*result);
|
|
||||||
AudioObjectPropertyAddress addr = {
|
|
||||||
kAudioDevicePropertyDeviceIsRunning,
|
|
||||||
kAudioDevicePropertyScopeOutput,
|
|
||||||
kAudioObjectPropertyElementMaster
|
|
||||||
};
|
|
||||||
|
|
||||||
return AudioObjectGetPropertyData(id,
|
|
||||||
&addr,
|
|
||||||
0,
|
|
||||||
NULL,
|
|
||||||
&size,
|
|
||||||
result);
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
/* Legacy versions of functions using deprecated APIs */
|
|
||||||
|
|
||||||
static OSStatus coreaudio_get_voice(AudioDeviceID *id)
|
|
||||||
{
|
|
||||||
UInt32 size = sizeof(*id);
|
|
||||||
|
|
||||||
return AudioHardwareGetProperty(
|
|
||||||
kAudioHardwarePropertyDefaultOutputDevice,
|
|
||||||
&size,
|
|
||||||
id);
|
|
||||||
}
|
|
||||||
|
|
||||||
static OSStatus coreaudio_get_framesizerange(AudioDeviceID id,
|
|
||||||
AudioValueRange *framerange)
|
|
||||||
{
|
|
||||||
UInt32 size = sizeof(*framerange);
|
|
||||||
|
|
||||||
return AudioDeviceGetProperty(
|
|
||||||
id,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
kAudioDevicePropertyBufferFrameSizeRange,
|
|
||||||
&size,
|
|
||||||
framerange);
|
|
||||||
}
|
|
||||||
|
|
||||||
static OSStatus coreaudio_get_framesize(AudioDeviceID id, UInt32 *framesize)
|
|
||||||
{
|
|
||||||
UInt32 size = sizeof(*framesize);
|
|
||||||
|
|
||||||
return AudioDeviceGetProperty(
|
|
||||||
id,
|
|
||||||
0,
|
|
||||||
false,
|
|
||||||
kAudioDevicePropertyBufferFrameSize,
|
|
||||||
&size,
|
|
||||||
framesize);
|
|
||||||
}
|
|
||||||
|
|
||||||
static OSStatus coreaudio_set_framesize(AudioDeviceID id, UInt32 *framesize)
|
|
||||||
{
|
|
||||||
UInt32 size = sizeof(*framesize);
|
|
||||||
|
|
||||||
return AudioDeviceSetProperty(
|
|
||||||
id,
|
|
||||||
NULL,
|
|
||||||
0,
|
|
||||||
false,
|
|
||||||
kAudioDevicePropertyBufferFrameSize,
|
|
||||||
size,
|
|
||||||
framesize);
|
|
||||||
}
|
|
||||||
|
|
||||||
static OSStatus coreaudio_get_streamformat(AudioDeviceID id,
|
|
||||||
AudioStreamBasicDescription *d)
|
|
||||||
{
|
|
||||||
UInt32 size = sizeof(*d);
|
|
||||||
|
|
||||||
return AudioDeviceGetProperty(
|
|
||||||
id,
|
|
||||||
0,
|
|
||||||
false,
|
|
||||||
kAudioDevicePropertyStreamFormat,
|
|
||||||
&size,
|
|
||||||
d);
|
|
||||||
}
|
|
||||||
|
|
||||||
static OSStatus coreaudio_set_streamformat(AudioDeviceID id,
|
|
||||||
AudioStreamBasicDescription *d)
|
|
||||||
{
|
|
||||||
UInt32 size = sizeof(*d);
|
|
||||||
|
|
||||||
return AudioDeviceSetProperty(
|
|
||||||
id,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
kAudioDevicePropertyStreamFormat,
|
|
||||||
size,
|
|
||||||
d);
|
|
||||||
}
|
|
||||||
|
|
||||||
static OSStatus coreaudio_get_isrunning(AudioDeviceID id, UInt32 *result)
|
|
||||||
{
|
|
||||||
UInt32 size = sizeof(*result);
|
|
||||||
|
|
||||||
return AudioDeviceGetProperty(
|
|
||||||
id,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
kAudioDevicePropertyDeviceIsRunning,
|
|
||||||
&size,
|
|
||||||
result);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static void coreaudio_logstatus (OSStatus status)
|
static void coreaudio_logstatus (OSStatus status)
|
||||||
{
|
{
|
||||||
const char *str = "BUG";
|
const char *str = "BUG";
|
||||||
@@ -370,7 +148,10 @@ static inline UInt32 isPlaying (AudioDeviceID outputDeviceID)
|
|||||||
{
|
{
|
||||||
OSStatus status;
|
OSStatus status;
|
||||||
UInt32 result = 0;
|
UInt32 result = 0;
|
||||||
status = coreaudio_get_isrunning(outputDeviceID, &result);
|
UInt32 propertySize = sizeof(outputDeviceID);
|
||||||
|
status = AudioDeviceGetProperty(
|
||||||
|
outputDeviceID, 0, 0,
|
||||||
|
kAudioDevicePropertyDeviceIsRunning, &propertySize, &result);
|
||||||
if (status != kAudioHardwareNoError) {
|
if (status != kAudioHardwareNoError) {
|
||||||
coreaudio_logerr(status,
|
coreaudio_logerr(status,
|
||||||
"Could not determine whether Device is playing\n");
|
"Could not determine whether Device is playing\n");
|
||||||
@@ -380,7 +161,7 @@ static inline UInt32 isPlaying (AudioDeviceID outputDeviceID)
|
|||||||
|
|
||||||
static void coreaudio_atexit (void)
|
static void coreaudio_atexit (void)
|
||||||
{
|
{
|
||||||
isAtexit = 1;
|
conf.isAtexit = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int coreaudio_lock (coreaudioVoiceOut *core, const char *fn_name)
|
static int coreaudio_lock (coreaudioVoiceOut *core, const char *fn_name)
|
||||||
@@ -506,15 +287,14 @@ static int coreaudio_write (SWVoiceOut *sw, void *buf, int len)
|
|||||||
return audio_pcm_sw_write (sw, buf, len);
|
return audio_pcm_sw_write (sw, buf, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as,
|
static int coreaudio_init_out (HWVoiceOut *hw, struct audsettings *as)
|
||||||
void *drv_opaque)
|
|
||||||
{
|
{
|
||||||
OSStatus status;
|
OSStatus status;
|
||||||
coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw;
|
coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw;
|
||||||
|
UInt32 propertySize;
|
||||||
int err;
|
int err;
|
||||||
const char *typ = "playback";
|
const char *typ = "playback";
|
||||||
AudioValueRange frameRange;
|
AudioValueRange frameRange;
|
||||||
CoreaudioConf *conf = drv_opaque;
|
|
||||||
|
|
||||||
/* create mutex */
|
/* create mutex */
|
||||||
err = pthread_mutex_init(&core->mutex, NULL);
|
err = pthread_mutex_init(&core->mutex, NULL);
|
||||||
@@ -525,7 +305,12 @@ static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as,
|
|||||||
|
|
||||||
audio_pcm_init_info (&hw->info, as);
|
audio_pcm_init_info (&hw->info, as);
|
||||||
|
|
||||||
status = coreaudio_get_voice(&core->outputDeviceID);
|
/* open default output device */
|
||||||
|
propertySize = sizeof(core->outputDeviceID);
|
||||||
|
status = AudioHardwareGetProperty(
|
||||||
|
kAudioHardwarePropertyDefaultOutputDevice,
|
||||||
|
&propertySize,
|
||||||
|
&core->outputDeviceID);
|
||||||
if (status != kAudioHardwareNoError) {
|
if (status != kAudioHardwareNoError) {
|
||||||
coreaudio_logerr2 (status, typ,
|
coreaudio_logerr2 (status, typ,
|
||||||
"Could not get default output Device\n");
|
"Could not get default output Device\n");
|
||||||
@@ -537,7 +322,13 @@ static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* get minimum and maximum buffer frame sizes */
|
/* get minimum and maximum buffer frame sizes */
|
||||||
status = coreaudio_get_framesizerange(core->outputDeviceID,
|
propertySize = sizeof(frameRange);
|
||||||
|
status = AudioDeviceGetProperty(
|
||||||
|
core->outputDeviceID,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
kAudioDevicePropertyBufferFrameSizeRange,
|
||||||
|
&propertySize,
|
||||||
&frameRange);
|
&frameRange);
|
||||||
if (status != kAudioHardwareNoError) {
|
if (status != kAudioHardwareNoError) {
|
||||||
coreaudio_logerr2 (status, typ,
|
coreaudio_logerr2 (status, typ,
|
||||||
@@ -545,20 +336,27 @@ static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as,
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (frameRange.mMinimum > conf->buffer_frames) {
|
if (frameRange.mMinimum > conf.buffer_frames) {
|
||||||
core->audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMinimum;
|
core->audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMinimum;
|
||||||
dolog ("warning: Upsizing Buffer Frames to %f\n", 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;
|
core->audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMaximum;
|
||||||
dolog ("warning: Downsizing Buffer Frames to %f\n", frameRange.mMaximum);
|
dolog ("warning: Downsizing Buffer Frames to %f\n", frameRange.mMaximum);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
core->audioDevicePropertyBufferFrameSize = conf->buffer_frames;
|
core->audioDevicePropertyBufferFrameSize = conf.buffer_frames;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* set Buffer Frame Size */
|
/* set Buffer Frame Size */
|
||||||
status = coreaudio_set_framesize(core->outputDeviceID,
|
propertySize = sizeof(core->audioDevicePropertyBufferFrameSize);
|
||||||
|
status = AudioDeviceSetProperty(
|
||||||
|
core->outputDeviceID,
|
||||||
|
NULL,
|
||||||
|
0,
|
||||||
|
false,
|
||||||
|
kAudioDevicePropertyBufferFrameSize,
|
||||||
|
propertySize,
|
||||||
&core->audioDevicePropertyBufferFrameSize);
|
&core->audioDevicePropertyBufferFrameSize);
|
||||||
if (status != kAudioHardwareNoError) {
|
if (status != kAudioHardwareNoError) {
|
||||||
coreaudio_logerr2 (status, typ,
|
coreaudio_logerr2 (status, typ,
|
||||||
@@ -568,17 +366,29 @@ static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* get Buffer Frame Size */
|
/* get Buffer Frame Size */
|
||||||
status = coreaudio_get_framesize(core->outputDeviceID,
|
propertySize = sizeof(core->audioDevicePropertyBufferFrameSize);
|
||||||
|
status = AudioDeviceGetProperty(
|
||||||
|
core->outputDeviceID,
|
||||||
|
0,
|
||||||
|
false,
|
||||||
|
kAudioDevicePropertyBufferFrameSize,
|
||||||
|
&propertySize,
|
||||||
&core->audioDevicePropertyBufferFrameSize);
|
&core->audioDevicePropertyBufferFrameSize);
|
||||||
if (status != kAudioHardwareNoError) {
|
if (status != kAudioHardwareNoError) {
|
||||||
coreaudio_logerr2 (status, typ,
|
coreaudio_logerr2 (status, typ,
|
||||||
"Could not get device buffer frame size\n");
|
"Could not get device buffer frame size\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
hw->samples = conf->nbuffers * core->audioDevicePropertyBufferFrameSize;
|
hw->samples = conf.nbuffers * core->audioDevicePropertyBufferFrameSize;
|
||||||
|
|
||||||
/* get StreamFormat */
|
/* get StreamFormat */
|
||||||
status = coreaudio_get_streamformat(core->outputDeviceID,
|
propertySize = sizeof(core->outputStreamBasicDescription);
|
||||||
|
status = AudioDeviceGetProperty(
|
||||||
|
core->outputDeviceID,
|
||||||
|
0,
|
||||||
|
false,
|
||||||
|
kAudioDevicePropertyStreamFormat,
|
||||||
|
&propertySize,
|
||||||
&core->outputStreamBasicDescription);
|
&core->outputStreamBasicDescription);
|
||||||
if (status != kAudioHardwareNoError) {
|
if (status != kAudioHardwareNoError) {
|
||||||
coreaudio_logerr2 (status, typ,
|
coreaudio_logerr2 (status, typ,
|
||||||
@@ -589,7 +399,14 @@ static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as,
|
|||||||
|
|
||||||
/* set Samplerate */
|
/* set Samplerate */
|
||||||
core->outputStreamBasicDescription.mSampleRate = (Float64) as->freq;
|
core->outputStreamBasicDescription.mSampleRate = (Float64) as->freq;
|
||||||
status = coreaudio_set_streamformat(core->outputDeviceID,
|
propertySize = sizeof(core->outputStreamBasicDescription);
|
||||||
|
status = AudioDeviceSetProperty(
|
||||||
|
core->outputDeviceID,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
kAudioDevicePropertyStreamFormat,
|
||||||
|
propertySize,
|
||||||
&core->outputStreamBasicDescription);
|
&core->outputStreamBasicDescription);
|
||||||
if (status != kAudioHardwareNoError) {
|
if (status != kAudioHardwareNoError) {
|
||||||
coreaudio_logerr2 (status, typ, "Could not set samplerate %d\n",
|
coreaudio_logerr2 (status, typ, "Could not set samplerate %d\n",
|
||||||
@@ -599,12 +416,8 @@ static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* set Callback */
|
/* set Callback */
|
||||||
core->ioprocid = NULL;
|
status = AudioDeviceAddIOProc(core->outputDeviceID, audioDeviceIOProc, hw);
|
||||||
status = AudioDeviceCreateIOProcID(core->outputDeviceID,
|
if (status != kAudioHardwareNoError) {
|
||||||
audioDeviceIOProc,
|
|
||||||
hw,
|
|
||||||
&core->ioprocid);
|
|
||||||
if (status != kAudioHardwareNoError || core->ioprocid == NULL) {
|
|
||||||
coreaudio_logerr2 (status, typ, "Could not set IOProc\n");
|
coreaudio_logerr2 (status, typ, "Could not set IOProc\n");
|
||||||
core->outputDeviceID = kAudioDeviceUnknown;
|
core->outputDeviceID = kAudioDeviceUnknown;
|
||||||
return -1;
|
return -1;
|
||||||
@@ -612,10 +425,10 @@ static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as,
|
|||||||
|
|
||||||
/* start Playback */
|
/* start Playback */
|
||||||
if (!isPlaying(core->outputDeviceID)) {
|
if (!isPlaying(core->outputDeviceID)) {
|
||||||
status = AudioDeviceStart(core->outputDeviceID, core->ioprocid);
|
status = AudioDeviceStart(core->outputDeviceID, audioDeviceIOProc);
|
||||||
if (status != kAudioHardwareNoError) {
|
if (status != kAudioHardwareNoError) {
|
||||||
coreaudio_logerr2 (status, typ, "Could not start playback\n");
|
coreaudio_logerr2 (status, typ, "Could not start playback\n");
|
||||||
AudioDeviceDestroyIOProcID(core->outputDeviceID, core->ioprocid);
|
AudioDeviceRemoveIOProc(core->outputDeviceID, audioDeviceIOProc);
|
||||||
core->outputDeviceID = kAudioDeviceUnknown;
|
core->outputDeviceID = kAudioDeviceUnknown;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -630,18 +443,18 @@ static void coreaudio_fini_out (HWVoiceOut *hw)
|
|||||||
int err;
|
int err;
|
||||||
coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw;
|
coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw;
|
||||||
|
|
||||||
if (!isAtexit) {
|
if (!conf.isAtexit) {
|
||||||
/* stop playback */
|
/* stop playback */
|
||||||
if (isPlaying(core->outputDeviceID)) {
|
if (isPlaying(core->outputDeviceID)) {
|
||||||
status = AudioDeviceStop(core->outputDeviceID, core->ioprocid);
|
status = AudioDeviceStop(core->outputDeviceID, audioDeviceIOProc);
|
||||||
if (status != kAudioHardwareNoError) {
|
if (status != kAudioHardwareNoError) {
|
||||||
coreaudio_logerr (status, "Could not stop playback\n");
|
coreaudio_logerr (status, "Could not stop playback\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* remove callback */
|
/* remove callback */
|
||||||
status = AudioDeviceDestroyIOProcID(core->outputDeviceID,
|
status = AudioDeviceRemoveIOProc(core->outputDeviceID,
|
||||||
core->ioprocid);
|
audioDeviceIOProc);
|
||||||
if (status != kAudioHardwareNoError) {
|
if (status != kAudioHardwareNoError) {
|
||||||
coreaudio_logerr (status, "Could not remove IOProc\n");
|
coreaudio_logerr (status, "Could not remove IOProc\n");
|
||||||
}
|
}
|
||||||
@@ -664,7 +477,7 @@ static int coreaudio_ctl_out (HWVoiceOut *hw, int cmd, ...)
|
|||||||
case VOICE_ENABLE:
|
case VOICE_ENABLE:
|
||||||
/* start playback */
|
/* start playback */
|
||||||
if (!isPlaying(core->outputDeviceID)) {
|
if (!isPlaying(core->outputDeviceID)) {
|
||||||
status = AudioDeviceStart(core->outputDeviceID, core->ioprocid);
|
status = AudioDeviceStart(core->outputDeviceID, audioDeviceIOProc);
|
||||||
if (status != kAudioHardwareNoError) {
|
if (status != kAudioHardwareNoError) {
|
||||||
coreaudio_logerr (status, "Could not resume playback\n");
|
coreaudio_logerr (status, "Could not resume playback\n");
|
||||||
}
|
}
|
||||||
@@ -673,10 +486,9 @@ static int coreaudio_ctl_out (HWVoiceOut *hw, int cmd, ...)
|
|||||||
|
|
||||||
case VOICE_DISABLE:
|
case VOICE_DISABLE:
|
||||||
/* stop playback */
|
/* stop playback */
|
||||||
if (!isAtexit) {
|
if (!conf.isAtexit) {
|
||||||
if (isPlaying(core->outputDeviceID)) {
|
if (isPlaying(core->outputDeviceID)) {
|
||||||
status = AudioDeviceStop(core->outputDeviceID,
|
status = AudioDeviceStop(core->outputDeviceID, audioDeviceIOProc);
|
||||||
core->ioprocid);
|
|
||||||
if (status != kAudioHardwareNoError) {
|
if (status != kAudioHardwareNoError) {
|
||||||
coreaudio_logerr (status, "Could not pause playback\n");
|
coreaudio_logerr (status, "Could not pause playback\n");
|
||||||
}
|
}
|
||||||
@@ -687,36 +499,28 @@ static int coreaudio_ctl_out (HWVoiceOut *hw, int cmd, ...)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static CoreaudioConf glob_conf = {
|
|
||||||
.buffer_frames = 512,
|
|
||||||
.nbuffers = 4,
|
|
||||||
};
|
|
||||||
|
|
||||||
static void *coreaudio_audio_init (void)
|
static void *coreaudio_audio_init (void)
|
||||||
{
|
{
|
||||||
CoreaudioConf *conf = g_malloc(sizeof(CoreaudioConf));
|
|
||||||
*conf = glob_conf;
|
|
||||||
|
|
||||||
atexit(coreaudio_atexit);
|
atexit(coreaudio_atexit);
|
||||||
return conf;
|
return &coreaudio_audio_init;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void coreaudio_audio_fini (void *opaque)
|
static void coreaudio_audio_fini (void *opaque)
|
||||||
{
|
{
|
||||||
g_free(opaque);
|
(void) opaque;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct audio_option coreaudio_options[] = {
|
static struct audio_option coreaudio_options[] = {
|
||||||
{
|
{
|
||||||
.name = "BUFFER_SIZE",
|
.name = "BUFFER_SIZE",
|
||||||
.tag = AUD_OPT_INT,
|
.tag = AUD_OPT_INT,
|
||||||
.valp = &glob_conf.buffer_frames,
|
.valp = &conf.buffer_frames,
|
||||||
.descr = "Size of the buffer in frames"
|
.descr = "Size of the buffer in frames"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "BUFFER_COUNT",
|
.name = "BUFFER_COUNT",
|
||||||
.tag = AUD_OPT_INT,
|
.tag = AUD_OPT_INT,
|
||||||
.valp = &glob_conf.nbuffers,
|
.valp = &conf.nbuffers,
|
||||||
.descr = "Number of buffers"
|
.descr = "Number of buffers"
|
||||||
},
|
},
|
||||||
{ /* End of list */ }
|
{ /* End of list */ }
|
||||||
|
|||||||
@@ -67,11 +67,11 @@ static int glue (dsound_lock_, TYPE) (
|
|||||||
LPVOID *p2p,
|
LPVOID *p2p,
|
||||||
DWORD *blen1p,
|
DWORD *blen1p,
|
||||||
DWORD *blen2p,
|
DWORD *blen2p,
|
||||||
int entire,
|
int entire
|
||||||
dsound *s
|
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
HRESULT hr;
|
HRESULT hr;
|
||||||
|
int i;
|
||||||
LPVOID p1 = NULL, p2 = NULL;
|
LPVOID p1 = NULL, p2 = NULL;
|
||||||
DWORD blen1 = 0, blen2 = 0;
|
DWORD blen1 = 0, blen2 = 0;
|
||||||
DWORD flag;
|
DWORD flag;
|
||||||
@@ -81,21 +81,40 @@ static int glue (dsound_lock_, TYPE) (
|
|||||||
#else
|
#else
|
||||||
flag = entire ? DSBLOCK_ENTIREBUFFER : 0;
|
flag = entire ? DSBLOCK_ENTIREBUFFER : 0;
|
||||||
#endif
|
#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
|
#ifndef DSBTYPE_IN
|
||||||
if (hr == DSERR_BUFFERLOST) {
|
if (hr == DSERR_BUFFERLOST) {
|
||||||
if (glue (dsound_restore_, TYPE) (buf, s)) {
|
if (glue (dsound_restore_, TYPE) (buf)) {
|
||||||
dsound_logerr (hr, "Could not lock " NAME "\n");
|
dsound_logerr (hr, "Could not lock " NAME "\n");
|
||||||
}
|
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
dsound_logerr (hr, "Could not lock " NAME "\n");
|
dsound_logerr (hr, "Could not lock " NAME "\n");
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i == conf.lock_retries) {
|
||||||
|
dolog ("%d attempts to lock " NAME " failed\n", i);
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
if ((p1 && (blen1 & info->align)) || (p2 && (blen2 & info->align))) {
|
if ((p1 && (blen1 & info->align)) || (p2 && (blen2 & info->align))) {
|
||||||
dolog ("DirectSound returned misaligned buffer %ld %ld\n",
|
dolog ("DirectSound returned misaligned buffer %ld %ld\n",
|
||||||
blen1, blen2);
|
blen1, blen2);
|
||||||
@@ -155,19 +174,16 @@ static void dsound_fini_out (HWVoiceOut *hw)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef DSBTYPE_IN
|
#ifdef DSBTYPE_IN
|
||||||
static int dsound_init_in(HWVoiceIn *hw, struct audsettings *as,
|
static int dsound_init_in (HWVoiceIn *hw, struct audsettings *as)
|
||||||
void *drv_opaque)
|
|
||||||
#else
|
#else
|
||||||
static int dsound_init_out(HWVoiceOut *hw, struct audsettings *as,
|
static int dsound_init_out (HWVoiceOut *hw, struct audsettings *as)
|
||||||
void *drv_opaque)
|
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
int err;
|
int err;
|
||||||
HRESULT hr;
|
HRESULT hr;
|
||||||
dsound *s = drv_opaque;
|
dsound *s = &glob_dsound;
|
||||||
WAVEFORMATEX wfx;
|
WAVEFORMATEX wfx;
|
||||||
struct audsettings obt_as;
|
struct audsettings obt_as;
|
||||||
DSoundConf *conf = &s->conf;
|
|
||||||
#ifdef DSBTYPE_IN
|
#ifdef DSBTYPE_IN
|
||||||
const char *typ = "ADC";
|
const char *typ = "ADC";
|
||||||
DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
|
DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
|
||||||
@@ -194,7 +210,7 @@ static int dsound_init_out(HWVoiceOut *hw, struct audsettings *as,
|
|||||||
bd.dwSize = sizeof (bd);
|
bd.dwSize = sizeof (bd);
|
||||||
bd.lpwfxFormat = &wfx;
|
bd.lpwfxFormat = &wfx;
|
||||||
#ifdef DSBTYPE_IN
|
#ifdef DSBTYPE_IN
|
||||||
bd.dwBufferBytes = conf->bufsize_in;
|
bd.dwBufferBytes = conf.bufsize_in;
|
||||||
hr = IDirectSoundCapture_CreateCaptureBuffer (
|
hr = IDirectSoundCapture_CreateCaptureBuffer (
|
||||||
s->dsound_capture,
|
s->dsound_capture,
|
||||||
&bd,
|
&bd,
|
||||||
@@ -203,7 +219,7 @@ static int dsound_init_out(HWVoiceOut *hw, struct audsettings *as,
|
|||||||
);
|
);
|
||||||
#else
|
#else
|
||||||
bd.dwFlags = DSBCAPS_STICKYFOCUS | DSBCAPS_GETCURRENTPOSITION2;
|
bd.dwFlags = DSBCAPS_STICKYFOCUS | DSBCAPS_GETCURRENTPOSITION2;
|
||||||
bd.dwBufferBytes = conf->bufsize_out;
|
bd.dwBufferBytes = conf.bufsize_out;
|
||||||
hr = IDirectSound_CreateSoundBuffer (
|
hr = IDirectSound_CreateSoundBuffer (
|
||||||
s->dsound,
|
s->dsound,
|
||||||
&bd,
|
&bd,
|
||||||
@@ -253,7 +269,6 @@ static int dsound_init_out(HWVoiceOut *hw, struct audsettings *as,
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
hw->samples = bc.dwBufferBytes >> hw->info.shift;
|
hw->samples = bc.dwBufferBytes >> hw->info.shift;
|
||||||
ds->s = s;
|
|
||||||
|
|
||||||
#ifdef DEBUG_DSOUND
|
#ifdef DEBUG_DSOUND
|
||||||
dolog ("caps %ld, desc %ld\n",
|
dolog ("caps %ld, desc %ld\n",
|
||||||
|
|||||||
@@ -41,25 +41,42 @@
|
|||||||
|
|
||||||
/* #define DEBUG_DSOUND */
|
/* #define DEBUG_DSOUND */
|
||||||
|
|
||||||
typedef struct {
|
static struct {
|
||||||
|
int lock_retries;
|
||||||
|
int restore_retries;
|
||||||
|
int getstatus_retries;
|
||||||
|
int set_primary;
|
||||||
int bufsize_in;
|
int bufsize_in;
|
||||||
int bufsize_out;
|
int bufsize_out;
|
||||||
|
struct audsettings settings;
|
||||||
int latency_millis;
|
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 {
|
typedef struct {
|
||||||
LPDIRECTSOUND dsound;
|
LPDIRECTSOUND dsound;
|
||||||
LPDIRECTSOUNDCAPTURE dsound_capture;
|
LPDIRECTSOUNDCAPTURE dsound_capture;
|
||||||
|
LPDIRECTSOUNDBUFFER dsound_primary_buffer;
|
||||||
struct audsettings settings;
|
struct audsettings settings;
|
||||||
DSoundConf conf;
|
|
||||||
} dsound;
|
} dsound;
|
||||||
|
|
||||||
|
static dsound glob_dsound;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
HWVoiceOut hw;
|
HWVoiceOut hw;
|
||||||
LPDIRECTSOUNDBUFFER dsound_buffer;
|
LPDIRECTSOUNDBUFFER dsound_buffer;
|
||||||
DWORD old_pos;
|
DWORD old_pos;
|
||||||
int first_time;
|
int first_time;
|
||||||
dsound *s;
|
|
||||||
#ifdef DEBUG_DSOUND
|
#ifdef DEBUG_DSOUND
|
||||||
DWORD old_ppos;
|
DWORD old_ppos;
|
||||||
DWORD played;
|
DWORD played;
|
||||||
@@ -71,7 +88,6 @@ typedef struct {
|
|||||||
HWVoiceIn hw;
|
HWVoiceIn hw;
|
||||||
int first_time;
|
int first_time;
|
||||||
LPDIRECTSOUNDCAPTUREBUFFER dsound_capture_buffer;
|
LPDIRECTSOUNDCAPTUREBUFFER dsound_capture_buffer;
|
||||||
dsound *s;
|
|
||||||
} DSoundVoiceIn;
|
} DSoundVoiceIn;
|
||||||
|
|
||||||
static void dsound_log_hresult (HRESULT hr)
|
static void dsound_log_hresult (HRESULT hr)
|
||||||
@@ -265,17 +281,29 @@ static void print_wave_format (WAVEFORMATEX *wfx)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb, dsound *s)
|
static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb)
|
||||||
{
|
{
|
||||||
HRESULT hr;
|
HRESULT hr;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < conf.restore_retries; ++i) {
|
||||||
hr = IDirectSoundBuffer_Restore (dsb);
|
hr = IDirectSoundBuffer_Restore (dsb);
|
||||||
|
|
||||||
if (hr != DS_OK) {
|
switch (hr) {
|
||||||
|
case DS_OK:
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
case DSERR_BUFFERLOST:
|
||||||
|
continue;
|
||||||
|
|
||||||
|
default:
|
||||||
dsound_logerr (hr, "Could not restore playback buffer\n");
|
dsound_logerr (hr, "Could not restore playback buffer\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 0;
|
}
|
||||||
|
|
||||||
|
dolog ("%d attempts to restore playback buffer failed\n", i);
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "dsound_template.h"
|
#include "dsound_template.h"
|
||||||
@@ -283,11 +311,12 @@ static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb, dsound *s)
|
|||||||
#include "dsound_template.h"
|
#include "dsound_template.h"
|
||||||
#undef DSBTYPE_IN
|
#undef DSBTYPE_IN
|
||||||
|
|
||||||
static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb, DWORD *statusp,
|
static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb, DWORD *statusp)
|
||||||
dsound *s)
|
|
||||||
{
|
{
|
||||||
HRESULT hr;
|
HRESULT hr;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < conf.getstatus_retries; ++i) {
|
||||||
hr = IDirectSoundBuffer_GetStatus (dsb, statusp);
|
hr = IDirectSoundBuffer_GetStatus (dsb, statusp);
|
||||||
if (FAILED (hr)) {
|
if (FAILED (hr)) {
|
||||||
dsound_logerr (hr, "Could not get playback buffer status\n");
|
dsound_logerr (hr, "Could not get playback buffer status\n");
|
||||||
@@ -295,9 +324,13 @@ static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb, DWORD *statusp,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (*statusp & DSERR_BUFFERLOST) {
|
if (*statusp & DSERR_BUFFERLOST) {
|
||||||
dsound_restore_out(dsb, s);
|
if (dsound_restore_out (dsb)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -343,8 +376,7 @@ static void dsound_write_sample (HWVoiceOut *hw, uint8_t *dst, int dst_len)
|
|||||||
hw->rpos = pos % hw->samples;
|
hw->rpos = pos % hw->samples;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb,
|
static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb)
|
||||||
dsound *s)
|
|
||||||
{
|
{
|
||||||
int err;
|
int err;
|
||||||
LPVOID p1, p2;
|
LPVOID p1, p2;
|
||||||
@@ -357,8 +389,7 @@ static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb,
|
|||||||
hw->samples << hw->info.shift,
|
hw->samples << hw->info.shift,
|
||||||
&p1, &p2,
|
&p1, &p2,
|
||||||
&blen1, &blen2,
|
&blen1, &blen2,
|
||||||
1,
|
1
|
||||||
s
|
|
||||||
);
|
);
|
||||||
if (err) {
|
if (err) {
|
||||||
return;
|
return;
|
||||||
@@ -384,9 +415,25 @@ static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb,
|
|||||||
dsound_unlock_out (dsb, p1, p2, blen1, blen2);
|
dsound_unlock_out (dsb, p1, p2, blen1, blen2);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int dsound_open (dsound *s)
|
static void dsound_close (dsound *s)
|
||||||
{
|
{
|
||||||
HRESULT hr;
|
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 hwnd;
|
||||||
|
|
||||||
hwnd = GetForegroundWindow ();
|
hwnd = GetForegroundWindow ();
|
||||||
@@ -402,16 +449,71 @@ static int dsound_open (dsound *s)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!conf.set_primary) {
|
||||||
return 0;
|
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, ...)
|
static int dsound_ctl_out (HWVoiceOut *hw, int cmd, ...)
|
||||||
{
|
{
|
||||||
HRESULT hr;
|
HRESULT hr;
|
||||||
DWORD status;
|
DWORD status;
|
||||||
DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
|
DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
|
||||||
LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
|
LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
|
||||||
dsound *s = ds->s;
|
|
||||||
|
|
||||||
if (!dsb) {
|
if (!dsb) {
|
||||||
dolog ("Attempt to control voice without a buffer\n");
|
dolog ("Attempt to control voice without a buffer\n");
|
||||||
@@ -420,7 +522,7 @@ static int dsound_ctl_out (HWVoiceOut *hw, int cmd, ...)
|
|||||||
|
|
||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
case VOICE_ENABLE:
|
case VOICE_ENABLE:
|
||||||
if (dsound_get_status_out (dsb, &status, s)) {
|
if (dsound_get_status_out (dsb, &status)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -429,7 +531,7 @@ static int dsound_ctl_out (HWVoiceOut *hw, int cmd, ...)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
dsound_clear_sample (hw, dsb, s);
|
dsound_clear_sample (hw, dsb);
|
||||||
|
|
||||||
hr = IDirectSoundBuffer_Play (dsb, 0, 0, DSBPLAY_LOOPING);
|
hr = IDirectSoundBuffer_Play (dsb, 0, 0, DSBPLAY_LOOPING);
|
||||||
if (FAILED (hr)) {
|
if (FAILED (hr)) {
|
||||||
@@ -439,7 +541,7 @@ static int dsound_ctl_out (HWVoiceOut *hw, int cmd, ...)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case VOICE_DISABLE:
|
case VOICE_DISABLE:
|
||||||
if (dsound_get_status_out (dsb, &status, s)) {
|
if (dsound_get_status_out (dsb, &status)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -476,8 +578,6 @@ static int dsound_run_out (HWVoiceOut *hw, int live)
|
|||||||
DWORD wpos, ppos, old_pos;
|
DWORD wpos, ppos, old_pos;
|
||||||
LPVOID p1, p2;
|
LPVOID p1, p2;
|
||||||
int bufsize;
|
int bufsize;
|
||||||
dsound *s = ds->s;
|
|
||||||
DSoundConf *conf = &s->conf;
|
|
||||||
|
|
||||||
if (!dsb) {
|
if (!dsb) {
|
||||||
dolog ("Attempt to run empty with playback buffer\n");
|
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;
|
len = live << hwshift;
|
||||||
|
|
||||||
if (ds->first_time) {
|
if (ds->first_time) {
|
||||||
if (conf->latency_millis) {
|
if (conf.latency_millis) {
|
||||||
DWORD cur_blat;
|
DWORD cur_blat;
|
||||||
|
|
||||||
cur_blat = audio_ring_dist (wpos, ppos, bufsize);
|
cur_blat = audio_ring_dist (wpos, ppos, bufsize);
|
||||||
ds->first_time = 0;
|
ds->first_time = 0;
|
||||||
old_pos = wpos;
|
old_pos = wpos;
|
||||||
old_pos +=
|
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 %= bufsize;
|
||||||
old_pos &= ~hw->info.align;
|
old_pos &= ~hw->info.align;
|
||||||
}
|
}
|
||||||
@@ -563,8 +663,7 @@ static int dsound_run_out (HWVoiceOut *hw, int live)
|
|||||||
len,
|
len,
|
||||||
&p1, &p2,
|
&p1, &p2,
|
||||||
&blen1, &blen2,
|
&blen1, &blen2,
|
||||||
0,
|
0
|
||||||
s
|
|
||||||
);
|
);
|
||||||
if (err) {
|
if (err) {
|
||||||
return 0;
|
return 0;
|
||||||
@@ -667,7 +766,6 @@ static int dsound_run_in (HWVoiceIn *hw)
|
|||||||
DWORD cpos, rpos;
|
DWORD cpos, rpos;
|
||||||
LPVOID p1, p2;
|
LPVOID p1, p2;
|
||||||
int hwshift;
|
int hwshift;
|
||||||
dsound *s = ds->s;
|
|
||||||
|
|
||||||
if (!dscb) {
|
if (!dscb) {
|
||||||
dolog ("Attempt to run without capture buffer\n");
|
dolog ("Attempt to run without capture buffer\n");
|
||||||
@@ -722,8 +820,7 @@ static int dsound_run_in (HWVoiceIn *hw)
|
|||||||
&p2,
|
&p2,
|
||||||
&blen1,
|
&blen1,
|
||||||
&blen2,
|
&blen2,
|
||||||
0,
|
0
|
||||||
s
|
|
||||||
);
|
);
|
||||||
if (err) {
|
if (err) {
|
||||||
return 0;
|
return 0;
|
||||||
@@ -746,19 +843,12 @@ static int dsound_run_in (HWVoiceIn *hw)
|
|||||||
return decr;
|
return decr;
|
||||||
}
|
}
|
||||||
|
|
||||||
static DSoundConf glob_conf = {
|
|
||||||
.bufsize_in = 16384,
|
|
||||||
.bufsize_out = 16384,
|
|
||||||
.latency_millis = 10
|
|
||||||
};
|
|
||||||
|
|
||||||
static void dsound_audio_fini (void *opaque)
|
static void dsound_audio_fini (void *opaque)
|
||||||
{
|
{
|
||||||
HRESULT hr;
|
HRESULT hr;
|
||||||
dsound *s = opaque;
|
dsound *s = opaque;
|
||||||
|
|
||||||
if (!s->dsound) {
|
if (!s->dsound) {
|
||||||
g_free(s);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -769,7 +859,6 @@ static void dsound_audio_fini (void *opaque)
|
|||||||
s->dsound = NULL;
|
s->dsound = NULL;
|
||||||
|
|
||||||
if (!s->dsound_capture) {
|
if (!s->dsound_capture) {
|
||||||
g_free(s);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -778,21 +867,17 @@ static void dsound_audio_fini (void *opaque)
|
|||||||
dsound_logerr (hr, "Could not release DirectSoundCapture\n");
|
dsound_logerr (hr, "Could not release DirectSoundCapture\n");
|
||||||
}
|
}
|
||||||
s->dsound_capture = NULL;
|
s->dsound_capture = NULL;
|
||||||
|
|
||||||
g_free(s);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void *dsound_audio_init (void)
|
static void *dsound_audio_init (void)
|
||||||
{
|
{
|
||||||
int err;
|
int err;
|
||||||
HRESULT hr;
|
HRESULT hr;
|
||||||
dsound *s = g_malloc0(sizeof(dsound));
|
dsound *s = &glob_dsound;
|
||||||
|
|
||||||
s->conf = glob_conf;
|
|
||||||
hr = CoInitialize (NULL);
|
hr = CoInitialize (NULL);
|
||||||
if (FAILED (hr)) {
|
if (FAILED (hr)) {
|
||||||
dsound_logerr (hr, "Could not initialize COM\n");
|
dsound_logerr (hr, "Could not initialize COM\n");
|
||||||
g_free(s);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -805,7 +890,6 @@ static void *dsound_audio_init (void)
|
|||||||
);
|
);
|
||||||
if (FAILED (hr)) {
|
if (FAILED (hr)) {
|
||||||
dsound_logerr (hr, "Could not create DirectSound instance\n");
|
dsound_logerr (hr, "Could not create DirectSound instance\n");
|
||||||
g_free(s);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -817,7 +901,7 @@ static void *dsound_audio_init (void)
|
|||||||
if (FAILED (hr)) {
|
if (FAILED (hr)) {
|
||||||
dsound_logerr (hr, "Could not release DirectSound\n");
|
dsound_logerr (hr, "Could not release DirectSound\n");
|
||||||
}
|
}
|
||||||
g_free(s);
|
s->dsound = NULL;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -854,22 +938,64 @@ static void *dsound_audio_init (void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static struct audio_option dsound_options[] = {
|
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",
|
.name = "LATENCY_MILLIS",
|
||||||
.tag = AUD_OPT_INT,
|
.tag = AUD_OPT_INT,
|
||||||
.valp = &glob_conf.latency_millis,
|
.valp = &conf.latency_millis,
|
||||||
.descr = "(undocumented)"
|
.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",
|
.name = "BUFSIZE_OUT",
|
||||||
.tag = AUD_OPT_INT,
|
.tag = AUD_OPT_INT,
|
||||||
.valp = &glob_conf.bufsize_out,
|
.valp = &conf.bufsize_out,
|
||||||
.descr = "(undocumented)"
|
.descr = "(undocumented)"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "BUFSIZE_IN",
|
.name = "BUFSIZE_IN",
|
||||||
.tag = AUD_OPT_INT,
|
.tag = AUD_OPT_INT,
|
||||||
.valp = &glob_conf.bufsize_in,
|
.valp = &conf.bufsize_in,
|
||||||
.descr = "(undocumented)"
|
.descr = "(undocumented)"
|
||||||
},
|
},
|
||||||
{ /* End of list */ }
|
{ /* 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)
|
||||||
|
};
|
||||||
@@ -348,6 +348,7 @@ void mixeng_clear (struct st_sample *buf, int len)
|
|||||||
|
|
||||||
void mixeng_volume (struct st_sample *buf, int len, struct mixeng_volume *vol)
|
void mixeng_volume (struct st_sample *buf, int len, struct mixeng_volume *vol)
|
||||||
{
|
{
|
||||||
|
#ifdef CONFIG_MIXEMU
|
||||||
if (vol->mute) {
|
if (vol->mute) {
|
||||||
mixeng_clear (buf, len);
|
mixeng_clear (buf, len);
|
||||||
return;
|
return;
|
||||||
@@ -363,4 +364,9 @@ void mixeng_volume (struct st_sample *buf, int len, struct mixeng_volume *vol)
|
|||||||
#endif
|
#endif
|
||||||
buf += 1;
|
buf += 1;
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
(void) buf;
|
||||||
|
(void) len;
|
||||||
|
(void) vol;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,7 +35,7 @@
|
|||||||
#define IN_T glue (glue (ITYPE, BSIZE), _t)
|
#define IN_T glue (glue (ITYPE, BSIZE), _t)
|
||||||
|
|
||||||
#ifdef FLOAT_MIXENG
|
#ifdef FLOAT_MIXENG
|
||||||
static inline mixeng_real glue (conv_, ET) (IN_T v)
|
static mixeng_real inline glue (conv_, ET) (IN_T v)
|
||||||
{
|
{
|
||||||
IN_T nv = ENDIAN_CONVERT (v);
|
IN_T nv = ENDIAN_CONVERT (v);
|
||||||
|
|
||||||
@@ -54,7 +54,7 @@ static inline mixeng_real glue (conv_, ET) (IN_T v)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline IN_T glue (clip_, ET) (mixeng_real v)
|
static IN_T inline glue (clip_, ET) (mixeng_real v)
|
||||||
{
|
{
|
||||||
if (v >= 0.5) {
|
if (v >= 0.5) {
|
||||||
return IN_MAX;
|
return IN_MAX;
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ static int no_run_out (HWVoiceOut *hw, int live)
|
|||||||
int64_t ticks;
|
int64_t ticks;
|
||||||
int64_t bytes;
|
int64_t bytes;
|
||||||
|
|
||||||
now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
|
now = qemu_get_clock_ns (vm_clock);
|
||||||
ticks = now - no->old_ticks;
|
ticks = now - no->old_ticks;
|
||||||
bytes = muldiv64 (ticks, hw->info.bytes_per_second, get_ticks_per_sec ());
|
bytes = muldiv64 (ticks, hw->info.bytes_per_second, get_ticks_per_sec ());
|
||||||
bytes = audio_MIN (bytes, INT_MAX);
|
bytes = audio_MIN (bytes, INT_MAX);
|
||||||
@@ -63,7 +63,7 @@ static int no_write (SWVoiceOut *sw, void *buf, int len)
|
|||||||
return audio_pcm_sw_write (sw, buf, 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);
|
audio_pcm_init_info (&hw->info, as);
|
||||||
hw->samples = 1024;
|
hw->samples = 1024;
|
||||||
@@ -82,7 +82,7 @@ static int no_ctl_out (HWVoiceOut *hw, int cmd, ...)
|
|||||||
return 0;
|
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);
|
audio_pcm_init_info (&hw->info, as);
|
||||||
hw->samples = 1024;
|
hw->samples = 1024;
|
||||||
@@ -102,7 +102,7 @@ static int no_run_in (HWVoiceIn *hw)
|
|||||||
int samples = 0;
|
int samples = 0;
|
||||||
|
|
||||||
if (dead) {
|
if (dead) {
|
||||||
int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
|
int64_t now = qemu_get_clock_ns (vm_clock);
|
||||||
int64_t ticks = now - no->old_ticks;
|
int64_t ticks = now - no->old_ticks;
|
||||||
int64_t bytes =
|
int64_t bytes =
|
||||||
muldiv64 (ticks, hw->info.bytes_per_second, get_ticks_per_sec ());
|
muldiv64 (ticks, hw->info.bytes_per_second, get_ticks_per_sec ());
|
||||||
|
|||||||
147
audio/ossaudio.c
147
audio/ossaudio.c
@@ -25,12 +25,15 @@
|
|||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
|
#ifdef __OpenBSD__
|
||||||
|
#include <soundcard.h>
|
||||||
|
#else
|
||||||
#include <sys/soundcard.h>
|
#include <sys/soundcard.h>
|
||||||
|
#endif
|
||||||
#include "qemu-common.h"
|
#include "qemu-common.h"
|
||||||
#include "qemu/main-loop.h"
|
#include "qemu/main-loop.h"
|
||||||
#include "qemu/host-utils.h"
|
#include "qemu/host-utils.h"
|
||||||
#include "audio.h"
|
#include "audio.h"
|
||||||
#include "trace.h"
|
|
||||||
|
|
||||||
#define AUDIO_CAP "oss"
|
#define AUDIO_CAP "oss"
|
||||||
#include "audio_int.h"
|
#include "audio_int.h"
|
||||||
@@ -39,16 +42,6 @@
|
|||||||
#define USE_DSP_POLICY
|
#define USE_DSP_POLICY
|
||||||
#endif
|
#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 {
|
typedef struct OSSVoiceOut {
|
||||||
HWVoiceOut hw;
|
HWVoiceOut hw;
|
||||||
void *pcm_buf;
|
void *pcm_buf;
|
||||||
@@ -58,7 +51,6 @@ typedef struct OSSVoiceOut {
|
|||||||
int fragsize;
|
int fragsize;
|
||||||
int mmapped;
|
int mmapped;
|
||||||
int pending;
|
int pending;
|
||||||
OSSConf *conf;
|
|
||||||
} OSSVoiceOut;
|
} OSSVoiceOut;
|
||||||
|
|
||||||
typedef struct OSSVoiceIn {
|
typedef struct OSSVoiceIn {
|
||||||
@@ -67,9 +59,28 @@ typedef struct OSSVoiceIn {
|
|||||||
int fd;
|
int fd;
|
||||||
int nfrags;
|
int nfrags;
|
||||||
int fragsize;
|
int fragsize;
|
||||||
OSSConf *conf;
|
|
||||||
} OSSVoiceIn;
|
} 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 {
|
struct oss_params {
|
||||||
int freq;
|
int freq;
|
||||||
audfmt_e fmt;
|
audfmt_e fmt;
|
||||||
@@ -131,18 +142,18 @@ static void oss_helper_poll_in (void *opaque)
|
|||||||
audio_run ("oss_poll_in");
|
audio_run ("oss_poll_in");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void oss_poll_out (HWVoiceOut *hw)
|
static int oss_poll_out (HWVoiceOut *hw)
|
||||||
{
|
{
|
||||||
OSSVoiceOut *oss = (OSSVoiceOut *) 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;
|
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)
|
static int oss_write (SWVoiceOut *sw, void *buf, int len)
|
||||||
@@ -265,18 +276,18 @@ static int oss_get_version (int fd, int *version, const char *typ)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
static int oss_open (int in, struct oss_params *req,
|
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 fd;
|
||||||
int oflags = conf->exclusive ? O_EXCL : 0;
|
int oflags = conf.exclusive ? O_EXCL : 0;
|
||||||
audio_buf_info abinfo;
|
audio_buf_info abinfo;
|
||||||
int fmt, freq, nchannels;
|
int fmt, freq, nchannels;
|
||||||
int setfragment = 1;
|
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";
|
const char *typ = in ? "ADC" : "DAC";
|
||||||
|
|
||||||
/* Kludge needed to have working mmap on Linux */
|
/* 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);
|
fd = open (dspname, oflags | O_NONBLOCK);
|
||||||
if (-1 == fd) {
|
if (-1 == fd) {
|
||||||
@@ -310,18 +321,20 @@ static int oss_open (int in, struct oss_params *req,
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef USE_DSP_POLICY
|
#ifdef USE_DSP_POLICY
|
||||||
if (conf->policy >= 0) {
|
if (conf.policy >= 0) {
|
||||||
int version;
|
int version;
|
||||||
|
|
||||||
if (!oss_get_version (fd, &version, typ)) {
|
if (!oss_get_version (fd, &version, typ)) {
|
||||||
trace_oss_version(version);
|
if (conf.debug) {
|
||||||
|
dolog ("OSS version = %#x\n", version);
|
||||||
|
}
|
||||||
|
|
||||||
if (version >= 0x040000) {
|
if (version >= 0x040000) {
|
||||||
int policy = conf->policy;
|
int policy = conf.policy;
|
||||||
if (ioctl (fd, SNDCTL_DSP_POLICY, &policy)) {
|
if (ioctl (fd, SNDCTL_DSP_POLICY, &policy)) {
|
||||||
oss_logerr2 (errno, typ,
|
oss_logerr2 (errno, typ,
|
||||||
"Failed to set timing policy to %d\n",
|
"Failed to set timing policy to %d\n",
|
||||||
conf->policy);
|
conf.policy);
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
setfragment = 0;
|
setfragment = 0;
|
||||||
@@ -449,12 +462,19 @@ static int oss_run_out (HWVoiceOut *hw, int live)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (abinfo.bytes > bufsize) {
|
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;
|
abinfo.bytes = bufsize;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (abinfo.bytes < 0) {
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -494,8 +514,7 @@ static void oss_fini_out (HWVoiceOut *hw)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int oss_init_out(HWVoiceOut *hw, struct audsettings *as,
|
static int oss_init_out (HWVoiceOut *hw, struct audsettings *as)
|
||||||
void *drv_opaque)
|
|
||||||
{
|
{
|
||||||
OSSVoiceOut *oss = (OSSVoiceOut *) hw;
|
OSSVoiceOut *oss = (OSSVoiceOut *) hw;
|
||||||
struct oss_params req, obt;
|
struct oss_params req, obt;
|
||||||
@@ -504,17 +523,16 @@ static int oss_init_out(HWVoiceOut *hw, struct audsettings *as,
|
|||||||
int fd;
|
int fd;
|
||||||
audfmt_e effective_fmt;
|
audfmt_e effective_fmt;
|
||||||
struct audsettings obt_as;
|
struct audsettings obt_as;
|
||||||
OSSConf *conf = drv_opaque;
|
|
||||||
|
|
||||||
oss->fd = -1;
|
oss->fd = -1;
|
||||||
|
|
||||||
req.fmt = aud_to_ossfmt (as->fmt, as->endianness);
|
req.fmt = aud_to_ossfmt (as->fmt, as->endianness);
|
||||||
req.freq = as->freq;
|
req.freq = as->freq;
|
||||||
req.nchannels = as->nchannels;
|
req.nchannels = as->nchannels;
|
||||||
req.fragsize = conf->fragsize;
|
req.fragsize = conf.fragsize;
|
||||||
req.nfrags = conf->nfrags;
|
req.nfrags = conf.nfrags;
|
||||||
|
|
||||||
if (oss_open (0, &req, &obt, &fd, conf)) {
|
if (oss_open (0, &req, &obt, &fd)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -541,7 +559,7 @@ static int oss_init_out(HWVoiceOut *hw, struct audsettings *as,
|
|||||||
hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift;
|
hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift;
|
||||||
|
|
||||||
oss->mmapped = 0;
|
oss->mmapped = 0;
|
||||||
if (conf->try_mmap) {
|
if (conf.try_mmap) {
|
||||||
oss->pcm_buf = mmap (
|
oss->pcm_buf = mmap (
|
||||||
NULL,
|
NULL,
|
||||||
hw->samples << hw->info.shift,
|
hw->samples << hw->info.shift,
|
||||||
@@ -601,7 +619,6 @@ static int oss_init_out(HWVoiceOut *hw, struct audsettings *as,
|
|||||||
}
|
}
|
||||||
|
|
||||||
oss->fd = fd;
|
oss->fd = fd;
|
||||||
oss->conf = conf;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -621,8 +638,7 @@ static int oss_ctl_out (HWVoiceOut *hw, int cmd, ...)
|
|||||||
va_end (ap);
|
va_end (ap);
|
||||||
|
|
||||||
ldebug ("enabling voice\n");
|
ldebug ("enabling voice\n");
|
||||||
if (poll_mode) {
|
if (poll_mode && oss_poll_out (hw)) {
|
||||||
oss_poll_out (hw);
|
|
||||||
poll_mode = 0;
|
poll_mode = 0;
|
||||||
}
|
}
|
||||||
hw->poll_mode = poll_mode;
|
hw->poll_mode = poll_mode;
|
||||||
@@ -664,7 +680,7 @@ static int oss_ctl_out (HWVoiceOut *hw, int cmd, ...)
|
|||||||
return 0;
|
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;
|
OSSVoiceIn *oss = (OSSVoiceIn *) hw;
|
||||||
struct oss_params req, obt;
|
struct oss_params req, obt;
|
||||||
@@ -673,16 +689,15 @@ static int oss_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
|
|||||||
int fd;
|
int fd;
|
||||||
audfmt_e effective_fmt;
|
audfmt_e effective_fmt;
|
||||||
struct audsettings obt_as;
|
struct audsettings obt_as;
|
||||||
OSSConf *conf = drv_opaque;
|
|
||||||
|
|
||||||
oss->fd = -1;
|
oss->fd = -1;
|
||||||
|
|
||||||
req.fmt = aud_to_ossfmt (as->fmt, as->endianness);
|
req.fmt = aud_to_ossfmt (as->fmt, as->endianness);
|
||||||
req.freq = as->freq;
|
req.freq = as->freq;
|
||||||
req.nchannels = as->nchannels;
|
req.nchannels = as->nchannels;
|
||||||
req.fragsize = conf->fragsize;
|
req.fragsize = conf.fragsize;
|
||||||
req.nfrags = conf->nfrags;
|
req.nfrags = conf.nfrags;
|
||||||
if (oss_open (1, &req, &obt, &fd, conf)) {
|
if (oss_open (1, &req, &obt, &fd)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -716,7 +731,6 @@ static int oss_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
|
|||||||
}
|
}
|
||||||
|
|
||||||
oss->fd = fd;
|
oss->fd = fd;
|
||||||
oss->conf = conf;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -726,9 +740,11 @@ static void oss_fini_in (HWVoiceIn *hw)
|
|||||||
|
|
||||||
oss_anal_close (&oss->fd);
|
oss_anal_close (&oss->fd);
|
||||||
|
|
||||||
|
if (oss->pcm_buf) {
|
||||||
g_free (oss->pcm_buf);
|
g_free (oss->pcm_buf);
|
||||||
oss->pcm_buf = NULL;
|
oss->pcm_buf = NULL;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static int oss_run_in (HWVoiceIn *hw)
|
static int oss_run_in (HWVoiceIn *hw)
|
||||||
{
|
{
|
||||||
@@ -818,8 +834,7 @@ static int oss_ctl_in (HWVoiceIn *hw, int cmd, ...)
|
|||||||
poll_mode = va_arg (ap, int);
|
poll_mode = va_arg (ap, int);
|
||||||
va_end (ap);
|
va_end (ap);
|
||||||
|
|
||||||
if (poll_mode) {
|
if (poll_mode && oss_poll_in (hw)) {
|
||||||
oss_poll_in (hw);
|
|
||||||
poll_mode = 0;
|
poll_mode = 0;
|
||||||
}
|
}
|
||||||
hw->poll_mode = poll_mode;
|
hw->poll_mode = poll_mode;
|
||||||
@@ -836,79 +851,67 @@ static int oss_ctl_in (HWVoiceIn *hw, int cmd, ...)
|
|||||||
return 0;
|
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)
|
static void *oss_audio_init (void)
|
||||||
{
|
{
|
||||||
OSSConf *conf = g_malloc(sizeof(OSSConf));
|
return &conf;
|
||||||
*conf = glob_conf;
|
|
||||||
|
|
||||||
if (access(conf->devpath_in, R_OK | W_OK) < 0 ||
|
|
||||||
access(conf->devpath_out, R_OK | W_OK) < 0) {
|
|
||||||
g_free(conf);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return conf;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void oss_audio_fini (void *opaque)
|
static void oss_audio_fini (void *opaque)
|
||||||
{
|
{
|
||||||
g_free(opaque);
|
(void) opaque;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct audio_option oss_options[] = {
|
static struct audio_option oss_options[] = {
|
||||||
{
|
{
|
||||||
.name = "FRAGSIZE",
|
.name = "FRAGSIZE",
|
||||||
.tag = AUD_OPT_INT,
|
.tag = AUD_OPT_INT,
|
||||||
.valp = &glob_conf.fragsize,
|
.valp = &conf.fragsize,
|
||||||
.descr = "Fragment size in bytes"
|
.descr = "Fragment size in bytes"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "NFRAGS",
|
.name = "NFRAGS",
|
||||||
.tag = AUD_OPT_INT,
|
.tag = AUD_OPT_INT,
|
||||||
.valp = &glob_conf.nfrags,
|
.valp = &conf.nfrags,
|
||||||
.descr = "Number of fragments"
|
.descr = "Number of fragments"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "MMAP",
|
.name = "MMAP",
|
||||||
.tag = AUD_OPT_BOOL,
|
.tag = AUD_OPT_BOOL,
|
||||||
.valp = &glob_conf.try_mmap,
|
.valp = &conf.try_mmap,
|
||||||
.descr = "Try using memory mapped access"
|
.descr = "Try using memory mapped access"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "DAC_DEV",
|
.name = "DAC_DEV",
|
||||||
.tag = AUD_OPT_STR,
|
.tag = AUD_OPT_STR,
|
||||||
.valp = &glob_conf.devpath_out,
|
.valp = &conf.devpath_out,
|
||||||
.descr = "Path to DAC device"
|
.descr = "Path to DAC device"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "ADC_DEV",
|
.name = "ADC_DEV",
|
||||||
.tag = AUD_OPT_STR,
|
.tag = AUD_OPT_STR,
|
||||||
.valp = &glob_conf.devpath_in,
|
.valp = &conf.devpath_in,
|
||||||
.descr = "Path to ADC device"
|
.descr = "Path to ADC device"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "EXCLUSIVE",
|
.name = "EXCLUSIVE",
|
||||||
.tag = AUD_OPT_BOOL,
|
.tag = AUD_OPT_BOOL,
|
||||||
.valp = &glob_conf.exclusive,
|
.valp = &conf.exclusive,
|
||||||
.descr = "Open device in exclusive mode (vmix wont work)"
|
.descr = "Open device in exclusive mode (vmix wont work)"
|
||||||
},
|
},
|
||||||
#ifdef USE_DSP_POLICY
|
#ifdef USE_DSP_POLICY
|
||||||
{
|
{
|
||||||
.name = "POLICY",
|
.name = "POLICY",
|
||||||
.tag = AUD_OPT_INT,
|
.tag = AUD_OPT_INT,
|
||||||
.valp = &glob_conf.policy,
|
.valp = &conf.policy,
|
||||||
.descr = "Set the timing policy of the device, -1 to use fragment mode",
|
.descr = "Set the timing policy of the device, -1 to use fragment mode",
|
||||||
},
|
},
|
||||||
#endif
|
#endif
|
||||||
|
{
|
||||||
|
.name = "DEBUG",
|
||||||
|
.tag = AUD_OPT_BOOL,
|
||||||
|
.valp = &conf.debug,
|
||||||
|
.descr = "Turn on some debugging messages"
|
||||||
|
},
|
||||||
{ /* End of list */ }
|
{ /* End of list */ }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
113
audio/paaudio.c
113
audio/paaudio.c
@@ -8,19 +8,6 @@
|
|||||||
#include "audio_int.h"
|
#include "audio_int.h"
|
||||||
#include "audio_pt_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 {
|
typedef struct {
|
||||||
HWVoiceOut hw;
|
HWVoiceOut hw;
|
||||||
int done;
|
int done;
|
||||||
@@ -30,7 +17,6 @@ typedef struct {
|
|||||||
pa_stream *stream;
|
pa_stream *stream;
|
||||||
void *pcm_buf;
|
void *pcm_buf;
|
||||||
struct audio_pt pt;
|
struct audio_pt pt;
|
||||||
paaudio *g;
|
|
||||||
} PAVoiceOut;
|
} PAVoiceOut;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -44,10 +30,20 @@ typedef struct {
|
|||||||
struct audio_pt pt;
|
struct audio_pt pt;
|
||||||
const void *read_data;
|
const void *read_data;
|
||||||
size_t read_index, read_length;
|
size_t read_index, read_length;
|
||||||
paaudio *g;
|
|
||||||
} PAVoiceIn;
|
} 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, ...)
|
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)
|
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);
|
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)
|
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);
|
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;
|
rpos = pa->rpos;
|
||||||
|
|
||||||
if (audio_pt_unlock (&pa->pt, AUDIO_FUNC)) {
|
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;
|
wpos = pa->wpos;
|
||||||
|
|
||||||
if (audio_pt_unlock (&pa->pt, AUDIO_FUNC)) {
|
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)
|
static void context_state_cb (pa_context *c, void *userdata)
|
||||||
{
|
{
|
||||||
paaudio *g = userdata;
|
paaudio *g = &glob_paaudio;
|
||||||
|
|
||||||
switch (pa_context_get_state(c)) {
|
switch (pa_context_get_state(c)) {
|
||||||
case PA_CONTEXT_READY:
|
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)
|
static void stream_state_cb (pa_stream *s, void * userdata)
|
||||||
{
|
{
|
||||||
paaudio *g = userdata;
|
paaudio *g = &glob_paaudio;
|
||||||
|
|
||||||
switch (pa_stream_get_state (s)) {
|
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)
|
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);
|
pa_threaded_mainloop_signal (g->mainloop, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static pa_stream *qpa_simple_new (
|
static pa_stream *qpa_simple_new (
|
||||||
paaudio *g,
|
const char *server,
|
||||||
const char *name,
|
const char *name,
|
||||||
pa_stream_direction_t dir,
|
pa_stream_direction_t dir,
|
||||||
const char *dev,
|
const char *dev,
|
||||||
|
const char *stream_name,
|
||||||
const pa_sample_spec *ss,
|
const pa_sample_spec *ss,
|
||||||
const pa_channel_map *map,
|
const pa_channel_map *map,
|
||||||
const pa_buffer_attr *attr,
|
const pa_buffer_attr *attr,
|
||||||
int *rerror)
|
int *rerror)
|
||||||
{
|
{
|
||||||
|
paaudio *g = &glob_paaudio;
|
||||||
int r;
|
int r;
|
||||||
pa_stream *stream;
|
pa_stream *stream;
|
||||||
|
|
||||||
@@ -536,36 +534,35 @@ fail:
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int qpa_init_out(HWVoiceOut *hw, struct audsettings *as,
|
static int qpa_init_out (HWVoiceOut *hw, struct audsettings *as)
|
||||||
void *drv_opaque)
|
|
||||||
{
|
{
|
||||||
int error;
|
int error;
|
||||||
pa_sample_spec ss;
|
static pa_sample_spec ss;
|
||||||
pa_buffer_attr ba;
|
static pa_buffer_attr ba;
|
||||||
struct audsettings obt_as = *as;
|
struct audsettings obt_as = *as;
|
||||||
PAVoiceOut *pa = (PAVoiceOut *) hw;
|
PAVoiceOut *pa = (PAVoiceOut *) hw;
|
||||||
paaudio *g = pa->g = drv_opaque;
|
|
||||||
|
|
||||||
ss.format = audfmt_to_pa (as->fmt, as->endianness);
|
ss.format = audfmt_to_pa (as->fmt, as->endianness);
|
||||||
ss.channels = as->nchannels;
|
ss.channels = as->nchannels;
|
||||||
ss.rate = as->freq;
|
ss.rate = as->freq;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* qemu audio tick runs at 100 Hz (by default), so processing
|
* qemu audio tick runs at 250 Hz (by default), so processing
|
||||||
* data chunks worth 10 ms of sound should be a good fit.
|
* data chunks worth 4 ms of sound should be a good fit.
|
||||||
*/
|
*/
|
||||||
ba.tlength = pa_usec_to_bytes (10 * 1000, &ss);
|
ba.tlength = pa_usec_to_bytes (4 * 1000, &ss);
|
||||||
ba.minreq = pa_usec_to_bytes (5 * 1000, &ss);
|
ba.minreq = pa_usec_to_bytes (2 * 1000, &ss);
|
||||||
ba.maxlength = -1;
|
ba.maxlength = -1;
|
||||||
ba.prebuf = -1;
|
ba.prebuf = -1;
|
||||||
|
|
||||||
obt_as.fmt = pa_to_audfmt (ss.format, &obt_as.endianness);
|
obt_as.fmt = pa_to_audfmt (ss.format, &obt_as.endianness);
|
||||||
|
|
||||||
pa->stream = qpa_simple_new (
|
pa->stream = qpa_simple_new (
|
||||||
g,
|
glob_paaudio.server,
|
||||||
"qemu",
|
"qemu",
|
||||||
PA_STREAM_PLAYBACK,
|
PA_STREAM_PLAYBACK,
|
||||||
g->conf.sink,
|
glob_paaudio.sink,
|
||||||
|
"pcm.playback",
|
||||||
&ss,
|
&ss,
|
||||||
NULL, /* channel map */
|
NULL, /* channel map */
|
||||||
&ba, /* buffering attributes */
|
&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);
|
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->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
|
||||||
pa->rpos = hw->rpos;
|
pa->rpos = hw->rpos;
|
||||||
if (!pa->pcm_buf) {
|
if (!pa->pcm_buf) {
|
||||||
@@ -604,13 +601,12 @@ static int qpa_init_out(HWVoiceOut *hw, struct audsettings *as,
|
|||||||
return -1;
|
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;
|
int error;
|
||||||
pa_sample_spec ss;
|
static pa_sample_spec ss;
|
||||||
struct audsettings obt_as = *as;
|
struct audsettings obt_as = *as;
|
||||||
PAVoiceIn *pa = (PAVoiceIn *) hw;
|
PAVoiceIn *pa = (PAVoiceIn *) hw;
|
||||||
paaudio *g = pa->g = drv_opaque;
|
|
||||||
|
|
||||||
ss.format = audfmt_to_pa (as->fmt, as->endianness);
|
ss.format = audfmt_to_pa (as->fmt, as->endianness);
|
||||||
ss.channels = as->nchannels;
|
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);
|
obt_as.fmt = pa_to_audfmt (ss.format, &obt_as.endianness);
|
||||||
|
|
||||||
pa->stream = qpa_simple_new (
|
pa->stream = qpa_simple_new (
|
||||||
g,
|
glob_paaudio.server,
|
||||||
"qemu",
|
"qemu",
|
||||||
PA_STREAM_RECORD,
|
PA_STREAM_RECORD,
|
||||||
g->conf.source,
|
glob_paaudio.source,
|
||||||
|
"pcm.capture",
|
||||||
&ss,
|
&ss,
|
||||||
NULL, /* channel map */
|
NULL, /* channel map */
|
||||||
NULL, /* buffering attributes */
|
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);
|
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->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
|
||||||
pa->wpos = hw->wpos;
|
pa->wpos = hw->wpos;
|
||||||
if (!pa->pcm_buf) {
|
if (!pa->pcm_buf) {
|
||||||
@@ -706,7 +703,7 @@ static int qpa_ctl_out (HWVoiceOut *hw, int cmd, ...)
|
|||||||
PAVoiceOut *pa = (PAVoiceOut *) hw;
|
PAVoiceOut *pa = (PAVoiceOut *) hw;
|
||||||
pa_operation *op;
|
pa_operation *op;
|
||||||
pa_cvolume v;
|
pa_cvolume v;
|
||||||
paaudio *g = pa->g;
|
paaudio *g = &glob_paaudio;
|
||||||
|
|
||||||
#ifdef PA_CHECK_VERSION /* macro is present in 0.9.16+ */
|
#ifdef PA_CHECK_VERSION /* macro is present in 0.9.16+ */
|
||||||
pa_cvolume_init (&v); /* function is present in 0.9.13+ */
|
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;
|
PAVoiceIn *pa = (PAVoiceIn *) hw;
|
||||||
pa_operation *op;
|
pa_operation *op;
|
||||||
pa_cvolume v;
|
pa_cvolume v;
|
||||||
paaudio *g = pa->g;
|
paaudio *g = &glob_paaudio;
|
||||||
|
|
||||||
#ifdef PA_CHECK_VERSION
|
#ifdef PA_CHECK_VERSION
|
||||||
pa_cvolume_init (&v);
|
pa_cvolume_init (&v);
|
||||||
@@ -808,31 +805,23 @@ static int qpa_ctl_in (HWVoiceIn *hw, int cmd, ...)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* common */
|
/* common */
|
||||||
static PAConf glob_conf = {
|
|
||||||
.samples = 4096,
|
|
||||||
};
|
|
||||||
|
|
||||||
static void *qpa_audio_init (void)
|
static void *qpa_audio_init (void)
|
||||||
{
|
{
|
||||||
paaudio *g = g_malloc(sizeof(paaudio));
|
paaudio *g = &glob_paaudio;
|
||||||
g->conf = glob_conf;
|
|
||||||
g->mainloop = NULL;
|
|
||||||
g->context = NULL;
|
|
||||||
|
|
||||||
g->mainloop = pa_threaded_mainloop_new ();
|
g->mainloop = pa_threaded_mainloop_new ();
|
||||||
if (!g->mainloop) {
|
if (!g->mainloop) {
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
g->context = pa_context_new (pa_threaded_mainloop_get_api (g->mainloop),
|
g->context = pa_context_new (pa_threaded_mainloop_get_api (g->mainloop), glob_paaudio.server);
|
||||||
g->conf.server);
|
|
||||||
if (!g->context) {
|
if (!g->context) {
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
pa_context_set_state_callback (g->context, context_state_cb, g);
|
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),
|
qpa_logerr (pa_context_errno (g->context),
|
||||||
"pa_context_connect() failed\n");
|
"pa_context_connect() failed\n");
|
||||||
goto fail;
|
goto fail;
|
||||||
@@ -865,13 +854,12 @@ static void *qpa_audio_init (void)
|
|||||||
|
|
||||||
pa_threaded_mainloop_unlock (g->mainloop);
|
pa_threaded_mainloop_unlock (g->mainloop);
|
||||||
|
|
||||||
return g;
|
return &glob_paaudio;
|
||||||
|
|
||||||
unlock_and_fail:
|
unlock_and_fail:
|
||||||
pa_threaded_mainloop_unlock (g->mainloop);
|
pa_threaded_mainloop_unlock (g->mainloop);
|
||||||
fail:
|
fail:
|
||||||
AUD_log (AUDIO_CAP, "Failed to initialize PA context");
|
AUD_log (AUDIO_CAP, "Failed to initialize PA context");
|
||||||
qpa_audio_fini(g);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -886,38 +874,39 @@ static void qpa_audio_fini (void *opaque)
|
|||||||
if (g->context) {
|
if (g->context) {
|
||||||
pa_context_disconnect (g->context);
|
pa_context_disconnect (g->context);
|
||||||
pa_context_unref (g->context);
|
pa_context_unref (g->context);
|
||||||
|
g->context = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (g->mainloop) {
|
if (g->mainloop) {
|
||||||
pa_threaded_mainloop_free (g->mainloop);
|
pa_threaded_mainloop_free (g->mainloop);
|
||||||
}
|
}
|
||||||
|
|
||||||
g_free(g);
|
g->mainloop = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct audio_option qpa_options[] = {
|
struct audio_option qpa_options[] = {
|
||||||
{
|
{
|
||||||
.name = "SAMPLES",
|
.name = "SAMPLES",
|
||||||
.tag = AUD_OPT_INT,
|
.tag = AUD_OPT_INT,
|
||||||
.valp = &glob_conf.samples,
|
.valp = &glob_paaudio.samples,
|
||||||
.descr = "buffer size in samples"
|
.descr = "buffer size in samples"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "SERVER",
|
.name = "SERVER",
|
||||||
.tag = AUD_OPT_STR,
|
.tag = AUD_OPT_STR,
|
||||||
.valp = &glob_conf.server,
|
.valp = &glob_paaudio.server,
|
||||||
.descr = "server address"
|
.descr = "server address"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "SINK",
|
.name = "SINK",
|
||||||
.tag = AUD_OPT_STR,
|
.tag = AUD_OPT_STR,
|
||||||
.valp = &glob_conf.sink,
|
.valp = &glob_paaudio.sink,
|
||||||
.descr = "sink device name"
|
.descr = "sink device name"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "SOURCE",
|
.name = "SOURCE",
|
||||||
.tag = AUD_OPT_STR,
|
.tag = AUD_OPT_STR,
|
||||||
.valp = &glob_conf.source,
|
.valp = &glob_paaudio.source,
|
||||||
.descr = "source device name"
|
.descr = "source device name"
|
||||||
},
|
},
|
||||||
{ /* End of list */ }
|
{ /* End of list */ }
|
||||||
|
|||||||
@@ -55,7 +55,6 @@ static struct SDLAudioState {
|
|||||||
SDL_mutex *mutex;
|
SDL_mutex *mutex;
|
||||||
SDL_sem *sem;
|
SDL_sem *sem;
|
||||||
int initialized;
|
int initialized;
|
||||||
bool driver_created;
|
|
||||||
} glob_sdl;
|
} glob_sdl;
|
||||||
typedef struct SDLAudioState SDLAudioState;
|
typedef struct SDLAudioState SDLAudioState;
|
||||||
|
|
||||||
@@ -333,8 +332,7 @@ static void sdl_fini_out (HWVoiceOut *hw)
|
|||||||
sdl_close (&glob_sdl);
|
sdl_close (&glob_sdl);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int sdl_init_out(HWVoiceOut *hw, struct audsettings *as,
|
static int sdl_init_out (HWVoiceOut *hw, struct audsettings *as)
|
||||||
void *drv_opaque)
|
|
||||||
{
|
{
|
||||||
SDLVoiceOut *sdl = (SDLVoiceOut *) hw;
|
SDLVoiceOut *sdl = (SDLVoiceOut *) hw;
|
||||||
SDLAudioState *s = &glob_sdl;
|
SDLAudioState *s = &glob_sdl;
|
||||||
@@ -394,10 +392,6 @@ static int sdl_ctl_out (HWVoiceOut *hw, int cmd, ...)
|
|||||||
static void *sdl_audio_init (void)
|
static void *sdl_audio_init (void)
|
||||||
{
|
{
|
||||||
SDLAudioState *s = &glob_sdl;
|
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)) {
|
if (SDL_InitSubSystem (SDL_INIT_AUDIO)) {
|
||||||
sdl_logerr ("SDL failed to initialize audio subsystem\n");
|
sdl_logerr ("SDL failed to initialize audio subsystem\n");
|
||||||
@@ -419,7 +413,6 @@ static void *sdl_audio_init (void)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
s->driver_created = true;
|
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -430,7 +423,6 @@ static void sdl_audio_fini (void *opaque)
|
|||||||
SDL_DestroySemaphore (s->sem);
|
SDL_DestroySemaphore (s->sem);
|
||||||
SDL_DestroyMutex (s->mutex);
|
SDL_DestroyMutex (s->mutex);
|
||||||
SDL_QuitSubSystem (SDL_INIT_AUDIO);
|
SDL_QuitSubSystem (SDL_INIT_AUDIO);
|
||||||
s->driver_created = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct audio_option sdl_options[] = {
|
static struct audio_option sdl_options[] = {
|
||||||
|
|||||||
@@ -18,7 +18,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "hw/hw.h"
|
#include "hw/hw.h"
|
||||||
#include "qemu/error-report.h"
|
|
||||||
#include "qemu/timer.h"
|
#include "qemu/timer.h"
|
||||||
#include "ui/qemu-spice.h"
|
#include "ui/qemu-spice.h"
|
||||||
|
|
||||||
@@ -26,17 +25,8 @@
|
|||||||
#include "audio.h"
|
#include "audio.h"
|
||||||
#include "audio_int.h"
|
#include "audio_int.h"
|
||||||
|
|
||||||
#if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
|
#define LINE_IN_SAMPLES 1024
|
||||||
#define LINE_OUT_SAMPLES (480 * 4)
|
#define LINE_OUT_SAMPLES 1024
|
||||||
#else
|
|
||||||
#define LINE_OUT_SAMPLES (256 * 4)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
|
|
||||||
#define LINE_IN_SAMPLES (480 * 4)
|
|
||||||
#else
|
|
||||||
#define LINE_IN_SAMPLES (256 * 4)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
typedef struct SpiceRateCtl {
|
typedef struct SpiceRateCtl {
|
||||||
int64_t start_ticks;
|
int64_t start_ticks;
|
||||||
@@ -91,7 +81,7 @@ static void spice_audio_fini (void *opaque)
|
|||||||
static void rate_start (SpiceRateCtl *rate)
|
static void rate_start (SpiceRateCtl *rate)
|
||||||
{
|
{
|
||||||
memset (rate, 0, sizeof (*rate));
|
memset (rate, 0, sizeof (*rate));
|
||||||
rate->start_ticks = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
|
rate->start_ticks = qemu_get_clock_ns (vm_clock);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int rate_get_samples (struct audio_pcm_info *info, SpiceRateCtl *rate)
|
static int rate_get_samples (struct audio_pcm_info *info, SpiceRateCtl *rate)
|
||||||
@@ -101,12 +91,12 @@ static int rate_get_samples (struct audio_pcm_info *info, SpiceRateCtl *rate)
|
|||||||
int64_t bytes;
|
int64_t bytes;
|
||||||
int64_t samples;
|
int64_t samples;
|
||||||
|
|
||||||
now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
|
now = qemu_get_clock_ns (vm_clock);
|
||||||
ticks = now - rate->start_ticks;
|
ticks = now - rate->start_ticks;
|
||||||
bytes = muldiv64 (ticks, info->bytes_per_second, get_ticks_per_sec ());
|
bytes = muldiv64 (ticks, info->bytes_per_second, get_ticks_per_sec ());
|
||||||
samples = (bytes - rate->bytes_sent) >> info->shift;
|
samples = (bytes - rate->bytes_sent) >> info->shift;
|
||||||
if (samples < 0 || samples > 65536) {
|
if (samples < 0 || samples > 65536) {
|
||||||
error_report("Resetting rate control (%" PRId64 " samples)", samples);
|
fprintf (stderr, "Resetting rate control (%" PRId64 " samples)\n", samples);
|
||||||
rate_start (rate);
|
rate_start (rate);
|
||||||
samples = 0;
|
samples = 0;
|
||||||
}
|
}
|
||||||
@@ -116,17 +106,12 @@ static int rate_get_samples (struct audio_pcm_info *info, SpiceRateCtl *rate)
|
|||||||
|
|
||||||
/* playback */
|
/* playback */
|
||||||
|
|
||||||
static int line_out_init(HWVoiceOut *hw, struct audsettings *as,
|
static int line_out_init (HWVoiceOut *hw, struct audsettings *as)
|
||||||
void *drv_opaque)
|
|
||||||
{
|
{
|
||||||
SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
|
SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
|
||||||
struct audsettings settings;
|
struct audsettings settings;
|
||||||
|
|
||||||
#if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
|
|
||||||
settings.freq = spice_server_get_best_playback_rate(NULL);
|
|
||||||
#else
|
|
||||||
settings.freq = SPICE_INTERFACE_PLAYBACK_FREQ;
|
settings.freq = SPICE_INTERFACE_PLAYBACK_FREQ;
|
||||||
#endif
|
|
||||||
settings.nchannels = SPICE_INTERFACE_PLAYBACK_CHAN;
|
settings.nchannels = SPICE_INTERFACE_PLAYBACK_CHAN;
|
||||||
settings.fmt = AUD_FMT_S16;
|
settings.fmt = AUD_FMT_S16;
|
||||||
settings.endianness = AUDIO_HOST_ENDIANNESS;
|
settings.endianness = AUDIO_HOST_ENDIANNESS;
|
||||||
@@ -137,9 +122,6 @@ static int line_out_init(HWVoiceOut *hw, struct audsettings *as,
|
|||||||
|
|
||||||
out->sin.base.sif = &playback_sif.base;
|
out->sin.base.sif = &playback_sif.base;
|
||||||
qemu_spice_add_interface (&out->sin.base);
|
qemu_spice_add_interface (&out->sin.base);
|
||||||
#if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
|
|
||||||
spice_server_set_playback_rate(&out->sin, settings.freq);
|
|
||||||
#endif
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -245,16 +227,12 @@ static int line_out_ctl (HWVoiceOut *hw, int cmd, ...)
|
|||||||
|
|
||||||
/* record */
|
/* 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);
|
SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
|
||||||
struct audsettings settings;
|
struct audsettings settings;
|
||||||
|
|
||||||
#if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
|
|
||||||
settings.freq = spice_server_get_best_record_rate(NULL);
|
|
||||||
#else
|
|
||||||
settings.freq = SPICE_INTERFACE_RECORD_FREQ;
|
settings.freq = SPICE_INTERFACE_RECORD_FREQ;
|
||||||
#endif
|
|
||||||
settings.nchannels = SPICE_INTERFACE_RECORD_CHAN;
|
settings.nchannels = SPICE_INTERFACE_RECORD_CHAN;
|
||||||
settings.fmt = AUD_FMT_S16;
|
settings.fmt = AUD_FMT_S16;
|
||||||
settings.endianness = AUDIO_HOST_ENDIANNESS;
|
settings.endianness = AUDIO_HOST_ENDIANNESS;
|
||||||
@@ -265,9 +243,6 @@ static int line_in_init(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
|
|||||||
|
|
||||||
in->sin.base.sif = &record_sif.base;
|
in->sin.base.sif = &record_sif.base;
|
||||||
qemu_spice_add_interface (&in->sin.base);
|
qemu_spice_add_interface (&in->sin.base);
|
||||||
#if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
|
|
||||||
spice_server_set_record_rate(&in->sin, settings.freq);
|
|
||||||
#endif
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -36,10 +36,15 @@ typedef struct WAVVoiceOut {
|
|||||||
int total_samples;
|
int total_samples;
|
||||||
} WAVVoiceOut;
|
} WAVVoiceOut;
|
||||||
|
|
||||||
typedef struct {
|
static struct {
|
||||||
struct audsettings settings;
|
struct audsettings settings;
|
||||||
const char *wav_path;
|
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)
|
static int wav_run_out (HWVoiceOut *hw, int live)
|
||||||
{
|
{
|
||||||
@@ -47,7 +52,7 @@ static int wav_run_out (HWVoiceOut *hw, int live)
|
|||||||
int rpos, decr, samples;
|
int rpos, decr, samples;
|
||||||
uint8_t *dst;
|
uint8_t *dst;
|
||||||
struct st_sample *src;
|
struct st_sample *src;
|
||||||
int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
|
int64_t now = qemu_get_clock_ns (vm_clock);
|
||||||
int64_t ticks = now - wav->old_ticks;
|
int64_t ticks = now - wav->old_ticks;
|
||||||
int64_t bytes =
|
int64_t bytes =
|
||||||
muldiv64 (ticks, hw->info.bytes_per_second, get_ticks_per_sec ());
|
muldiv64 (ticks, hw->info.bytes_per_second, get_ticks_per_sec ());
|
||||||
@@ -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,
|
static int wav_init_out (HWVoiceOut *hw, struct audsettings *as)
|
||||||
void *drv_opaque)
|
|
||||||
{
|
{
|
||||||
WAVVoiceOut *wav = (WAVVoiceOut *) hw;
|
WAVVoiceOut *wav = (WAVVoiceOut *) hw;
|
||||||
int bits16 = 0, stereo = 0;
|
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,
|
0x02, 0x00, 0x44, 0xac, 0x00, 0x00, 0x10, 0xb1, 0x02, 0x00, 0x04,
|
||||||
0x00, 0x10, 0x00, 0x64, 0x61, 0x74, 0x61, 0x00, 0x00, 0x00, 0x00
|
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;
|
stereo = wav_as.nchannels == 2;
|
||||||
switch (wav_as.fmt) {
|
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 + 28, hw->info.freq << (bits16 + stereo), 4);
|
||||||
le_store (hdr + 32, 1 << (bits16 + stereo), 2);
|
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) {
|
if (!wav->f) {
|
||||||
dolog ("Failed to open wave file `%s'\nReason: %s\n",
|
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);
|
g_free (wav->pcm_buf);
|
||||||
wav->pcm_buf = NULL;
|
wav->pcm_buf = NULL;
|
||||||
return -1;
|
return -1;
|
||||||
@@ -221,49 +226,40 @@ static int wav_ctl_out (HWVoiceOut *hw, int cmd, ...)
|
|||||||
return 0;
|
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)
|
static void *wav_audio_init (void)
|
||||||
{
|
{
|
||||||
WAVConf *conf = g_malloc(sizeof(WAVConf));
|
return &conf;
|
||||||
*conf = glob_conf;
|
|
||||||
return conf;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void wav_audio_fini (void *opaque)
|
static void wav_audio_fini (void *opaque)
|
||||||
{
|
{
|
||||||
|
(void) opaque;
|
||||||
ldebug ("wav_fini");
|
ldebug ("wav_fini");
|
||||||
g_free(opaque);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct audio_option wav_options[] = {
|
static struct audio_option wav_options[] = {
|
||||||
{
|
{
|
||||||
.name = "FREQUENCY",
|
.name = "FREQUENCY",
|
||||||
.tag = AUD_OPT_INT,
|
.tag = AUD_OPT_INT,
|
||||||
.valp = &glob_conf.settings.freq,
|
.valp = &conf.settings.freq,
|
||||||
.descr = "Frequency"
|
.descr = "Frequency"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "FORMAT",
|
.name = "FORMAT",
|
||||||
.tag = AUD_OPT_FMT,
|
.tag = AUD_OPT_FMT,
|
||||||
.valp = &glob_conf.settings.fmt,
|
.valp = &conf.settings.fmt,
|
||||||
.descr = "Format"
|
.descr = "Format"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "DAC_FIXED_CHANNELS",
|
.name = "DAC_FIXED_CHANNELS",
|
||||||
.tag = AUD_OPT_INT,
|
.tag = AUD_OPT_INT,
|
||||||
.valp = &glob_conf.settings.nchannels,
|
.valp = &conf.settings.nchannels,
|
||||||
.descr = "Number of channels (1 - mono, 2 - stereo)"
|
.descr = "Number of channels (1 - mono, 2 - stereo)"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "PATH",
|
.name = "PATH",
|
||||||
.tag = AUD_OPT_STR,
|
.tag = AUD_OPT_STR,
|
||||||
.valp = &glob_conf.wav_path,
|
.valp = &conf.wav_path,
|
||||||
.descr = "Path to wave file"
|
.descr = "Path to wave file"
|
||||||
},
|
},
|
||||||
{ /* End of list */ }
|
{ /* End of list */ }
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
#include "hw/hw.h"
|
#include "hw/hw.h"
|
||||||
#include "monitor/monitor.h"
|
#include "monitor/monitor.h"
|
||||||
#include "qemu/error-report.h"
|
|
||||||
#include "audio.h"
|
#include "audio.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -64,7 +63,8 @@ static void wav_destroy (void *opaque)
|
|||||||
}
|
}
|
||||||
doclose:
|
doclose:
|
||||||
if (fclose (wav->f)) {
|
if (fclose (wav->f)) {
|
||||||
error_report("wav_destroy: fclose failed: %s", strerror(errno));
|
fprintf (stderr, "wav_destroy: fclose failed: %s",
|
||||||
|
strerror (errno));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
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)
|
||||||
|
};
|
||||||
@@ -1,11 +1,2 @@
|
|||||||
common-obj-y += rng.o rng-egd.o
|
common-obj-y += rng.o rng-egd.o
|
||||||
common-obj-$(CONFIG_POSIX) += rng-random.o
|
common-obj-$(CONFIG_POSIX) += rng-random.o
|
||||||
|
|
||||||
common-obj-y += msmouse.o testdev.o
|
|
||||||
common-obj-$(CONFIG_BRLAPI) += baum.o
|
|
||||||
baum.o-cflags := $(SDL_CFLAGS)
|
|
||||||
|
|
||||||
common-obj-$(CONFIG_TPM) += tpm.o
|
|
||||||
|
|
||||||
common-obj-y += hostmem.o hostmem-ram.o
|
|
||||||
common-obj-$(CONFIG_LINUX) += hostmem-file.o
|
|
||||||
|
|||||||
@@ -1,132 +0,0 @@
|
|||||||
/*
|
|
||||||
* QEMU Host Memory Backend for hugetlbfs
|
|
||||||
*
|
|
||||||
* Copyright (C) 2013-2014 Red Hat Inc
|
|
||||||
*
|
|
||||||
* Authors:
|
|
||||||
* Paolo Bonzini <pbonzini@redhat.com>
|
|
||||||
*
|
|
||||||
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
||||||
* See the COPYING file in the top-level directory.
|
|
||||||
*/
|
|
||||||
#include "qemu-common.h"
|
|
||||||
#include "sysemu/hostmem.h"
|
|
||||||
#include "sysemu/sysemu.h"
|
|
||||||
#include "qom/object_interfaces.h"
|
|
||||||
|
|
||||||
/* hostmem-file.c */
|
|
||||||
/**
|
|
||||||
* @TYPE_MEMORY_BACKEND_FILE:
|
|
||||||
* name of backend that uses mmap on a file descriptor
|
|
||||||
*/
|
|
||||||
#define TYPE_MEMORY_BACKEND_FILE "memory-backend-file"
|
|
||||||
|
|
||||||
#define MEMORY_BACKEND_FILE(obj) \
|
|
||||||
OBJECT_CHECK(HostMemoryBackendFile, (obj), TYPE_MEMORY_BACKEND_FILE)
|
|
||||||
|
|
||||||
typedef struct HostMemoryBackendFile HostMemoryBackendFile;
|
|
||||||
|
|
||||||
struct HostMemoryBackendFile {
|
|
||||||
HostMemoryBackend parent_obj;
|
|
||||||
|
|
||||||
bool share;
|
|
||||||
char *mem_path;
|
|
||||||
};
|
|
||||||
|
|
||||||
static void
|
|
||||||
file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
|
|
||||||
{
|
|
||||||
HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend);
|
|
||||||
|
|
||||||
if (!backend->size) {
|
|
||||||
error_setg(errp, "can't create backend with size 0");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!fb->mem_path) {
|
|
||||||
error_setg(errp, "mem-path property not set");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
#ifndef CONFIG_LINUX
|
|
||||||
error_setg(errp, "-mem-path not supported on this host");
|
|
||||||
#else
|
|
||||||
if (!memory_region_size(&backend->mr)) {
|
|
||||||
backend->force_prealloc = mem_prealloc;
|
|
||||||
memory_region_init_ram_from_file(&backend->mr, OBJECT(backend),
|
|
||||||
object_get_canonical_path(OBJECT(backend)),
|
|
||||||
backend->size, fb->share,
|
|
||||||
fb->mem_path, errp);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
file_backend_class_init(ObjectClass *oc, void *data)
|
|
||||||
{
|
|
||||||
HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
|
|
||||||
|
|
||||||
bc->alloc = file_backend_memory_alloc;
|
|
||||||
}
|
|
||||||
|
|
||||||
static char *get_mem_path(Object *o, Error **errp)
|
|
||||||
{
|
|
||||||
HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
|
|
||||||
|
|
||||||
return g_strdup(fb->mem_path);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void set_mem_path(Object *o, const char *str, Error **errp)
|
|
||||||
{
|
|
||||||
HostMemoryBackend *backend = MEMORY_BACKEND(o);
|
|
||||||
HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
|
|
||||||
|
|
||||||
if (memory_region_size(&backend->mr)) {
|
|
||||||
error_setg(errp, "cannot change property value");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
g_free(fb->mem_path);
|
|
||||||
fb->mem_path = g_strdup(str);
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool file_memory_backend_get_share(Object *o, Error **errp)
|
|
||||||
{
|
|
||||||
HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
|
|
||||||
|
|
||||||
return fb->share;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void file_memory_backend_set_share(Object *o, bool value, Error **errp)
|
|
||||||
{
|
|
||||||
HostMemoryBackend *backend = MEMORY_BACKEND(o);
|
|
||||||
HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
|
|
||||||
|
|
||||||
if (memory_region_size(&backend->mr)) {
|
|
||||||
error_setg(errp, "cannot change property value");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
fb->share = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
file_backend_instance_init(Object *o)
|
|
||||||
{
|
|
||||||
object_property_add_bool(o, "share",
|
|
||||||
file_memory_backend_get_share,
|
|
||||||
file_memory_backend_set_share, NULL);
|
|
||||||
object_property_add_str(o, "mem-path", get_mem_path,
|
|
||||||
set_mem_path, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
static const TypeInfo file_backend_info = {
|
|
||||||
.name = TYPE_MEMORY_BACKEND_FILE,
|
|
||||||
.parent = TYPE_MEMORY_BACKEND,
|
|
||||||
.class_init = file_backend_class_init,
|
|
||||||
.instance_init = file_backend_instance_init,
|
|
||||||
.instance_size = sizeof(HostMemoryBackendFile),
|
|
||||||
};
|
|
||||||
|
|
||||||
static void register_types(void)
|
|
||||||
{
|
|
||||||
type_register_static(&file_backend_info);
|
|
||||||
}
|
|
||||||
|
|
||||||
type_init(register_types);
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
/*
|
|
||||||
* QEMU Host Memory Backend
|
|
||||||
*
|
|
||||||
* Copyright (C) 2013-2014 Red Hat Inc
|
|
||||||
*
|
|
||||||
* Authors:
|
|
||||||
* Igor Mammedov <imammedo@redhat.com>
|
|
||||||
*
|
|
||||||
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
||||||
* See the COPYING file in the top-level directory.
|
|
||||||
*/
|
|
||||||
#include "sysemu/hostmem.h"
|
|
||||||
#include "qom/object_interfaces.h"
|
|
||||||
|
|
||||||
#define TYPE_MEMORY_BACKEND_RAM "memory-backend-ram"
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
|
||||||
ram_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
|
|
||||||
{
|
|
||||||
char *path;
|
|
||||||
|
|
||||||
if (!backend->size) {
|
|
||||||
error_setg(errp, "can't create backend with size 0");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
path = object_get_canonical_path_component(OBJECT(backend));
|
|
||||||
memory_region_init_ram(&backend->mr, OBJECT(backend), path,
|
|
||||||
backend->size, errp);
|
|
||||||
g_free(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
ram_backend_class_init(ObjectClass *oc, void *data)
|
|
||||||
{
|
|
||||||
HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
|
|
||||||
|
|
||||||
bc->alloc = ram_backend_memory_alloc;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const TypeInfo ram_backend_info = {
|
|
||||||
.name = TYPE_MEMORY_BACKEND_RAM,
|
|
||||||
.parent = TYPE_MEMORY_BACKEND,
|
|
||||||
.class_init = ram_backend_class_init,
|
|
||||||
};
|
|
||||||
|
|
||||||
static void register_types(void)
|
|
||||||
{
|
|
||||||
type_register_static(&ram_backend_info);
|
|
||||||
}
|
|
||||||
|
|
||||||
type_init(register_types);
|
|
||||||
@@ -1,374 +0,0 @@
|
|||||||
/*
|
|
||||||
* QEMU Host Memory Backend
|
|
||||||
*
|
|
||||||
* Copyright (C) 2013-2014 Red Hat Inc
|
|
||||||
*
|
|
||||||
* Authors:
|
|
||||||
* Igor Mammedov <imammedo@redhat.com>
|
|
||||||
*
|
|
||||||
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
||||||
* See the COPYING file in the top-level directory.
|
|
||||||
*/
|
|
||||||
#include "sysemu/hostmem.h"
|
|
||||||
#include "hw/boards.h"
|
|
||||||
#include "qapi/visitor.h"
|
|
||||||
#include "qapi-types.h"
|
|
||||||
#include "qapi-visit.h"
|
|
||||||
#include "qemu/config-file.h"
|
|
||||||
#include "qom/object_interfaces.h"
|
|
||||||
|
|
||||||
#ifdef CONFIG_NUMA
|
|
||||||
#include <numaif.h>
|
|
||||||
QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_DEFAULT != MPOL_DEFAULT);
|
|
||||||
QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_PREFERRED != MPOL_PREFERRED);
|
|
||||||
QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_BIND != MPOL_BIND);
|
|
||||||
QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_INTERLEAVE != MPOL_INTERLEAVE);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static void
|
|
||||||
host_memory_backend_get_size(Object *obj, Visitor *v, void *opaque,
|
|
||||||
const char *name, Error **errp)
|
|
||||||
{
|
|
||||||
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
||||||
uint64_t value = backend->size;
|
|
||||||
|
|
||||||
visit_type_size(v, &value, name, errp);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
host_memory_backend_set_size(Object *obj, Visitor *v, void *opaque,
|
|
||||||
const char *name, Error **errp)
|
|
||||||
{
|
|
||||||
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
||||||
Error *local_err = NULL;
|
|
||||||
uint64_t value;
|
|
||||||
|
|
||||||
if (memory_region_size(&backend->mr)) {
|
|
||||||
error_setg(&local_err, "cannot change property value");
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
visit_type_size(v, &value, name, &local_err);
|
|
||||||
if (local_err) {
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
if (!value) {
|
|
||||||
error_setg(&local_err, "Property '%s.%s' doesn't take value '%"
|
|
||||||
PRIu64 "'", object_get_typename(obj), name, value);
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
backend->size = value;
|
|
||||||
out:
|
|
||||||
error_propagate(errp, local_err);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
host_memory_backend_get_host_nodes(Object *obj, Visitor *v, void *opaque,
|
|
||||||
const char *name, Error **errp)
|
|
||||||
{
|
|
||||||
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
||||||
uint16List *host_nodes = NULL;
|
|
||||||
uint16List **node = &host_nodes;
|
|
||||||
unsigned long value;
|
|
||||||
|
|
||||||
value = find_first_bit(backend->host_nodes, MAX_NODES);
|
|
||||||
if (value == MAX_NODES) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
*node = g_malloc0(sizeof(**node));
|
|
||||||
(*node)->value = value;
|
|
||||||
node = &(*node)->next;
|
|
||||||
|
|
||||||
do {
|
|
||||||
value = find_next_bit(backend->host_nodes, MAX_NODES, value + 1);
|
|
||||||
if (value == MAX_NODES) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
*node = g_malloc0(sizeof(**node));
|
|
||||||
(*node)->value = value;
|
|
||||||
node = &(*node)->next;
|
|
||||||
} while (true);
|
|
||||||
|
|
||||||
visit_type_uint16List(v, &host_nodes, name, errp);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
host_memory_backend_set_host_nodes(Object *obj, Visitor *v, void *opaque,
|
|
||||||
const char *name, Error **errp)
|
|
||||||
{
|
|
||||||
#ifdef CONFIG_NUMA
|
|
||||||
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
||||||
uint16List *l = NULL;
|
|
||||||
|
|
||||||
visit_type_uint16List(v, &l, name, errp);
|
|
||||||
|
|
||||||
while (l) {
|
|
||||||
bitmap_set(backend->host_nodes, l->value, 1);
|
|
||||||
l = l->next;
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
error_setg(errp, "NUMA node binding are not supported by this QEMU");
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
|
||||||
host_memory_backend_get_policy(Object *obj, Error **errp G_GNUC_UNUSED)
|
|
||||||
{
|
|
||||||
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
||||||
return backend->policy;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
host_memory_backend_set_policy(Object *obj, int policy, Error **errp)
|
|
||||||
{
|
|
||||||
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
||||||
backend->policy = policy;
|
|
||||||
|
|
||||||
#ifndef CONFIG_NUMA
|
|
||||||
if (policy != HOST_MEM_POLICY_DEFAULT) {
|
|
||||||
error_setg(errp, "NUMA policies are not supported by this QEMU");
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool host_memory_backend_get_merge(Object *obj, Error **errp)
|
|
||||||
{
|
|
||||||
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
||||||
|
|
||||||
return backend->merge;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void host_memory_backend_set_merge(Object *obj, bool value, Error **errp)
|
|
||||||
{
|
|
||||||
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
||||||
|
|
||||||
if (!memory_region_size(&backend->mr)) {
|
|
||||||
backend->merge = value;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (value != backend->merge) {
|
|
||||||
void *ptr = memory_region_get_ram_ptr(&backend->mr);
|
|
||||||
uint64_t sz = memory_region_size(&backend->mr);
|
|
||||||
|
|
||||||
qemu_madvise(ptr, sz,
|
|
||||||
value ? QEMU_MADV_MERGEABLE : QEMU_MADV_UNMERGEABLE);
|
|
||||||
backend->merge = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool host_memory_backend_get_dump(Object *obj, Error **errp)
|
|
||||||
{
|
|
||||||
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
||||||
|
|
||||||
return backend->dump;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void host_memory_backend_set_dump(Object *obj, bool value, Error **errp)
|
|
||||||
{
|
|
||||||
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
||||||
|
|
||||||
if (!memory_region_size(&backend->mr)) {
|
|
||||||
backend->dump = value;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (value != backend->dump) {
|
|
||||||
void *ptr = memory_region_get_ram_ptr(&backend->mr);
|
|
||||||
uint64_t sz = memory_region_size(&backend->mr);
|
|
||||||
|
|
||||||
qemu_madvise(ptr, sz,
|
|
||||||
value ? QEMU_MADV_DODUMP : QEMU_MADV_DONTDUMP);
|
|
||||||
backend->dump = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool host_memory_backend_get_prealloc(Object *obj, Error **errp)
|
|
||||||
{
|
|
||||||
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
||||||
|
|
||||||
return backend->prealloc || backend->force_prealloc;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void host_memory_backend_set_prealloc(Object *obj, bool value,
|
|
||||||
Error **errp)
|
|
||||||
{
|
|
||||||
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
||||||
|
|
||||||
if (backend->force_prealloc) {
|
|
||||||
if (value) {
|
|
||||||
error_setg(errp,
|
|
||||||
"remove -mem-prealloc to use the prealloc property");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!memory_region_size(&backend->mr)) {
|
|
||||||
backend->prealloc = value;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (value && !backend->prealloc) {
|
|
||||||
int fd = memory_region_get_fd(&backend->mr);
|
|
||||||
void *ptr = memory_region_get_ram_ptr(&backend->mr);
|
|
||||||
uint64_t sz = memory_region_size(&backend->mr);
|
|
||||||
|
|
||||||
os_mem_prealloc(fd, ptr, sz);
|
|
||||||
backend->prealloc = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void host_memory_backend_init(Object *obj)
|
|
||||||
{
|
|
||||||
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
||||||
MachineState *machine = MACHINE(qdev_get_machine());
|
|
||||||
|
|
||||||
backend->merge = machine_mem_merge(machine);
|
|
||||||
backend->dump = machine_dump_guest_core(machine);
|
|
||||||
backend->prealloc = mem_prealloc;
|
|
||||||
|
|
||||||
object_property_add_bool(obj, "merge",
|
|
||||||
host_memory_backend_get_merge,
|
|
||||||
host_memory_backend_set_merge, NULL);
|
|
||||||
object_property_add_bool(obj, "dump",
|
|
||||||
host_memory_backend_get_dump,
|
|
||||||
host_memory_backend_set_dump, NULL);
|
|
||||||
object_property_add_bool(obj, "prealloc",
|
|
||||||
host_memory_backend_get_prealloc,
|
|
||||||
host_memory_backend_set_prealloc, NULL);
|
|
||||||
object_property_add(obj, "size", "int",
|
|
||||||
host_memory_backend_get_size,
|
|
||||||
host_memory_backend_set_size, NULL, NULL, NULL);
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
MemoryRegion *
|
|
||||||
host_memory_backend_get_memory(HostMemoryBackend *backend, Error **errp)
|
|
||||||
{
|
|
||||||
return memory_region_size(&backend->mr) ? &backend->mr : NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
|
|
||||||
{
|
|
||||||
HostMemoryBackend *backend = MEMORY_BACKEND(uc);
|
|
||||||
HostMemoryBackendClass *bc = MEMORY_BACKEND_GET_CLASS(uc);
|
|
||||||
Error *local_err = NULL;
|
|
||||||
void *ptr;
|
|
||||||
uint64_t sz;
|
|
||||||
|
|
||||||
if (bc->alloc) {
|
|
||||||
bc->alloc(backend, &local_err);
|
|
||||||
if (local_err) {
|
|
||||||
error_propagate(errp, local_err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ptr = memory_region_get_ram_ptr(&backend->mr);
|
|
||||||
sz = memory_region_size(&backend->mr);
|
|
||||||
|
|
||||||
if (backend->merge) {
|
|
||||||
qemu_madvise(ptr, sz, QEMU_MADV_MERGEABLE);
|
|
||||||
}
|
|
||||||
if (!backend->dump) {
|
|
||||||
qemu_madvise(ptr, sz, QEMU_MADV_DONTDUMP);
|
|
||||||
}
|
|
||||||
#ifdef CONFIG_NUMA
|
|
||||||
unsigned long lastbit = find_last_bit(backend->host_nodes, MAX_NODES);
|
|
||||||
/* lastbit == MAX_NODES means maxnode = 0 */
|
|
||||||
unsigned long maxnode = (lastbit + 1) % (MAX_NODES + 1);
|
|
||||||
/* ensure policy won't be ignored in case memory is preallocated
|
|
||||||
* before mbind(). note: MPOL_MF_STRICT is ignored on hugepages so
|
|
||||||
* this doesn't catch hugepage case. */
|
|
||||||
unsigned flags = MPOL_MF_STRICT | MPOL_MF_MOVE;
|
|
||||||
|
|
||||||
/* check for invalid host-nodes and policies and give more verbose
|
|
||||||
* error messages than mbind(). */
|
|
||||||
if (maxnode && backend->policy == MPOL_DEFAULT) {
|
|
||||||
error_setg(errp, "host-nodes must be empty for policy default,"
|
|
||||||
" or you should explicitly specify a policy other"
|
|
||||||
" than default");
|
|
||||||
return;
|
|
||||||
} else if (maxnode == 0 && backend->policy != MPOL_DEFAULT) {
|
|
||||||
error_setg(errp, "host-nodes must be set for policy %s",
|
|
||||||
HostMemPolicy_lookup[backend->policy]);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* We can have up to MAX_NODES nodes, but we need to pass maxnode+1
|
|
||||||
* as argument to mbind() due to an old Linux bug (feature?) which
|
|
||||||
* cuts off the last specified node. This means backend->host_nodes
|
|
||||||
* must have MAX_NODES+1 bits available.
|
|
||||||
*/
|
|
||||||
assert(sizeof(backend->host_nodes) >=
|
|
||||||
BITS_TO_LONGS(MAX_NODES + 1) * sizeof(unsigned long));
|
|
||||||
assert(maxnode <= MAX_NODES);
|
|
||||||
if (mbind(ptr, sz, backend->policy,
|
|
||||||
maxnode ? backend->host_nodes : NULL, maxnode + 1, flags)) {
|
|
||||||
if (backend->policy != MPOL_DEFAULT || errno != ENOSYS) {
|
|
||||||
error_setg_errno(errp, errno,
|
|
||||||
"cannot bind memory to host NUMA nodes");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
/* Preallocate memory after the NUMA policy has been instantiated.
|
|
||||||
* This is necessary to guarantee memory is allocated with
|
|
||||||
* specified NUMA policy in place.
|
|
||||||
*/
|
|
||||||
if (backend->prealloc) {
|
|
||||||
os_mem_prealloc(memory_region_get_fd(&backend->mr), ptr, sz);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool
|
|
||||||
host_memory_backend_can_be_deleted(UserCreatable *uc, Error **errp)
|
|
||||||
{
|
|
||||||
MemoryRegion *mr;
|
|
||||||
|
|
||||||
mr = host_memory_backend_get_memory(MEMORY_BACKEND(uc), errp);
|
|
||||||
if (memory_region_is_mapped(mr)) {
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
host_memory_backend_class_init(ObjectClass *oc, void *data)
|
|
||||||
{
|
|
||||||
UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
|
|
||||||
|
|
||||||
ucc->complete = host_memory_backend_memory_complete;
|
|
||||||
ucc->can_be_deleted = host_memory_backend_can_be_deleted;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const TypeInfo host_memory_backend_info = {
|
|
||||||
.name = TYPE_MEMORY_BACKEND,
|
|
||||||
.parent = TYPE_OBJECT,
|
|
||||||
.abstract = true,
|
|
||||||
.class_size = sizeof(HostMemoryBackendClass),
|
|
||||||
.class_init = host_memory_backend_class_init,
|
|
||||||
.instance_size = sizeof(HostMemoryBackend),
|
|
||||||
.instance_init = host_memory_backend_init,
|
|
||||||
.interfaces = (InterfaceInfo[]) {
|
|
||||||
{ TYPE_USER_CREATABLE },
|
|
||||||
{ }
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
static void register_types(void)
|
|
||||||
{
|
|
||||||
type_register_static(&host_memory_backend_info);
|
|
||||||
}
|
|
||||||
|
|
||||||
type_init(register_types);
|
|
||||||
@@ -10,8 +10,8 @@
|
|||||||
* See the COPYING file in the top-level directory.
|
* See the COPYING file in the top-level directory.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "sysemu/rng.h"
|
#include "qemu/rng.h"
|
||||||
#include "sysemu/char.h"
|
#include "char/char.h"
|
||||||
#include "qapi/qmp/qerror.h"
|
#include "qapi/qmp/qerror.h"
|
||||||
#include "hw/qdev.h" /* just for DEFINE_PROP_CHR */
|
#include "hw/qdev.h" /* just for DEFINE_PROP_CHR */
|
||||||
|
|
||||||
@@ -91,14 +91,12 @@ static int rng_egd_chr_can_read(void *opaque)
|
|||||||
static void rng_egd_chr_read(void *opaque, const uint8_t *buf, int size)
|
static void rng_egd_chr_read(void *opaque, const uint8_t *buf, int size)
|
||||||
{
|
{
|
||||||
RngEgd *s = RNG_EGD(opaque);
|
RngEgd *s = RNG_EGD(opaque);
|
||||||
size_t buf_offset = 0;
|
|
||||||
|
|
||||||
while (size > 0 && s->requests) {
|
while (size > 0 && s->requests) {
|
||||||
RngRequest *req = s->requests->data;
|
RngRequest *req = s->requests->data;
|
||||||
int len = MIN(size, req->size - req->offset);
|
int len = MIN(size, req->size - req->offset);
|
||||||
|
|
||||||
memcpy(req->data + req->offset, buf + buf_offset, len);
|
memcpy(req->data + req->offset, buf, len);
|
||||||
buf_offset += len;
|
|
||||||
req->offset += len;
|
req->offset += len;
|
||||||
size -= len;
|
size -= len;
|
||||||
|
|
||||||
@@ -140,20 +138,14 @@ static void rng_egd_opened(RngBackend *b, Error **errp)
|
|||||||
RngEgd *s = RNG_EGD(b);
|
RngEgd *s = RNG_EGD(b);
|
||||||
|
|
||||||
if (s->chr_name == NULL) {
|
if (s->chr_name == NULL) {
|
||||||
error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
|
error_set(errp, QERR_INVALID_PARAMETER_VALUE,
|
||||||
"chardev", "a valid character device");
|
"chardev", "a valid character device");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
s->chr = qemu_chr_find(s->chr_name);
|
s->chr = qemu_chr_find(s->chr_name);
|
||||||
if (s->chr == NULL) {
|
if (s->chr == NULL) {
|
||||||
error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
|
error_set(errp, QERR_DEVICE_NOT_FOUND, s->chr_name);
|
||||||
"Device '%s' not found", s->chr_name);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (qemu_chr_fe_claim(s->chr) != 0) {
|
|
||||||
error_setg(errp, QERR_DEVICE_IN_USE, s->chr_name);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,7 +160,7 @@ static void rng_egd_set_chardev(Object *obj, const char *value, Error **errp)
|
|||||||
RngEgd *s = RNG_EGD(b);
|
RngEgd *s = RNG_EGD(b);
|
||||||
|
|
||||||
if (b->opened) {
|
if (b->opened) {
|
||||||
error_setg(errp, QERR_PERMISSION_DENIED);
|
error_set(errp, QERR_PERMISSION_DENIED);
|
||||||
} else {
|
} else {
|
||||||
g_free(s->chr_name);
|
g_free(s->chr_name);
|
||||||
s->chr_name = g_strdup(value);
|
s->chr_name = g_strdup(value);
|
||||||
@@ -199,7 +191,6 @@ static void rng_egd_finalize(Object *obj)
|
|||||||
|
|
||||||
if (s->chr) {
|
if (s->chr) {
|
||||||
qemu_chr_add_handlers(s->chr, NULL, NULL, NULL, NULL);
|
qemu_chr_add_handlers(s->chr, NULL, NULL, NULL, NULL);
|
||||||
qemu_chr_fe_release(s->chr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
g_free(s->chr_name);
|
g_free(s->chr_name);
|
||||||
|
|||||||
@@ -10,8 +10,8 @@
|
|||||||
* See the COPYING file in the top-level directory.
|
* See the COPYING file in the top-level directory.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "sysemu/rng-random.h"
|
#include "qemu/rng-random.h"
|
||||||
#include "sysemu/rng.h"
|
#include "qemu/rng.h"
|
||||||
#include "qapi/qmp/qerror.h"
|
#include "qapi/qmp/qerror.h"
|
||||||
#include "qemu/main-loop.h"
|
#include "qemu/main-loop.h"
|
||||||
|
|
||||||
@@ -74,12 +74,13 @@ static void rng_random_opened(RngBackend *b, Error **errp)
|
|||||||
RndRandom *s = RNG_RANDOM(b);
|
RndRandom *s = RNG_RANDOM(b);
|
||||||
|
|
||||||
if (s->filename == NULL) {
|
if (s->filename == NULL) {
|
||||||
error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
|
error_set(errp, QERR_INVALID_PARAMETER_VALUE,
|
||||||
"filename", "a valid filename");
|
"filename", "a valid filename");
|
||||||
} else {
|
} else {
|
||||||
s->fd = qemu_open(s->filename, O_RDONLY | O_NONBLOCK);
|
s->fd = qemu_open(s->filename, O_RDONLY | O_NONBLOCK);
|
||||||
|
|
||||||
if (s->fd == -1) {
|
if (s->fd == -1) {
|
||||||
error_setg_file_open(errp, errno, s->filename);
|
error_set(errp, QERR_OPEN_FILE_FAILED, s->filename);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -88,9 +89,13 @@ static char *rng_random_get_filename(Object *obj, Error **errp)
|
|||||||
{
|
{
|
||||||
RndRandom *s = RNG_RANDOM(obj);
|
RndRandom *s = RNG_RANDOM(obj);
|
||||||
|
|
||||||
|
if (s->filename) {
|
||||||
return g_strdup(s->filename);
|
return g_strdup(s->filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
static void rng_random_set_filename(Object *obj, const char *filename,
|
static void rng_random_set_filename(Object *obj, const char *filename,
|
||||||
Error **errp)
|
Error **errp)
|
||||||
{
|
{
|
||||||
@@ -98,11 +103,14 @@ static void rng_random_set_filename(Object *obj, const char *filename,
|
|||||||
RndRandom *s = RNG_RANDOM(obj);
|
RndRandom *s = RNG_RANDOM(obj);
|
||||||
|
|
||||||
if (b->opened) {
|
if (b->opened) {
|
||||||
error_setg(errp, QERR_PERMISSION_DENIED);
|
error_set(errp, QERR_PERMISSION_DENIED);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (s->filename) {
|
||||||
g_free(s->filename);
|
g_free(s->filename);
|
||||||
|
}
|
||||||
|
|
||||||
s->filename = g_strdup(filename);
|
s->filename = g_strdup(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,15 +124,15 @@ static void rng_random_init(Object *obj)
|
|||||||
NULL);
|
NULL);
|
||||||
|
|
||||||
s->filename = g_strdup("/dev/random");
|
s->filename = g_strdup("/dev/random");
|
||||||
s->fd = -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void rng_random_finalize(Object *obj)
|
static void rng_random_finalize(Object *obj)
|
||||||
{
|
{
|
||||||
RndRandom *s = RNG_RANDOM(obj);
|
RndRandom *s = RNG_RANDOM(obj);
|
||||||
|
|
||||||
if (s->fd != -1) {
|
|
||||||
qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
|
qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
|
||||||
|
|
||||||
|
if (s->fd != -1) {
|
||||||
qemu_close(s->fd);
|
qemu_close(s->fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,9 +10,8 @@
|
|||||||
* See the COPYING file in the top-level directory.
|
* See the COPYING file in the top-level directory.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "sysemu/rng.h"
|
#include "qemu/rng.h"
|
||||||
#include "qapi/qmp/qerror.h"
|
#include "qapi/qmp/qerror.h"
|
||||||
#include "qom/object_interfaces.h"
|
|
||||||
|
|
||||||
void rng_backend_request_entropy(RngBackend *s, size_t size,
|
void rng_backend_request_entropy(RngBackend *s, size_t size,
|
||||||
EntropyReceiveFunc *receive_entropy,
|
EntropyReceiveFunc *receive_entropy,
|
||||||
@@ -41,35 +40,32 @@ static bool rng_backend_prop_get_opened(Object *obj, Error **errp)
|
|||||||
return s->opened;
|
return s->opened;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void rng_backend_complete(UserCreatable *uc, Error **errp)
|
void rng_backend_open(RngBackend *s, Error **errp)
|
||||||
{
|
{
|
||||||
object_property_set_bool(OBJECT(uc), true, "opened", errp);
|
object_property_set_bool(OBJECT(s), true, "opened", errp);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void rng_backend_prop_set_opened(Object *obj, bool value, Error **errp)
|
static void rng_backend_prop_set_opened(Object *obj, bool value, Error **errp)
|
||||||
{
|
{
|
||||||
RngBackend *s = RNG_BACKEND(obj);
|
RngBackend *s = RNG_BACKEND(obj);
|
||||||
RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
|
RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
|
||||||
Error *local_err = NULL;
|
|
||||||
|
|
||||||
if (value == s->opened) {
|
if (value == s->opened) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!value && s->opened) {
|
if (!value && s->opened) {
|
||||||
error_setg(errp, QERR_PERMISSION_DENIED);
|
error_set(errp, QERR_PERMISSION_DENIED);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (k->opened) {
|
if (k->opened) {
|
||||||
k->opened(s, &local_err);
|
k->opened(s, errp);
|
||||||
if (local_err) {
|
|
||||||
error_propagate(errp, local_err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
s->opened = true;
|
if (!error_is_set(errp)) {
|
||||||
|
s->opened = value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void rng_backend_init(Object *obj)
|
static void rng_backend_init(Object *obj)
|
||||||
@@ -80,25 +76,13 @@ static void rng_backend_init(Object *obj)
|
|||||||
NULL);
|
NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void rng_backend_class_init(ObjectClass *oc, void *data)
|
|
||||||
{
|
|
||||||
UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
|
|
||||||
|
|
||||||
ucc->complete = rng_backend_complete;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const TypeInfo rng_backend_info = {
|
static const TypeInfo rng_backend_info = {
|
||||||
.name = TYPE_RNG_BACKEND,
|
.name = TYPE_RNG_BACKEND,
|
||||||
.parent = TYPE_OBJECT,
|
.parent = TYPE_OBJECT,
|
||||||
.instance_size = sizeof(RngBackend),
|
.instance_size = sizeof(RngBackend),
|
||||||
.instance_init = rng_backend_init,
|
.instance_init = rng_backend_init,
|
||||||
.class_size = sizeof(RngBackendClass),
|
.class_size = sizeof(RngBackendClass),
|
||||||
.class_init = rng_backend_class_init,
|
|
||||||
.abstract = true,
|
.abstract = true,
|
||||||
.interfaces = (InterfaceInfo[]) {
|
|
||||||
{ TYPE_USER_CREATABLE },
|
|
||||||
{ }
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static void register_types(void)
|
static void register_types(void)
|
||||||
|
|||||||
@@ -1,135 +0,0 @@
|
|||||||
/*
|
|
||||||
* QEMU Char Device for testsuite control
|
|
||||||
*
|
|
||||||
* Copyright (c) 2014 Red Hat, Inc.
|
|
||||||
*
|
|
||||||
* Author: Paolo Bonzini <pbonzini@redhat.com>
|
|
||||||
*
|
|
||||||
* 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 "sysemu/char.h"
|
|
||||||
|
|
||||||
#define BUF_SIZE 32
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
CharDriverState *chr;
|
|
||||||
uint8_t in_buf[32];
|
|
||||||
int in_buf_used;
|
|
||||||
} TestdevCharState;
|
|
||||||
|
|
||||||
/* Try to interpret a whole incoming packet */
|
|
||||||
static int testdev_eat_packet(TestdevCharState *testdev)
|
|
||||||
{
|
|
||||||
const uint8_t *cur = testdev->in_buf;
|
|
||||||
int len = testdev->in_buf_used;
|
|
||||||
uint8_t c;
|
|
||||||
int arg;
|
|
||||||
|
|
||||||
#define EAT(c) do { \
|
|
||||||
if (!len--) { \
|
|
||||||
return 0; \
|
|
||||||
} \
|
|
||||||
c = *cur++; \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
EAT(c);
|
|
||||||
|
|
||||||
while (isspace(c)) {
|
|
||||||
EAT(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
arg = 0;
|
|
||||||
while (isdigit(c)) {
|
|
||||||
arg = arg * 10 + c - '0';
|
|
||||||
EAT(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (isspace(c)) {
|
|
||||||
EAT(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (c) {
|
|
||||||
case 'q':
|
|
||||||
exit((arg << 1) | 1);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return cur - testdev->in_buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* The other end is writing some data. Store it and try to interpret */
|
|
||||||
static int testdev_write(CharDriverState *chr, const uint8_t *buf, int len)
|
|
||||||
{
|
|
||||||
TestdevCharState *testdev = chr->opaque;
|
|
||||||
int tocopy, eaten, orig_len = len;
|
|
||||||
|
|
||||||
while (len) {
|
|
||||||
/* Complete our buffer as much as possible */
|
|
||||||
tocopy = MIN(len, BUF_SIZE - testdev->in_buf_used);
|
|
||||||
|
|
||||||
memcpy(testdev->in_buf + testdev->in_buf_used, buf, tocopy);
|
|
||||||
testdev->in_buf_used += tocopy;
|
|
||||||
buf += tocopy;
|
|
||||||
len -= tocopy;
|
|
||||||
|
|
||||||
/* Interpret it as much as possible */
|
|
||||||
while (testdev->in_buf_used > 0 &&
|
|
||||||
(eaten = testdev_eat_packet(testdev)) > 0) {
|
|
||||||
memmove(testdev->in_buf, testdev->in_buf + eaten,
|
|
||||||
testdev->in_buf_used - eaten);
|
|
||||||
testdev->in_buf_used -= eaten;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return orig_len;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void testdev_close(struct CharDriverState *chr)
|
|
||||||
{
|
|
||||||
TestdevCharState *testdev = chr->opaque;
|
|
||||||
|
|
||||||
g_free(testdev);
|
|
||||||
}
|
|
||||||
|
|
||||||
static CharDriverState *chr_testdev_init(const char *id,
|
|
||||||
ChardevBackend *backend,
|
|
||||||
ChardevReturn *ret,
|
|
||||||
Error **errp)
|
|
||||||
{
|
|
||||||
TestdevCharState *testdev;
|
|
||||||
CharDriverState *chr;
|
|
||||||
|
|
||||||
testdev = g_new0(TestdevCharState, 1);
|
|
||||||
testdev->chr = chr = g_new0(CharDriverState, 1);
|
|
||||||
|
|
||||||
chr->opaque = testdev;
|
|
||||||
chr->chr_write = testdev_write;
|
|
||||||
chr->chr_close = testdev_close;
|
|
||||||
|
|
||||||
return chr;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void register_types(void)
|
|
||||||
{
|
|
||||||
register_char_driver("testdev", CHARDEV_BACKEND_KIND_TESTDEV, NULL,
|
|
||||||
chr_testdev_init);
|
|
||||||
}
|
|
||||||
|
|
||||||
type_init(register_types);
|
|
||||||
196
backends/tpm.c
196
backends/tpm.c
@@ -1,196 +0,0 @@
|
|||||||
/*
|
|
||||||
* QEMU TPM Backend
|
|
||||||
*
|
|
||||||
* Copyright IBM, Corp. 2013
|
|
||||||
*
|
|
||||||
* Authors:
|
|
||||||
* Stefan Berger <stefanb@us.ibm.com>
|
|
||||||
*
|
|
||||||
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
||||||
* See the COPYING file in the top-level directory.
|
|
||||||
*
|
|
||||||
* Based on backends/rng.c by Anthony Liguori
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "sysemu/tpm_backend.h"
|
|
||||||
#include "qapi/qmp/qerror.h"
|
|
||||||
#include "sysemu/tpm.h"
|
|
||||||
#include "qemu/thread.h"
|
|
||||||
#include "sysemu/tpm_backend_int.h"
|
|
||||||
|
|
||||||
enum TpmType tpm_backend_get_type(TPMBackend *s)
|
|
||||||
{
|
|
||||||
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
||||||
|
|
||||||
return k->ops->type;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *tpm_backend_get_desc(TPMBackend *s)
|
|
||||||
{
|
|
||||||
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
||||||
|
|
||||||
return k->ops->desc();
|
|
||||||
}
|
|
||||||
|
|
||||||
void tpm_backend_destroy(TPMBackend *s)
|
|
||||||
{
|
|
||||||
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
||||||
|
|
||||||
k->ops->destroy(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
int tpm_backend_init(TPMBackend *s, TPMState *state,
|
|
||||||
TPMRecvDataCB *datacb)
|
|
||||||
{
|
|
||||||
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
||||||
|
|
||||||
return k->ops->init(s, state, datacb);
|
|
||||||
}
|
|
||||||
|
|
||||||
int tpm_backend_startup_tpm(TPMBackend *s)
|
|
||||||
{
|
|
||||||
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
||||||
|
|
||||||
return k->ops->startup_tpm(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool tpm_backend_had_startup_error(TPMBackend *s)
|
|
||||||
{
|
|
||||||
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
||||||
|
|
||||||
return k->ops->had_startup_error(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t tpm_backend_realloc_buffer(TPMBackend *s, TPMSizedBuffer *sb)
|
|
||||||
{
|
|
||||||
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
||||||
|
|
||||||
return k->ops->realloc_buffer(sb);
|
|
||||||
}
|
|
||||||
|
|
||||||
void tpm_backend_deliver_request(TPMBackend *s)
|
|
||||||
{
|
|
||||||
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
||||||
|
|
||||||
k->ops->deliver_request(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
void tpm_backend_reset(TPMBackend *s)
|
|
||||||
{
|
|
||||||
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
||||||
|
|
||||||
k->ops->reset(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
void tpm_backend_cancel_cmd(TPMBackend *s)
|
|
||||||
{
|
|
||||||
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
||||||
|
|
||||||
k->ops->cancel_cmd(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool tpm_backend_get_tpm_established_flag(TPMBackend *s)
|
|
||||||
{
|
|
||||||
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(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);
|
|
||||||
|
|
||||||
return s->opened;
|
|
||||||
}
|
|
||||||
|
|
||||||
void tpm_backend_open(TPMBackend *s, Error **errp)
|
|
||||||
{
|
|
||||||
object_property_set_bool(OBJECT(s), true, "opened", errp);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void tpm_backend_prop_set_opened(Object *obj, bool value, Error **errp)
|
|
||||||
{
|
|
||||||
TPMBackend *s = TPM_BACKEND(obj);
|
|
||||||
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
|
|
||||||
Error *local_err = NULL;
|
|
||||||
|
|
||||||
if (value == s->opened) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!value && s->opened) {
|
|
||||||
error_setg(errp, QERR_PERMISSION_DENIED);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (k->opened) {
|
|
||||||
k->opened(s, &local_err);
|
|
||||||
if (local_err) {
|
|
||||||
error_propagate(errp, local_err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
s->opened = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void tpm_backend_instance_init(Object *obj)
|
|
||||||
{
|
|
||||||
object_property_add_bool(obj, "opened",
|
|
||||||
tpm_backend_prop_get_opened,
|
|
||||||
tpm_backend_prop_set_opened,
|
|
||||||
NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
void tpm_backend_thread_deliver_request(TPMBackendThread *tbt)
|
|
||||||
{
|
|
||||||
g_thread_pool_push(tbt->pool, (gpointer)TPM_BACKEND_CMD_PROCESS_CMD, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
void tpm_backend_thread_create(TPMBackendThread *tbt,
|
|
||||||
GFunc func, gpointer user_data)
|
|
||||||
{
|
|
||||||
if (!tbt->pool) {
|
|
||||||
tbt->pool = g_thread_pool_new(func, user_data, 1, TRUE, NULL);
|
|
||||||
g_thread_pool_push(tbt->pool, (gpointer)TPM_BACKEND_CMD_INIT, NULL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void tpm_backend_thread_end(TPMBackendThread *tbt)
|
|
||||||
{
|
|
||||||
if (tbt->pool) {
|
|
||||||
g_thread_pool_push(tbt->pool, (gpointer)TPM_BACKEND_CMD_END, NULL);
|
|
||||||
g_thread_pool_free(tbt->pool, FALSE, TRUE);
|
|
||||||
tbt->pool = NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static const TypeInfo tpm_backend_info = {
|
|
||||||
.name = TYPE_TPM_BACKEND,
|
|
||||||
.parent = TYPE_OBJECT,
|
|
||||||
.instance_size = sizeof(TPMBackend),
|
|
||||||
.instance_init = tpm_backend_instance_init,
|
|
||||||
.class_size = sizeof(TPMBackendClass),
|
|
||||||
.abstract = true,
|
|
||||||
};
|
|
||||||
|
|
||||||
static void register_types(void)
|
|
||||||
{
|
|
||||||
type_register_static(&tpm_backend_info);
|
|
||||||
}
|
|
||||||
|
|
||||||
type_init(register_types);
|
|
||||||
87
balloon.c
87
balloon.c
@@ -24,44 +24,17 @@
|
|||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "qemu-common.h"
|
#include "monitor/monitor.h"
|
||||||
#include "exec/cpu-common.h"
|
#include "exec/cpu-common.h"
|
||||||
#include "sysemu/kvm.h"
|
#include "sysemu/kvm.h"
|
||||||
#include "sysemu/balloon.h"
|
#include "sysemu/balloon.h"
|
||||||
#include "trace.h"
|
#include "trace.h"
|
||||||
#include "qmp-commands.h"
|
#include "qmp-commands.h"
|
||||||
#include "qapi/qmp/qerror.h"
|
|
||||||
#include "qapi/qmp/qjson.h"
|
#include "qapi/qmp/qjson.h"
|
||||||
|
|
||||||
static QEMUBalloonEvent *balloon_event_fn;
|
static QEMUBalloonEvent *balloon_event_fn;
|
||||||
static QEMUBalloonStatus *balloon_stat_fn;
|
static QEMUBalloonStatus *balloon_stat_fn;
|
||||||
static void *balloon_opaque;
|
static void *balloon_opaque;
|
||||||
static bool balloon_inhibited;
|
|
||||||
|
|
||||||
bool qemu_balloon_is_inhibited(void)
|
|
||||||
{
|
|
||||||
return balloon_inhibited;
|
|
||||||
}
|
|
||||||
|
|
||||||
void qemu_balloon_inhibit(bool state)
|
|
||||||
{
|
|
||||||
balloon_inhibited = state;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool have_balloon(Error **errp)
|
|
||||||
{
|
|
||||||
if (kvm_enabled() && !kvm_has_sync_mmu()) {
|
|
||||||
error_set(errp, ERROR_CLASS_KVM_MISSING_CAP,
|
|
||||||
"Using KVM without synchronous MMU, balloon unavailable");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!balloon_event_fn) {
|
|
||||||
error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
|
|
||||||
"No balloon device has been activated");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
int qemu_add_balloon_handler(QEMUBalloonEvent *event_func,
|
int qemu_add_balloon_handler(QEMUBalloonEvent *event_func,
|
||||||
QEMUBalloonStatus *stat_func, void *opaque)
|
QEMUBalloonStatus *stat_func, void *opaque)
|
||||||
@@ -70,6 +43,7 @@ int qemu_add_balloon_handler(QEMUBalloonEvent *event_func,
|
|||||||
/* We're already registered one balloon handler. How many can
|
/* We're already registered one balloon handler. How many can
|
||||||
* a guest really have?
|
* a guest really have?
|
||||||
*/
|
*/
|
||||||
|
error_report("Another balloon device already registered");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
balloon_event_fn = event_func;
|
balloon_event_fn = event_func;
|
||||||
@@ -88,30 +62,71 @@ void qemu_remove_balloon_handler(void *opaque)
|
|||||||
balloon_opaque = NULL;
|
balloon_opaque = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int qemu_balloon(ram_addr_t target)
|
||||||
|
{
|
||||||
|
if (!balloon_event_fn) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
trace_balloon_event(balloon_opaque, target);
|
||||||
|
balloon_event_fn(balloon_opaque, target);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int qemu_balloon_status(BalloonInfo *info)
|
||||||
|
{
|
||||||
|
if (!balloon_stat_fn) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
balloon_stat_fn(balloon_opaque, info);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void qemu_balloon_changed(int64_t actual)
|
||||||
|
{
|
||||||
|
QObject *data;
|
||||||
|
|
||||||
|
data = qobject_from_jsonf("{ 'actual': %" PRId64 " }",
|
||||||
|
actual);
|
||||||
|
|
||||||
|
monitor_protocol_event(QEVENT_BALLOON_CHANGE, data);
|
||||||
|
|
||||||
|
qobject_decref(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
BalloonInfo *qmp_query_balloon(Error **errp)
|
BalloonInfo *qmp_query_balloon(Error **errp)
|
||||||
{
|
{
|
||||||
BalloonInfo *info;
|
BalloonInfo *info;
|
||||||
|
|
||||||
if (!have_balloon(errp)) {
|
if (kvm_enabled() && !kvm_has_sync_mmu()) {
|
||||||
|
error_set(errp, QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
info = g_malloc0(sizeof(*info));
|
info = g_malloc0(sizeof(*info));
|
||||||
balloon_stat_fn(balloon_opaque, info);
|
|
||||||
|
if (qemu_balloon_status(info) == 0) {
|
||||||
|
error_set(errp, QERR_DEVICE_NOT_ACTIVE, "balloon");
|
||||||
|
qapi_free_BalloonInfo(info);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
return info;
|
return info;
|
||||||
}
|
}
|
||||||
|
|
||||||
void qmp_balloon(int64_t target, Error **errp)
|
void qmp_balloon(int64_t value, Error **errp)
|
||||||
{
|
{
|
||||||
if (!have_balloon(errp)) {
|
if (kvm_enabled() && !kvm_has_sync_mmu()) {
|
||||||
|
error_set(errp, QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (target <= 0) {
|
if (value <= 0) {
|
||||||
error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "target", "a size");
|
error_set(errp, QERR_INVALID_PARAMETER_VALUE, "target", "a size");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
trace_balloon_event(balloon_opaque, target);
|
if (qemu_balloon(value) == 0) {
|
||||||
balloon_event_fn(balloon_opaque, target);
|
error_set(errp, QERR_DEVICE_NOT_ACTIVE, "balloon");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,16 +14,13 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "qemu-common.h"
|
#include "qemu-common.h"
|
||||||
#include "block/block.h"
|
#include "block/block_int.h"
|
||||||
#include "qemu/error-report.h"
|
|
||||||
#include "qemu/main-loop.h"
|
|
||||||
#include "hw/hw.h"
|
#include "hw/hw.h"
|
||||||
#include "qemu/queue.h"
|
#include "qemu/queue.h"
|
||||||
#include "qemu/timer.h"
|
#include "qemu/timer.h"
|
||||||
#include "migration/block.h"
|
#include "migration/block.h"
|
||||||
#include "migration/migration.h"
|
#include "migration/migration.h"
|
||||||
#include "sysemu/blockdev.h"
|
#include "sysemu/blockdev.h"
|
||||||
#include "sysemu/block-backend.h"
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
#define BLOCK_SIZE (1 << 20)
|
#define BLOCK_SIZE (1 << 20)
|
||||||
@@ -32,12 +29,9 @@
|
|||||||
#define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
|
#define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
|
||||||
#define BLK_MIG_FLAG_EOS 0x02
|
#define BLK_MIG_FLAG_EOS 0x02
|
||||||
#define BLK_MIG_FLAG_PROGRESS 0x04
|
#define BLK_MIG_FLAG_PROGRESS 0x04
|
||||||
#define BLK_MIG_FLAG_ZERO_BLOCK 0x08
|
|
||||||
|
|
||||||
#define MAX_IS_ALLOCATED_SEARCH 65536
|
#define MAX_IS_ALLOCATED_SEARCH 65536
|
||||||
|
|
||||||
#define MAX_INFLIGHT_IO 512
|
|
||||||
|
|
||||||
//#define DEBUG_BLK_MIGRATION
|
//#define DEBUG_BLK_MIGRATION
|
||||||
|
|
||||||
#ifdef DEBUG_BLK_MIGRATION
|
#ifdef DEBUG_BLK_MIGRATION
|
||||||
@@ -49,103 +43,58 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef struct BlkMigDevState {
|
typedef struct BlkMigDevState {
|
||||||
/* Written during setup phase. Can be read without a lock. */
|
|
||||||
BlockDriverState *bs;
|
BlockDriverState *bs;
|
||||||
int shared_base;
|
|
||||||
int64_t total_sectors;
|
|
||||||
QSIMPLEQ_ENTRY(BlkMigDevState) entry;
|
|
||||||
|
|
||||||
/* Only used by migration thread. Does not need a lock. */
|
|
||||||
int bulk_completed;
|
int bulk_completed;
|
||||||
|
int shared_base;
|
||||||
int64_t cur_sector;
|
int64_t cur_sector;
|
||||||
int64_t cur_dirty;
|
int64_t cur_dirty;
|
||||||
|
|
||||||
/* Protected by block migration lock. */
|
|
||||||
unsigned long *aio_bitmap;
|
|
||||||
int64_t completed_sectors;
|
int64_t completed_sectors;
|
||||||
BdrvDirtyBitmap *dirty_bitmap;
|
int64_t total_sectors;
|
||||||
Error *blocker;
|
int64_t dirty;
|
||||||
|
QSIMPLEQ_ENTRY(BlkMigDevState) entry;
|
||||||
|
unsigned long *aio_bitmap;
|
||||||
} BlkMigDevState;
|
} BlkMigDevState;
|
||||||
|
|
||||||
typedef struct BlkMigBlock {
|
typedef struct BlkMigBlock {
|
||||||
/* Only used by migration thread. */
|
|
||||||
uint8_t *buf;
|
uint8_t *buf;
|
||||||
BlkMigDevState *bmds;
|
BlkMigDevState *bmds;
|
||||||
int64_t sector;
|
int64_t sector;
|
||||||
int nr_sectors;
|
int nr_sectors;
|
||||||
struct iovec iov;
|
struct iovec iov;
|
||||||
QEMUIOVector qiov;
|
QEMUIOVector qiov;
|
||||||
BlockAIOCB *aiocb;
|
BlockDriverAIOCB *aiocb;
|
||||||
|
|
||||||
/* Protected by block migration lock. */
|
|
||||||
int ret;
|
int ret;
|
||||||
QSIMPLEQ_ENTRY(BlkMigBlock) entry;
|
QSIMPLEQ_ENTRY(BlkMigBlock) entry;
|
||||||
} BlkMigBlock;
|
} BlkMigBlock;
|
||||||
|
|
||||||
typedef struct BlkMigState {
|
typedef struct BlkMigState {
|
||||||
/* Written during setup phase. Can be read without a lock. */
|
|
||||||
int blk_enable;
|
int blk_enable;
|
||||||
int shared_base;
|
int shared_base;
|
||||||
QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list;
|
QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list;
|
||||||
int64_t total_sector_sum;
|
|
||||||
bool zero_blocks;
|
|
||||||
|
|
||||||
/* Protected by lock. */
|
|
||||||
QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list;
|
QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list;
|
||||||
int submitted;
|
int submitted;
|
||||||
int read_done;
|
int read_done;
|
||||||
|
|
||||||
/* Only used by migration thread. Does not need a lock. */
|
|
||||||
int transferred;
|
int transferred;
|
||||||
|
int64_t total_sector_sum;
|
||||||
int prev_progress;
|
int prev_progress;
|
||||||
int bulk_completed;
|
int bulk_completed;
|
||||||
|
long double prev_time_offset;
|
||||||
/* Lock must be taken _inside_ the iothread lock. */
|
|
||||||
QemuMutex lock;
|
|
||||||
} BlkMigState;
|
} BlkMigState;
|
||||||
|
|
||||||
static BlkMigState block_mig_state;
|
static BlkMigState block_mig_state;
|
||||||
|
|
||||||
static void blk_mig_lock(void)
|
|
||||||
{
|
|
||||||
qemu_mutex_lock(&block_mig_state.lock);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void blk_mig_unlock(void)
|
|
||||||
{
|
|
||||||
qemu_mutex_unlock(&block_mig_state.lock);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Must run outside of the iothread lock during the bulk phase,
|
|
||||||
* or the VM will stall.
|
|
||||||
*/
|
|
||||||
|
|
||||||
static void blk_send(QEMUFile *f, BlkMigBlock * blk)
|
static void blk_send(QEMUFile *f, BlkMigBlock * blk)
|
||||||
{
|
{
|
||||||
int len;
|
int len;
|
||||||
uint64_t flags = BLK_MIG_FLAG_DEVICE_BLOCK;
|
|
||||||
|
|
||||||
if (block_mig_state.zero_blocks &&
|
|
||||||
buffer_is_zero(blk->buf, BLOCK_SIZE)) {
|
|
||||||
flags |= BLK_MIG_FLAG_ZERO_BLOCK;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* sector number and flags */
|
/* sector number and flags */
|
||||||
qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
|
qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
|
||||||
| flags);
|
| BLK_MIG_FLAG_DEVICE_BLOCK);
|
||||||
|
|
||||||
/* device name */
|
/* device name */
|
||||||
len = strlen(bdrv_get_device_name(blk->bmds->bs));
|
len = strlen(blk->bmds->bs->device_name);
|
||||||
qemu_put_byte(f, len);
|
qemu_put_byte(f, len);
|
||||||
qemu_put_buffer(f, (uint8_t *)bdrv_get_device_name(blk->bmds->bs), len);
|
qemu_put_buffer(f, (uint8_t *)blk->bmds->bs->device_name, len);
|
||||||
|
|
||||||
/* if a block is zero we need to flush here since the network
|
|
||||||
* bandwidth is now a lot higher than the storage device bandwidth.
|
|
||||||
* thus if we queue zero blocks we slow down the migration */
|
|
||||||
if (flags & BLK_MIG_FLAG_ZERO_BLOCK) {
|
|
||||||
qemu_fflush(f);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
qemu_put_buffer(f, blk->buf, BLOCK_SIZE);
|
qemu_put_buffer(f, blk->buf, BLOCK_SIZE);
|
||||||
}
|
}
|
||||||
@@ -160,11 +109,9 @@ uint64_t blk_mig_bytes_transferred(void)
|
|||||||
BlkMigDevState *bmds;
|
BlkMigDevState *bmds;
|
||||||
uint64_t sum = 0;
|
uint64_t sum = 0;
|
||||||
|
|
||||||
blk_mig_lock();
|
|
||||||
QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
|
QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
|
||||||
sum += bmds->completed_sectors;
|
sum += bmds->completed_sectors;
|
||||||
}
|
}
|
||||||
blk_mig_unlock();
|
|
||||||
return sum << BDRV_SECTOR_BITS;
|
return sum << BDRV_SECTOR_BITS;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,14 +131,11 @@ uint64_t blk_mig_bytes_total(void)
|
|||||||
return sum << BDRV_SECTOR_BITS;
|
return sum << BDRV_SECTOR_BITS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Called with migration lock held. */
|
|
||||||
|
|
||||||
static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector)
|
static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector)
|
||||||
{
|
{
|
||||||
int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
|
int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
|
||||||
|
|
||||||
if (sector < bdrv_nb_sectors(bmds->bs)) {
|
if ((sector << BDRV_SECTOR_BITS) < bdrv_getlength(bmds->bs)) {
|
||||||
return !!(bmds->aio_bitmap[chunk / (sizeof(unsigned long) * 8)] &
|
return !!(bmds->aio_bitmap[chunk / (sizeof(unsigned long) * 8)] &
|
||||||
(1UL << (chunk % (sizeof(unsigned long) * 8))));
|
(1UL << (chunk % (sizeof(unsigned long) * 8))));
|
||||||
} else {
|
} else {
|
||||||
@@ -199,8 +143,6 @@ static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Called with migration lock held. */
|
|
||||||
|
|
||||||
static void bmds_set_aio_inflight(BlkMigDevState *bmds, int64_t sector_num,
|
static void bmds_set_aio_inflight(BlkMigDevState *bmds, int64_t sector_num,
|
||||||
int nb_sectors, int set)
|
int nb_sectors, int set)
|
||||||
{
|
{
|
||||||
@@ -228,32 +170,30 @@ static void alloc_aio_bitmap(BlkMigDevState *bmds)
|
|||||||
BlockDriverState *bs = bmds->bs;
|
BlockDriverState *bs = bmds->bs;
|
||||||
int64_t bitmap_size;
|
int64_t bitmap_size;
|
||||||
|
|
||||||
bitmap_size = bdrv_nb_sectors(bs) + BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
|
bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
|
||||||
|
BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
|
||||||
bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
|
bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
|
||||||
|
|
||||||
bmds->aio_bitmap = g_malloc0(bitmap_size);
|
bmds->aio_bitmap = g_malloc0(bitmap_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Never hold migration lock when yielding to the main loop! */
|
|
||||||
|
|
||||||
static void blk_mig_read_cb(void *opaque, int ret)
|
static void blk_mig_read_cb(void *opaque, int ret)
|
||||||
{
|
{
|
||||||
|
long double curr_time = qemu_get_clock_ns(rt_clock);
|
||||||
BlkMigBlock *blk = opaque;
|
BlkMigBlock *blk = opaque;
|
||||||
|
|
||||||
blk_mig_lock();
|
|
||||||
blk->ret = ret;
|
blk->ret = ret;
|
||||||
|
|
||||||
|
block_mig_state.prev_time_offset = curr_time;
|
||||||
|
|
||||||
QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
|
QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
|
||||||
bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0);
|
bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0);
|
||||||
|
|
||||||
block_mig_state.submitted--;
|
block_mig_state.submitted--;
|
||||||
block_mig_state.read_done++;
|
block_mig_state.read_done++;
|
||||||
assert(block_mig_state.submitted >= 0);
|
assert(block_mig_state.submitted >= 0);
|
||||||
blk_mig_unlock();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Called with no lock taken. */
|
|
||||||
|
|
||||||
static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
|
static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
|
||||||
{
|
{
|
||||||
int64_t total_sectors = bmds->total_sectors;
|
int64_t total_sectors = bmds->total_sectors;
|
||||||
@@ -263,13 +203,11 @@ static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
|
|||||||
int nr_sectors;
|
int nr_sectors;
|
||||||
|
|
||||||
if (bmds->shared_base) {
|
if (bmds->shared_base) {
|
||||||
qemu_mutex_lock_iothread();
|
|
||||||
while (cur_sector < total_sectors &&
|
while (cur_sector < total_sectors &&
|
||||||
!bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH,
|
!bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH,
|
||||||
&nr_sectors)) {
|
&nr_sectors)) {
|
||||||
cur_sector += nr_sectors;
|
cur_sector += nr_sectors;
|
||||||
}
|
}
|
||||||
qemu_mutex_unlock_iothread();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cur_sector >= total_sectors) {
|
if (cur_sector >= total_sectors) {
|
||||||
@@ -288,7 +226,7 @@ static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
|
|||||||
nr_sectors = total_sectors - cur_sector;
|
nr_sectors = total_sectors - cur_sector;
|
||||||
}
|
}
|
||||||
|
|
||||||
blk = g_new(BlkMigBlock, 1);
|
blk = g_malloc(sizeof(BlkMigBlock));
|
||||||
blk->buf = g_malloc(BLOCK_SIZE);
|
blk->buf = g_malloc(BLOCK_SIZE);
|
||||||
blk->bmds = bmds;
|
blk->bmds = bmds;
|
||||||
blk->sector = cur_sector;
|
blk->sector = cur_sector;
|
||||||
@@ -298,105 +236,74 @@ static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
|
|||||||
blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
|
blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
|
||||||
qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
|
qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
|
||||||
|
|
||||||
blk_mig_lock();
|
if (block_mig_state.submitted == 0) {
|
||||||
block_mig_state.submitted++;
|
block_mig_state.prev_time_offset = qemu_get_clock_ns(rt_clock);
|
||||||
blk_mig_unlock();
|
}
|
||||||
|
|
||||||
qemu_mutex_lock_iothread();
|
|
||||||
blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
|
blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
|
||||||
nr_sectors, blk_mig_read_cb, blk);
|
nr_sectors, blk_mig_read_cb, blk);
|
||||||
|
block_mig_state.submitted++;
|
||||||
|
|
||||||
bdrv_reset_dirty_bitmap(bmds->dirty_bitmap, cur_sector, nr_sectors);
|
bdrv_reset_dirty(bs, cur_sector, nr_sectors);
|
||||||
qemu_mutex_unlock_iothread();
|
|
||||||
|
|
||||||
bmds->cur_sector = cur_sector + nr_sectors;
|
bmds->cur_sector = cur_sector + nr_sectors;
|
||||||
|
|
||||||
return (bmds->cur_sector >= total_sectors);
|
return (bmds->cur_sector >= total_sectors);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Called with iothread lock taken. */
|
static void set_dirty_tracking(int enable)
|
||||||
|
|
||||||
static int set_dirty_tracking(void)
|
|
||||||
{
|
|
||||||
BlkMigDevState *bmds;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
|
|
||||||
bmds->dirty_bitmap = bdrv_create_dirty_bitmap(bmds->bs, BLOCK_SIZE,
|
|
||||||
NULL, NULL);
|
|
||||||
if (!bmds->dirty_bitmap) {
|
|
||||||
ret = -errno;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
fail:
|
|
||||||
QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
|
|
||||||
if (bmds->dirty_bitmap) {
|
|
||||||
bdrv_release_dirty_bitmap(bmds->bs, bmds->dirty_bitmap);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void unset_dirty_tracking(void)
|
|
||||||
{
|
{
|
||||||
BlkMigDevState *bmds;
|
BlkMigDevState *bmds;
|
||||||
|
|
||||||
QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
|
QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
|
||||||
bdrv_release_dirty_bitmap(bmds->bs, bmds->dirty_bitmap);
|
bdrv_set_dirty_tracking(bmds->bs, enable ? BLOCK_SIZE : 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void init_blk_migration(QEMUFile *f)
|
static void init_blk_migration_it(void *opaque, BlockDriverState *bs)
|
||||||
{
|
{
|
||||||
BlockDriverState *bs;
|
|
||||||
BlkMigDevState *bmds;
|
BlkMigDevState *bmds;
|
||||||
int64_t sectors;
|
int64_t sectors;
|
||||||
|
|
||||||
block_mig_state.submitted = 0;
|
if (!bdrv_is_read_only(bs)) {
|
||||||
block_mig_state.read_done = 0;
|
sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
|
||||||
block_mig_state.transferred = 0;
|
|
||||||
block_mig_state.total_sector_sum = 0;
|
|
||||||
block_mig_state.prev_progress = -1;
|
|
||||||
block_mig_state.bulk_completed = 0;
|
|
||||||
block_mig_state.zero_blocks = migrate_zero_blocks();
|
|
||||||
|
|
||||||
for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
|
|
||||||
if (bdrv_is_read_only(bs)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
sectors = bdrv_nb_sectors(bs);
|
|
||||||
if (sectors <= 0) {
|
if (sectors <= 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
bmds = g_new0(BlkMigDevState, 1);
|
bmds = g_malloc0(sizeof(BlkMigDevState));
|
||||||
bmds->bs = bs;
|
bmds->bs = bs;
|
||||||
bmds->bulk_completed = 0;
|
bmds->bulk_completed = 0;
|
||||||
bmds->total_sectors = sectors;
|
bmds->total_sectors = sectors;
|
||||||
bmds->completed_sectors = 0;
|
bmds->completed_sectors = 0;
|
||||||
bmds->shared_base = block_mig_state.shared_base;
|
bmds->shared_base = block_mig_state.shared_base;
|
||||||
alloc_aio_bitmap(bmds);
|
alloc_aio_bitmap(bmds);
|
||||||
error_setg(&bmds->blocker, "block device is in use by migration");
|
drive_get_ref(drive_get_by_blockdev(bs));
|
||||||
bdrv_op_block_all(bs, bmds->blocker);
|
bdrv_set_in_use(bs, 1);
|
||||||
bdrv_ref(bs);
|
|
||||||
|
|
||||||
block_mig_state.total_sector_sum += sectors;
|
block_mig_state.total_sector_sum += sectors;
|
||||||
|
|
||||||
if (bmds->shared_base) {
|
if (bmds->shared_base) {
|
||||||
DPRINTF("Start migration for %s with shared base image\n",
|
DPRINTF("Start migration for %s with shared base image\n",
|
||||||
bdrv_get_device_name(bs));
|
bs->device_name);
|
||||||
} else {
|
} else {
|
||||||
DPRINTF("Start full migration for %s\n", bdrv_get_device_name(bs));
|
DPRINTF("Start full migration for %s\n", bs->device_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
|
QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Called with no lock taken. */
|
static void init_blk_migration(QEMUFile *f)
|
||||||
|
{
|
||||||
|
block_mig_state.submitted = 0;
|
||||||
|
block_mig_state.read_done = 0;
|
||||||
|
block_mig_state.transferred = 0;
|
||||||
|
block_mig_state.total_sector_sum = 0;
|
||||||
|
block_mig_state.prev_progress = -1;
|
||||||
|
block_mig_state.bulk_completed = 0;
|
||||||
|
|
||||||
|
bdrv_iterate(init_blk_migration_it, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
static int blk_mig_save_bulked_block(QEMUFile *f)
|
static int blk_mig_save_bulked_block(QEMUFile *f)
|
||||||
{
|
{
|
||||||
@@ -444,8 +351,6 @@ static void blk_mig_reset_dirty_cursor(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Called with iothread lock taken. */
|
|
||||||
|
|
||||||
static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds,
|
static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds,
|
||||||
int is_async)
|
int is_async)
|
||||||
{
|
{
|
||||||
@@ -456,21 +361,17 @@ static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds,
|
|||||||
int ret = -EIO;
|
int ret = -EIO;
|
||||||
|
|
||||||
for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {
|
for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {
|
||||||
blk_mig_lock();
|
|
||||||
if (bmds_aio_inflight(bmds, sector)) {
|
if (bmds_aio_inflight(bmds, sector)) {
|
||||||
blk_mig_unlock();
|
bdrv_drain_all();
|
||||||
bdrv_drain(bmds->bs);
|
|
||||||
} else {
|
|
||||||
blk_mig_unlock();
|
|
||||||
}
|
}
|
||||||
if (bdrv_get_dirty(bmds->bs, bmds->dirty_bitmap, sector)) {
|
if (bdrv_get_dirty(bmds->bs, sector)) {
|
||||||
|
|
||||||
if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
|
if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
|
||||||
nr_sectors = total_sectors - sector;
|
nr_sectors = total_sectors - sector;
|
||||||
} else {
|
} else {
|
||||||
nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
|
nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
|
||||||
}
|
}
|
||||||
blk = g_new(BlkMigBlock, 1);
|
blk = g_malloc(sizeof(BlkMigBlock));
|
||||||
blk->buf = g_malloc(BLOCK_SIZE);
|
blk->buf = g_malloc(BLOCK_SIZE);
|
||||||
blk->bmds = bmds;
|
blk->bmds = bmds;
|
||||||
blk->sector = sector;
|
blk->sector = sector;
|
||||||
@@ -481,13 +382,14 @@ static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds,
|
|||||||
blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
|
blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
|
||||||
qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
|
qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
|
||||||
|
|
||||||
|
if (block_mig_state.submitted == 0) {
|
||||||
|
block_mig_state.prev_time_offset = qemu_get_clock_ns(rt_clock);
|
||||||
|
}
|
||||||
|
|
||||||
blk->aiocb = bdrv_aio_readv(bmds->bs, sector, &blk->qiov,
|
blk->aiocb = bdrv_aio_readv(bmds->bs, sector, &blk->qiov,
|
||||||
nr_sectors, blk_mig_read_cb, blk);
|
nr_sectors, blk_mig_read_cb, blk);
|
||||||
|
|
||||||
blk_mig_lock();
|
|
||||||
block_mig_state.submitted++;
|
block_mig_state.submitted++;
|
||||||
bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);
|
bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);
|
||||||
blk_mig_unlock();
|
|
||||||
} else {
|
} else {
|
||||||
ret = bdrv_read(bmds->bs, sector, blk->buf, nr_sectors);
|
ret = bdrv_read(bmds->bs, sector, blk->buf, nr_sectors);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
@@ -499,7 +401,7 @@ static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds,
|
|||||||
g_free(blk);
|
g_free(blk);
|
||||||
}
|
}
|
||||||
|
|
||||||
bdrv_reset_dirty_bitmap(bmds->dirty_bitmap, sector, nr_sectors);
|
bdrv_reset_dirty(bmds->bs, sector, nr_sectors);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
|
sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
|
||||||
@@ -515,9 +417,7 @@ error:
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Called with iothread lock taken.
|
/* return value:
|
||||||
*
|
|
||||||
* return value:
|
|
||||||
* 0: too much data for max_downtime
|
* 0: too much data for max_downtime
|
||||||
* 1: few enough data for max_downtime
|
* 1: few enough data for max_downtime
|
||||||
*/
|
*/
|
||||||
@@ -536,8 +436,6 @@ static int blk_mig_save_dirty_block(QEMUFile *f, int is_async)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Called with no locks taken. */
|
|
||||||
|
|
||||||
static int flush_blks(QEMUFile *f)
|
static int flush_blks(QEMUFile *f)
|
||||||
{
|
{
|
||||||
BlkMigBlock *blk;
|
BlkMigBlock *blk;
|
||||||
@@ -547,7 +445,6 @@ static int flush_blks(QEMUFile *f)
|
|||||||
__FUNCTION__, block_mig_state.submitted, block_mig_state.read_done,
|
__FUNCTION__, block_mig_state.submitted, block_mig_state.read_done,
|
||||||
block_mig_state.transferred);
|
block_mig_state.transferred);
|
||||||
|
|
||||||
blk_mig_lock();
|
|
||||||
while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
|
while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
|
||||||
if (qemu_file_rate_limit(f)) {
|
if (qemu_file_rate_limit(f)) {
|
||||||
break;
|
break;
|
||||||
@@ -556,12 +453,9 @@ static int flush_blks(QEMUFile *f)
|
|||||||
ret = blk->ret;
|
ret = blk->ret;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
blk_send(f, blk);
|
||||||
|
|
||||||
QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
|
QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
|
||||||
blk_mig_unlock();
|
|
||||||
blk_send(f, blk);
|
|
||||||
blk_mig_lock();
|
|
||||||
|
|
||||||
g_free(blk->buf);
|
g_free(blk->buf);
|
||||||
g_free(blk);
|
g_free(blk);
|
||||||
|
|
||||||
@@ -569,7 +463,6 @@ static int flush_blks(QEMUFile *f)
|
|||||||
block_mig_state.transferred++;
|
block_mig_state.transferred++;
|
||||||
assert(block_mig_state.read_done >= 0);
|
assert(block_mig_state.read_done >= 0);
|
||||||
}
|
}
|
||||||
blk_mig_unlock();
|
|
||||||
|
|
||||||
DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__,
|
DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__,
|
||||||
block_mig_state.submitted, block_mig_state.read_done,
|
block_mig_state.submitted, block_mig_state.read_done,
|
||||||
@@ -577,37 +470,31 @@ static int flush_blks(QEMUFile *f)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Called with iothread lock taken. */
|
|
||||||
|
|
||||||
static int64_t get_remaining_dirty(void)
|
static int64_t get_remaining_dirty(void)
|
||||||
{
|
{
|
||||||
BlkMigDevState *bmds;
|
BlkMigDevState *bmds;
|
||||||
int64_t dirty = 0;
|
int64_t dirty = 0;
|
||||||
|
|
||||||
QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
|
QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
|
||||||
dirty += bdrv_get_dirty_count(bmds->dirty_bitmap);
|
dirty += bdrv_get_dirty_count(bmds->bs);
|
||||||
}
|
}
|
||||||
|
|
||||||
return dirty << BDRV_SECTOR_BITS;
|
return dirty << BDRV_SECTOR_BITS;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Called with iothread lock taken. */
|
static void blk_mig_cleanup(void)
|
||||||
|
|
||||||
static void block_migration_cleanup(void *opaque)
|
|
||||||
{
|
{
|
||||||
BlkMigDevState *bmds;
|
BlkMigDevState *bmds;
|
||||||
BlkMigBlock *blk;
|
BlkMigBlock *blk;
|
||||||
|
|
||||||
bdrv_drain_all();
|
bdrv_drain_all();
|
||||||
|
|
||||||
unset_dirty_tracking();
|
set_dirty_tracking(0);
|
||||||
|
|
||||||
blk_mig_lock();
|
|
||||||
while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
|
while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
|
||||||
QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
|
QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
|
||||||
bdrv_op_unblock_all(bmds->bs, bmds->blocker);
|
bdrv_set_in_use(bmds->bs, 0);
|
||||||
error_free(bmds->blocker);
|
drive_put_ref(drive_get_by_blockdev(bmds->bs));
|
||||||
bdrv_unref(bmds->bs);
|
|
||||||
g_free(bmds->aio_bitmap);
|
g_free(bmds->aio_bitmap);
|
||||||
g_free(bmds);
|
g_free(bmds);
|
||||||
}
|
}
|
||||||
@@ -617,7 +504,11 @@ static void block_migration_cleanup(void *opaque)
|
|||||||
g_free(blk->buf);
|
g_free(blk->buf);
|
||||||
g_free(blk);
|
g_free(blk);
|
||||||
}
|
}
|
||||||
blk_mig_unlock();
|
}
|
||||||
|
|
||||||
|
static void block_migration_cancel(void *opaque)
|
||||||
|
{
|
||||||
|
blk_mig_cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
static int block_save_setup(QEMUFile *f, void *opaque)
|
static int block_save_setup(QEMUFile *f, void *opaque)
|
||||||
@@ -627,94 +518,73 @@ static int block_save_setup(QEMUFile *f, void *opaque)
|
|||||||
DPRINTF("Enter save live setup submitted %d transferred %d\n",
|
DPRINTF("Enter save live setup submitted %d transferred %d\n",
|
||||||
block_mig_state.submitted, block_mig_state.transferred);
|
block_mig_state.submitted, block_mig_state.transferred);
|
||||||
|
|
||||||
qemu_mutex_lock_iothread();
|
|
||||||
init_blk_migration(f);
|
init_blk_migration(f);
|
||||||
|
|
||||||
/* start track dirty blocks */
|
/* start track dirty blocks */
|
||||||
ret = set_dirty_tracking();
|
set_dirty_tracking(1);
|
||||||
|
|
||||||
|
ret = flush_blks(f);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
qemu_mutex_unlock_iothread();
|
blk_mig_cleanup();
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
qemu_mutex_unlock_iothread();
|
|
||||||
|
|
||||||
ret = flush_blks(f);
|
|
||||||
blk_mig_reset_dirty_cursor();
|
blk_mig_reset_dirty_cursor();
|
||||||
|
|
||||||
qemu_put_be64(f, BLK_MIG_FLAG_EOS);
|
qemu_put_be64(f, BLK_MIG_FLAG_EOS);
|
||||||
|
|
||||||
return ret;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int block_save_iterate(QEMUFile *f, void *opaque)
|
static int block_save_iterate(QEMUFile *f, void *opaque)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
int64_t last_ftell = qemu_ftell(f);
|
int64_t last_ftell = qemu_ftell(f);
|
||||||
int64_t delta_ftell;
|
|
||||||
|
|
||||||
DPRINTF("Enter save live iterate submitted %d transferred %d\n",
|
DPRINTF("Enter save live iterate submitted %d transferred %d\n",
|
||||||
block_mig_state.submitted, block_mig_state.transferred);
|
block_mig_state.submitted, block_mig_state.transferred);
|
||||||
|
|
||||||
ret = flush_blks(f);
|
ret = flush_blks(f);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
|
blk_mig_cleanup();
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
blk_mig_reset_dirty_cursor();
|
blk_mig_reset_dirty_cursor();
|
||||||
|
|
||||||
/* control the rate of transfer */
|
/* control the rate of transfer */
|
||||||
blk_mig_lock();
|
|
||||||
while ((block_mig_state.submitted +
|
while ((block_mig_state.submitted +
|
||||||
block_mig_state.read_done) * BLOCK_SIZE <
|
block_mig_state.read_done) * BLOCK_SIZE <
|
||||||
qemu_file_get_rate_limit(f) &&
|
qemu_file_get_rate_limit(f)) {
|
||||||
(block_mig_state.submitted +
|
|
||||||
block_mig_state.read_done) <
|
|
||||||
MAX_INFLIGHT_IO) {
|
|
||||||
blk_mig_unlock();
|
|
||||||
if (block_mig_state.bulk_completed == 0) {
|
if (block_mig_state.bulk_completed == 0) {
|
||||||
/* first finish the bulk phase */
|
/* first finish the bulk phase */
|
||||||
if (blk_mig_save_bulked_block(f) == 0) {
|
if (blk_mig_save_bulked_block(f) == 0) {
|
||||||
/* finished saving bulk on all devices */
|
/* finished saving bulk on all devices */
|
||||||
block_mig_state.bulk_completed = 1;
|
block_mig_state.bulk_completed = 1;
|
||||||
}
|
}
|
||||||
ret = 0;
|
|
||||||
} else {
|
} else {
|
||||||
/* Always called with iothread lock taken for
|
|
||||||
* simplicity, block_save_complete also calls it.
|
|
||||||
*/
|
|
||||||
qemu_mutex_lock_iothread();
|
|
||||||
ret = blk_mig_save_dirty_block(f, 1);
|
ret = blk_mig_save_dirty_block(f, 1);
|
||||||
qemu_mutex_unlock_iothread();
|
|
||||||
}
|
|
||||||
if (ret < 0) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
blk_mig_lock();
|
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
/* no more dirty blocks */
|
/* no more dirty blocks */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
blk_mig_unlock();
|
}
|
||||||
|
if (ret < 0) {
|
||||||
|
blk_mig_cleanup();
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
ret = flush_blks(f);
|
ret = flush_blks(f);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
|
blk_mig_cleanup();
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
qemu_put_be64(f, BLK_MIG_FLAG_EOS);
|
qemu_put_be64(f, BLK_MIG_FLAG_EOS);
|
||||||
delta_ftell = qemu_ftell(f) - last_ftell;
|
|
||||||
if (delta_ftell > 0) {
|
|
||||||
return 1;
|
|
||||||
} else if (delta_ftell < 0) {
|
|
||||||
return -1;
|
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Called with iothread lock taken. */
|
return qemu_ftell(f) - last_ftell;
|
||||||
|
}
|
||||||
|
|
||||||
static int block_save_complete(QEMUFile *f, void *opaque)
|
static int block_save_complete(QEMUFile *f, void *opaque)
|
||||||
{
|
{
|
||||||
@@ -725,6 +595,7 @@ static int block_save_complete(QEMUFile *f, void *opaque)
|
|||||||
|
|
||||||
ret = flush_blks(f);
|
ret = flush_blks(f);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
|
blk_mig_cleanup();
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -732,17 +603,16 @@ static int block_save_complete(QEMUFile *f, void *opaque)
|
|||||||
|
|
||||||
/* we know for sure that save bulk is completed and
|
/* we know for sure that save bulk is completed and
|
||||||
all async read completed */
|
all async read completed */
|
||||||
blk_mig_lock();
|
|
||||||
assert(block_mig_state.submitted == 0);
|
assert(block_mig_state.submitted == 0);
|
||||||
blk_mig_unlock();
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
ret = blk_mig_save_dirty_block(f, 0);
|
ret = blk_mig_save_dirty_block(f, 0);
|
||||||
|
} while (ret == 0);
|
||||||
|
|
||||||
|
blk_mig_cleanup();
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
} while (ret == 0);
|
|
||||||
|
|
||||||
/* report completion */
|
/* report completion */
|
||||||
qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
|
qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
|
||||||
|
|
||||||
@@ -753,29 +623,20 @@ static int block_save_complete(QEMUFile *f, void *opaque)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void block_save_pending(QEMUFile *f, void *opaque, uint64_t max_size,
|
static uint64_t block_save_pending(QEMUFile *f, void *opaque, uint64_t max_size)
|
||||||
uint64_t *non_postcopiable_pending,
|
|
||||||
uint64_t *postcopiable_pending)
|
|
||||||
{
|
{
|
||||||
/* Estimate pending number of bytes to send */
|
/* Estimate pending number of bytes to send */
|
||||||
uint64_t pending;
|
uint64_t pending = get_remaining_dirty() +
|
||||||
|
|
||||||
qemu_mutex_lock_iothread();
|
|
||||||
blk_mig_lock();
|
|
||||||
pending = get_remaining_dirty() +
|
|
||||||
block_mig_state.submitted * BLOCK_SIZE +
|
block_mig_state.submitted * BLOCK_SIZE +
|
||||||
block_mig_state.read_done * BLOCK_SIZE;
|
block_mig_state.read_done * BLOCK_SIZE;
|
||||||
|
|
||||||
/* Report at least one block pending during bulk phase */
|
/* Report at least one block pending during bulk phase */
|
||||||
if (pending <= max_size && !block_mig_state.bulk_completed) {
|
if (pending == 0 && !block_mig_state.bulk_completed) {
|
||||||
pending = max_size + BLOCK_SIZE;
|
pending = BLOCK_SIZE;
|
||||||
}
|
}
|
||||||
blk_mig_unlock();
|
|
||||||
qemu_mutex_unlock_iothread();
|
|
||||||
|
|
||||||
DPRINTF("Enter save live pending %" PRIu64 "\n", pending);
|
DPRINTF("Enter save live pending %" PRIu64 "\n", pending);
|
||||||
/* We don't do postcopy */
|
return pending;
|
||||||
*non_postcopiable_pending += pending;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int block_load(QEMUFile *f, void *opaque, int version_id)
|
static int block_load(QEMUFile *f, void *opaque, int version_id)
|
||||||
@@ -785,7 +646,6 @@ static int block_load(QEMUFile *f, void *opaque, int version_id)
|
|||||||
char device_name[256];
|
char device_name[256];
|
||||||
int64_t addr;
|
int64_t addr;
|
||||||
BlockDriverState *bs, *bs_prev = NULL;
|
BlockDriverState *bs, *bs_prev = NULL;
|
||||||
BlockBackend *blk;
|
|
||||||
uint8_t *buf;
|
uint8_t *buf;
|
||||||
int64_t total_sectors = 0;
|
int64_t total_sectors = 0;
|
||||||
int nr_sectors;
|
int nr_sectors;
|
||||||
@@ -803,22 +663,16 @@ static int block_load(QEMUFile *f, void *opaque, int version_id)
|
|||||||
qemu_get_buffer(f, (uint8_t *)device_name, len);
|
qemu_get_buffer(f, (uint8_t *)device_name, len);
|
||||||
device_name[len] = '\0';
|
device_name[len] = '\0';
|
||||||
|
|
||||||
blk = blk_by_name(device_name);
|
bs = bdrv_find(device_name);
|
||||||
if (!blk) {
|
|
||||||
fprintf(stderr, "Error unknown block device %s\n",
|
|
||||||
device_name);
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
bs = blk_bs(blk);
|
|
||||||
if (!bs) {
|
if (!bs) {
|
||||||
fprintf(stderr, "Block device %s has no medium\n",
|
fprintf(stderr, "Error unknown block device %s\n",
|
||||||
device_name);
|
device_name);
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bs != bs_prev) {
|
if (bs != bs_prev) {
|
||||||
bs_prev = bs;
|
bs_prev = bs;
|
||||||
total_sectors = bdrv_nb_sectors(bs);
|
total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
|
||||||
if (total_sectors <= 0) {
|
if (total_sectors <= 0) {
|
||||||
error_report("Error getting length of block device %s",
|
error_report("Error getting length of block device %s",
|
||||||
device_name);
|
device_name);
|
||||||
@@ -832,16 +686,12 @@ static int block_load(QEMUFile *f, void *opaque, int version_id)
|
|||||||
nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
|
nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (flags & BLK_MIG_FLAG_ZERO_BLOCK) {
|
|
||||||
ret = bdrv_write_zeroes(bs, addr, nr_sectors,
|
|
||||||
BDRV_REQ_MAY_UNMAP);
|
|
||||||
} else {
|
|
||||||
buf = g_malloc(BLOCK_SIZE);
|
buf = g_malloc(BLOCK_SIZE);
|
||||||
|
|
||||||
qemu_get_buffer(f, buf, BLOCK_SIZE);
|
qemu_get_buffer(f, buf, BLOCK_SIZE);
|
||||||
ret = bdrv_write(bs, addr, buf, nr_sectors);
|
ret = bdrv_write(bs, addr, buf, nr_sectors);
|
||||||
g_free(buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
g_free(buf);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -880,14 +730,14 @@ static bool block_is_active(void *opaque)
|
|||||||
return block_mig_state.blk_enable == 1;
|
return block_mig_state.blk_enable == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static SaveVMHandlers savevm_block_handlers = {
|
SaveVMHandlers savevm_block_handlers = {
|
||||||
.set_params = block_set_params,
|
.set_params = block_set_params,
|
||||||
.save_live_setup = block_save_setup,
|
.save_live_setup = block_save_setup,
|
||||||
.save_live_iterate = block_save_iterate,
|
.save_live_iterate = block_save_iterate,
|
||||||
.save_live_complete_precopy = block_save_complete,
|
.save_live_complete = block_save_complete,
|
||||||
.save_live_pending = block_save_pending,
|
.save_live_pending = block_save_pending,
|
||||||
.load_state = block_load,
|
.load_state = block_load,
|
||||||
.cleanup = block_migration_cleanup,
|
.cancel = block_migration_cancel,
|
||||||
.is_active = block_is_active,
|
.is_active = block_is_active,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -895,7 +745,6 @@ void blk_mig_init(void)
|
|||||||
{
|
{
|
||||||
QSIMPLEQ_INIT(&block_mig_state.bmds_list);
|
QSIMPLEQ_INIT(&block_mig_state.bmds_list);
|
||||||
QSIMPLEQ_INIT(&block_mig_state.blk_list);
|
QSIMPLEQ_INIT(&block_mig_state.blk_list);
|
||||||
qemu_mutex_init(&block_mig_state.lock);
|
|
||||||
|
|
||||||
register_savevm_live(NULL, "block", 0, 1, &savevm_block_handlers,
|
register_savevm_live(NULL, "block", 0, 1, &savevm_block_handlers,
|
||||||
&block_mig_state);
|
&block_mig_state);
|
||||||
@@ -1,44 +1,24 @@
|
|||||||
block-obj-y += raw_bsd.o qcow.o vdi.o vmdk.o cloop.o bochs.o vpc.o vvfat.o
|
block-obj-y += raw.o cow.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 += 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.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o
|
||||||
block-obj-y += qed-check.o
|
block-obj-y += qed-check.o
|
||||||
block-obj-$(CONFIG_VHDX) += vhdx.o vhdx-endian.o vhdx-log.o
|
|
||||||
block-obj-y += quorum.o
|
|
||||||
block-obj-y += parallels.o blkdebug.o blkverify.o
|
block-obj-y += parallels.o blkdebug.o blkverify.o
|
||||||
block-obj-y += block-backend.o snapshot.o qapi.o
|
|
||||||
block-obj-$(CONFIG_WIN32) += raw-win32.o win32-aio.o
|
block-obj-$(CONFIG_WIN32) += raw-win32.o win32-aio.o
|
||||||
block-obj-$(CONFIG_POSIX) += raw-posix.o
|
block-obj-$(CONFIG_POSIX) += raw-posix.o
|
||||||
block-obj-$(CONFIG_LINUX_AIO) += linux-aio.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 += nbd.o nbd-client.o sheepdog.o
|
ifeq ($(CONFIG_POSIX),y)
|
||||||
|
block-obj-y += nbd.o sheepdog.o
|
||||||
block-obj-$(CONFIG_LIBISCSI) += iscsi.o
|
block-obj-$(CONFIG_LIBISCSI) += iscsi.o
|
||||||
block-obj-$(CONFIG_LIBNFS) += nfs.o
|
|
||||||
block-obj-$(CONFIG_CURL) += curl.o
|
block-obj-$(CONFIG_CURL) += curl.o
|
||||||
block-obj-$(CONFIG_RBD) += rbd.o
|
block-obj-$(CONFIG_RBD) += rbd.o
|
||||||
block-obj-$(CONFIG_GLUSTERFS) += gluster.o
|
block-obj-$(CONFIG_GLUSTERFS) += gluster.o
|
||||||
block-obj-$(CONFIG_ARCHIPELAGO) += archipelago.o
|
endif
|
||||||
block-obj-$(CONFIG_LIBSSH2) += ssh.o
|
|
||||||
block-obj-y += accounting.o
|
|
||||||
block-obj-y += write-threshold.o
|
|
||||||
|
|
||||||
common-obj-y += stream.o
|
common-obj-y += stream.o
|
||||||
common-obj-y += commit.o
|
common-obj-y += commit.o
|
||||||
common-obj-y += backup.o
|
common-obj-y += mirror.o
|
||||||
|
block-obj-y += dictzip.o
|
||||||
|
block-obj-y += tar.o
|
||||||
|
|
||||||
iscsi.o-cflags := $(LIBISCSI_CFLAGS)
|
$(obj)/curl.o: QEMU_CFLAGS+=$(CURL_CFLAGS)
|
||||||
iscsi.o-libs := $(LIBISCSI_LIBS)
|
|
||||||
curl.o-cflags := $(CURL_CFLAGS)
|
|
||||||
curl.o-libs := $(CURL_LIBS)
|
|
||||||
rbd.o-cflags := $(RBD_CFLAGS)
|
|
||||||
rbd.o-libs := $(RBD_LIBS)
|
|
||||||
gluster.o-cflags := $(GLUSTERFS_CFLAGS)
|
|
||||||
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
|
|
||||||
|
|||||||
@@ -1,172 +0,0 @@
|
|||||||
/*
|
|
||||||
* QEMU System Emulator block accounting
|
|
||||||
*
|
|
||||||
* Copyright (c) 2011 Christoph Hellwig
|
|
||||||
* Copyright (c) 2015 Igalia, S.L.
|
|
||||||
*
|
|
||||||
* 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 "block/accounting.h"
|
|
||||||
#include "block/block_int.h"
|
|
||||||
#include "qemu/timer.h"
|
|
||||||
#include "sysemu/qtest.h"
|
|
||||||
|
|
||||||
static QEMUClockType clock_type = QEMU_CLOCK_REALTIME;
|
|
||||||
static const int qtest_latency_ns = NANOSECONDS_PER_SECOND / 1000;
|
|
||||||
|
|
||||||
void block_acct_init(BlockAcctStats *stats, bool account_invalid,
|
|
||||||
bool account_failed)
|
|
||||||
{
|
|
||||||
stats->account_invalid = account_invalid;
|
|
||||||
stats->account_failed = account_failed;
|
|
||||||
|
|
||||||
if (qtest_enabled()) {
|
|
||||||
clock_type = QEMU_CLOCK_VIRTUAL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void block_acct_cleanup(BlockAcctStats *stats)
|
|
||||||
{
|
|
||||||
BlockAcctTimedStats *s, *next;
|
|
||||||
QSLIST_FOREACH_SAFE(s, &stats->intervals, entries, next) {
|
|
||||||
g_free(s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void block_acct_add_interval(BlockAcctStats *stats, unsigned interval_length)
|
|
||||||
{
|
|
||||||
BlockAcctTimedStats *s;
|
|
||||||
unsigned i;
|
|
||||||
|
|
||||||
s = g_new0(BlockAcctTimedStats, 1);
|
|
||||||
s->interval_length = interval_length;
|
|
||||||
QSLIST_INSERT_HEAD(&stats->intervals, s, entries);
|
|
||||||
|
|
||||||
for (i = 0; i < BLOCK_MAX_IOTYPE; i++) {
|
|
||||||
timed_average_init(&s->latency[i], clock_type,
|
|
||||||
(uint64_t) interval_length * NANOSECONDS_PER_SECOND);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
BlockAcctTimedStats *block_acct_interval_next(BlockAcctStats *stats,
|
|
||||||
BlockAcctTimedStats *s)
|
|
||||||
{
|
|
||||||
if (s == NULL) {
|
|
||||||
return QSLIST_FIRST(&stats->intervals);
|
|
||||||
} else {
|
|
||||||
return QSLIST_NEXT(s, entries);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void block_acct_start(BlockAcctStats *stats, BlockAcctCookie *cookie,
|
|
||||||
int64_t bytes, enum BlockAcctType type)
|
|
||||||
{
|
|
||||||
assert(type < BLOCK_MAX_IOTYPE);
|
|
||||||
|
|
||||||
cookie->bytes = bytes;
|
|
||||||
cookie->start_time_ns = qemu_clock_get_ns(clock_type);
|
|
||||||
cookie->type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
void block_acct_done(BlockAcctStats *stats, BlockAcctCookie *cookie)
|
|
||||||
{
|
|
||||||
BlockAcctTimedStats *s;
|
|
||||||
int64_t time_ns = qemu_clock_get_ns(clock_type);
|
|
||||||
int64_t latency_ns = time_ns - cookie->start_time_ns;
|
|
||||||
|
|
||||||
if (qtest_enabled()) {
|
|
||||||
latency_ns = qtest_latency_ns;
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(cookie->type < BLOCK_MAX_IOTYPE);
|
|
||||||
|
|
||||||
stats->nr_bytes[cookie->type] += cookie->bytes;
|
|
||||||
stats->nr_ops[cookie->type]++;
|
|
||||||
stats->total_time_ns[cookie->type] += latency_ns;
|
|
||||||
stats->last_access_time_ns = time_ns;
|
|
||||||
|
|
||||||
QSLIST_FOREACH(s, &stats->intervals, entries) {
|
|
||||||
timed_average_account(&s->latency[cookie->type], latency_ns);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void block_acct_failed(BlockAcctStats *stats, BlockAcctCookie *cookie)
|
|
||||||
{
|
|
||||||
assert(cookie->type < BLOCK_MAX_IOTYPE);
|
|
||||||
|
|
||||||
stats->failed_ops[cookie->type]++;
|
|
||||||
|
|
||||||
if (stats->account_failed) {
|
|
||||||
BlockAcctTimedStats *s;
|
|
||||||
int64_t time_ns = qemu_clock_get_ns(clock_type);
|
|
||||||
int64_t latency_ns = time_ns - cookie->start_time_ns;
|
|
||||||
|
|
||||||
if (qtest_enabled()) {
|
|
||||||
latency_ns = qtest_latency_ns;
|
|
||||||
}
|
|
||||||
|
|
||||||
stats->total_time_ns[cookie->type] += latency_ns;
|
|
||||||
stats->last_access_time_ns = time_ns;
|
|
||||||
|
|
||||||
QSLIST_FOREACH(s, &stats->intervals, entries) {
|
|
||||||
timed_average_account(&s->latency[cookie->type], latency_ns);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void block_acct_invalid(BlockAcctStats *stats, enum BlockAcctType type)
|
|
||||||
{
|
|
||||||
assert(type < BLOCK_MAX_IOTYPE);
|
|
||||||
|
|
||||||
/* block_acct_done() and block_acct_failed() update
|
|
||||||
* total_time_ns[], but this one does not. The reason is that
|
|
||||||
* invalid requests are accounted during their submission,
|
|
||||||
* therefore there's no actual I/O involved. */
|
|
||||||
|
|
||||||
stats->invalid_ops[type]++;
|
|
||||||
|
|
||||||
if (stats->account_invalid) {
|
|
||||||
stats->last_access_time_ns = qemu_clock_get_ns(clock_type);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void block_acct_merge_done(BlockAcctStats *stats, enum BlockAcctType type,
|
|
||||||
int num_requests)
|
|
||||||
{
|
|
||||||
assert(type < BLOCK_MAX_IOTYPE);
|
|
||||||
stats->merged[type] += num_requests;
|
|
||||||
}
|
|
||||||
|
|
||||||
int64_t block_acct_idle_time_ns(BlockAcctStats *stats)
|
|
||||||
{
|
|
||||||
return qemu_clock_get_ns(clock_type) - stats->last_access_time_ns;
|
|
||||||
}
|
|
||||||
|
|
||||||
double block_acct_queue_depth(BlockAcctTimedStats *stats,
|
|
||||||
enum BlockAcctType type)
|
|
||||||
{
|
|
||||||
uint64_t sum, elapsed;
|
|
||||||
|
|
||||||
assert(type < BLOCK_MAX_IOTYPE);
|
|
||||||
|
|
||||||
sum = timed_average_sum(&stats->latency[type], &elapsed);
|
|
||||||
|
|
||||||
return (double) sum / elapsed;
|
|
||||||
}
|
|
||||||
1084
block/archipelago.c
1084
block/archipelago.c
File diff suppressed because it is too large
Load Diff
586
block/backup.c
586
block/backup.c
@@ -1,586 +0,0 @@
|
|||||||
/*
|
|
||||||
* QEMU backup
|
|
||||||
*
|
|
||||||
* Copyright (C) 2013 Proxmox Server Solutions
|
|
||||||
*
|
|
||||||
* Authors:
|
|
||||||
* Dietmar Maurer (dietmar@proxmox.com)
|
|
||||||
*
|
|
||||||
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
||||||
* See the COPYING file in the top-level directory.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#include "trace.h"
|
|
||||||
#include "block/block.h"
|
|
||||||
#include "block/block_int.h"
|
|
||||||
#include "block/blockjob.h"
|
|
||||||
#include "qapi/qmp/qerror.h"
|
|
||||||
#include "qemu/ratelimit.h"
|
|
||||||
#include "sysemu/block-backend.h"
|
|
||||||
|
|
||||||
#define BACKUP_CLUSTER_BITS 16
|
|
||||||
#define BACKUP_CLUSTER_SIZE (1 << BACKUP_CLUSTER_BITS)
|
|
||||||
#define BACKUP_SECTORS_PER_CLUSTER (BACKUP_CLUSTER_SIZE / BDRV_SECTOR_SIZE)
|
|
||||||
|
|
||||||
#define SLICE_TIME 100000000ULL /* ns */
|
|
||||||
|
|
||||||
typedef struct CowRequest {
|
|
||||||
int64_t start;
|
|
||||||
int64_t end;
|
|
||||||
QLIST_ENTRY(CowRequest) list;
|
|
||||||
CoQueue wait_queue; /* coroutines blocked on this request */
|
|
||||||
} CowRequest;
|
|
||||||
|
|
||||||
typedef struct BackupBlockJob {
|
|
||||||
BlockJob common;
|
|
||||||
BlockDriverState *target;
|
|
||||||
/* bitmap for sync=incremental */
|
|
||||||
BdrvDirtyBitmap *sync_bitmap;
|
|
||||||
MirrorSyncMode sync_mode;
|
|
||||||
RateLimit limit;
|
|
||||||
BlockdevOnError on_source_error;
|
|
||||||
BlockdevOnError on_target_error;
|
|
||||||
CoRwlock flush_rwlock;
|
|
||||||
uint64_t sectors_read;
|
|
||||||
HBitmap *bitmap;
|
|
||||||
QLIST_HEAD(, CowRequest) inflight_reqs;
|
|
||||||
} BackupBlockJob;
|
|
||||||
|
|
||||||
/* See if in-flight requests overlap and wait for them to complete */
|
|
||||||
static void coroutine_fn wait_for_overlapping_requests(BackupBlockJob *job,
|
|
||||||
int64_t start,
|
|
||||||
int64_t end)
|
|
||||||
{
|
|
||||||
CowRequest *req;
|
|
||||||
bool retry;
|
|
||||||
|
|
||||||
do {
|
|
||||||
retry = false;
|
|
||||||
QLIST_FOREACH(req, &job->inflight_reqs, list) {
|
|
||||||
if (end > req->start && start < req->end) {
|
|
||||||
qemu_co_queue_wait(&req->wait_queue);
|
|
||||||
retry = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} while (retry);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Keep track of an in-flight request */
|
|
||||||
static void cow_request_begin(CowRequest *req, BackupBlockJob *job,
|
|
||||||
int64_t start, int64_t end)
|
|
||||||
{
|
|
||||||
req->start = start;
|
|
||||||
req->end = end;
|
|
||||||
qemu_co_queue_init(&req->wait_queue);
|
|
||||||
QLIST_INSERT_HEAD(&job->inflight_reqs, req, list);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Forget about a completed request */
|
|
||||||
static void cow_request_end(CowRequest *req)
|
|
||||||
{
|
|
||||||
QLIST_REMOVE(req, list);
|
|
||||||
qemu_co_queue_restart_all(&req->wait_queue);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int coroutine_fn backup_do_cow(BlockDriverState *bs,
|
|
||||||
int64_t sector_num, int nb_sectors,
|
|
||||||
bool *error_is_read,
|
|
||||||
bool is_write_notifier)
|
|
||||||
{
|
|
||||||
BackupBlockJob *job = (BackupBlockJob *)bs->job;
|
|
||||||
CowRequest cow_request;
|
|
||||||
struct iovec iov;
|
|
||||||
QEMUIOVector bounce_qiov;
|
|
||||||
void *bounce_buffer = NULL;
|
|
||||||
int ret = 0;
|
|
||||||
int64_t start, end;
|
|
||||||
int n;
|
|
||||||
|
|
||||||
qemu_co_rwlock_rdlock(&job->flush_rwlock);
|
|
||||||
|
|
||||||
start = sector_num / BACKUP_SECTORS_PER_CLUSTER;
|
|
||||||
end = DIV_ROUND_UP(sector_num + nb_sectors, BACKUP_SECTORS_PER_CLUSTER);
|
|
||||||
|
|
||||||
trace_backup_do_cow_enter(job, start, sector_num, nb_sectors);
|
|
||||||
|
|
||||||
wait_for_overlapping_requests(job, start, end);
|
|
||||||
cow_request_begin(&cow_request, job, start, end);
|
|
||||||
|
|
||||||
for (; start < end; start++) {
|
|
||||||
if (hbitmap_get(job->bitmap, start)) {
|
|
||||||
trace_backup_do_cow_skip(job, start);
|
|
||||||
continue; /* already copied */
|
|
||||||
}
|
|
||||||
|
|
||||||
trace_backup_do_cow_process(job, start);
|
|
||||||
|
|
||||||
n = MIN(BACKUP_SECTORS_PER_CLUSTER,
|
|
||||||
job->common.len / BDRV_SECTOR_SIZE -
|
|
||||||
start * BACKUP_SECTORS_PER_CLUSTER);
|
|
||||||
|
|
||||||
if (!bounce_buffer) {
|
|
||||||
bounce_buffer = qemu_blockalign(bs, BACKUP_CLUSTER_SIZE);
|
|
||||||
}
|
|
||||||
iov.iov_base = bounce_buffer;
|
|
||||||
iov.iov_len = n * BDRV_SECTOR_SIZE;
|
|
||||||
qemu_iovec_init_external(&bounce_qiov, &iov, 1);
|
|
||||||
|
|
||||||
if (is_write_notifier) {
|
|
||||||
ret = bdrv_co_readv_no_serialising(bs,
|
|
||||||
start * BACKUP_SECTORS_PER_CLUSTER,
|
|
||||||
n, &bounce_qiov);
|
|
||||||
} else {
|
|
||||||
ret = bdrv_co_readv(bs, start * BACKUP_SECTORS_PER_CLUSTER, n,
|
|
||||||
&bounce_qiov);
|
|
||||||
}
|
|
||||||
if (ret < 0) {
|
|
||||||
trace_backup_do_cow_read_fail(job, start, ret);
|
|
||||||
if (error_is_read) {
|
|
||||||
*error_is_read = true;
|
|
||||||
}
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (buffer_is_zero(iov.iov_base, iov.iov_len)) {
|
|
||||||
ret = bdrv_co_write_zeroes(job->target,
|
|
||||||
start * BACKUP_SECTORS_PER_CLUSTER,
|
|
||||||
n, BDRV_REQ_MAY_UNMAP);
|
|
||||||
} else {
|
|
||||||
ret = bdrv_co_writev(job->target,
|
|
||||||
start * BACKUP_SECTORS_PER_CLUSTER, n,
|
|
||||||
&bounce_qiov);
|
|
||||||
}
|
|
||||||
if (ret < 0) {
|
|
||||||
trace_backup_do_cow_write_fail(job, start, ret);
|
|
||||||
if (error_is_read) {
|
|
||||||
*error_is_read = false;
|
|
||||||
}
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
hbitmap_set(job->bitmap, start, 1);
|
|
||||||
|
|
||||||
/* Publish progress, guest I/O counts as progress too. Note that the
|
|
||||||
* offset field is an opaque progress value, it is not a disk offset.
|
|
||||||
*/
|
|
||||||
job->sectors_read += n;
|
|
||||||
job->common.offset += n * BDRV_SECTOR_SIZE;
|
|
||||||
}
|
|
||||||
|
|
||||||
out:
|
|
||||||
if (bounce_buffer) {
|
|
||||||
qemu_vfree(bounce_buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
cow_request_end(&cow_request);
|
|
||||||
|
|
||||||
trace_backup_do_cow_return(job, sector_num, nb_sectors, ret);
|
|
||||||
|
|
||||||
qemu_co_rwlock_unlock(&job->flush_rwlock);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int coroutine_fn backup_before_write_notify(
|
|
||||||
NotifierWithReturn *notifier,
|
|
||||||
void *opaque)
|
|
||||||
{
|
|
||||||
BdrvTrackedRequest *req = opaque;
|
|
||||||
int64_t sector_num = req->offset >> BDRV_SECTOR_BITS;
|
|
||||||
int nb_sectors = req->bytes >> BDRV_SECTOR_BITS;
|
|
||||||
|
|
||||||
assert((req->offset & (BDRV_SECTOR_SIZE - 1)) == 0);
|
|
||||||
assert((req->bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
|
|
||||||
|
|
||||||
return backup_do_cow(req->bs, sector_num, nb_sectors, NULL, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
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");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void backup_iostatus_reset(BlockJob *job)
|
|
||||||
{
|
|
||||||
BackupBlockJob *s = container_of(job, BackupBlockJob, common);
|
|
||||||
|
|
||||||
if (s->target->blk) {
|
|
||||||
blk_iostatus_reset(s->target->blk);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void backup_cleanup_sync_bitmap(BackupBlockJob *job, int ret)
|
|
||||||
{
|
|
||||||
BdrvDirtyBitmap *bm;
|
|
||||||
BlockDriverState *bs = job->common.bs;
|
|
||||||
|
|
||||||
if (ret < 0 || block_job_is_cancelled(&job->common)) {
|
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void backup_commit(BlockJob *job)
|
|
||||||
{
|
|
||||||
BackupBlockJob *s = container_of(job, BackupBlockJob, common);
|
|
||||||
if (s->sync_bitmap) {
|
|
||||||
backup_cleanup_sync_bitmap(s, 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void backup_abort(BlockJob *job)
|
|
||||||
{
|
|
||||||
BackupBlockJob *s = container_of(job, BackupBlockJob, common);
|
|
||||||
if (s->sync_bitmap) {
|
|
||||||
backup_cleanup_sync_bitmap(s, -1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static const BlockJobDriver backup_job_driver = {
|
|
||||||
.instance_size = sizeof(BackupBlockJob),
|
|
||||||
.job_type = BLOCK_JOB_TYPE_BACKUP,
|
|
||||||
.set_speed = backup_set_speed,
|
|
||||||
.iostatus_reset = backup_iostatus_reset,
|
|
||||||
.commit = backup_commit,
|
|
||||||
.abort = backup_abort,
|
|
||||||
};
|
|
||||||
|
|
||||||
static BlockErrorAction backup_error_action(BackupBlockJob *job,
|
|
||||||
bool read, int error)
|
|
||||||
{
|
|
||||||
if (read) {
|
|
||||||
return block_job_error_action(&job->common, job->common.bs,
|
|
||||||
job->on_source_error, true, error);
|
|
||||||
} else {
|
|
||||||
return block_job_error_action(&job->common, job->target,
|
|
||||||
job->on_target_error, false, error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
int ret;
|
|
||||||
} BackupCompleteData;
|
|
||||||
|
|
||||||
static void backup_complete(BlockJob *job, void *opaque)
|
|
||||||
{
|
|
||||||
BackupBlockJob *s = container_of(job, BackupBlockJob, common);
|
|
||||||
BackupCompleteData *data = opaque;
|
|
||||||
|
|
||||||
bdrv_unref(s->target);
|
|
||||||
|
|
||||||
block_job_completed(job, data->ret);
|
|
||||||
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,
|
|
||||||
false);
|
|
||||||
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;
|
|
||||||
BackupCompleteData *data;
|
|
||||||
BlockDriverState *bs = job->common.bs;
|
|
||||||
BlockDriverState *target = job->target;
|
|
||||||
BlockdevOnError on_target_error = job->on_target_error;
|
|
||||||
NotifierWithReturn before_write = {
|
|
||||||
.notify = backup_before_write_notify,
|
|
||||||
};
|
|
||||||
int64_t start, end;
|
|
||||||
int ret = 0;
|
|
||||||
|
|
||||||
QLIST_INIT(&job->inflight_reqs);
|
|
||||||
qemu_co_rwlock_init(&job->flush_rwlock);
|
|
||||||
|
|
||||||
start = 0;
|
|
||||||
end = DIV_ROUND_UP(job->common.len, BACKUP_CLUSTER_SIZE);
|
|
||||||
|
|
||||||
job->bitmap = hbitmap_alloc(end, 0);
|
|
||||||
|
|
||||||
bdrv_set_enable_write_cache(target, true);
|
|
||||||
if (target->blk) {
|
|
||||||
blk_set_on_error(target->blk, on_target_error, on_target_error);
|
|
||||||
blk_iostatus_enable(target->blk);
|
|
||||||
}
|
|
||||||
|
|
||||||
bdrv_add_before_write_notifier(bs, &before_write);
|
|
||||||
|
|
||||||
if (job->sync_mode == MIRROR_SYNC_MODE_NONE) {
|
|
||||||
while (!block_job_is_cancelled(&job->common)) {
|
|
||||||
/* Yield until the job is cancelled. We just let our before_write
|
|
||||||
* notify callback service CoW requests. */
|
|
||||||
job->common.busy = false;
|
|
||||||
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)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (job->sync_mode == MIRROR_SYNC_MODE_TOP) {
|
|
||||||
int i, n;
|
|
||||||
int alloced = 0;
|
|
||||||
|
|
||||||
/* Check to see if these blocks are already in the
|
|
||||||
* backing file. */
|
|
||||||
|
|
||||||
for (i = 0; i < BACKUP_SECTORS_PER_CLUSTER;) {
|
|
||||||
/* bdrv_is_allocated() only returns true/false based
|
|
||||||
* on the first set of sectors it comes across that
|
|
||||||
* are are all in the same state.
|
|
||||||
* For that reason we must verify each sector in the
|
|
||||||
* backup cluster length. We end up copying more than
|
|
||||||
* needed but at some point that is always the case. */
|
|
||||||
alloced =
|
|
||||||
bdrv_is_allocated(bs,
|
|
||||||
start * BACKUP_SECTORS_PER_CLUSTER + i,
|
|
||||||
BACKUP_SECTORS_PER_CLUSTER - i, &n);
|
|
||||||
i += n;
|
|
||||||
|
|
||||||
if (alloced == 1 || n == 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* If the above loop never found any sectors that are in
|
|
||||||
* the topmost image, skip this backup. */
|
|
||||||
if (alloced == 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/* FULL sync mode we copy the whole drive. */
|
|
||||||
ret = backup_do_cow(bs, start * BACKUP_SECTORS_PER_CLUSTER,
|
|
||||||
BACKUP_SECTORS_PER_CLUSTER, &error_is_read, false);
|
|
||||||
if (ret < 0) {
|
|
||||||
/* Depending on error action, fail now or retry cluster */
|
|
||||||
BlockErrorAction action =
|
|
||||||
backup_error_action(job, error_is_read, -ret);
|
|
||||||
if (action == BLOCK_ERROR_ACTION_REPORT) {
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
start--;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
notifier_with_return_remove(&before_write);
|
|
||||||
|
|
||||||
/* wait until pending backup_do_cow() calls have completed */
|
|
||||||
qemu_co_rwlock_wrlock(&job->flush_rwlock);
|
|
||||||
qemu_co_rwlock_unlock(&job->flush_rwlock);
|
|
||||||
hbitmap_free(job->bitmap);
|
|
||||||
|
|
||||||
if (target->blk) {
|
|
||||||
blk_iostatus_disable(target->blk);
|
|
||||||
}
|
|
||||||
bdrv_op_unblock_all(target, job->common.blocker);
|
|
||||||
|
|
||||||
data = g_malloc(sizeof(*data));
|
|
||||||
data->ret = ret;
|
|
||||||
block_job_defer_to_main_loop(&job->common, backup_complete, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
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,
|
|
||||||
BlockJobTxn *txn, Error **errp)
|
|
||||||
{
|
|
||||||
int64_t len;
|
|
||||||
|
|
||||||
assert(bs);
|
|
||||||
assert(target);
|
|
||||||
assert(cb);
|
|
||||||
|
|
||||||
if (bs == target) {
|
|
||||||
error_setg(errp, "Source and target cannot be the same");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((on_source_error == BLOCKDEV_ON_ERROR_STOP ||
|
|
||||||
on_source_error == BLOCKDEV_ON_ERROR_ENOSPC) &&
|
|
||||||
(!bs->blk || !blk_iostatus_is_enabled(bs->blk))) {
|
|
||||||
error_setg(errp, QERR_INVALID_PARAMETER, "on-source-error");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!bdrv_is_inserted(bs)) {
|
|
||||||
error_setg(errp, "Device is not inserted: %s",
|
|
||||||
bdrv_get_device_name(bs));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!bdrv_is_inserted(target)) {
|
|
||||||
error_setg(errp, "Device is not inserted: %s",
|
|
||||||
bdrv_get_device_name(target));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_BACKUP_TARGET, errp)) {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
BackupBlockJob *job = block_job_create(&backup_job_driver, bs, speed,
|
|
||||||
cb, opaque, errp);
|
|
||||||
if (!job) {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
bdrv_op_block_all(target, job->common.blocker);
|
|
||||||
|
|
||||||
job->on_source_error = on_source_error;
|
|
||||||
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);
|
|
||||||
block_job_txn_add_job(txn, &job->common);
|
|
||||||
qemu_coroutine_enter(job->common.co, job);
|
|
||||||
return;
|
|
||||||
|
|
||||||
error:
|
|
||||||
if (sync_bitmap) {
|
|
||||||
bdrv_reclaim_dirty_bitmap(bs, sync_bitmap, NULL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
393
block/blkdebug.c
393
block/blkdebug.c
@@ -26,23 +26,18 @@
|
|||||||
#include "qemu/config-file.h"
|
#include "qemu/config-file.h"
|
||||||
#include "block/block_int.h"
|
#include "block/block_int.h"
|
||||||
#include "qemu/module.h"
|
#include "qemu/module.h"
|
||||||
#include "qapi/qmp/qbool.h"
|
|
||||||
#include "qapi/qmp/qdict.h"
|
|
||||||
#include "qapi/qmp/qint.h"
|
|
||||||
#include "qapi/qmp/qstring.h"
|
|
||||||
#include "sysemu/qtest.h"
|
|
||||||
|
|
||||||
typedef struct BDRVBlkdebugState {
|
typedef struct BDRVBlkdebugState {
|
||||||
int state;
|
int state;
|
||||||
int new_state;
|
int new_state;
|
||||||
|
|
||||||
QLIST_HEAD(, BlkdebugRule) rules[BLKDBG__MAX];
|
QLIST_HEAD(, BlkdebugRule) rules[BLKDBG_EVENT_MAX];
|
||||||
QSIMPLEQ_HEAD(, BlkdebugRule) active_rules;
|
QSIMPLEQ_HEAD(, BlkdebugRule) active_rules;
|
||||||
QLIST_HEAD(, BlkdebugSuspendedReq) suspended_reqs;
|
QLIST_HEAD(, BlkdebugSuspendedReq) suspended_reqs;
|
||||||
} BDRVBlkdebugState;
|
} BDRVBlkdebugState;
|
||||||
|
|
||||||
typedef struct BlkdebugAIOCB {
|
typedef struct BlkdebugAIOCB {
|
||||||
BlockAIOCB common;
|
BlockDriverAIOCB common;
|
||||||
QEMUBH *bh;
|
QEMUBH *bh;
|
||||||
int ret;
|
int ret;
|
||||||
} BlkdebugAIOCB;
|
} BlkdebugAIOCB;
|
||||||
@@ -53,8 +48,11 @@ typedef struct BlkdebugSuspendedReq {
|
|||||||
QLIST_ENTRY(BlkdebugSuspendedReq) next;
|
QLIST_ENTRY(BlkdebugSuspendedReq) next;
|
||||||
} BlkdebugSuspendedReq;
|
} BlkdebugSuspendedReq;
|
||||||
|
|
||||||
|
static void blkdebug_aio_cancel(BlockDriverAIOCB *blockacb);
|
||||||
|
|
||||||
static const AIOCBInfo blkdebug_aiocb_info = {
|
static const AIOCBInfo blkdebug_aiocb_info = {
|
||||||
.aiocb_size = sizeof(BlkdebugAIOCB),
|
.aiocb_size = sizeof(BlkdebugAIOCB),
|
||||||
|
.cancel = blkdebug_aio_cancel,
|
||||||
};
|
};
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
@@ -64,7 +62,7 @@ enum {
|
|||||||
};
|
};
|
||||||
|
|
||||||
typedef struct BlkdebugRule {
|
typedef struct BlkdebugRule {
|
||||||
BlkdebugEvent event;
|
BlkDebugEvent event;
|
||||||
int action;
|
int action;
|
||||||
int state;
|
int state;
|
||||||
union {
|
union {
|
||||||
@@ -143,12 +141,55 @@ static QemuOptsList *config_groups[] = {
|
|||||||
NULL
|
NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
static int get_event_by_name(const char *name, BlkdebugEvent *event)
|
static const char *event_names[BLKDBG_EVENT_MAX] = {
|
||||||
|
[BLKDBG_L1_UPDATE] = "l1_update",
|
||||||
|
[BLKDBG_L1_GROW_ALLOC_TABLE] = "l1_grow.alloc_table",
|
||||||
|
[BLKDBG_L1_GROW_WRITE_TABLE] = "l1_grow.write_table",
|
||||||
|
[BLKDBG_L1_GROW_ACTIVATE_TABLE] = "l1_grow.activate_table",
|
||||||
|
|
||||||
|
[BLKDBG_L2_LOAD] = "l2_load",
|
||||||
|
[BLKDBG_L2_UPDATE] = "l2_update",
|
||||||
|
[BLKDBG_L2_UPDATE_COMPRESSED] = "l2_update_compressed",
|
||||||
|
[BLKDBG_L2_ALLOC_COW_READ] = "l2_alloc.cow_read",
|
||||||
|
[BLKDBG_L2_ALLOC_WRITE] = "l2_alloc.write",
|
||||||
|
|
||||||
|
[BLKDBG_READ_AIO] = "read_aio",
|
||||||
|
[BLKDBG_READ_BACKING_AIO] = "read_backing_aio",
|
||||||
|
[BLKDBG_READ_COMPRESSED] = "read_compressed",
|
||||||
|
|
||||||
|
[BLKDBG_WRITE_AIO] = "write_aio",
|
||||||
|
[BLKDBG_WRITE_COMPRESSED] = "write_compressed",
|
||||||
|
|
||||||
|
[BLKDBG_VMSTATE_LOAD] = "vmstate_load",
|
||||||
|
[BLKDBG_VMSTATE_SAVE] = "vmstate_save",
|
||||||
|
|
||||||
|
[BLKDBG_COW_READ] = "cow_read",
|
||||||
|
[BLKDBG_COW_WRITE] = "cow_write",
|
||||||
|
|
||||||
|
[BLKDBG_REFTABLE_LOAD] = "reftable_load",
|
||||||
|
[BLKDBG_REFTABLE_GROW] = "reftable_grow",
|
||||||
|
|
||||||
|
[BLKDBG_REFBLOCK_LOAD] = "refblock_load",
|
||||||
|
[BLKDBG_REFBLOCK_UPDATE] = "refblock_update",
|
||||||
|
[BLKDBG_REFBLOCK_UPDATE_PART] = "refblock_update_part",
|
||||||
|
[BLKDBG_REFBLOCK_ALLOC] = "refblock_alloc",
|
||||||
|
[BLKDBG_REFBLOCK_ALLOC_HOOKUP] = "refblock_alloc.hookup",
|
||||||
|
[BLKDBG_REFBLOCK_ALLOC_WRITE] = "refblock_alloc.write",
|
||||||
|
[BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS] = "refblock_alloc.write_blocks",
|
||||||
|
[BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE] = "refblock_alloc.write_table",
|
||||||
|
[BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE] = "refblock_alloc.switch_table",
|
||||||
|
|
||||||
|
[BLKDBG_CLUSTER_ALLOC] = "cluster_alloc",
|
||||||
|
[BLKDBG_CLUSTER_ALLOC_BYTES] = "cluster_alloc_bytes",
|
||||||
|
[BLKDBG_CLUSTER_FREE] = "cluster_free",
|
||||||
|
};
|
||||||
|
|
||||||
|
static int get_event_by_name(const char *name, BlkDebugEvent *event)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < BLKDBG__MAX; i++) {
|
for (i = 0; i < BLKDBG_EVENT_MAX; i++) {
|
||||||
if (!strcmp(BlkdebugEvent_lookup[i], name)) {
|
if (!strcmp(event_names[i], name)) {
|
||||||
*event = i;
|
*event = i;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -162,21 +203,17 @@ struct add_rule_data {
|
|||||||
int action;
|
int action;
|
||||||
};
|
};
|
||||||
|
|
||||||
static int add_rule(void *opaque, QemuOpts *opts, Error **errp)
|
static int add_rule(QemuOpts *opts, void *opaque)
|
||||||
{
|
{
|
||||||
struct add_rule_data *d = opaque;
|
struct add_rule_data *d = opaque;
|
||||||
BDRVBlkdebugState *s = d->s;
|
BDRVBlkdebugState *s = d->s;
|
||||||
const char* event_name;
|
const char* event_name;
|
||||||
BlkdebugEvent event;
|
BlkDebugEvent event;
|
||||||
struct BlkdebugRule *rule;
|
struct BlkdebugRule *rule;
|
||||||
|
|
||||||
/* Find the right event for the rule */
|
/* Find the right event for the rule */
|
||||||
event_name = qemu_opt_get(opts, "event");
|
event_name = qemu_opt_get(opts, "event");
|
||||||
if (!event_name) {
|
if (!event_name || get_event_by_name(event_name, &event) < 0) {
|
||||||
error_setg(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);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -230,214 +267,125 @@ static void remove_rule(BlkdebugRule *rule)
|
|||||||
g_free(rule);
|
g_free(rule);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int read_config(BDRVBlkdebugState *s, const char *filename,
|
static int read_config(BDRVBlkdebugState *s, const char *filename)
|
||||||
QDict *options, Error **errp)
|
|
||||||
{
|
{
|
||||||
FILE *f = NULL;
|
FILE *f;
|
||||||
int ret;
|
int ret;
|
||||||
struct add_rule_data d;
|
struct add_rule_data d;
|
||||||
Error *local_err = NULL;
|
|
||||||
|
|
||||||
if (filename) {
|
/* Allow usage without config file */
|
||||||
|
if (!*filename) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
f = fopen(filename, "r");
|
f = fopen(filename, "r");
|
||||||
if (f == NULL) {
|
if (f == NULL) {
|
||||||
error_setg_errno(errp, errno, "Could not read blkdebug config file");
|
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = qemu_config_parse(f, config_groups, filename);
|
ret = qemu_config_parse(f, config_groups, filename);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_setg(errp, "Could not parse blkdebug config file");
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
qemu_config_parse_qdict(options, config_groups, &local_err);
|
|
||||||
if (local_err) {
|
|
||||||
error_propagate(errp, local_err);
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
d.s = s;
|
d.s = s;
|
||||||
d.action = ACTION_INJECT_ERROR;
|
d.action = ACTION_INJECT_ERROR;
|
||||||
qemu_opts_foreach(&inject_error_opts, add_rule, &d, &local_err);
|
qemu_opts_foreach(&inject_error_opts, add_rule, &d, 0);
|
||||||
if (local_err) {
|
|
||||||
error_propagate(errp, local_err);
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
d.action = ACTION_SET_STATE;
|
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, 0);
|
||||||
if (local_err) {
|
|
||||||
error_propagate(errp, local_err);
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
fail:
|
fail:
|
||||||
qemu_opts_reset(&inject_error_opts);
|
qemu_opts_reset(&inject_error_opts);
|
||||||
qemu_opts_reset(&set_state_opts);
|
qemu_opts_reset(&set_state_opts);
|
||||||
if (f) {
|
|
||||||
fclose(f);
|
fclose(f);
|
||||||
}
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
|
/* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
|
||||||
static void blkdebug_parse_filename(const char *filename, QDict *options,
|
static int blkdebug_open(BlockDriverState *bs, const char *filename, int flags)
|
||||||
Error **errp)
|
|
||||||
{
|
|
||||||
const char *c;
|
|
||||||
|
|
||||||
/* Parse the blkdebug: prefix */
|
|
||||||
if (!strstart(filename, "blkdebug:", &filename)) {
|
|
||||||
/* There was no prefix; therefore, all options have to be already
|
|
||||||
present in the QDict (except for the filename) */
|
|
||||||
qdict_put(options, "x-image", qstring_from_str(filename));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Parse config file path */
|
|
||||||
c = strchr(filename, ':');
|
|
||||||
if (c == NULL) {
|
|
||||||
error_setg(errp, "blkdebug requires both config file and image path");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c != filename) {
|
|
||||||
QString *config_path;
|
|
||||||
config_path = qstring_from_substr(filename, 0, c - filename - 1);
|
|
||||||
qdict_put(options, "config", config_path);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* TODO Allow multi-level nesting and set file.filename here */
|
|
||||||
filename = c + 1;
|
|
||||||
qdict_put(options, "x-image", qstring_from_str(filename));
|
|
||||||
}
|
|
||||||
|
|
||||||
static QemuOptsList runtime_opts = {
|
|
||||||
.name = "blkdebug",
|
|
||||||
.head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
|
|
||||||
.desc = {
|
|
||||||
{
|
|
||||||
.name = "config",
|
|
||||||
.type = QEMU_OPT_STRING,
|
|
||||||
.help = "Path to the configuration file",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
.name = "x-image",
|
|
||||||
.type = QEMU_OPT_STRING,
|
|
||||||
.help = "[internal use only, will be removed]",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
.name = "align",
|
|
||||||
.type = QEMU_OPT_SIZE,
|
|
||||||
.help = "Required alignment in bytes",
|
|
||||||
},
|
|
||||||
{ /* end of list */ }
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
|
|
||||||
Error **errp)
|
|
||||||
{
|
{
|
||||||
BDRVBlkdebugState *s = bs->opaque;
|
BDRVBlkdebugState *s = bs->opaque;
|
||||||
QemuOpts *opts;
|
|
||||||
Error *local_err = NULL;
|
|
||||||
const char *config;
|
|
||||||
uint64_t align;
|
|
||||||
int ret;
|
int ret;
|
||||||
|
char *config, *c;
|
||||||
|
|
||||||
opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
|
/* Parse the blkdebug: prefix */
|
||||||
qemu_opts_absorb_qdict(opts, options, &local_err);
|
if (strncmp(filename, "blkdebug:", strlen("blkdebug:"))) {
|
||||||
if (local_err) {
|
return -EINVAL;
|
||||||
error_propagate(errp, local_err);
|
}
|
||||||
ret = -EINVAL;
|
filename += strlen("blkdebug:");
|
||||||
goto out;
|
|
||||||
|
/* Read rules from config file */
|
||||||
|
c = strchr(filename, ':');
|
||||||
|
if (c == NULL) {
|
||||||
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Read rules from config file or command line options */
|
config = g_strdup(filename);
|
||||||
config = qemu_opt_get(opts, "config");
|
config[c - filename] = '\0';
|
||||||
ret = read_config(s, config, options, errp);
|
ret = read_config(s, config);
|
||||||
if (ret) {
|
g_free(config);
|
||||||
goto out;
|
if (ret < 0) {
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
filename = c + 1;
|
||||||
|
|
||||||
/* Set initial state */
|
/* Set initial state */
|
||||||
s->state = 1;
|
s->state = 1;
|
||||||
|
|
||||||
/* Open the image file */
|
/* Open the backing file */
|
||||||
bs->file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options, "image",
|
ret = bdrv_file_open(&bs->file, filename, flags);
|
||||||
bs, &child_file, false, &local_err);
|
if (ret < 0) {
|
||||||
if (local_err) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
error_propagate(errp, local_err);
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Set request alignment */
|
|
||||||
align = qemu_opt_get_size(opts, "align", bs->request_alignment);
|
|
||||||
if (align > 0 && align < INT_MAX && !(align & (align - 1))) {
|
|
||||||
bs->request_alignment = align;
|
|
||||||
} else {
|
|
||||||
error_setg(errp, "Invalid alignment");
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail_unref;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = 0;
|
|
||||||
goto out;
|
|
||||||
|
|
||||||
fail_unref:
|
|
||||||
bdrv_unref_child(bs, bs->file);
|
|
||||||
out:
|
|
||||||
qemu_opts_del(opts);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static void error_callback_bh(void *opaque)
|
static void error_callback_bh(void *opaque)
|
||||||
{
|
{
|
||||||
struct BlkdebugAIOCB *acb = opaque;
|
struct BlkdebugAIOCB *acb = opaque;
|
||||||
qemu_bh_delete(acb->bh);
|
qemu_bh_delete(acb->bh);
|
||||||
acb->common.cb(acb->common.opaque, acb->ret);
|
acb->common.cb(acb->common.opaque, acb->ret);
|
||||||
qemu_aio_unref(acb);
|
qemu_aio_release(acb);
|
||||||
}
|
}
|
||||||
|
|
||||||
static BlockAIOCB *inject_error(BlockDriverState *bs,
|
static void blkdebug_aio_cancel(BlockDriverAIOCB *blockacb)
|
||||||
BlockCompletionFunc *cb, void *opaque, BlkdebugRule *rule)
|
{
|
||||||
|
BlkdebugAIOCB *acb = container_of(blockacb, BlkdebugAIOCB, common);
|
||||||
|
qemu_aio_release(acb);
|
||||||
|
}
|
||||||
|
|
||||||
|
static BlockDriverAIOCB *inject_error(BlockDriverState *bs,
|
||||||
|
BlockDriverCompletionFunc *cb, void *opaque, BlkdebugRule *rule)
|
||||||
{
|
{
|
||||||
BDRVBlkdebugState *s = bs->opaque;
|
BDRVBlkdebugState *s = bs->opaque;
|
||||||
int error = rule->options.inject.error;
|
int error = rule->options.inject.error;
|
||||||
struct BlkdebugAIOCB *acb;
|
struct BlkdebugAIOCB *acb;
|
||||||
QEMUBH *bh;
|
QEMUBH *bh;
|
||||||
bool immediately = rule->options.inject.immediately;
|
|
||||||
|
|
||||||
if (rule->options.inject.once) {
|
if (rule->options.inject.once) {
|
||||||
QSIMPLEQ_REMOVE(&s->active_rules, rule, BlkdebugRule, active_next);
|
QSIMPLEQ_INIT(&s->active_rules);
|
||||||
remove_rule(rule);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (immediately) {
|
if (rule->options.inject.immediately) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
acb = qemu_aio_get(&blkdebug_aiocb_info, bs, cb, opaque);
|
acb = qemu_aio_get(&blkdebug_aiocb_info, bs, cb, opaque);
|
||||||
acb->ret = -error;
|
acb->ret = -error;
|
||||||
|
|
||||||
bh = aio_bh_new(bdrv_get_aio_context(bs), error_callback_bh, acb);
|
bh = qemu_bh_new(error_callback_bh, acb);
|
||||||
acb->bh = bh;
|
acb->bh = bh;
|
||||||
qemu_bh_schedule(bh);
|
qemu_bh_schedule(bh);
|
||||||
|
|
||||||
return &acb->common;
|
return &acb->common;
|
||||||
}
|
}
|
||||||
|
|
||||||
static BlockAIOCB *blkdebug_aio_readv(BlockDriverState *bs,
|
static BlockDriverAIOCB *blkdebug_aio_readv(BlockDriverState *bs,
|
||||||
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
||||||
BlockCompletionFunc *cb, void *opaque)
|
BlockDriverCompletionFunc *cb, void *opaque)
|
||||||
{
|
{
|
||||||
BDRVBlkdebugState *s = bs->opaque;
|
BDRVBlkdebugState *s = bs->opaque;
|
||||||
BlkdebugRule *rule = NULL;
|
BlkdebugRule *rule = NULL;
|
||||||
@@ -454,13 +402,12 @@ static BlockAIOCB *blkdebug_aio_readv(BlockDriverState *bs,
|
|||||||
return inject_error(bs, cb, opaque, rule);
|
return inject_error(bs, cb, opaque, rule);
|
||||||
}
|
}
|
||||||
|
|
||||||
return bdrv_aio_readv(bs->file->bs, sector_num, qiov, nb_sectors,
|
return bdrv_aio_readv(bs->file, sector_num, qiov, nb_sectors, cb, opaque);
|
||||||
cb, opaque);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static BlockAIOCB *blkdebug_aio_writev(BlockDriverState *bs,
|
static BlockDriverAIOCB *blkdebug_aio_writev(BlockDriverState *bs,
|
||||||
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
||||||
BlockCompletionFunc *cb, void *opaque)
|
BlockDriverCompletionFunc *cb, void *opaque)
|
||||||
{
|
{
|
||||||
BDRVBlkdebugState *s = bs->opaque;
|
BDRVBlkdebugState *s = bs->opaque;
|
||||||
BlkdebugRule *rule = NULL;
|
BlkdebugRule *rule = NULL;
|
||||||
@@ -477,27 +424,7 @@ static BlockAIOCB *blkdebug_aio_writev(BlockDriverState *bs,
|
|||||||
return inject_error(bs, cb, opaque, rule);
|
return inject_error(bs, cb, opaque, rule);
|
||||||
}
|
}
|
||||||
|
|
||||||
return bdrv_aio_writev(bs->file->bs, sector_num, qiov, nb_sectors,
|
return bdrv_aio_writev(bs->file, sector_num, qiov, nb_sectors, cb, opaque);
|
||||||
cb, opaque);
|
|
||||||
}
|
|
||||||
|
|
||||||
static BlockAIOCB *blkdebug_aio_flush(BlockDriverState *bs,
|
|
||||||
BlockCompletionFunc *cb, void *opaque)
|
|
||||||
{
|
|
||||||
BDRVBlkdebugState *s = bs->opaque;
|
|
||||||
BlkdebugRule *rule = NULL;
|
|
||||||
|
|
||||||
QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
|
|
||||||
if (rule->options.inject.sector == -1) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rule && rule->options.inject.error) {
|
|
||||||
return inject_error(bs, cb, opaque, rule);
|
|
||||||
}
|
|
||||||
|
|
||||||
return bdrv_aio_flush(bs->file->bs, cb, opaque);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -507,7 +434,7 @@ static void blkdebug_close(BlockDriverState *bs)
|
|||||||
BlkdebugRule *rule, *next;
|
BlkdebugRule *rule, *next;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < BLKDBG__MAX; i++) {
|
for (i = 0; i < BLKDBG_EVENT_MAX; i++) {
|
||||||
QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
|
QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
|
||||||
remove_rule(rule);
|
remove_rule(rule);
|
||||||
}
|
}
|
||||||
@@ -527,13 +454,9 @@ static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule)
|
|||||||
remove_rule(rule);
|
remove_rule(rule);
|
||||||
QLIST_INSERT_HEAD(&s->suspended_reqs, &r, next);
|
QLIST_INSERT_HEAD(&s->suspended_reqs, &r, next);
|
||||||
|
|
||||||
if (!qtest_enabled()) {
|
|
||||||
printf("blkdebug: Suspended request '%s'\n", r.tag);
|
printf("blkdebug: Suspended request '%s'\n", r.tag);
|
||||||
}
|
|
||||||
qemu_coroutine_yield();
|
qemu_coroutine_yield();
|
||||||
if (!qtest_enabled()) {
|
|
||||||
printf("blkdebug: Resuming request '%s'\n", r.tag);
|
printf("blkdebug: Resuming request '%s'\n", r.tag);
|
||||||
}
|
|
||||||
|
|
||||||
QLIST_REMOVE(&r, next);
|
QLIST_REMOVE(&r, next);
|
||||||
g_free(r.tag);
|
g_free(r.tag);
|
||||||
@@ -570,13 +493,13 @@ static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule,
|
|||||||
return injected;
|
return injected;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void blkdebug_debug_event(BlockDriverState *bs, BlkdebugEvent event)
|
static void blkdebug_debug_event(BlockDriverState *bs, BlkDebugEvent event)
|
||||||
{
|
{
|
||||||
BDRVBlkdebugState *s = bs->opaque;
|
BDRVBlkdebugState *s = bs->opaque;
|
||||||
struct BlkdebugRule *rule, *next;
|
struct BlkdebugRule *rule, *next;
|
||||||
bool injected;
|
bool injected;
|
||||||
|
|
||||||
assert((int)event >= 0 && event < BLKDBG__MAX);
|
assert((int)event >= 0 && event < BLKDBG_EVENT_MAX);
|
||||||
|
|
||||||
injected = false;
|
injected = false;
|
||||||
s->new_state = s->state;
|
s->new_state = s->state;
|
||||||
@@ -591,7 +514,7 @@ static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event,
|
|||||||
{
|
{
|
||||||
BDRVBlkdebugState *s = bs->opaque;
|
BDRVBlkdebugState *s = bs->opaque;
|
||||||
struct BlkdebugRule *rule;
|
struct BlkdebugRule *rule;
|
||||||
BlkdebugEvent blkdebug_event;
|
BlkDebugEvent blkdebug_event;
|
||||||
|
|
||||||
if (get_event_by_name(event, &blkdebug_event) < 0) {
|
if (get_event_by_name(event, &blkdebug_event) < 0) {
|
||||||
return -ENOENT;
|
return -ENOENT;
|
||||||
@@ -614,9 +537,9 @@ static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event,
|
|||||||
static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag)
|
static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag)
|
||||||
{
|
{
|
||||||
BDRVBlkdebugState *s = bs->opaque;
|
BDRVBlkdebugState *s = bs->opaque;
|
||||||
BlkdebugSuspendedReq *r, *next;
|
BlkdebugSuspendedReq *r;
|
||||||
|
|
||||||
QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, next) {
|
QLIST_FOREACH(r, &s->suspended_reqs, next) {
|
||||||
if (!strcmp(r->tag, tag)) {
|
if (!strcmp(r->tag, tag)) {
|
||||||
qemu_coroutine_enter(r->co, NULL);
|
qemu_coroutine_enter(r->co, NULL);
|
||||||
return 0;
|
return 0;
|
||||||
@@ -625,31 +548,6 @@ static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag)
|
|||||||
return -ENOENT;
|
return -ENOENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int blkdebug_debug_remove_breakpoint(BlockDriverState *bs,
|
|
||||||
const char *tag)
|
|
||||||
{
|
|
||||||
BDRVBlkdebugState *s = bs->opaque;
|
|
||||||
BlkdebugSuspendedReq *r, *r_next;
|
|
||||||
BlkdebugRule *rule, *next;
|
|
||||||
int i, ret = -ENOENT;
|
|
||||||
|
|
||||||
for (i = 0; i < BLKDBG__MAX; i++) {
|
|
||||||
QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
|
|
||||||
if (rule->action == ACTION_SUSPEND &&
|
|
||||||
!strcmp(rule->options.suspend.tag, tag)) {
|
|
||||||
remove_rule(rule);
|
|
||||||
ret = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, r_next) {
|
|
||||||
if (!strcmp(r->tag, tag)) {
|
|
||||||
qemu_coroutine_enter(r->co, NULL);
|
|
||||||
ret = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag)
|
static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag)
|
||||||
{
|
{
|
||||||
@@ -666,85 +564,24 @@ static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag)
|
|||||||
|
|
||||||
static int64_t blkdebug_getlength(BlockDriverState *bs)
|
static int64_t blkdebug_getlength(BlockDriverState *bs)
|
||||||
{
|
{
|
||||||
return bdrv_getlength(bs->file->bs);
|
return bdrv_getlength(bs->file);
|
||||||
}
|
|
||||||
|
|
||||||
static int blkdebug_truncate(BlockDriverState *bs, int64_t offset)
|
|
||||||
{
|
|
||||||
return bdrv_truncate(bs->file->bs, offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void blkdebug_refresh_filename(BlockDriverState *bs, QDict *options)
|
|
||||||
{
|
|
||||||
QDict *opts;
|
|
||||||
const QDictEntry *e;
|
|
||||||
bool force_json = false;
|
|
||||||
|
|
||||||
for (e = qdict_first(options); e; e = qdict_next(options, e)) {
|
|
||||||
if (strcmp(qdict_entry_key(e), "config") &&
|
|
||||||
strcmp(qdict_entry_key(e), "x-image"))
|
|
||||||
{
|
|
||||||
force_json = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (force_json && !bs->file->bs->full_open_options) {
|
|
||||||
/* The config file cannot be recreated, so creating a plain filename
|
|
||||||
* is impossible */
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!force_json && bs->file->bs->exact_filename[0]) {
|
|
||||||
snprintf(bs->exact_filename, sizeof(bs->exact_filename),
|
|
||||||
"blkdebug:%s:%s",
|
|
||||||
qdict_get_try_str(options, "config") ?: "",
|
|
||||||
bs->file->bs->exact_filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
opts = qdict_new();
|
|
||||||
qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str("blkdebug")));
|
|
||||||
|
|
||||||
QINCREF(bs->file->bs->full_open_options);
|
|
||||||
qdict_put_obj(opts, "image", QOBJECT(bs->file->bs->full_open_options));
|
|
||||||
|
|
||||||
for (e = qdict_first(options); e; e = qdict_next(options, e)) {
|
|
||||||
if (strcmp(qdict_entry_key(e), "x-image")) {
|
|
||||||
qobject_incref(qdict_entry_value(e));
|
|
||||||
qdict_put_obj(opts, qdict_entry_key(e), qdict_entry_value(e));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bs->full_open_options = opts;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int blkdebug_reopen_prepare(BDRVReopenState *reopen_state,
|
|
||||||
BlockReopenQueue *queue, Error **errp)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static BlockDriver bdrv_blkdebug = {
|
static BlockDriver bdrv_blkdebug = {
|
||||||
.format_name = "blkdebug",
|
.format_name = "blkdebug",
|
||||||
.protocol_name = "blkdebug",
|
.protocol_name = "blkdebug",
|
||||||
|
|
||||||
.instance_size = sizeof(BDRVBlkdebugState),
|
.instance_size = sizeof(BDRVBlkdebugState),
|
||||||
|
|
||||||
.bdrv_parse_filename = blkdebug_parse_filename,
|
|
||||||
.bdrv_file_open = blkdebug_open,
|
.bdrv_file_open = blkdebug_open,
|
||||||
.bdrv_close = blkdebug_close,
|
.bdrv_close = blkdebug_close,
|
||||||
.bdrv_reopen_prepare = blkdebug_reopen_prepare,
|
|
||||||
.bdrv_getlength = blkdebug_getlength,
|
.bdrv_getlength = blkdebug_getlength,
|
||||||
.bdrv_truncate = blkdebug_truncate,
|
|
||||||
.bdrv_refresh_filename = blkdebug_refresh_filename,
|
|
||||||
|
|
||||||
.bdrv_aio_readv = blkdebug_aio_readv,
|
.bdrv_aio_readv = blkdebug_aio_readv,
|
||||||
.bdrv_aio_writev = blkdebug_aio_writev,
|
.bdrv_aio_writev = blkdebug_aio_writev,
|
||||||
.bdrv_aio_flush = blkdebug_aio_flush,
|
|
||||||
|
|
||||||
.bdrv_debug_event = blkdebug_debug_event,
|
.bdrv_debug_event = blkdebug_debug_event,
|
||||||
.bdrv_debug_breakpoint = blkdebug_debug_breakpoint,
|
.bdrv_debug_breakpoint = blkdebug_debug_breakpoint,
|
||||||
.bdrv_debug_remove_breakpoint
|
|
||||||
= blkdebug_debug_remove_breakpoint,
|
|
||||||
.bdrv_debug_resume = blkdebug_debug_resume,
|
.bdrv_debug_resume = blkdebug_debug_resume,
|
||||||
.bdrv_debug_is_suspended = blkdebug_debug_is_suspended,
|
.bdrv_debug_is_suspended = blkdebug_debug_is_suspended,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -10,16 +10,14 @@
|
|||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include "qemu/sockets.h" /* for EINPROGRESS on Windows */
|
#include "qemu/sockets.h" /* for EINPROGRESS on Windows */
|
||||||
#include "block/block_int.h"
|
#include "block/block_int.h"
|
||||||
#include "qapi/qmp/qdict.h"
|
|
||||||
#include "qapi/qmp/qstring.h"
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
BdrvChild *test_file;
|
BlockDriverState *test_file;
|
||||||
} BDRVBlkverifyState;
|
} BDRVBlkverifyState;
|
||||||
|
|
||||||
typedef struct BlkverifyAIOCB BlkverifyAIOCB;
|
typedef struct BlkverifyAIOCB BlkverifyAIOCB;
|
||||||
struct BlkverifyAIOCB {
|
struct BlkverifyAIOCB {
|
||||||
BlockAIOCB common;
|
BlockDriverAIOCB common;
|
||||||
QEMUBH *bh;
|
QEMUBH *bh;
|
||||||
|
|
||||||
/* Request metadata */
|
/* Request metadata */
|
||||||
@@ -29,6 +27,7 @@ struct BlkverifyAIOCB {
|
|||||||
|
|
||||||
int ret; /* first completed request's result */
|
int ret; /* first completed request's result */
|
||||||
unsigned int done; /* completion counter */
|
unsigned int done; /* completion counter */
|
||||||
|
bool *finished; /* completion signal for cancel */
|
||||||
|
|
||||||
QEMUIOVector *qiov; /* user I/O vector */
|
QEMUIOVector *qiov; /* user I/O vector */
|
||||||
QEMUIOVector raw_qiov; /* cloned I/O vector for raw file */
|
QEMUIOVector raw_qiov; /* cloned I/O vector for raw file */
|
||||||
@@ -37,8 +36,21 @@ struct BlkverifyAIOCB {
|
|||||||
void (*verify)(BlkverifyAIOCB *acb);
|
void (*verify)(BlkverifyAIOCB *acb);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static void blkverify_aio_cancel(BlockDriverAIOCB *blockacb)
|
||||||
|
{
|
||||||
|
BlkverifyAIOCB *acb = (BlkverifyAIOCB *)blockacb;
|
||||||
|
bool finished = false;
|
||||||
|
|
||||||
|
/* Wait until request completes, invokes its callback, and frees itself */
|
||||||
|
acb->finished = &finished;
|
||||||
|
while (!finished) {
|
||||||
|
qemu_aio_wait();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static const AIOCBInfo blkverify_aiocb_info = {
|
static const AIOCBInfo blkverify_aiocb_info = {
|
||||||
.aiocb_size = sizeof(BlkverifyAIOCB),
|
.aiocb_size = sizeof(BlkverifyAIOCB),
|
||||||
|
.cancel = blkverify_aio_cancel,
|
||||||
};
|
};
|
||||||
|
|
||||||
static void GCC_FMT_ATTR(2, 3) blkverify_err(BlkverifyAIOCB *acb,
|
static void GCC_FMT_ATTR(2, 3) blkverify_err(BlkverifyAIOCB *acb,
|
||||||
@@ -57,104 +69,50 @@ static void GCC_FMT_ATTR(2, 3) blkverify_err(BlkverifyAIOCB *acb,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Valid blkverify filenames look like blkverify:path/to/raw_image:path/to/image */
|
/* Valid blkverify filenames look like blkverify:path/to/raw_image:path/to/image */
|
||||||
static void blkverify_parse_filename(const char *filename, QDict *options,
|
static int blkverify_open(BlockDriverState *bs, const char *filename, int flags)
|
||||||
Error **errp)
|
|
||||||
{
|
{
|
||||||
const char *c;
|
BDRVBlkverifyState *s = bs->opaque;
|
||||||
QString *raw_path;
|
int ret;
|
||||||
|
char *raw, *c;
|
||||||
|
|
||||||
/* Parse the blkverify: prefix */
|
/* Parse the blkverify: prefix */
|
||||||
if (!strstart(filename, "blkverify:", &filename)) {
|
if (strncmp(filename, "blkverify:", strlen("blkverify:"))) {
|
||||||
/* There was no prefix; therefore, all options have to be already
|
return -EINVAL;
|
||||||
present in the QDict (except for the filename) */
|
|
||||||
qdict_put(options, "x-image", qstring_from_str(filename));
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
filename += strlen("blkverify:");
|
||||||
|
|
||||||
/* Parse the raw image filename */
|
/* Parse the raw image filename */
|
||||||
c = strchr(filename, ':');
|
c = strchr(filename, ':');
|
||||||
if (c == NULL) {
|
if (c == NULL) {
|
||||||
error_setg(errp, "blkverify requires raw copy and original image path");
|
return -EINVAL;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO Implement option pass-through and set raw.filename here */
|
raw = g_strdup(filename);
|
||||||
raw_path = qstring_from_substr(filename, 0, c - filename - 1);
|
raw[c - filename] = '\0';
|
||||||
qdict_put(options, "x-raw", raw_path);
|
ret = bdrv_file_open(&bs->file, raw, flags);
|
||||||
|
g_free(raw);
|
||||||
/* TODO Allow multi-level nesting and set file.filename here */
|
if (ret < 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
filename = c + 1;
|
filename = c + 1;
|
||||||
qdict_put(options, "x-image", qstring_from_str(filename));
|
|
||||||
}
|
|
||||||
|
|
||||||
static QemuOptsList runtime_opts = {
|
|
||||||
.name = "blkverify",
|
|
||||||
.head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
|
|
||||||
.desc = {
|
|
||||||
{
|
|
||||||
.name = "x-raw",
|
|
||||||
.type = QEMU_OPT_STRING,
|
|
||||||
.help = "[internal use only, will be removed]",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
.name = "x-image",
|
|
||||||
.type = QEMU_OPT_STRING,
|
|
||||||
.help = "[internal use only, will be removed]",
|
|
||||||
},
|
|
||||||
{ /* end of list */ }
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
static int blkverify_open(BlockDriverState *bs, QDict *options, int flags,
|
|
||||||
Error **errp)
|
|
||||||
{
|
|
||||||
BDRVBlkverifyState *s = bs->opaque;
|
|
||||||
QemuOpts *opts;
|
|
||||||
Error *local_err = NULL;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
|
|
||||||
qemu_opts_absorb_qdict(opts, options, &local_err);
|
|
||||||
if (local_err) {
|
|
||||||
error_propagate(errp, local_err);
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Open the raw file */
|
|
||||||
bs->file = bdrv_open_child(qemu_opt_get(opts, "x-raw"), options, "raw",
|
|
||||||
bs, &child_file, false, &local_err);
|
|
||||||
if (local_err) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
error_propagate(errp, local_err);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Open the test file */
|
/* Open the test file */
|
||||||
s->test_file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options,
|
s->test_file = bdrv_new("");
|
||||||
"test", bs, &child_format, false,
|
ret = bdrv_open(s->test_file, filename, flags, NULL);
|
||||||
&local_err);
|
if (ret < 0) {
|
||||||
if (local_err) {
|
bdrv_delete(s->test_file);
|
||||||
ret = -EINVAL;
|
s->test_file = NULL;
|
||||||
error_propagate(errp, local_err);
|
return ret;
|
||||||
goto fail;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = 0;
|
return 0;
|
||||||
fail:
|
|
||||||
if (ret < 0) {
|
|
||||||
bdrv_unref_child(bs, bs->file);
|
|
||||||
}
|
|
||||||
qemu_opts_del(opts);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void blkverify_close(BlockDriverState *bs)
|
static void blkverify_close(BlockDriverState *bs)
|
||||||
{
|
{
|
||||||
BDRVBlkverifyState *s = bs->opaque;
|
BDRVBlkverifyState *s = bs->opaque;
|
||||||
|
|
||||||
bdrv_unref_child(bs, s->test_file);
|
bdrv_delete(s->test_file);
|
||||||
s->test_file = NULL;
|
s->test_file = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,13 +120,117 @@ static int64_t blkverify_getlength(BlockDriverState *bs)
|
|||||||
{
|
{
|
||||||
BDRVBlkverifyState *s = bs->opaque;
|
BDRVBlkverifyState *s = bs->opaque;
|
||||||
|
|
||||||
return bdrv_getlength(s->test_file->bs);
|
return bdrv_getlength(s->test_file);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check that I/O vector contents are identical
|
||||||
|
*
|
||||||
|
* @a: I/O vector
|
||||||
|
* @b: I/O vector
|
||||||
|
* @ret: Offset to first mismatching byte or -1 if match
|
||||||
|
*/
|
||||||
|
static ssize_t blkverify_iovec_compare(QEMUIOVector *a, QEMUIOVector *b)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
ssize_t offset = 0;
|
||||||
|
|
||||||
|
assert(a->niov == b->niov);
|
||||||
|
for (i = 0; i < a->niov; i++) {
|
||||||
|
size_t len = 0;
|
||||||
|
uint8_t *p = (uint8_t *)a->iov[i].iov_base;
|
||||||
|
uint8_t *q = (uint8_t *)b->iov[i].iov_base;
|
||||||
|
|
||||||
|
assert(a->iov[i].iov_len == b->iov[i].iov_len);
|
||||||
|
while (len < a->iov[i].iov_len && *p++ == *q++) {
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
|
||||||
|
offset += len;
|
||||||
|
|
||||||
|
if (len != a->iov[i].iov_len) {
|
||||||
|
return offset;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int src_index;
|
||||||
|
struct iovec *src_iov;
|
||||||
|
void *dest_base;
|
||||||
|
} IOVectorSortElem;
|
||||||
|
|
||||||
|
static int sortelem_cmp_src_base(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
const IOVectorSortElem *elem_a = a;
|
||||||
|
const IOVectorSortElem *elem_b = b;
|
||||||
|
|
||||||
|
/* Don't overflow */
|
||||||
|
if (elem_a->src_iov->iov_base < elem_b->src_iov->iov_base) {
|
||||||
|
return -1;
|
||||||
|
} else if (elem_a->src_iov->iov_base > elem_b->src_iov->iov_base) {
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int sortelem_cmp_src_index(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
const IOVectorSortElem *elem_a = a;
|
||||||
|
const IOVectorSortElem *elem_b = b;
|
||||||
|
|
||||||
|
return elem_a->src_index - elem_b->src_index;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copy contents of I/O vector
|
||||||
|
*
|
||||||
|
* The relative relationships of overlapping iovecs are preserved. This is
|
||||||
|
* necessary to ensure identical semantics in the cloned I/O vector.
|
||||||
|
*/
|
||||||
|
static void blkverify_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src,
|
||||||
|
void *buf)
|
||||||
|
{
|
||||||
|
IOVectorSortElem sortelems[src->niov];
|
||||||
|
void *last_end;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
/* Sort by source iovecs by base address */
|
||||||
|
for (i = 0; i < src->niov; i++) {
|
||||||
|
sortelems[i].src_index = i;
|
||||||
|
sortelems[i].src_iov = &src->iov[i];
|
||||||
|
}
|
||||||
|
qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_base);
|
||||||
|
|
||||||
|
/* Allocate buffer space taking into account overlapping iovecs */
|
||||||
|
last_end = NULL;
|
||||||
|
for (i = 0; i < src->niov; i++) {
|
||||||
|
struct iovec *cur = sortelems[i].src_iov;
|
||||||
|
ptrdiff_t rewind = 0;
|
||||||
|
|
||||||
|
/* Detect overlap */
|
||||||
|
if (last_end && last_end > cur->iov_base) {
|
||||||
|
rewind = last_end - cur->iov_base;
|
||||||
|
}
|
||||||
|
|
||||||
|
sortelems[i].dest_base = buf - rewind;
|
||||||
|
buf += cur->iov_len - MIN(rewind, cur->iov_len);
|
||||||
|
last_end = MAX(cur->iov_base + cur->iov_len, last_end);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Sort by source iovec index and build destination iovec */
|
||||||
|
qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_index);
|
||||||
|
for (i = 0; i < src->niov; i++) {
|
||||||
|
qemu_iovec_add(dest, sortelems[i].dest_base, src->iov[i].iov_len);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static BlkverifyAIOCB *blkverify_aio_get(BlockDriverState *bs, bool is_write,
|
static BlkverifyAIOCB *blkverify_aio_get(BlockDriverState *bs, bool is_write,
|
||||||
int64_t sector_num, QEMUIOVector *qiov,
|
int64_t sector_num, QEMUIOVector *qiov,
|
||||||
int nb_sectors,
|
int nb_sectors,
|
||||||
BlockCompletionFunc *cb,
|
BlockDriverCompletionFunc *cb,
|
||||||
void *opaque)
|
void *opaque)
|
||||||
{
|
{
|
||||||
BlkverifyAIOCB *acb = qemu_aio_get(&blkverify_aiocb_info, bs, cb, opaque);
|
BlkverifyAIOCB *acb = qemu_aio_get(&blkverify_aiocb_info, bs, cb, opaque);
|
||||||
@@ -182,6 +244,7 @@ static BlkverifyAIOCB *blkverify_aio_get(BlockDriverState *bs, bool is_write,
|
|||||||
acb->qiov = qiov;
|
acb->qiov = qiov;
|
||||||
acb->buf = NULL;
|
acb->buf = NULL;
|
||||||
acb->verify = NULL;
|
acb->verify = NULL;
|
||||||
|
acb->finished = NULL;
|
||||||
return acb;
|
return acb;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -195,7 +258,10 @@ static void blkverify_aio_bh(void *opaque)
|
|||||||
qemu_vfree(acb->buf);
|
qemu_vfree(acb->buf);
|
||||||
}
|
}
|
||||||
acb->common.cb(acb->common.opaque, acb->ret);
|
acb->common.cb(acb->common.opaque, acb->ret);
|
||||||
qemu_aio_unref(acb);
|
if (acb->finished) {
|
||||||
|
*acb->finished = true;
|
||||||
|
}
|
||||||
|
qemu_aio_release(acb);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void blkverify_aio_cb(void *opaque, int ret)
|
static void blkverify_aio_cb(void *opaque, int ret)
|
||||||
@@ -216,8 +282,7 @@ static void blkverify_aio_cb(void *opaque, int ret)
|
|||||||
acb->verify(acb);
|
acb->verify(acb);
|
||||||
}
|
}
|
||||||
|
|
||||||
acb->bh = aio_bh_new(bdrv_get_aio_context(acb->common.bs),
|
acb->bh = qemu_bh_new(blkverify_aio_bh, acb);
|
||||||
blkverify_aio_bh, acb);
|
|
||||||
qemu_bh_schedule(acb->bh);
|
qemu_bh_schedule(acb->bh);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -225,140 +290,72 @@ static void blkverify_aio_cb(void *opaque, int ret)
|
|||||||
|
|
||||||
static void blkverify_verify_readv(BlkverifyAIOCB *acb)
|
static void blkverify_verify_readv(BlkverifyAIOCB *acb)
|
||||||
{
|
{
|
||||||
ssize_t offset = qemu_iovec_compare(acb->qiov, &acb->raw_qiov);
|
ssize_t offset = blkverify_iovec_compare(acb->qiov, &acb->raw_qiov);
|
||||||
if (offset != -1) {
|
if (offset != -1) {
|
||||||
blkverify_err(acb, "contents mismatch in sector %" PRId64,
|
blkverify_err(acb, "contents mismatch in sector %" PRId64,
|
||||||
acb->sector_num + (int64_t)(offset / BDRV_SECTOR_SIZE));
|
acb->sector_num + (int64_t)(offset / BDRV_SECTOR_SIZE));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static BlockAIOCB *blkverify_aio_readv(BlockDriverState *bs,
|
static BlockDriverAIOCB *blkverify_aio_readv(BlockDriverState *bs,
|
||||||
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
||||||
BlockCompletionFunc *cb, void *opaque)
|
BlockDriverCompletionFunc *cb, void *opaque)
|
||||||
{
|
{
|
||||||
BDRVBlkverifyState *s = bs->opaque;
|
BDRVBlkverifyState *s = bs->opaque;
|
||||||
BlkverifyAIOCB *acb = blkverify_aio_get(bs, false, sector_num, qiov,
|
BlkverifyAIOCB *acb = blkverify_aio_get(bs, false, sector_num, qiov,
|
||||||
nb_sectors, cb, opaque);
|
nb_sectors, cb, opaque);
|
||||||
|
|
||||||
acb->verify = blkverify_verify_readv;
|
acb->verify = blkverify_verify_readv;
|
||||||
acb->buf = qemu_blockalign(bs->file->bs, qiov->size);
|
acb->buf = qemu_blockalign(bs->file, qiov->size);
|
||||||
qemu_iovec_init(&acb->raw_qiov, acb->qiov->niov);
|
qemu_iovec_init(&acb->raw_qiov, acb->qiov->niov);
|
||||||
qemu_iovec_clone(&acb->raw_qiov, qiov, acb->buf);
|
blkverify_iovec_clone(&acb->raw_qiov, qiov, acb->buf);
|
||||||
|
|
||||||
bdrv_aio_readv(s->test_file->bs, sector_num, qiov, nb_sectors,
|
bdrv_aio_readv(s->test_file, sector_num, qiov, nb_sectors,
|
||||||
blkverify_aio_cb, acb);
|
blkverify_aio_cb, acb);
|
||||||
bdrv_aio_readv(bs->file->bs, sector_num, &acb->raw_qiov, nb_sectors,
|
bdrv_aio_readv(bs->file, sector_num, &acb->raw_qiov, nb_sectors,
|
||||||
blkverify_aio_cb, acb);
|
blkverify_aio_cb, acb);
|
||||||
return &acb->common;
|
return &acb->common;
|
||||||
}
|
}
|
||||||
|
|
||||||
static BlockAIOCB *blkverify_aio_writev(BlockDriverState *bs,
|
static BlockDriverAIOCB *blkverify_aio_writev(BlockDriverState *bs,
|
||||||
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
||||||
BlockCompletionFunc *cb, void *opaque)
|
BlockDriverCompletionFunc *cb, void *opaque)
|
||||||
{
|
{
|
||||||
BDRVBlkverifyState *s = bs->opaque;
|
BDRVBlkverifyState *s = bs->opaque;
|
||||||
BlkverifyAIOCB *acb = blkverify_aio_get(bs, true, sector_num, qiov,
|
BlkverifyAIOCB *acb = blkverify_aio_get(bs, true, sector_num, qiov,
|
||||||
nb_sectors, cb, opaque);
|
nb_sectors, cb, opaque);
|
||||||
|
|
||||||
bdrv_aio_writev(s->test_file->bs, sector_num, qiov, nb_sectors,
|
bdrv_aio_writev(s->test_file, sector_num, qiov, nb_sectors,
|
||||||
blkverify_aio_cb, acb);
|
blkverify_aio_cb, acb);
|
||||||
bdrv_aio_writev(bs->file->bs, sector_num, qiov, nb_sectors,
|
bdrv_aio_writev(bs->file, sector_num, qiov, nb_sectors,
|
||||||
blkverify_aio_cb, acb);
|
blkverify_aio_cb, acb);
|
||||||
return &acb->common;
|
return &acb->common;
|
||||||
}
|
}
|
||||||
|
|
||||||
static BlockAIOCB *blkverify_aio_flush(BlockDriverState *bs,
|
static BlockDriverAIOCB *blkverify_aio_flush(BlockDriverState *bs,
|
||||||
BlockCompletionFunc *cb,
|
BlockDriverCompletionFunc *cb,
|
||||||
void *opaque)
|
void *opaque)
|
||||||
{
|
{
|
||||||
BDRVBlkverifyState *s = bs->opaque;
|
BDRVBlkverifyState *s = bs->opaque;
|
||||||
|
|
||||||
/* Only flush test file, the raw file is not important */
|
/* Only flush test file, the raw file is not important */
|
||||||
return bdrv_aio_flush(s->test_file->bs, cb, opaque);
|
return bdrv_aio_flush(s->test_file, cb, opaque);
|
||||||
}
|
|
||||||
|
|
||||||
static bool blkverify_recurse_is_first_non_filter(BlockDriverState *bs,
|
|
||||||
BlockDriverState *candidate)
|
|
||||||
{
|
|
||||||
BDRVBlkverifyState *s = bs->opaque;
|
|
||||||
|
|
||||||
bool perm = bdrv_recurse_is_first_non_filter(bs->file->bs, candidate);
|
|
||||||
|
|
||||||
if (perm) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return bdrv_recurse_is_first_non_filter(s->test_file->bs, candidate);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Propagate AioContext changes to ->test_file */
|
|
||||||
static void blkverify_detach_aio_context(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
BDRVBlkverifyState *s = bs->opaque;
|
|
||||||
|
|
||||||
bdrv_detach_aio_context(s->test_file->bs);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void blkverify_attach_aio_context(BlockDriverState *bs,
|
|
||||||
AioContext *new_context)
|
|
||||||
{
|
|
||||||
BDRVBlkverifyState *s = bs->opaque;
|
|
||||||
|
|
||||||
bdrv_attach_aio_context(s->test_file->bs, new_context);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void blkverify_refresh_filename(BlockDriverState *bs, QDict *options)
|
|
||||||
{
|
|
||||||
BDRVBlkverifyState *s = bs->opaque;
|
|
||||||
|
|
||||||
/* bs->file->bs has already been refreshed */
|
|
||||||
bdrv_refresh_filename(s->test_file->bs);
|
|
||||||
|
|
||||||
if (bs->file->bs->full_open_options
|
|
||||||
&& s->test_file->bs->full_open_options)
|
|
||||||
{
|
|
||||||
QDict *opts = qdict_new();
|
|
||||||
qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str("blkverify")));
|
|
||||||
|
|
||||||
QINCREF(bs->file->bs->full_open_options);
|
|
||||||
qdict_put_obj(opts, "raw", QOBJECT(bs->file->bs->full_open_options));
|
|
||||||
QINCREF(s->test_file->bs->full_open_options);
|
|
||||||
qdict_put_obj(opts, "test",
|
|
||||||
QOBJECT(s->test_file->bs->full_open_options));
|
|
||||||
|
|
||||||
bs->full_open_options = opts;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bs->file->bs->exact_filename[0]
|
|
||||||
&& s->test_file->bs->exact_filename[0])
|
|
||||||
{
|
|
||||||
snprintf(bs->exact_filename, sizeof(bs->exact_filename),
|
|
||||||
"blkverify:%s:%s",
|
|
||||||
bs->file->bs->exact_filename,
|
|
||||||
s->test_file->bs->exact_filename);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static BlockDriver bdrv_blkverify = {
|
static BlockDriver bdrv_blkverify = {
|
||||||
.format_name = "blkverify",
|
.format_name = "blkverify",
|
||||||
.protocol_name = "blkverify",
|
.protocol_name = "blkverify",
|
||||||
|
|
||||||
.instance_size = sizeof(BDRVBlkverifyState),
|
.instance_size = sizeof(BDRVBlkverifyState),
|
||||||
|
|
||||||
.bdrv_parse_filename = blkverify_parse_filename,
|
.bdrv_getlength = blkverify_getlength,
|
||||||
|
|
||||||
.bdrv_file_open = blkverify_open,
|
.bdrv_file_open = blkverify_open,
|
||||||
.bdrv_close = blkverify_close,
|
.bdrv_close = blkverify_close,
|
||||||
.bdrv_getlength = blkverify_getlength,
|
|
||||||
.bdrv_refresh_filename = blkverify_refresh_filename,
|
|
||||||
|
|
||||||
.bdrv_aio_readv = blkverify_aio_readv,
|
.bdrv_aio_readv = blkverify_aio_readv,
|
||||||
.bdrv_aio_writev = blkverify_aio_writev,
|
.bdrv_aio_writev = blkverify_aio_writev,
|
||||||
.bdrv_aio_flush = blkverify_aio_flush,
|
.bdrv_aio_flush = blkverify_aio_flush,
|
||||||
|
|
||||||
.bdrv_attach_aio_context = blkverify_attach_aio_context,
|
|
||||||
.bdrv_detach_aio_context = blkverify_detach_aio_context,
|
|
||||||
|
|
||||||
.is_filter = true,
|
|
||||||
.bdrv_recurse_is_first_non_filter = blkverify_recurse_is_first_non_filter,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static void bdrv_blkverify_init(void)
|
static void bdrv_blkverify_init(void)
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -93,8 +93,7 @@ static int bochs_probe(const uint8_t *buf, int buf_size, const char *filename)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int bochs_open(BlockDriverState *bs, QDict *options, int flags,
|
static int bochs_open(BlockDriverState *bs, int flags)
|
||||||
Error **errp)
|
|
||||||
{
|
{
|
||||||
BDRVBochsState *s = bs->opaque;
|
BDRVBochsState *s = bs->opaque;
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
@@ -103,7 +102,7 @@ static int bochs_open(BlockDriverState *bs, QDict *options, int flags,
|
|||||||
|
|
||||||
bs->read_only = 1; // no write support yet
|
bs->read_only = 1; // no write support yet
|
||||||
|
|
||||||
ret = bdrv_pread(bs->file->bs, 0, &bochs, sizeof(bochs));
|
ret = bdrv_pread(bs->file, 0, &bochs, sizeof(bochs));
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -113,8 +112,7 @@ static int bochs_open(BlockDriverState *bs, QDict *options, int flags,
|
|||||||
strcmp(bochs.subtype, GROWING_TYPE) ||
|
strcmp(bochs.subtype, GROWING_TYPE) ||
|
||||||
((le32_to_cpu(bochs.version) != HEADER_VERSION) &&
|
((le32_to_cpu(bochs.version) != HEADER_VERSION) &&
|
||||||
(le32_to_cpu(bochs.version) != HEADER_V1))) {
|
(le32_to_cpu(bochs.version) != HEADER_V1))) {
|
||||||
error_setg(errp, "Image not in Bochs format");
|
return -EMEDIUMTYPE;
|
||||||
return -EINVAL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (le32_to_cpu(bochs.version) == HEADER_V1) {
|
if (le32_to_cpu(bochs.version) == HEADER_V1) {
|
||||||
@@ -127,17 +125,14 @@ static int bochs_open(BlockDriverState *bs, QDict *options, int flags,
|
|||||||
* needed for the largest image that bximage can create (~8 TB). */
|
* needed for the largest image that bximage can create (~8 TB). */
|
||||||
s->catalog_size = le32_to_cpu(bochs.catalog);
|
s->catalog_size = le32_to_cpu(bochs.catalog);
|
||||||
if (s->catalog_size > 0x100000) {
|
if (s->catalog_size > 0x100000) {
|
||||||
error_setg(errp, "Catalog size is too large");
|
qerror_report(ERROR_CLASS_GENERIC_ERROR,
|
||||||
|
"Catalog size is too large");
|
||||||
return -EFBIG;
|
return -EFBIG;
|
||||||
}
|
}
|
||||||
|
|
||||||
s->catalog_bitmap = g_try_new(uint32_t, s->catalog_size);
|
s->catalog_bitmap = g_malloc(s->catalog_size * 4);
|
||||||
if (s->catalog_size && s->catalog_bitmap == NULL) {
|
|
||||||
error_setg(errp, "Could not allocate memory for catalog");
|
|
||||||
return -ENOMEM;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = bdrv_pread(bs->file->bs, le32_to_cpu(bochs.header), s->catalog_bitmap,
|
ret = bdrv_pread(bs->file, le32_to_cpu(bochs.header), s->catalog_bitmap,
|
||||||
s->catalog_size * 4);
|
s->catalog_size * 4);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto fail;
|
goto fail;
|
||||||
@@ -152,27 +147,20 @@ static int bochs_open(BlockDriverState *bs, QDict *options, int flags,
|
|||||||
s->extent_blocks = 1 + (le32_to_cpu(bochs.extent) - 1) / 512;
|
s->extent_blocks = 1 + (le32_to_cpu(bochs.extent) - 1) / 512;
|
||||||
|
|
||||||
s->extent_size = le32_to_cpu(bochs.extent);
|
s->extent_size = le32_to_cpu(bochs.extent);
|
||||||
if (s->extent_size < BDRV_SECTOR_SIZE) {
|
if (s->extent_size == 0) {
|
||||||
/* bximage actually never creates extents smaller than 4k */
|
qerror_report(ERROR_CLASS_GENERIC_ERROR,
|
||||||
error_setg(errp, "Extent size must be at least 512");
|
"Extent size may not be zero");
|
||||||
ret = -EINVAL;
|
return -EINVAL;
|
||||||
goto fail;
|
|
||||||
} else if (!is_power_of_2(s->extent_size)) {
|
|
||||||
error_setg(errp, "Extent size %" PRIu32 " is not a power of two",
|
|
||||||
s->extent_size);
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
} else if (s->extent_size > 0x800000) {
|
} else if (s->extent_size > 0x800000) {
|
||||||
error_setg(errp, "Extent size %" PRIu32 " is too large",
|
qerror_report(ERROR_CLASS_GENERIC_ERROR,
|
||||||
|
"Extent size %" PRIu32 " is too large",
|
||||||
s->extent_size);
|
s->extent_size);
|
||||||
ret = -EINVAL;
|
return -EINVAL;
|
||||||
goto fail;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (s->catalog_size < DIV_ROUND_UP(bs->total_sectors,
|
if (s->catalog_size < bs->total_sectors / s->extent_size) {
|
||||||
s->extent_size / BDRV_SECTOR_SIZE))
|
qerror_report(ERROR_CLASS_GENERIC_ERROR,
|
||||||
{
|
"Catalog size is too small for this disk size");
|
||||||
error_setg(errp, "Catalog size is too small for this disk size");
|
|
||||||
ret = -EINVAL;
|
ret = -EINVAL;
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
@@ -191,14 +179,13 @@ static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num)
|
|||||||
uint64_t offset = sector_num * 512;
|
uint64_t offset = sector_num * 512;
|
||||||
uint64_t extent_index, extent_offset, bitmap_offset;
|
uint64_t extent_index, extent_offset, bitmap_offset;
|
||||||
char bitmap_entry;
|
char bitmap_entry;
|
||||||
int ret;
|
|
||||||
|
|
||||||
// seek to sector
|
// seek to sector
|
||||||
extent_index = offset / s->extent_size;
|
extent_index = offset / s->extent_size;
|
||||||
extent_offset = (offset % s->extent_size) / 512;
|
extent_offset = (offset % s->extent_size) / 512;
|
||||||
|
|
||||||
if (s->catalog_bitmap[extent_index] == 0xffffffff) {
|
if (s->catalog_bitmap[extent_index] == 0xffffffff) {
|
||||||
return 0; /* not allocated */
|
return -1; /* not allocated */
|
||||||
}
|
}
|
||||||
|
|
||||||
bitmap_offset = s->data_offset +
|
bitmap_offset = s->data_offset +
|
||||||
@@ -206,14 +193,13 @@ static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num)
|
|||||||
(s->extent_blocks + s->bitmap_blocks));
|
(s->extent_blocks + s->bitmap_blocks));
|
||||||
|
|
||||||
/* read in bitmap for current extent */
|
/* read in bitmap for current extent */
|
||||||
ret = bdrv_pread(bs->file->bs, bitmap_offset + (extent_offset / 8),
|
if (bdrv_pread(bs->file, bitmap_offset + (extent_offset / 8),
|
||||||
&bitmap_entry, 1);
|
&bitmap_entry, 1) != 1) {
|
||||||
if (ret < 0) {
|
return -1;
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!((bitmap_entry >> (extent_offset % 8)) & 1)) {
|
if (!((bitmap_entry >> (extent_offset % 8)) & 1)) {
|
||||||
return 0; /* not allocated */
|
return -1; /* not allocated */
|
||||||
}
|
}
|
||||||
|
|
||||||
return bitmap_offset + (512 * (s->bitmap_blocks + extent_offset));
|
return bitmap_offset + (512 * (s->bitmap_blocks + extent_offset));
|
||||||
@@ -226,16 +212,13 @@ static int bochs_read(BlockDriverState *bs, int64_t sector_num,
|
|||||||
|
|
||||||
while (nb_sectors > 0) {
|
while (nb_sectors > 0) {
|
||||||
int64_t block_offset = seek_to_sector(bs, sector_num);
|
int64_t block_offset = seek_to_sector(bs, sector_num);
|
||||||
if (block_offset < 0) {
|
if (block_offset >= 0) {
|
||||||
return block_offset;
|
ret = bdrv_pread(bs->file, block_offset, buf, 512);
|
||||||
} else if (block_offset > 0) {
|
if (ret != 512) {
|
||||||
ret = bdrv_pread(bs->file->bs, block_offset, buf, 512);
|
return -1;
|
||||||
if (ret < 0) {
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else
|
||||||
memset(buf, 0, 512);
|
memset(buf, 0, 512);
|
||||||
}
|
|
||||||
nb_sectors--;
|
nb_sectors--;
|
||||||
sector_num++;
|
sector_num++;
|
||||||
buf += 512;
|
buf += 512;
|
||||||
|
|||||||
@@ -56,8 +56,7 @@ static int cloop_probe(const uint8_t *buf, int buf_size, const char *filename)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int cloop_open(BlockDriverState *bs, QDict *options, int flags,
|
static int cloop_open(BlockDriverState *bs, int flags)
|
||||||
Error **errp)
|
|
||||||
{
|
{
|
||||||
BDRVCloopState *s = bs->opaque;
|
BDRVCloopState *s = bs->opaque;
|
||||||
uint32_t offsets_size, max_compressed_block_size = 1, i;
|
uint32_t offsets_size, max_compressed_block_size = 1, i;
|
||||||
@@ -66,18 +65,20 @@ static int cloop_open(BlockDriverState *bs, QDict *options, int flags,
|
|||||||
bs->read_only = 1;
|
bs->read_only = 1;
|
||||||
|
|
||||||
/* read header */
|
/* read header */
|
||||||
ret = bdrv_pread(bs->file->bs, 128, &s->block_size, 4);
|
ret = bdrv_pread(bs->file, 128, &s->block_size, 4);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
s->block_size = be32_to_cpu(s->block_size);
|
s->block_size = be32_to_cpu(s->block_size);
|
||||||
if (s->block_size % 512) {
|
if (s->block_size % 512) {
|
||||||
error_setg(errp, "block_size %" PRIu32 " must be a multiple of 512",
|
qerror_report(ERROR_CLASS_GENERIC_ERROR,
|
||||||
|
"block_size %u must be a multiple of 512",
|
||||||
s->block_size);
|
s->block_size);
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
if (s->block_size == 0) {
|
if (s->block_size == 0) {
|
||||||
error_setg(errp, "block_size cannot be zero");
|
qerror_report(ERROR_CLASS_GENERIC_ERROR,
|
||||||
|
"block_size cannot be zero");
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,13 +87,14 @@ static int cloop_open(BlockDriverState *bs, QDict *options, int flags,
|
|||||||
* need a buffer this big.
|
* need a buffer this big.
|
||||||
*/
|
*/
|
||||||
if (s->block_size > MAX_BLOCK_SIZE) {
|
if (s->block_size > MAX_BLOCK_SIZE) {
|
||||||
error_setg(errp, "block_size %" PRIu32 " must be %u MB or less",
|
qerror_report(ERROR_CLASS_GENERIC_ERROR,
|
||||||
|
"block_size %u must be %u MB or less",
|
||||||
s->block_size,
|
s->block_size,
|
||||||
MAX_BLOCK_SIZE / (1024 * 1024));
|
MAX_BLOCK_SIZE / (1024 * 1024));
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = bdrv_pread(bs->file->bs, 128 + 4, &s->n_blocks, 4);
|
ret = bdrv_pread(bs->file, 128 + 4, &s->n_blocks, 4);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -101,7 +103,8 @@ static int cloop_open(BlockDriverState *bs, QDict *options, int flags,
|
|||||||
/* read offsets */
|
/* read offsets */
|
||||||
if (s->n_blocks > (UINT32_MAX - 1) / sizeof(uint64_t)) {
|
if (s->n_blocks > (UINT32_MAX - 1) / sizeof(uint64_t)) {
|
||||||
/* Prevent integer overflow */
|
/* Prevent integer overflow */
|
||||||
error_setg(errp, "n_blocks %" PRIu32 " must be %zu or less",
|
qerror_report(ERROR_CLASS_GENERIC_ERROR,
|
||||||
|
"n_blocks %u must be %zu or less",
|
||||||
s->n_blocks,
|
s->n_blocks,
|
||||||
(UINT32_MAX - 1) / sizeof(uint64_t));
|
(UINT32_MAX - 1) / sizeof(uint64_t));
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
@@ -112,18 +115,14 @@ static int cloop_open(BlockDriverState *bs, QDict *options, int flags,
|
|||||||
* fail or overflows bdrv_pread() size. In practice the 512 MB
|
* fail or overflows bdrv_pread() size. In practice the 512 MB
|
||||||
* offsets[] limit supports 16 TB images at 256 KB block size.
|
* offsets[] limit supports 16 TB images at 256 KB block size.
|
||||||
*/
|
*/
|
||||||
error_setg(errp, "image requires too many offsets, "
|
qerror_report(ERROR_CLASS_GENERIC_ERROR,
|
||||||
|
"image requires too many offsets, "
|
||||||
"try increasing block size");
|
"try increasing block size");
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
s->offsets = g_malloc(offsets_size);
|
||||||
|
|
||||||
s->offsets = g_try_malloc(offsets_size);
|
ret = bdrv_pread(bs->file, 128 + 4 + 4, s->offsets, offsets_size);
|
||||||
if (s->offsets == NULL) {
|
|
||||||
error_setg(errp, "Could not allocate offsets table");
|
|
||||||
return -ENOMEM;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = bdrv_pread(bs->file->bs, 128 + 4 + 4, s->offsets, offsets_size);
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
@@ -137,8 +136,9 @@ static int cloop_open(BlockDriverState *bs, QDict *options, int flags,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (s->offsets[i] < s->offsets[i - 1]) {
|
if (s->offsets[i] < s->offsets[i - 1]) {
|
||||||
error_setg(errp, "offsets not monotonically increasing at "
|
qerror_report(ERROR_CLASS_GENERIC_ERROR,
|
||||||
"index %" PRIu32 ", image file is corrupt", i);
|
"offsets not monotonically increasing at "
|
||||||
|
"index %u, image file is corrupt", i);
|
||||||
ret = -EINVAL;
|
ret = -EINVAL;
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
@@ -151,8 +151,9 @@ static int cloop_open(BlockDriverState *bs, QDict *options, int flags,
|
|||||||
* ridiculous s->compressed_block allocation.
|
* ridiculous s->compressed_block allocation.
|
||||||
*/
|
*/
|
||||||
if (size > 2 * MAX_BLOCK_SIZE) {
|
if (size > 2 * MAX_BLOCK_SIZE) {
|
||||||
error_setg(errp, "invalid compressed block size at index %" PRIu32
|
qerror_report(ERROR_CLASS_GENERIC_ERROR,
|
||||||
", image file is corrupt", i);
|
"invalid compressed block size at index %u, "
|
||||||
|
"image file is corrupt", i);
|
||||||
ret = -EINVAL;
|
ret = -EINVAL;
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
@@ -163,20 +164,8 @@ static int cloop_open(BlockDriverState *bs, QDict *options, int flags,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* initialize zlib engine */
|
/* initialize zlib engine */
|
||||||
s->compressed_block = g_try_malloc(max_compressed_block_size + 1);
|
s->compressed_block = g_malloc(max_compressed_block_size + 1);
|
||||||
if (s->compressed_block == NULL) {
|
s->uncompressed_block = g_malloc(s->block_size);
|
||||||
error_setg(errp, "Could not allocate compressed_block");
|
|
||||||
ret = -ENOMEM;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
s->uncompressed_block = g_try_malloc(s->block_size);
|
|
||||||
if (s->uncompressed_block == NULL) {
|
|
||||||
error_setg(errp, "Could not allocate uncompressed_block");
|
|
||||||
ret = -ENOMEM;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inflateInit(&s->zstream) != Z_OK) {
|
if (inflateInit(&s->zstream) != Z_OK) {
|
||||||
ret = -EINVAL;
|
ret = -EINVAL;
|
||||||
goto fail;
|
goto fail;
|
||||||
@@ -203,8 +192,8 @@ static inline int cloop_read_block(BlockDriverState *bs, int block_num)
|
|||||||
int ret;
|
int ret;
|
||||||
uint32_t bytes = s->offsets[block_num + 1] - s->offsets[block_num];
|
uint32_t bytes = s->offsets[block_num + 1] - s->offsets[block_num];
|
||||||
|
|
||||||
ret = bdrv_pread(bs->file->bs, s->offsets[block_num],
|
ret = bdrv_pread(bs->file, s->offsets[block_num], s->compressed_block,
|
||||||
s->compressed_block, bytes);
|
bytes);
|
||||||
if (ret != bytes) {
|
if (ret != bytes) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|||||||
113
block/commit.c
113
block/commit.c
@@ -15,9 +15,7 @@
|
|||||||
#include "trace.h"
|
#include "trace.h"
|
||||||
#include "block/block_int.h"
|
#include "block/block_int.h"
|
||||||
#include "block/blockjob.h"
|
#include "block/blockjob.h"
|
||||||
#include "qapi/qmp/qerror.h"
|
|
||||||
#include "qemu/ratelimit.h"
|
#include "qemu/ratelimit.h"
|
||||||
#include "sysemu/block-backend.h"
|
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
/*
|
/*
|
||||||
@@ -39,7 +37,6 @@ typedef struct CommitBlockJob {
|
|||||||
BlockdevOnError on_error;
|
BlockdevOnError on_error;
|
||||||
int base_flags;
|
int base_flags;
|
||||||
int orig_overlay_flags;
|
int orig_overlay_flags;
|
||||||
char *backing_file_str;
|
|
||||||
} CommitBlockJob;
|
} CommitBlockJob;
|
||||||
|
|
||||||
static int coroutine_fn commit_populate(BlockDriverState *bs,
|
static int coroutine_fn commit_populate(BlockDriverState *bs,
|
||||||
@@ -62,50 +59,17 @@ static int coroutine_fn commit_populate(BlockDriverState *bs,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef struct {
|
static void coroutine_fn commit_run(void *opaque)
|
||||||
int ret;
|
|
||||||
} CommitCompleteData;
|
|
||||||
|
|
||||||
static void commit_complete(BlockJob *job, void *opaque)
|
|
||||||
{
|
{
|
||||||
CommitBlockJob *s = container_of(job, CommitBlockJob, common);
|
CommitBlockJob *s = opaque;
|
||||||
CommitCompleteData *data = opaque;
|
|
||||||
BlockDriverState *active = s->active;
|
BlockDriverState *active = s->active;
|
||||||
BlockDriverState *top = s->top;
|
BlockDriverState *top = s->top;
|
||||||
BlockDriverState *base = s->base;
|
BlockDriverState *base = s->base;
|
||||||
BlockDriverState *overlay_bs;
|
BlockDriverState *overlay_bs;
|
||||||
int ret = data->ret;
|
|
||||||
|
|
||||||
if (!block_job_is_cancelled(&s->common) && ret == 0) {
|
|
||||||
/* success */
|
|
||||||
ret = bdrv_drop_intermediate(active, top, base, s->backing_file_str);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* restore base open flags here if appropriate (e.g., change the base back
|
|
||||||
* to r/o). These reopens do not need to be atomic, since we won't abort
|
|
||||||
* even on failure here */
|
|
||||||
if (s->base_flags != bdrv_get_flags(base)) {
|
|
||||||
bdrv_reopen(base, s->base_flags, NULL);
|
|
||||||
}
|
|
||||||
overlay_bs = bdrv_find_overlay(active, top);
|
|
||||||
if (overlay_bs && s->orig_overlay_flags != bdrv_get_flags(overlay_bs)) {
|
|
||||||
bdrv_reopen(overlay_bs, s->orig_overlay_flags, NULL);
|
|
||||||
}
|
|
||||||
g_free(s->backing_file_str);
|
|
||||||
block_job_completed(&s->common, ret);
|
|
||||||
g_free(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void coroutine_fn commit_run(void *opaque)
|
|
||||||
{
|
|
||||||
CommitBlockJob *s = opaque;
|
|
||||||
CommitCompleteData *data;
|
|
||||||
BlockDriverState *top = s->top;
|
|
||||||
BlockDriverState *base = s->base;
|
|
||||||
int64_t sector_num, end;
|
int64_t sector_num, end;
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
void *buf = NULL;
|
void *buf;
|
||||||
int bytes_written = 0;
|
int bytes_written = 0;
|
||||||
int64_t base_len;
|
int64_t base_len;
|
||||||
|
|
||||||
@@ -113,18 +77,18 @@ static void coroutine_fn commit_run(void *opaque)
|
|||||||
|
|
||||||
|
|
||||||
if (s->common.len < 0) {
|
if (s->common.len < 0) {
|
||||||
goto out;
|
goto exit_restore_reopen;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = base_len = bdrv_getlength(base);
|
ret = base_len = bdrv_getlength(base);
|
||||||
if (base_len < 0) {
|
if (base_len < 0) {
|
||||||
goto out;
|
goto exit_restore_reopen;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (base_len < s->common.len) {
|
if (base_len < s->common.len) {
|
||||||
ret = bdrv_truncate(base, s->common.len);
|
ret = bdrv_truncate(base, s->common.len);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
goto out;
|
goto exit_restore_reopen;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,12 +103,12 @@ wait:
|
|||||||
/* Note that even when no rate limit is applied we need to yield
|
/* Note that even when no rate limit is applied we need to yield
|
||||||
* with no pending I/O here so that bdrv_drain_all() returns.
|
* with no pending I/O here so that bdrv_drain_all() returns.
|
||||||
*/
|
*/
|
||||||
block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
|
block_job_sleep_ns(&s->common, rt_clock, delay_ns);
|
||||||
if (block_job_is_cancelled(&s->common)) {
|
if (block_job_is_cancelled(&s->common)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
/* Copy if allocated above the base */
|
/* Copy if allocated above the base */
|
||||||
ret = bdrv_is_allocated_above(top, base, sector_num,
|
ret = bdrv_co_is_allocated_above(top, base, sector_num,
|
||||||
COMMIT_BUFFER_SIZE / BDRV_SECTOR_SIZE,
|
COMMIT_BUFFER_SIZE / BDRV_SECTOR_SIZE,
|
||||||
&n);
|
&n);
|
||||||
copy = (ret == 1);
|
copy = (ret == 1);
|
||||||
@@ -163,7 +127,7 @@ wait:
|
|||||||
if (s->on_error == BLOCKDEV_ON_ERROR_STOP ||
|
if (s->on_error == BLOCKDEV_ON_ERROR_STOP ||
|
||||||
s->on_error == BLOCKDEV_ON_ERROR_REPORT||
|
s->on_error == BLOCKDEV_ON_ERROR_REPORT||
|
||||||
(s->on_error == BLOCKDEV_ON_ERROR_ENOSPC && ret == -ENOSPC)) {
|
(s->on_error == BLOCKDEV_ON_ERROR_ENOSPC && ret == -ENOSPC)) {
|
||||||
goto out;
|
goto exit_free_buf;
|
||||||
} else {
|
} else {
|
||||||
n = 0;
|
n = 0;
|
||||||
continue;
|
continue;
|
||||||
@@ -175,12 +139,27 @@ wait:
|
|||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
|
|
||||||
out:
|
if (!block_job_is_cancelled(&s->common) && sector_num == end) {
|
||||||
|
/* success */
|
||||||
|
ret = bdrv_drop_intermediate(active, top, base);
|
||||||
|
}
|
||||||
|
|
||||||
|
exit_free_buf:
|
||||||
qemu_vfree(buf);
|
qemu_vfree(buf);
|
||||||
|
|
||||||
data = g_malloc(sizeof(*data));
|
exit_restore_reopen:
|
||||||
data->ret = ret;
|
/* restore base open flags here if appropriate (e.g., change the base back
|
||||||
block_job_defer_to_main_loop(&s->common, commit_complete, data);
|
* to r/o). These reopens do not need to be atomic, since we won't abort
|
||||||
|
* even on failure here */
|
||||||
|
if (s->base_flags != bdrv_get_flags(base)) {
|
||||||
|
bdrv_reopen(base, s->base_flags, NULL);
|
||||||
|
}
|
||||||
|
overlay_bs = bdrv_find_overlay(active, top);
|
||||||
|
if (overlay_bs && s->orig_overlay_flags != bdrv_get_flags(overlay_bs)) {
|
||||||
|
bdrv_reopen(overlay_bs, s->orig_overlay_flags, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
block_job_completed(&s->common, ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void commit_set_speed(BlockJob *job, int64_t speed, Error **errp)
|
static void commit_set_speed(BlockJob *job, int64_t speed, Error **errp)
|
||||||
@@ -188,22 +167,22 @@ static void commit_set_speed(BlockJob *job, int64_t speed, Error **errp)
|
|||||||
CommitBlockJob *s = container_of(job, CommitBlockJob, common);
|
CommitBlockJob *s = container_of(job, CommitBlockJob, common);
|
||||||
|
|
||||||
if (speed < 0) {
|
if (speed < 0) {
|
||||||
error_setg(errp, QERR_INVALID_PARAMETER, "speed");
|
error_set(errp, QERR_INVALID_PARAMETER, "speed");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
|
ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
|
||||||
}
|
}
|
||||||
|
|
||||||
static const BlockJobDriver commit_job_driver = {
|
static BlockJobType commit_job_type = {
|
||||||
.instance_size = sizeof(CommitBlockJob),
|
.instance_size = sizeof(CommitBlockJob),
|
||||||
.job_type = BLOCK_JOB_TYPE_COMMIT,
|
.job_type = "commit",
|
||||||
.set_speed = commit_set_speed,
|
.set_speed = commit_set_speed,
|
||||||
};
|
};
|
||||||
|
|
||||||
void commit_start(BlockDriverState *bs, BlockDriverState *base,
|
void commit_start(BlockDriverState *bs, BlockDriverState *base,
|
||||||
BlockDriverState *top, int64_t speed,
|
BlockDriverState *top, int64_t speed,
|
||||||
BlockdevOnError on_error, BlockCompletionFunc *cb,
|
BlockdevOnError on_error, BlockDriverCompletionFunc *cb,
|
||||||
void *opaque, const char *backing_file_str, Error **errp)
|
void *opaque, Error **errp)
|
||||||
{
|
{
|
||||||
CommitBlockJob *s;
|
CommitBlockJob *s;
|
||||||
BlockReopenQueue *reopen_queue = NULL;
|
BlockReopenQueue *reopen_queue = NULL;
|
||||||
@@ -214,12 +193,18 @@ void commit_start(BlockDriverState *bs, BlockDriverState *base,
|
|||||||
|
|
||||||
if ((on_error == BLOCKDEV_ON_ERROR_STOP ||
|
if ((on_error == BLOCKDEV_ON_ERROR_STOP ||
|
||||||
on_error == BLOCKDEV_ON_ERROR_ENOSPC) &&
|
on_error == BLOCKDEV_ON_ERROR_ENOSPC) &&
|
||||||
(!bs->blk || !blk_iostatus_is_enabled(bs->blk))) {
|
!bdrv_iostatus_is_enabled(bs)) {
|
||||||
error_setg(errp, "Invalid parameter combination");
|
error_set(errp, QERR_INVALID_PARAMETER_COMBINATION);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Once we support top == active layer, remove this check */
|
||||||
|
if (top == bs) {
|
||||||
|
error_setg(errp,
|
||||||
|
"Top image as the active layer is currently unsupported");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(top != bs);
|
|
||||||
if (top == base) {
|
if (top == base) {
|
||||||
error_setg(errp, "Invalid files for merge: top and base are the same");
|
error_setg(errp, "Invalid files for merge: top and base are the same");
|
||||||
return;
|
return;
|
||||||
@@ -236,14 +221,14 @@ void commit_start(BlockDriverState *bs, BlockDriverState *base,
|
|||||||
orig_overlay_flags = bdrv_get_flags(overlay_bs);
|
orig_overlay_flags = bdrv_get_flags(overlay_bs);
|
||||||
|
|
||||||
/* convert base & overlay_bs to r/w, if necessary */
|
/* convert base & overlay_bs to r/w, if necessary */
|
||||||
if (!(orig_overlay_flags & BDRV_O_RDWR)) {
|
|
||||||
reopen_queue = bdrv_reopen_queue(reopen_queue, overlay_bs, NULL,
|
|
||||||
orig_overlay_flags | BDRV_O_RDWR);
|
|
||||||
}
|
|
||||||
if (!(orig_base_flags & BDRV_O_RDWR)) {
|
if (!(orig_base_flags & BDRV_O_RDWR)) {
|
||||||
reopen_queue = bdrv_reopen_queue(reopen_queue, base, NULL,
|
reopen_queue = bdrv_reopen_queue(reopen_queue, base,
|
||||||
orig_base_flags | BDRV_O_RDWR);
|
orig_base_flags | BDRV_O_RDWR);
|
||||||
}
|
}
|
||||||
|
if (!(orig_overlay_flags & BDRV_O_RDWR)) {
|
||||||
|
reopen_queue = bdrv_reopen_queue(reopen_queue, overlay_bs,
|
||||||
|
orig_overlay_flags | BDRV_O_RDWR);
|
||||||
|
}
|
||||||
if (reopen_queue) {
|
if (reopen_queue) {
|
||||||
bdrv_reopen_multiple(reopen_queue, &local_err);
|
bdrv_reopen_multiple(reopen_queue, &local_err);
|
||||||
if (local_err != NULL) {
|
if (local_err != NULL) {
|
||||||
@@ -253,7 +238,7 @@ void commit_start(BlockDriverState *bs, BlockDriverState *base,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
s = block_job_create(&commit_job_driver, bs, speed, cb, opaque, errp);
|
s = block_job_create(&commit_job_type, bs, speed, cb, opaque, errp);
|
||||||
if (!s) {
|
if (!s) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -265,8 +250,6 @@ void commit_start(BlockDriverState *bs, BlockDriverState *base,
|
|||||||
s->base_flags = orig_base_flags;
|
s->base_flags = orig_base_flags;
|
||||||
s->orig_overlay_flags = orig_overlay_flags;
|
s->orig_overlay_flags = orig_overlay_flags;
|
||||||
|
|
||||||
s->backing_file_str = g_strdup(backing_file_str);
|
|
||||||
|
|
||||||
s->on_error = on_error;
|
s->on_error = on_error;
|
||||||
s->common.co = qemu_coroutine_create(commit_run);
|
s->common.co = qemu_coroutine_create(commit_run);
|
||||||
|
|
||||||
|
|||||||
356
block/cow.c
Normal file
356
block/cow.c
Normal file
@@ -0,0 +1,356 @@
|
|||||||
|
/*
|
||||||
|
* Block driver for the COW format
|
||||||
|
*
|
||||||
|
* Copyright (c) 2004 Fabrice Bellard
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||||
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
#include "qemu-common.h"
|
||||||
|
#include "block/block_int.h"
|
||||||
|
#include "qemu/module.h"
|
||||||
|
|
||||||
|
/**************************************************************/
|
||||||
|
/* COW block driver using file system holes */
|
||||||
|
|
||||||
|
/* user mode linux compatible COW file */
|
||||||
|
#define COW_MAGIC 0x4f4f4f4d /* MOOO */
|
||||||
|
#define COW_VERSION 2
|
||||||
|
|
||||||
|
struct cow_header_v2 {
|
||||||
|
uint32_t magic;
|
||||||
|
uint32_t version;
|
||||||
|
char backing_file[1024];
|
||||||
|
int32_t mtime;
|
||||||
|
uint64_t size;
|
||||||
|
uint32_t sectorsize;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct BDRVCowState {
|
||||||
|
CoMutex lock;
|
||||||
|
int64_t cow_sectors_offset;
|
||||||
|
} BDRVCowState;
|
||||||
|
|
||||||
|
static int cow_probe(const uint8_t *buf, int buf_size, const char *filename)
|
||||||
|
{
|
||||||
|
const struct cow_header_v2 *cow_header = (const void *)buf;
|
||||||
|
|
||||||
|
if (buf_size >= sizeof(struct cow_header_v2) &&
|
||||||
|
be32_to_cpu(cow_header->magic) == COW_MAGIC &&
|
||||||
|
be32_to_cpu(cow_header->version) == COW_VERSION)
|
||||||
|
return 100;
|
||||||
|
else
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cow_open(BlockDriverState *bs, int flags)
|
||||||
|
{
|
||||||
|
BDRVCowState *s = bs->opaque;
|
||||||
|
struct cow_header_v2 cow_header;
|
||||||
|
int bitmap_size;
|
||||||
|
int64_t size;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
/* see if it is a cow image */
|
||||||
|
ret = bdrv_pread(bs->file, 0, &cow_header, sizeof(cow_header));
|
||||||
|
if (ret < 0) {
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (be32_to_cpu(cow_header.magic) != COW_MAGIC) {
|
||||||
|
ret = -EMEDIUMTYPE;
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (be32_to_cpu(cow_header.version) != COW_VERSION) {
|
||||||
|
char version[64];
|
||||||
|
snprintf(version, sizeof(version),
|
||||||
|
"COW version %d", cow_header.version);
|
||||||
|
qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
|
||||||
|
bs->device_name, "cow", version);
|
||||||
|
ret = -ENOTSUP;
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* cow image found */
|
||||||
|
size = be64_to_cpu(cow_header.size);
|
||||||
|
bs->total_sectors = size / 512;
|
||||||
|
|
||||||
|
pstrcpy(bs->backing_file, sizeof(bs->backing_file),
|
||||||
|
cow_header.backing_file);
|
||||||
|
|
||||||
|
bitmap_size = ((bs->total_sectors + 7) >> 3) + sizeof(cow_header);
|
||||||
|
s->cow_sectors_offset = (bitmap_size + 511) & ~511;
|
||||||
|
qemu_co_mutex_init(&s->lock);
|
||||||
|
return 0;
|
||||||
|
fail:
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* XXX(hch): right now these functions are extremely inefficient.
|
||||||
|
* We should just read the whole bitmap we'll need in one go instead.
|
||||||
|
*/
|
||||||
|
static inline int cow_set_bit(BlockDriverState *bs, int64_t bitnum)
|
||||||
|
{
|
||||||
|
uint64_t offset = sizeof(struct cow_header_v2) + bitnum / 8;
|
||||||
|
uint8_t bitmap;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
ret = bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap));
|
||||||
|
if (ret < 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
bitmap |= (1 << (bitnum % 8));
|
||||||
|
|
||||||
|
ret = bdrv_pwrite_sync(bs->file, offset, &bitmap, sizeof(bitmap));
|
||||||
|
if (ret < 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int is_bit_set(BlockDriverState *bs, int64_t bitnum)
|
||||||
|
{
|
||||||
|
uint64_t offset = sizeof(struct cow_header_v2) + bitnum / 8;
|
||||||
|
uint8_t bitmap;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
ret = bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap));
|
||||||
|
if (ret < 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
return !!(bitmap & (1 << (bitnum % 8)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Return true if first block has been changed (ie. current version is
|
||||||
|
* in COW file). Set the number of continuous blocks for which that
|
||||||
|
* is true. */
|
||||||
|
static int coroutine_fn cow_co_is_allocated(BlockDriverState *bs,
|
||||||
|
int64_t sector_num, int nb_sectors, int *num_same)
|
||||||
|
{
|
||||||
|
int changed;
|
||||||
|
|
||||||
|
if (nb_sectors == 0) {
|
||||||
|
*num_same = nb_sectors;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
changed = is_bit_set(bs, sector_num);
|
||||||
|
if (changed < 0) {
|
||||||
|
return 0; /* XXX: how to return I/O errors? */
|
||||||
|
}
|
||||||
|
|
||||||
|
for (*num_same = 1; *num_same < nb_sectors; (*num_same)++) {
|
||||||
|
if (is_bit_set(bs, sector_num + *num_same) != changed)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return changed;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cow_update_bitmap(BlockDriverState *bs, int64_t sector_num,
|
||||||
|
int nb_sectors)
|
||||||
|
{
|
||||||
|
int error = 0;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < nb_sectors; i++) {
|
||||||
|
error = cow_set_bit(bs, sector_num + i);
|
||||||
|
if (error) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int coroutine_fn cow_read(BlockDriverState *bs, int64_t sector_num,
|
||||||
|
uint8_t *buf, int nb_sectors)
|
||||||
|
{
|
||||||
|
BDRVCowState *s = bs->opaque;
|
||||||
|
int ret, n;
|
||||||
|
|
||||||
|
while (nb_sectors > 0) {
|
||||||
|
if (bdrv_co_is_allocated(bs, sector_num, nb_sectors, &n)) {
|
||||||
|
ret = bdrv_pread(bs->file,
|
||||||
|
s->cow_sectors_offset + sector_num * 512,
|
||||||
|
buf, n * 512);
|
||||||
|
if (ret < 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (bs->backing_hd) {
|
||||||
|
/* read from the base image */
|
||||||
|
ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
|
||||||
|
if (ret < 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
memset(buf, 0, n * 512);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nb_sectors -= n;
|
||||||
|
sector_num += n;
|
||||||
|
buf += n * 512;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static coroutine_fn int cow_co_read(BlockDriverState *bs, int64_t sector_num,
|
||||||
|
uint8_t *buf, int nb_sectors)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
BDRVCowState *s = bs->opaque;
|
||||||
|
qemu_co_mutex_lock(&s->lock);
|
||||||
|
ret = cow_read(bs, sector_num, buf, nb_sectors);
|
||||||
|
qemu_co_mutex_unlock(&s->lock);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cow_write(BlockDriverState *bs, int64_t sector_num,
|
||||||
|
const uint8_t *buf, int nb_sectors)
|
||||||
|
{
|
||||||
|
BDRVCowState *s = bs->opaque;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
ret = bdrv_pwrite(bs->file, s->cow_sectors_offset + sector_num * 512,
|
||||||
|
buf, nb_sectors * 512);
|
||||||
|
if (ret < 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
return cow_update_bitmap(bs, sector_num, nb_sectors);
|
||||||
|
}
|
||||||
|
|
||||||
|
static coroutine_fn int cow_co_write(BlockDriverState *bs, int64_t sector_num,
|
||||||
|
const uint8_t *buf, int nb_sectors)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
BDRVCowState *s = bs->opaque;
|
||||||
|
qemu_co_mutex_lock(&s->lock);
|
||||||
|
ret = cow_write(bs, sector_num, buf, nb_sectors);
|
||||||
|
qemu_co_mutex_unlock(&s->lock);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void cow_close(BlockDriverState *bs)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cow_create(const char *filename, QEMUOptionParameter *options)
|
||||||
|
{
|
||||||
|
struct cow_header_v2 cow_header;
|
||||||
|
struct stat st;
|
||||||
|
int64_t image_sectors = 0;
|
||||||
|
const char *image_filename = NULL;
|
||||||
|
int ret;
|
||||||
|
BlockDriverState *cow_bs;
|
||||||
|
|
||||||
|
/* Read out options */
|
||||||
|
while (options && options->name) {
|
||||||
|
if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
|
||||||
|
image_sectors = options->value.n / 512;
|
||||||
|
} else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
|
||||||
|
image_filename = options->value.s;
|
||||||
|
}
|
||||||
|
options++;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = bdrv_create_file(filename, options);
|
||||||
|
if (ret < 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = bdrv_file_open(&cow_bs, filename, BDRV_O_RDWR);
|
||||||
|
if (ret < 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(&cow_header, 0, sizeof(cow_header));
|
||||||
|
cow_header.magic = cpu_to_be32(COW_MAGIC);
|
||||||
|
cow_header.version = cpu_to_be32(COW_VERSION);
|
||||||
|
if (image_filename) {
|
||||||
|
/* Note: if no file, we put a dummy mtime */
|
||||||
|
cow_header.mtime = cpu_to_be32(0);
|
||||||
|
|
||||||
|
if (stat(image_filename, &st) != 0) {
|
||||||
|
goto mtime_fail;
|
||||||
|
}
|
||||||
|
cow_header.mtime = cpu_to_be32(st.st_mtime);
|
||||||
|
mtime_fail:
|
||||||
|
pstrcpy(cow_header.backing_file, sizeof(cow_header.backing_file),
|
||||||
|
image_filename);
|
||||||
|
}
|
||||||
|
cow_header.sectorsize = cpu_to_be32(512);
|
||||||
|
cow_header.size = cpu_to_be64(image_sectors * 512);
|
||||||
|
ret = bdrv_pwrite(cow_bs, 0, &cow_header, sizeof(cow_header));
|
||||||
|
if (ret < 0) {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* resize to include at least all the bitmap */
|
||||||
|
ret = bdrv_truncate(cow_bs,
|
||||||
|
sizeof(cow_header) + ((image_sectors + 7) >> 3));
|
||||||
|
if (ret < 0) {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
exit:
|
||||||
|
bdrv_delete(cow_bs);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static QEMUOptionParameter cow_create_options[] = {
|
||||||
|
{
|
||||||
|
.name = BLOCK_OPT_SIZE,
|
||||||
|
.type = OPT_SIZE,
|
||||||
|
.help = "Virtual disk size"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
.name = BLOCK_OPT_BACKING_FILE,
|
||||||
|
.type = OPT_STRING,
|
||||||
|
.help = "File name of a base image"
|
||||||
|
},
|
||||||
|
{ NULL }
|
||||||
|
};
|
||||||
|
|
||||||
|
static BlockDriver bdrv_cow = {
|
||||||
|
.format_name = "cow",
|
||||||
|
.instance_size = sizeof(BDRVCowState),
|
||||||
|
|
||||||
|
.bdrv_probe = cow_probe,
|
||||||
|
.bdrv_open = cow_open,
|
||||||
|
.bdrv_close = cow_close,
|
||||||
|
.bdrv_create = cow_create,
|
||||||
|
|
||||||
|
.bdrv_read = cow_co_read,
|
||||||
|
.bdrv_write = cow_co_write,
|
||||||
|
.bdrv_co_is_allocated = cow_co_is_allocated,
|
||||||
|
|
||||||
|
.create_options = cow_create_options,
|
||||||
|
};
|
||||||
|
|
||||||
|
static void bdrv_cow_init(void)
|
||||||
|
{
|
||||||
|
bdrv_register(&bdrv_cow);
|
||||||
|
}
|
||||||
|
|
||||||
|
block_init(bdrv_cow_init);
|
||||||
492
block/curl.c
492
block/curl.c
@@ -22,13 +22,10 @@
|
|||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
#include "qemu-common.h"
|
#include "qemu-common.h"
|
||||||
#include "qemu/error-report.h"
|
|
||||||
#include "block/block_int.h"
|
#include "block/block_int.h"
|
||||||
#include "qapi/qmp/qbool.h"
|
|
||||||
#include "qapi/qmp/qstring.h"
|
|
||||||
#include <curl/curl.h>
|
#include <curl/curl.h>
|
||||||
|
|
||||||
// #define DEBUG_CURL
|
// #define DEBUG
|
||||||
// #define DEBUG_VERBOSE
|
// #define DEBUG_VERBOSE
|
||||||
|
|
||||||
#ifdef DEBUG_CURL
|
#ifdef DEBUG_CURL
|
||||||
@@ -37,26 +34,6 @@
|
|||||||
#define DPRINTF(fmt, ...) do { } while (0)
|
#define DPRINTF(fmt, ...) do { } while (0)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if LIBCURL_VERSION_NUM >= 0x071000
|
|
||||||
/* The multi interface timer callback was introduced in 7.16.0 */
|
|
||||||
#define NEED_CURL_TIMER_CALLBACK
|
|
||||||
#define HAVE_SOCKET_ACTION
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef HAVE_SOCKET_ACTION
|
|
||||||
/* If curl_multi_socket_action isn't available, define it statically here in
|
|
||||||
* terms of curl_multi_socket. Note that ev_bitmask will be ignored, which is
|
|
||||||
* less efficient but still safe. */
|
|
||||||
static CURLMcode __curl_multi_socket_action(CURLM *multi_handle,
|
|
||||||
curl_socket_t sockfd,
|
|
||||||
int ev_bitmask,
|
|
||||||
int *running_handles)
|
|
||||||
{
|
|
||||||
return curl_multi_socket(multi_handle, sockfd, running_handles);
|
|
||||||
}
|
|
||||||
#define curl_multi_socket_action __curl_multi_socket_action
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \
|
#define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \
|
||||||
CURLPROTO_FTP | CURLPROTO_FTPS | \
|
CURLPROTO_FTP | CURLPROTO_FTPS | \
|
||||||
CURLPROTO_TFTP)
|
CURLPROTO_TFTP)
|
||||||
@@ -64,24 +41,16 @@ static CURLMcode __curl_multi_socket_action(CURLM *multi_handle,
|
|||||||
#define CURL_NUM_STATES 8
|
#define CURL_NUM_STATES 8
|
||||||
#define CURL_NUM_ACB 8
|
#define CURL_NUM_ACB 8
|
||||||
#define SECTOR_SIZE 512
|
#define SECTOR_SIZE 512
|
||||||
#define READ_AHEAD_DEFAULT (256 * 1024)
|
#define READ_AHEAD_SIZE (256 * 1024)
|
||||||
#define CURL_TIMEOUT_DEFAULT 5
|
|
||||||
#define CURL_TIMEOUT_MAX 10000
|
|
||||||
|
|
||||||
#define FIND_RET_NONE 0
|
#define FIND_RET_NONE 0
|
||||||
#define FIND_RET_OK 1
|
#define FIND_RET_OK 1
|
||||||
#define FIND_RET_WAIT 2
|
#define FIND_RET_WAIT 2
|
||||||
|
|
||||||
#define CURL_BLOCK_OPT_URL "url"
|
|
||||||
#define CURL_BLOCK_OPT_READAHEAD "readahead"
|
|
||||||
#define CURL_BLOCK_OPT_SSLVERIFY "sslverify"
|
|
||||||
#define CURL_BLOCK_OPT_TIMEOUT "timeout"
|
|
||||||
#define CURL_BLOCK_OPT_COOKIE "cookie"
|
|
||||||
|
|
||||||
struct BDRVCURLState;
|
struct BDRVCURLState;
|
||||||
|
|
||||||
typedef struct CURLAIOCB {
|
typedef struct CURLAIOCB {
|
||||||
BlockAIOCB common;
|
BlockDriverAIOCB common;
|
||||||
QEMUBH *bh;
|
QEMUBH *bh;
|
||||||
QEMUIOVector *qiov;
|
QEMUIOVector *qiov;
|
||||||
|
|
||||||
@@ -97,7 +66,6 @@ typedef struct CURLState
|
|||||||
struct BDRVCURLState *s;
|
struct BDRVCURLState *s;
|
||||||
CURLAIOCB *acb[CURL_NUM_ACB];
|
CURLAIOCB *acb[CURL_NUM_ACB];
|
||||||
CURL *curl;
|
CURL *curl;
|
||||||
curl_socket_t sock_fd;
|
|
||||||
char *orig_buf;
|
char *orig_buf;
|
||||||
size_t buf_start;
|
size_t buf_start;
|
||||||
size_t buf_off;
|
size_t buf_off;
|
||||||
@@ -109,80 +77,47 @@ typedef struct CURLState
|
|||||||
|
|
||||||
typedef struct BDRVCURLState {
|
typedef struct BDRVCURLState {
|
||||||
CURLM *multi;
|
CURLM *multi;
|
||||||
QEMUTimer timer;
|
|
||||||
size_t len;
|
size_t len;
|
||||||
CURLState states[CURL_NUM_STATES];
|
CURLState states[CURL_NUM_STATES];
|
||||||
char *url;
|
char *url;
|
||||||
size_t readahead_size;
|
size_t readahead_size;
|
||||||
bool sslverify;
|
|
||||||
uint64_t timeout;
|
|
||||||
char *cookie;
|
|
||||||
bool accept_range;
|
|
||||||
AioContext *aio_context;
|
|
||||||
} BDRVCURLState;
|
} BDRVCURLState;
|
||||||
|
|
||||||
static void curl_clean_state(CURLState *s);
|
static void curl_clean_state(CURLState *s);
|
||||||
static void curl_multi_do(void *arg);
|
static void curl_multi_do(void *arg);
|
||||||
static void curl_multi_read(void *arg);
|
static int curl_aio_flush(void *opaque);
|
||||||
|
|
||||||
#ifdef NEED_CURL_TIMER_CALLBACK
|
|
||||||
static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque)
|
|
||||||
{
|
|
||||||
BDRVCURLState *s = opaque;
|
|
||||||
|
|
||||||
DPRINTF("CURL: timer callback timeout_ms %ld\n", timeout_ms);
|
|
||||||
if (timeout_ms == -1) {
|
|
||||||
timer_del(&s->timer);
|
|
||||||
} else {
|
|
||||||
int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000;
|
|
||||||
timer_mod(&s->timer,
|
|
||||||
qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action,
|
static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action,
|
||||||
void *userp, void *sp)
|
void *s, void *sp)
|
||||||
{
|
{
|
||||||
BDRVCURLState *s;
|
|
||||||
CURLState *state = NULL;
|
|
||||||
curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state);
|
|
||||||
state->sock_fd = fd;
|
|
||||||
s = state->s;
|
|
||||||
|
|
||||||
DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action, fd);
|
DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action, fd);
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case CURL_POLL_IN:
|
case CURL_POLL_IN:
|
||||||
aio_set_fd_handler(s->aio_context, fd, false,
|
qemu_aio_set_fd_handler(fd, curl_multi_do, NULL, curl_aio_flush, s);
|
||||||
curl_multi_read, NULL, state);
|
|
||||||
break;
|
break;
|
||||||
case CURL_POLL_OUT:
|
case CURL_POLL_OUT:
|
||||||
aio_set_fd_handler(s->aio_context, fd, false,
|
qemu_aio_set_fd_handler(fd, NULL, curl_multi_do, curl_aio_flush, s);
|
||||||
NULL, curl_multi_do, state);
|
|
||||||
break;
|
break;
|
||||||
case CURL_POLL_INOUT:
|
case CURL_POLL_INOUT:
|
||||||
aio_set_fd_handler(s->aio_context, fd, false,
|
qemu_aio_set_fd_handler(fd, curl_multi_do, curl_multi_do,
|
||||||
curl_multi_read, curl_multi_do, state);
|
curl_aio_flush, s);
|
||||||
break;
|
break;
|
||||||
case CURL_POLL_REMOVE:
|
case CURL_POLL_REMOVE:
|
||||||
aio_set_fd_handler(s->aio_context, fd, false,
|
qemu_aio_set_fd_handler(fd, NULL, NULL, NULL, NULL);
|
||||||
NULL, NULL, NULL);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
|
static size_t curl_size_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
|
||||||
{
|
{
|
||||||
BDRVCURLState *s = opaque;
|
CURLState *s = ((CURLState*)opaque);
|
||||||
size_t realsize = size * nmemb;
|
size_t realsize = size * nmemb;
|
||||||
const char *accept_line = "Accept-Ranges: bytes";
|
size_t fsize;
|
||||||
|
|
||||||
if (realsize >= strlen(accept_line)
|
if(sscanf(ptr, "Content-Length: %zd", &fsize) == 1) {
|
||||||
&& strncmp((char *)ptr, accept_line, strlen(accept_line)) == 0) {
|
s->s->len = fsize;
|
||||||
s->accept_range = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return realsize;
|
return realsize;
|
||||||
@@ -197,7 +132,7 @@ static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
|
|||||||
DPRINTF("CURL: Just reading %zd bytes\n", realsize);
|
DPRINTF("CURL: Just reading %zd bytes\n", realsize);
|
||||||
|
|
||||||
if (!s || !s->orig_buf)
|
if (!s || !s->orig_buf)
|
||||||
return 0;
|
goto read_end;
|
||||||
|
|
||||||
if (s->buf_off >= s->buf_len) {
|
if (s->buf_off >= s->buf_len) {
|
||||||
/* buffer full, read nothing */
|
/* buffer full, read nothing */
|
||||||
@@ -217,11 +152,12 @@ static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
|
|||||||
qemu_iovec_from_buf(acb->qiov, 0, s->orig_buf + acb->start,
|
qemu_iovec_from_buf(acb->qiov, 0, s->orig_buf + acb->start,
|
||||||
acb->end - acb->start);
|
acb->end - acb->start);
|
||||||
acb->common.cb(acb->common.opaque, 0);
|
acb->common.cb(acb->common.opaque, 0);
|
||||||
qemu_aio_unref(acb);
|
qemu_aio_release(acb);
|
||||||
s->acb[i] = NULL;
|
s->acb[i] = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
read_end:
|
||||||
return realsize;
|
return realsize;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -256,8 +192,7 @@ static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Wait for unfinished chunks
|
// Wait for unfinished chunks
|
||||||
if (state->in_use &&
|
if ((start >= state->buf_start) &&
|
||||||
(start >= state->buf_start) &&
|
|
||||||
(start <= buf_fend) &&
|
(start <= buf_fend) &&
|
||||||
(end >= state->buf_start) &&
|
(end >= state->buf_start) &&
|
||||||
(end <= buf_fend))
|
(end <= buf_fend))
|
||||||
@@ -279,40 +214,40 @@ static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len,
|
|||||||
return FIND_RET_NONE;
|
return FIND_RET_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void curl_multi_check_completion(BDRVCURLState *s)
|
static void curl_multi_do(void *arg)
|
||||||
{
|
{
|
||||||
|
BDRVCURLState *s = (BDRVCURLState *)arg;
|
||||||
|
int running;
|
||||||
|
int r;
|
||||||
int msgs_in_queue;
|
int msgs_in_queue;
|
||||||
|
|
||||||
|
if (!s->multi)
|
||||||
|
return;
|
||||||
|
|
||||||
|
do {
|
||||||
|
r = curl_multi_socket_all(s->multi, &running);
|
||||||
|
} while(r == CURLM_CALL_MULTI_PERFORM);
|
||||||
|
|
||||||
/* Try to find done transfers, so we can free the easy
|
/* Try to find done transfers, so we can free the easy
|
||||||
* handle again. */
|
* handle again. */
|
||||||
for (;;) {
|
do {
|
||||||
CURLMsg *msg;
|
CURLMsg *msg;
|
||||||
msg = curl_multi_info_read(s->multi, &msgs_in_queue);
|
msg = curl_multi_info_read(s->multi, &msgs_in_queue);
|
||||||
|
|
||||||
/* Quit when there are no more completions */
|
|
||||||
if (!msg)
|
if (!msg)
|
||||||
break;
|
break;
|
||||||
|
if (msg->msg == CURLMSG_NONE)
|
||||||
|
break;
|
||||||
|
|
||||||
if (msg->msg == CURLMSG_DONE) {
|
switch (msg->msg) {
|
||||||
|
case CURLMSG_DONE:
|
||||||
|
{
|
||||||
CURLState *state = NULL;
|
CURLState *state = NULL;
|
||||||
curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE,
|
curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, (char**)&state);
|
||||||
(char **)&state);
|
|
||||||
|
|
||||||
/* ACBs for successful messages get completed in curl_read_cb */
|
/* ACBs for successful messages get completed in curl_read_cb */
|
||||||
if (msg->data.result != CURLE_OK) {
|
if (msg->data.result != CURLE_OK) {
|
||||||
int i;
|
int i;
|
||||||
static int errcount = 100;
|
|
||||||
|
|
||||||
/* Don't lose the original error message from curl, since
|
|
||||||
* it contains extra data.
|
|
||||||
*/
|
|
||||||
if (errcount > 0) {
|
|
||||||
error_report("curl: %s", state->errmsg);
|
|
||||||
if (--errcount == 0) {
|
|
||||||
error_report("curl: further errors suppressed");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < CURL_NUM_ACB; i++) {
|
for (i = 0; i < CURL_NUM_ACB; i++) {
|
||||||
CURLAIOCB *acb = state->acb[i];
|
CURLAIOCB *acb = state->acb[i];
|
||||||
|
|
||||||
@@ -320,8 +255,8 @@ static void curl_multi_check_completion(BDRVCURLState *s)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
acb->common.cb(acb->common.opaque, -EPROTO);
|
acb->common.cb(acb->common.opaque, -EIO);
|
||||||
qemu_aio_unref(acb);
|
qemu_aio_release(acb);
|
||||||
state->acb[i] = NULL;
|
state->acb[i] = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -329,52 +264,14 @@ static void curl_multi_check_completion(BDRVCURLState *s)
|
|||||||
curl_clean_state(state);
|
curl_clean_state(state);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
msgs_in_queue = 0;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
} while(msgs_in_queue);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void curl_multi_do(void *arg)
|
static CURLState *curl_init_state(BDRVCURLState *s)
|
||||||
{
|
|
||||||
CURLState *s = (CURLState *)arg;
|
|
||||||
int running;
|
|
||||||
int r;
|
|
||||||
|
|
||||||
if (!s->s->multi) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
do {
|
|
||||||
r = curl_multi_socket_action(s->s->multi, s->sock_fd, 0, &running);
|
|
||||||
} while(r == CURLM_CALL_MULTI_PERFORM);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static void curl_multi_read(void *arg)
|
|
||||||
{
|
|
||||||
CURLState *s = (CURLState *)arg;
|
|
||||||
|
|
||||||
curl_multi_do(arg);
|
|
||||||
curl_multi_check_completion(s->s);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void curl_multi_timeout_do(void *arg)
|
|
||||||
{
|
|
||||||
#ifdef NEED_CURL_TIMER_CALLBACK
|
|
||||||
BDRVCURLState *s = (BDRVCURLState *)arg;
|
|
||||||
int running;
|
|
||||||
|
|
||||||
if (!s->multi) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
|
|
||||||
|
|
||||||
curl_multi_check_completion(s);
|
|
||||||
#else
|
|
||||||
abort();
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static CURLState *curl_init_state(BlockDriverState *bs, BDRVCURLState *s)
|
|
||||||
{
|
{
|
||||||
CURLState *state = NULL;
|
CURLState *state = NULL;
|
||||||
int i, j;
|
int i, j;
|
||||||
@@ -392,24 +289,20 @@ static CURLState *curl_init_state(BlockDriverState *bs, BDRVCURLState *s)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (!state) {
|
if (!state) {
|
||||||
aio_poll(bdrv_get_aio_context(bs), true);
|
g_usleep(100);
|
||||||
|
curl_multi_do(s);
|
||||||
}
|
}
|
||||||
} while(!state);
|
} while(!state);
|
||||||
|
|
||||||
if (!state->curl) {
|
if (state->curl)
|
||||||
|
goto has_curl;
|
||||||
|
|
||||||
state->curl = curl_easy_init();
|
state->curl = curl_easy_init();
|
||||||
if (!state->curl) {
|
if (!state->curl)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
curl_easy_setopt(state->curl, CURLOPT_URL, s->url);
|
curl_easy_setopt(state->curl, CURLOPT_URL, s->url);
|
||||||
curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER,
|
curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, 5);
|
||||||
(long) s->sslverify);
|
curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_read_cb);
|
||||||
if (s->cookie) {
|
|
||||||
curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie);
|
|
||||||
}
|
|
||||||
curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, (long)s->timeout);
|
|
||||||
curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION,
|
|
||||||
(void *)curl_read_cb);
|
|
||||||
curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state);
|
curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state);
|
||||||
curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state);
|
curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state);
|
||||||
curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1);
|
curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1);
|
||||||
@@ -432,7 +325,8 @@ static CURLState *curl_init_state(BlockDriverState *bs, BDRVCURLState *s)
|
|||||||
#ifdef DEBUG_VERBOSE
|
#ifdef DEBUG_VERBOSE
|
||||||
curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1);
|
curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1);
|
||||||
#endif
|
#endif
|
||||||
}
|
|
||||||
|
has_curl:
|
||||||
|
|
||||||
state->s = s;
|
state->s = s;
|
||||||
|
|
||||||
@@ -446,203 +340,133 @@ static void curl_clean_state(CURLState *s)
|
|||||||
s->in_use = 0;
|
s->in_use = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void curl_parse_filename(const char *filename, QDict *options,
|
static int curl_open(BlockDriverState *bs, const char *filename, int flags)
|
||||||
Error **errp)
|
|
||||||
{
|
|
||||||
qdict_put(options, CURL_BLOCK_OPT_URL, qstring_from_str(filename));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void curl_detach_aio_context(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
BDRVCURLState *s = bs->opaque;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
for (i = 0; i < CURL_NUM_STATES; i++) {
|
|
||||||
if (s->states[i].in_use) {
|
|
||||||
curl_clean_state(&s->states[i]);
|
|
||||||
}
|
|
||||||
if (s->states[i].curl) {
|
|
||||||
curl_easy_cleanup(s->states[i].curl);
|
|
||||||
s->states[i].curl = NULL;
|
|
||||||
}
|
|
||||||
g_free(s->states[i].orig_buf);
|
|
||||||
s->states[i].orig_buf = NULL;
|
|
||||||
}
|
|
||||||
if (s->multi) {
|
|
||||||
curl_multi_cleanup(s->multi);
|
|
||||||
s->multi = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
timer_del(&s->timer);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void curl_attach_aio_context(BlockDriverState *bs,
|
|
||||||
AioContext *new_context)
|
|
||||||
{
|
|
||||||
BDRVCURLState *s = bs->opaque;
|
|
||||||
|
|
||||||
aio_timer_init(new_context, &s->timer,
|
|
||||||
QEMU_CLOCK_REALTIME, SCALE_NS,
|
|
||||||
curl_multi_timeout_do, s);
|
|
||||||
|
|
||||||
assert(!s->multi);
|
|
||||||
s->multi = curl_multi_init();
|
|
||||||
s->aio_context = new_context;
|
|
||||||
curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb);
|
|
||||||
#ifdef NEED_CURL_TIMER_CALLBACK
|
|
||||||
curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s);
|
|
||||||
curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static QemuOptsList runtime_opts = {
|
|
||||||
.name = "curl",
|
|
||||||
.head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
|
|
||||||
.desc = {
|
|
||||||
{
|
|
||||||
.name = CURL_BLOCK_OPT_URL,
|
|
||||||
.type = QEMU_OPT_STRING,
|
|
||||||
.help = "URL to open",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
.name = CURL_BLOCK_OPT_READAHEAD,
|
|
||||||
.type = QEMU_OPT_SIZE,
|
|
||||||
.help = "Readahead size",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
.name = CURL_BLOCK_OPT_SSLVERIFY,
|
|
||||||
.type = QEMU_OPT_BOOL,
|
|
||||||
.help = "Verify SSL certificate"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
.name = CURL_BLOCK_OPT_TIMEOUT,
|
|
||||||
.type = QEMU_OPT_NUMBER,
|
|
||||||
.help = "Curl timeout"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
.name = CURL_BLOCK_OPT_COOKIE,
|
|
||||||
.type = QEMU_OPT_STRING,
|
|
||||||
.help = "Pass the cookie or list of cookies with each request"
|
|
||||||
},
|
|
||||||
{ /* end of list */ }
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
static int curl_open(BlockDriverState *bs, QDict *options, int flags,
|
|
||||||
Error **errp)
|
|
||||||
{
|
{
|
||||||
BDRVCURLState *s = bs->opaque;
|
BDRVCURLState *s = bs->opaque;
|
||||||
CURLState *state = NULL;
|
CURLState *state = NULL;
|
||||||
QemuOpts *opts;
|
|
||||||
Error *local_err = NULL;
|
|
||||||
const char *file;
|
|
||||||
const char *cookie;
|
|
||||||
double d;
|
double d;
|
||||||
|
|
||||||
|
#define RA_OPTSTR ":readahead="
|
||||||
|
char *file;
|
||||||
|
char *ra;
|
||||||
|
const char *ra_val;
|
||||||
|
int parse_state = 0;
|
||||||
|
|
||||||
static int inited = 0;
|
static int inited = 0;
|
||||||
|
|
||||||
if (flags & BDRV_O_RDWR) {
|
file = g_strdup(filename);
|
||||||
error_setg(errp, "curl block device does not support writes");
|
s->readahead_size = READ_AHEAD_SIZE;
|
||||||
return -EROFS;
|
|
||||||
|
/* Parse a trailing ":readahead=#:" param, if present. */
|
||||||
|
ra = file + strlen(file) - 1;
|
||||||
|
while (ra >= file) {
|
||||||
|
if (parse_state == 0) {
|
||||||
|
if (*ra == ':')
|
||||||
|
parse_state++;
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
} else if (parse_state == 1) {
|
||||||
|
if (*ra > '9' || *ra < '0') {
|
||||||
|
char *opt_start = ra - strlen(RA_OPTSTR) + 1;
|
||||||
|
if (opt_start > file &&
|
||||||
|
strncmp(opt_start, RA_OPTSTR, strlen(RA_OPTSTR)) == 0) {
|
||||||
|
ra_val = ra + 1;
|
||||||
|
ra -= strlen(RA_OPTSTR) - 1;
|
||||||
|
*ra = '\0';
|
||||||
|
s->readahead_size = atoi(ra_val);
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ra--;
|
||||||
}
|
}
|
||||||
|
|
||||||
opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
|
|
||||||
qemu_opts_absorb_qdict(opts, options, &local_err);
|
|
||||||
if (local_err) {
|
|
||||||
error_propagate(errp, local_err);
|
|
||||||
goto out_noclean;
|
|
||||||
}
|
|
||||||
|
|
||||||
s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD,
|
|
||||||
READ_AHEAD_DEFAULT);
|
|
||||||
if ((s->readahead_size & 0x1ff) != 0) {
|
if ((s->readahead_size & 0x1ff) != 0) {
|
||||||
error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
|
fprintf(stderr, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512\n",
|
||||||
s->readahead_size);
|
s->readahead_size);
|
||||||
goto out_noclean;
|
goto out_noclean;
|
||||||
}
|
}
|
||||||
|
|
||||||
s->timeout = qemu_opt_get_number(opts, CURL_BLOCK_OPT_TIMEOUT,
|
|
||||||
CURL_TIMEOUT_DEFAULT);
|
|
||||||
if (s->timeout > CURL_TIMEOUT_MAX) {
|
|
||||||
error_setg(errp, "timeout parameter is too large or negative");
|
|
||||||
goto out_noclean;
|
|
||||||
}
|
|
||||||
|
|
||||||
s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY, true);
|
|
||||||
|
|
||||||
cookie = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE);
|
|
||||||
s->cookie = g_strdup(cookie);
|
|
||||||
|
|
||||||
file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL);
|
|
||||||
if (file == NULL) {
|
|
||||||
error_setg(errp, "curl block driver requires an 'url' option");
|
|
||||||
goto out_noclean;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!inited) {
|
if (!inited) {
|
||||||
curl_global_init(CURL_GLOBAL_ALL);
|
curl_global_init(CURL_GLOBAL_ALL);
|
||||||
inited = 1;
|
inited = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
DPRINTF("CURL: Opening %s\n", file);
|
DPRINTF("CURL: Opening %s\n", file);
|
||||||
s->aio_context = bdrv_get_aio_context(bs);
|
s->url = file;
|
||||||
s->url = g_strdup(file);
|
state = curl_init_state(s);
|
||||||
state = curl_init_state(bs, s);
|
|
||||||
if (!state)
|
if (!state)
|
||||||
goto out_noclean;
|
goto out_noclean;
|
||||||
|
|
||||||
// Get file size
|
// Get file size
|
||||||
|
|
||||||
s->accept_range = false;
|
|
||||||
curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1);
|
curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1);
|
||||||
curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION,
|
curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_size_cb);
|
||||||
curl_header_cb);
|
|
||||||
curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s);
|
|
||||||
if (curl_easy_perform(state->curl))
|
if (curl_easy_perform(state->curl))
|
||||||
goto out;
|
goto out;
|
||||||
curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d);
|
curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d);
|
||||||
|
curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_read_cb);
|
||||||
|
curl_easy_setopt(state->curl, CURLOPT_NOBODY, 0);
|
||||||
if (d)
|
if (d)
|
||||||
s->len = (size_t)d;
|
s->len = (size_t)d;
|
||||||
else if(!s->len)
|
else if(!s->len)
|
||||||
goto out;
|
goto out;
|
||||||
if ((!strncasecmp(s->url, "http://", strlen("http://"))
|
|
||||||
|| !strncasecmp(s->url, "https://", strlen("https://")))
|
|
||||||
&& !s->accept_range) {
|
|
||||||
pstrcpy(state->errmsg, CURL_ERROR_SIZE,
|
|
||||||
"Server does not support 'range' (byte ranges).");
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
DPRINTF("CURL: Size = %zd\n", s->len);
|
DPRINTF("CURL: Size = %zd\n", s->len);
|
||||||
|
|
||||||
curl_clean_state(state);
|
curl_clean_state(state);
|
||||||
curl_easy_cleanup(state->curl);
|
curl_easy_cleanup(state->curl);
|
||||||
state->curl = NULL;
|
state->curl = NULL;
|
||||||
|
|
||||||
curl_attach_aio_context(bs, bdrv_get_aio_context(bs));
|
// Now we know the file exists and its size, so let's
|
||||||
|
// initialize the multi interface!
|
||||||
|
|
||||||
|
s->multi = curl_multi_init();
|
||||||
|
curl_multi_setopt( s->multi, CURLMOPT_SOCKETDATA, s);
|
||||||
|
curl_multi_setopt( s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb );
|
||||||
|
curl_multi_do(s);
|
||||||
|
|
||||||
qemu_opts_del(opts);
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
out:
|
out:
|
||||||
error_setg(errp, "CURL: Error opening file: %s", state->errmsg);
|
fprintf(stderr, "CURL: Error opening file: %s\n", state->errmsg);
|
||||||
curl_easy_cleanup(state->curl);
|
curl_easy_cleanup(state->curl);
|
||||||
state->curl = NULL;
|
state->curl = NULL;
|
||||||
out_noclean:
|
out_noclean:
|
||||||
g_free(s->cookie);
|
g_free(file);
|
||||||
g_free(s->url);
|
|
||||||
qemu_opts_del(opts);
|
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int curl_aio_flush(void *opaque)
|
||||||
|
{
|
||||||
|
BDRVCURLState *s = opaque;
|
||||||
|
int i, j;
|
||||||
|
|
||||||
|
for (i=0; i < CURL_NUM_STATES; i++) {
|
||||||
|
for(j=0; j < CURL_NUM_ACB; j++) {
|
||||||
|
if (s->states[i].acb[j]) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void curl_aio_cancel(BlockDriverAIOCB *blockacb)
|
||||||
|
{
|
||||||
|
// Do we have to implement canceling? Seems to work without...
|
||||||
|
}
|
||||||
|
|
||||||
static const AIOCBInfo curl_aiocb_info = {
|
static const AIOCBInfo curl_aiocb_info = {
|
||||||
.aiocb_size = sizeof(CURLAIOCB),
|
.aiocb_size = sizeof(CURLAIOCB),
|
||||||
|
.cancel = curl_aio_cancel,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static void curl_readv_bh_cb(void *p)
|
static void curl_readv_bh_cb(void *p)
|
||||||
{
|
{
|
||||||
CURLState *state;
|
CURLState *state;
|
||||||
int running;
|
|
||||||
|
|
||||||
CURLAIOCB *acb = p;
|
CURLAIOCB *acb = p;
|
||||||
BDRVCURLState *s = acb->common.bs->opaque;
|
BDRVCURLState *s = acb->common.bs->opaque;
|
||||||
@@ -657,7 +481,7 @@ static void curl_readv_bh_cb(void *p)
|
|||||||
// we can just call the callback and be done.
|
// we can just call the callback and be done.
|
||||||
switch (curl_find_buf(s, start, acb->nb_sectors * SECTOR_SIZE, acb)) {
|
switch (curl_find_buf(s, start, acb->nb_sectors * SECTOR_SIZE, acb)) {
|
||||||
case FIND_RET_OK:
|
case FIND_RET_OK:
|
||||||
qemu_aio_unref(acb);
|
qemu_aio_release(acb);
|
||||||
// fall through
|
// fall through
|
||||||
case FIND_RET_WAIT:
|
case FIND_RET_WAIT:
|
||||||
return;
|
return;
|
||||||
@@ -666,10 +490,10 @@ static void curl_readv_bh_cb(void *p)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// No cache found, so let's start a new request
|
// No cache found, so let's start a new request
|
||||||
state = curl_init_state(acb->common.bs, s);
|
state = curl_init_state(s);
|
||||||
if (!state) {
|
if (!state) {
|
||||||
acb->common.cb(acb->common.opaque, -EIO);
|
acb->common.cb(acb->common.opaque, -EIO);
|
||||||
qemu_aio_unref(acb);
|
qemu_aio_release(acb);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -677,17 +501,12 @@ static void curl_readv_bh_cb(void *p)
|
|||||||
acb->end = (acb->nb_sectors * SECTOR_SIZE);
|
acb->end = (acb->nb_sectors * SECTOR_SIZE);
|
||||||
|
|
||||||
state->buf_off = 0;
|
state->buf_off = 0;
|
||||||
|
if (state->orig_buf)
|
||||||
g_free(state->orig_buf);
|
g_free(state->orig_buf);
|
||||||
state->buf_start = start;
|
state->buf_start = start;
|
||||||
state->buf_len = acb->end + s->readahead_size;
|
state->buf_len = acb->end + s->readahead_size;
|
||||||
end = MIN(start + state->buf_len, s->len) - 1;
|
end = MIN(start + state->buf_len, s->len) - 1;
|
||||||
state->orig_buf = g_try_malloc(state->buf_len);
|
state->orig_buf = g_malloc(state->buf_len);
|
||||||
if (state->buf_len && state->orig_buf == NULL) {
|
|
||||||
curl_clean_state(state);
|
|
||||||
acb->common.cb(acb->common.opaque, -ENOMEM);
|
|
||||||
qemu_aio_unref(acb);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
state->acb[0] = acb;
|
state->acb[0] = acb;
|
||||||
|
|
||||||
snprintf(state->range, 127, "%zd-%zd", start, end);
|
snprintf(state->range, 127, "%zd-%zd", start, end);
|
||||||
@@ -696,14 +515,13 @@ static void curl_readv_bh_cb(void *p)
|
|||||||
curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range);
|
curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range);
|
||||||
|
|
||||||
curl_multi_add_handle(s->multi, state->curl);
|
curl_multi_add_handle(s->multi, state->curl);
|
||||||
|
curl_multi_do(s);
|
||||||
|
|
||||||
/* Tell curl it needs to kick things off */
|
|
||||||
curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static BlockAIOCB *curl_aio_readv(BlockDriverState *bs,
|
static BlockDriverAIOCB *curl_aio_readv(BlockDriverState *bs,
|
||||||
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
||||||
BlockCompletionFunc *cb, void *opaque)
|
BlockDriverCompletionFunc *cb, void *opaque)
|
||||||
{
|
{
|
||||||
CURLAIOCB *acb;
|
CURLAIOCB *acb;
|
||||||
|
|
||||||
@@ -713,7 +531,13 @@ static BlockAIOCB *curl_aio_readv(BlockDriverState *bs,
|
|||||||
acb->sector_num = sector_num;
|
acb->sector_num = sector_num;
|
||||||
acb->nb_sectors = nb_sectors;
|
acb->nb_sectors = nb_sectors;
|
||||||
|
|
||||||
acb->bh = aio_bh_new(bdrv_get_aio_context(bs), curl_readv_bh_cb, acb);
|
acb->bh = qemu_bh_new(curl_readv_bh_cb, acb);
|
||||||
|
|
||||||
|
if (!acb->bh) {
|
||||||
|
DPRINTF("CURL: qemu_bh_new failed\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
qemu_bh_schedule(acb->bh);
|
qemu_bh_schedule(acb->bh);
|
||||||
return &acb->common;
|
return &acb->common;
|
||||||
}
|
}
|
||||||
@@ -721,11 +545,23 @@ static BlockAIOCB *curl_aio_readv(BlockDriverState *bs,
|
|||||||
static void curl_close(BlockDriverState *bs)
|
static void curl_close(BlockDriverState *bs)
|
||||||
{
|
{
|
||||||
BDRVCURLState *s = bs->opaque;
|
BDRVCURLState *s = bs->opaque;
|
||||||
|
int i;
|
||||||
|
|
||||||
DPRINTF("CURL: Close\n");
|
DPRINTF("CURL: Close\n");
|
||||||
curl_detach_aio_context(bs);
|
for (i=0; i<CURL_NUM_STATES; i++) {
|
||||||
|
if (s->states[i].in_use)
|
||||||
g_free(s->cookie);
|
curl_clean_state(&s->states[i]);
|
||||||
|
if (s->states[i].curl) {
|
||||||
|
curl_easy_cleanup(s->states[i].curl);
|
||||||
|
s->states[i].curl = NULL;
|
||||||
|
}
|
||||||
|
if (s->states[i].orig_buf) {
|
||||||
|
g_free(s->states[i].orig_buf);
|
||||||
|
s->states[i].orig_buf = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (s->multi)
|
||||||
|
curl_multi_cleanup(s->multi);
|
||||||
g_free(s->url);
|
g_free(s->url);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -740,15 +576,11 @@ static BlockDriver bdrv_http = {
|
|||||||
.protocol_name = "http",
|
.protocol_name = "http",
|
||||||
|
|
||||||
.instance_size = sizeof(BDRVCURLState),
|
.instance_size = sizeof(BDRVCURLState),
|
||||||
.bdrv_parse_filename = curl_parse_filename,
|
|
||||||
.bdrv_file_open = curl_open,
|
.bdrv_file_open = curl_open,
|
||||||
.bdrv_close = curl_close,
|
.bdrv_close = curl_close,
|
||||||
.bdrv_getlength = curl_getlength,
|
.bdrv_getlength = curl_getlength,
|
||||||
|
|
||||||
.bdrv_aio_readv = curl_aio_readv,
|
.bdrv_aio_readv = curl_aio_readv,
|
||||||
|
|
||||||
.bdrv_detach_aio_context = curl_detach_aio_context,
|
|
||||||
.bdrv_attach_aio_context = curl_attach_aio_context,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static BlockDriver bdrv_https = {
|
static BlockDriver bdrv_https = {
|
||||||
@@ -756,15 +588,11 @@ static BlockDriver bdrv_https = {
|
|||||||
.protocol_name = "https",
|
.protocol_name = "https",
|
||||||
|
|
||||||
.instance_size = sizeof(BDRVCURLState),
|
.instance_size = sizeof(BDRVCURLState),
|
||||||
.bdrv_parse_filename = curl_parse_filename,
|
|
||||||
.bdrv_file_open = curl_open,
|
.bdrv_file_open = curl_open,
|
||||||
.bdrv_close = curl_close,
|
.bdrv_close = curl_close,
|
||||||
.bdrv_getlength = curl_getlength,
|
.bdrv_getlength = curl_getlength,
|
||||||
|
|
||||||
.bdrv_aio_readv = curl_aio_readv,
|
.bdrv_aio_readv = curl_aio_readv,
|
||||||
|
|
||||||
.bdrv_detach_aio_context = curl_detach_aio_context,
|
|
||||||
.bdrv_attach_aio_context = curl_attach_aio_context,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static BlockDriver bdrv_ftp = {
|
static BlockDriver bdrv_ftp = {
|
||||||
@@ -772,15 +600,11 @@ static BlockDriver bdrv_ftp = {
|
|||||||
.protocol_name = "ftp",
|
.protocol_name = "ftp",
|
||||||
|
|
||||||
.instance_size = sizeof(BDRVCURLState),
|
.instance_size = sizeof(BDRVCURLState),
|
||||||
.bdrv_parse_filename = curl_parse_filename,
|
|
||||||
.bdrv_file_open = curl_open,
|
.bdrv_file_open = curl_open,
|
||||||
.bdrv_close = curl_close,
|
.bdrv_close = curl_close,
|
||||||
.bdrv_getlength = curl_getlength,
|
.bdrv_getlength = curl_getlength,
|
||||||
|
|
||||||
.bdrv_aio_readv = curl_aio_readv,
|
.bdrv_aio_readv = curl_aio_readv,
|
||||||
|
|
||||||
.bdrv_detach_aio_context = curl_detach_aio_context,
|
|
||||||
.bdrv_attach_aio_context = curl_attach_aio_context,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static BlockDriver bdrv_ftps = {
|
static BlockDriver bdrv_ftps = {
|
||||||
@@ -788,15 +612,11 @@ static BlockDriver bdrv_ftps = {
|
|||||||
.protocol_name = "ftps",
|
.protocol_name = "ftps",
|
||||||
|
|
||||||
.instance_size = sizeof(BDRVCURLState),
|
.instance_size = sizeof(BDRVCURLState),
|
||||||
.bdrv_parse_filename = curl_parse_filename,
|
|
||||||
.bdrv_file_open = curl_open,
|
.bdrv_file_open = curl_open,
|
||||||
.bdrv_close = curl_close,
|
.bdrv_close = curl_close,
|
||||||
.bdrv_getlength = curl_getlength,
|
.bdrv_getlength = curl_getlength,
|
||||||
|
|
||||||
.bdrv_aio_readv = curl_aio_readv,
|
.bdrv_aio_readv = curl_aio_readv,
|
||||||
|
|
||||||
.bdrv_detach_aio_context = curl_detach_aio_context,
|
|
||||||
.bdrv_attach_aio_context = curl_attach_aio_context,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static BlockDriver bdrv_tftp = {
|
static BlockDriver bdrv_tftp = {
|
||||||
@@ -804,15 +624,11 @@ static BlockDriver bdrv_tftp = {
|
|||||||
.protocol_name = "tftp",
|
.protocol_name = "tftp",
|
||||||
|
|
||||||
.instance_size = sizeof(BDRVCURLState),
|
.instance_size = sizeof(BDRVCURLState),
|
||||||
.bdrv_parse_filename = curl_parse_filename,
|
|
||||||
.bdrv_file_open = curl_open,
|
.bdrv_file_open = curl_open,
|
||||||
.bdrv_close = curl_close,
|
.bdrv_close = curl_close,
|
||||||
.bdrv_getlength = curl_getlength,
|
.bdrv_getlength = curl_getlength,
|
||||||
|
|
||||||
.bdrv_aio_readv = curl_aio_readv,
|
.bdrv_aio_readv = curl_aio_readv,
|
||||||
|
|
||||||
.bdrv_detach_aio_context = curl_detach_aio_context,
|
|
||||||
.bdrv_attach_aio_context = curl_attach_aio_context,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static void curl_block_init(void)
|
static void curl_block_init(void)
|
||||||
|
|||||||
572
block/dictzip.c
Normal file
572
block/dictzip.c
Normal file
@@ -0,0 +1,572 @@
|
|||||||
|
/*
|
||||||
|
* 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 {
|
||||||
|
BlockDriverAIOCB 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 dictzip_probe(const uint8_t *buf, int buf_size, const char *filename)
|
||||||
|
{
|
||||||
|
if (buf_size < 2)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
/* We match on every gzip file */
|
||||||
|
if ((buf[0] == GZ_MAGIC1) && (buf[1] == GZ_MAGIC2))
|
||||||
|
return 100;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 int dictzip_open(BlockDriverState *bs, const char *filename, int flags)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
const char *fname = filename;
|
||||||
|
|
||||||
|
if (!strncmp(filename, "dzip://", 7))
|
||||||
|
fname += 7;
|
||||||
|
else if (!strncmp(filename, "dzip:", 5))
|
||||||
|
fname += 5;
|
||||||
|
|
||||||
|
ret = bdrv_file_open(&s->hd, fname, flags);
|
||||||
|
if (ret < 0)
|
||||||
|
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 = %d | %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 %#x - %#x = offset %#x -> %#x\n", i * s->chunk_len, (i+1) * s->chunk_len, s->offsets[i], offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
fail:
|
||||||
|
fprintf(stderr, "DictZip: Error opening file: %s\n", err);
|
||||||
|
bdrv_delete(s->hd);
|
||||||
|
if (s->chunks)
|
||||||
|
g_free(s->chunks);
|
||||||
|
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_release(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_release(acb);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void dictzip_aio_cancel(BlockDriverAIOCB *blockacb)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
static const AIOCBInfo dictzip_aiocb_info = {
|
||||||
|
.aiocb_size = sizeof(DictZipAIOCB),
|
||||||
|
.cancel = dictzip_aio_cancel,
|
||||||
|
};
|
||||||
|
|
||||||
|
/* This is where we get a request from a caller to read something */
|
||||||
|
static BlockDriverAIOCB *dictzip_aio_readv(BlockDriverState *bs,
|
||||||
|
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
||||||
|
BlockDriverCompletionFunc *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 %d - %d => %d - %d\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_probe = dictzip_probe,
|
||||||
|
|
||||||
|
.bdrv_aio_readv = dictzip_aio_readv,
|
||||||
|
};
|
||||||
|
|
||||||
|
static void dictzip_block_init(void)
|
||||||
|
{
|
||||||
|
bdrv_register(&bdrv_dictzip);
|
||||||
|
}
|
||||||
|
|
||||||
|
block_init(dictzip_block_init);
|
||||||
536
block/dmg.c
536
block/dmg.c
@@ -24,13 +24,8 @@
|
|||||||
#include "qemu-common.h"
|
#include "qemu-common.h"
|
||||||
#include "block/block_int.h"
|
#include "block/block_int.h"
|
||||||
#include "qemu/bswap.h"
|
#include "qemu/bswap.h"
|
||||||
#include "qemu/error-report.h"
|
|
||||||
#include "qemu/module.h"
|
#include "qemu/module.h"
|
||||||
#include <zlib.h>
|
#include <zlib.h>
|
||||||
#ifdef CONFIG_BZIP2
|
|
||||||
#include <bzlib.h>
|
|
||||||
#endif
|
|
||||||
#include <glib.h>
|
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
/* Limit chunk sizes to prevent unreasonable amounts of memory being used
|
/* Limit chunk sizes to prevent unreasonable amounts of memory being used
|
||||||
@@ -60,23 +55,13 @@ typedef struct BDRVDMGState {
|
|||||||
uint8_t *compressed_chunk;
|
uint8_t *compressed_chunk;
|
||||||
uint8_t *uncompressed_chunk;
|
uint8_t *uncompressed_chunk;
|
||||||
z_stream zstream;
|
z_stream zstream;
|
||||||
#ifdef CONFIG_BZIP2
|
|
||||||
bz_stream bzstream;
|
|
||||||
#endif
|
|
||||||
} BDRVDMGState;
|
} BDRVDMGState;
|
||||||
|
|
||||||
static int dmg_probe(const uint8_t *buf, int buf_size, const char *filename)
|
static int dmg_probe(const uint8_t *buf, int buf_size, const char *filename)
|
||||||
{
|
{
|
||||||
int len;
|
int len=strlen(filename);
|
||||||
|
if(len>4 && !strcmp(filename+len-4,".dmg"))
|
||||||
if (!filename) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
len = strlen(filename);
|
|
||||||
if (len > 4 && !strcmp(filename + len - 4, ".dmg")) {
|
|
||||||
return 2;
|
return 2;
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,7 +70,7 @@ static int read_uint64(BlockDriverState *bs, int64_t offset, uint64_t *result)
|
|||||||
uint64_t buffer;
|
uint64_t buffer;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
ret = bdrv_pread(bs->file->bs, offset, &buffer, 8);
|
ret = bdrv_pread(bs->file, offset, &buffer, 8);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -99,7 +84,7 @@ static int read_uint32(BlockDriverState *bs, int64_t offset, uint32_t *result)
|
|||||||
uint32_t buffer;
|
uint32_t buffer;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
ret = bdrv_pread(bs->file->bs, offset, &buffer, 4);
|
ret = bdrv_pread(bs->file, offset, &buffer, 4);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -108,16 +93,6 @@ static int read_uint32(BlockDriverState *bs, int64_t offset, uint32_t *result)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline uint64_t buff_read_uint64(const uint8_t *buffer, int64_t offset)
|
|
||||||
{
|
|
||||||
return be64_to_cpu(*(uint64_t *)&buffer[offset]);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline uint32_t buff_read_uint32(const uint8_t *buffer, int64_t offset)
|
|
||||||
{
|
|
||||||
return be32_to_cpu(*(uint32_t *)&buffer[offset]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Increase max chunk sizes, if necessary. This function is used to calculate
|
/* Increase max chunk sizes, if necessary. This function is used to calculate
|
||||||
* the buffer sizes needed for compressed/uncompressed chunk I/O.
|
* the buffer sizes needed for compressed/uncompressed chunk I/O.
|
||||||
*/
|
*/
|
||||||
@@ -130,7 +105,6 @@ static void update_max_chunk_size(BDRVDMGState *s, uint32_t chunk,
|
|||||||
|
|
||||||
switch (s->types[chunk]) {
|
switch (s->types[chunk]) {
|
||||||
case 0x80000005: /* zlib compressed */
|
case 0x80000005: /* zlib compressed */
|
||||||
case 0x80000006: /* bzip2 compressed */
|
|
||||||
compressed_size = s->lengths[chunk];
|
compressed_size = s->lengths[chunk];
|
||||||
uncompressed_sectors = s->sectorcounts[chunk];
|
uncompressed_sectors = s->sectorcounts[chunk];
|
||||||
break;
|
break;
|
||||||
@@ -138,9 +112,7 @@ static void update_max_chunk_size(BDRVDMGState *s, uint32_t chunk,
|
|||||||
uncompressed_sectors = (s->lengths[chunk] + 511) / 512;
|
uncompressed_sectors = (s->lengths[chunk] + 511) / 512;
|
||||||
break;
|
break;
|
||||||
case 2: /* zero */
|
case 2: /* zero */
|
||||||
/* as the all-zeroes block may be large, it is treated specially: the
|
uncompressed_sectors = s->sectorcounts[chunk];
|
||||||
* sector is not copied from a large buffer, a simple memset is used
|
|
||||||
* instead. Therefore uncompressed_sectors does not need to be set. */
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,99 +124,79 @@ static void update_max_chunk_size(BDRVDMGState *s, uint32_t chunk,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int64_t dmg_find_koly_offset(BlockDriverState *file_bs, Error **errp)
|
static int dmg_open(BlockDriverState *bs, int flags)
|
||||||
{
|
{
|
||||||
int64_t length;
|
BDRVDMGState *s = bs->opaque;
|
||||||
int64_t offset = 0;
|
uint64_t info_begin, info_end, last_in_offset, last_out_offset;
|
||||||
uint8_t buffer[515];
|
uint32_t count, tmp;
|
||||||
int i, ret;
|
uint32_t max_compressed_size = 1, max_sectors_per_chunk = 1, i;
|
||||||
|
int64_t offset;
|
||||||
/* bdrv_getlength returns a multiple of block size (512), rounded up. Since
|
|
||||||
* dmg images can have odd sizes, try to look for the "koly" magic which
|
|
||||||
* marks the begin of the UDIF trailer (512 bytes). This magic can be found
|
|
||||||
* in the last 511 bytes of the second-last sector or the first 4 bytes of
|
|
||||||
* the last sector (search space: 515 bytes) */
|
|
||||||
length = bdrv_getlength(file_bs);
|
|
||||||
if (length < 0) {
|
|
||||||
error_setg_errno(errp, -length,
|
|
||||||
"Failed to get file size while reading UDIF trailer");
|
|
||||||
return length;
|
|
||||||
} else if (length < 512) {
|
|
||||||
error_setg(errp, "dmg file must be at least 512 bytes long");
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
if (length > 511 + 512) {
|
|
||||||
offset = length - 511 - 512;
|
|
||||||
}
|
|
||||||
length = length < 515 ? length : 515;
|
|
||||||
ret = bdrv_pread(file_bs, offset, buffer, length);
|
|
||||||
if (ret < 0) {
|
|
||||||
error_setg_errno(errp, -ret, "Failed while reading UDIF trailer");
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
for (i = 0; i < length - 3; i++) {
|
|
||||||
if (buffer[i] == 'k' && buffer[i+1] == 'o' &&
|
|
||||||
buffer[i+2] == 'l' && buffer[i+3] == 'y') {
|
|
||||||
return offset + i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
error_setg(errp, "Could not locate UDIF trailer in dmg file");
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* used when building the sector table */
|
|
||||||
typedef struct DmgHeaderState {
|
|
||||||
/* used internally by dmg_read_mish_block to remember offsets of blocks
|
|
||||||
* across calls */
|
|
||||||
uint64_t data_fork_offset;
|
|
||||||
/* exported for dmg_open */
|
|
||||||
uint32_t max_compressed_size;
|
|
||||||
uint32_t max_sectors_per_chunk;
|
|
||||||
} DmgHeaderState;
|
|
||||||
|
|
||||||
static bool dmg_is_known_block_type(uint32_t entry_type)
|
|
||||||
{
|
|
||||||
switch (entry_type) {
|
|
||||||
case 0x00000001: /* uncompressed */
|
|
||||||
case 0x00000002: /* zeroes */
|
|
||||||
case 0x80000005: /* zlib */
|
|
||||||
#ifdef CONFIG_BZIP2
|
|
||||||
case 0x80000006: /* bzip2 */
|
|
||||||
#endif
|
|
||||||
return true;
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static int dmg_read_mish_block(BDRVDMGState *s, DmgHeaderState *ds,
|
|
||||||
uint8_t *buffer, uint32_t count)
|
|
||||||
{
|
|
||||||
uint32_t type, i;
|
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
|
bs->read_only = 1;
|
||||||
|
s->n_chunks = 0;
|
||||||
|
s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
|
||||||
|
|
||||||
|
/* read offset of info blocks */
|
||||||
|
offset = bdrv_getlength(bs->file);
|
||||||
|
if (offset < 0) {
|
||||||
|
ret = offset;
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
offset -= 0x1d8;
|
||||||
|
|
||||||
|
ret = read_uint64(bs, offset, &info_begin);
|
||||||
|
if (ret < 0) {
|
||||||
|
goto fail;
|
||||||
|
} else if (info_begin == 0) {
|
||||||
|
ret = -EINVAL;
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = read_uint32(bs, info_begin, &tmp);
|
||||||
|
if (ret < 0) {
|
||||||
|
goto fail;
|
||||||
|
} else if (tmp != 0x100) {
|
||||||
|
ret = -EINVAL;
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = read_uint32(bs, info_begin + 4, &count);
|
||||||
|
if (ret < 0) {
|
||||||
|
goto fail;
|
||||||
|
} else if (count == 0) {
|
||||||
|
ret = -EINVAL;
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
info_end = info_begin + count;
|
||||||
|
|
||||||
|
offset = info_begin + 0x100;
|
||||||
|
|
||||||
|
/* read offsets */
|
||||||
|
last_in_offset = last_out_offset = 0;
|
||||||
|
while (offset < info_end) {
|
||||||
|
uint32_t type;
|
||||||
|
|
||||||
|
ret = read_uint32(bs, offset, &count);
|
||||||
|
if (ret < 0) {
|
||||||
|
goto fail;
|
||||||
|
} else if (count == 0) {
|
||||||
|
ret = -EINVAL;
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
offset += 4;
|
||||||
|
|
||||||
|
ret = read_uint32(bs, offset, &type);
|
||||||
|
if (ret < 0) {
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type == 0x6d697368 && count >= 244) {
|
||||||
size_t new_size;
|
size_t new_size;
|
||||||
uint32_t chunk_count;
|
uint32_t chunk_count;
|
||||||
int64_t offset = 0;
|
|
||||||
uint64_t data_offset;
|
|
||||||
uint64_t in_offset = ds->data_fork_offset;
|
|
||||||
uint64_t out_offset;
|
|
||||||
|
|
||||||
type = buff_read_uint32(buffer, offset);
|
offset += 4;
|
||||||
/* skip data that is not a valid MISH block (invalid magic or too small) */
|
offset += 200;
|
||||||
if (type != 0x6d697368 || count < 244) {
|
|
||||||
/* assume success for now */
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* chunk offsets are relative to this sector number */
|
|
||||||
out_offset = buff_read_uint64(buffer, offset + 8);
|
|
||||||
|
|
||||||
/* location in data fork for (compressed) blob (in bytes) */
|
|
||||||
data_offset = buff_read_uint64(buffer, offset + 0x18);
|
|
||||||
in_offset += data_offset;
|
|
||||||
|
|
||||||
/* move to begin of chunk entries */
|
|
||||||
offset += 204;
|
|
||||||
|
|
||||||
chunk_count = (count - 204) / 40;
|
chunk_count = (count - 204) / 40;
|
||||||
new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);
|
new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);
|
||||||
@@ -255,274 +207,77 @@ static int dmg_read_mish_block(BDRVDMGState *s, DmgHeaderState *ds,
|
|||||||
s->sectorcounts = g_realloc(s->sectorcounts, new_size);
|
s->sectorcounts = g_realloc(s->sectorcounts, new_size);
|
||||||
|
|
||||||
for (i = s->n_chunks; i < s->n_chunks + chunk_count; i++) {
|
for (i = s->n_chunks; i < s->n_chunks + chunk_count; i++) {
|
||||||
s->types[i] = buff_read_uint32(buffer, offset);
|
ret = read_uint32(bs, offset, &s->types[i]);
|
||||||
if (!dmg_is_known_block_type(s->types[i])) {
|
if (ret < 0) {
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
offset += 4;
|
||||||
|
if (s->types[i] != 0x80000005 && s->types[i] != 1 &&
|
||||||
|
s->types[i] != 2) {
|
||||||
|
if (s->types[i] == 0xffffffff && i > 0) {
|
||||||
|
last_in_offset = s->offsets[i - 1] + s->lengths[i - 1];
|
||||||
|
last_out_offset = s->sectors[i - 1] +
|
||||||
|
s->sectorcounts[i - 1];
|
||||||
|
}
|
||||||
chunk_count--;
|
chunk_count--;
|
||||||
i--;
|
i--;
|
||||||
offset += 40;
|
offset += 36;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
offset += 4;
|
||||||
|
|
||||||
/* sector number */
|
ret = read_uint64(bs, offset, &s->sectors[i]);
|
||||||
s->sectors[i] = buff_read_uint64(buffer, offset + 8);
|
if (ret < 0) {
|
||||||
s->sectors[i] += out_offset;
|
goto fail;
|
||||||
|
}
|
||||||
|
s->sectors[i] += last_out_offset;
|
||||||
|
offset += 8;
|
||||||
|
|
||||||
/* sector count */
|
ret = read_uint64(bs, offset, &s->sectorcounts[i]);
|
||||||
s->sectorcounts[i] = buff_read_uint64(buffer, offset + 0x10);
|
if (ret < 0) {
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
offset += 8;
|
||||||
|
|
||||||
/* all-zeroes sector (type 2) does not need to be "uncompressed" and can
|
if (s->sectorcounts[i] > DMG_SECTORCOUNTS_MAX) {
|
||||||
* therefore be unbounded. */
|
error_report("sector count %" PRIu64 " for chunk %u is "
|
||||||
if (s->types[i] != 2 && s->sectorcounts[i] > DMG_SECTORCOUNTS_MAX) {
|
"larger than max (%u)",
|
||||||
error_report("sector count %" PRIu64 " for chunk %" PRIu32
|
|
||||||
" is larger than max (%u)",
|
|
||||||
s->sectorcounts[i], i, DMG_SECTORCOUNTS_MAX);
|
s->sectorcounts[i], i, DMG_SECTORCOUNTS_MAX);
|
||||||
ret = -EINVAL;
|
ret = -EINVAL;
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* offset in (compressed) data fork */
|
ret = read_uint64(bs, offset, &s->offsets[i]);
|
||||||
s->offsets[i] = buff_read_uint64(buffer, offset + 0x18);
|
if (ret < 0) {
|
||||||
s->offsets[i] += in_offset;
|
goto fail;
|
||||||
|
}
|
||||||
|
s->offsets[i] += last_in_offset;
|
||||||
|
offset += 8;
|
||||||
|
|
||||||
/* length in (compressed) data fork */
|
ret = read_uint64(bs, offset, &s->lengths[i]);
|
||||||
s->lengths[i] = buff_read_uint64(buffer, offset + 0x20);
|
if (ret < 0) {
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
offset += 8;
|
||||||
|
|
||||||
if (s->lengths[i] > DMG_LENGTHS_MAX) {
|
if (s->lengths[i] > DMG_LENGTHS_MAX) {
|
||||||
error_report("length %" PRIu64 " for chunk %" PRIu32
|
error_report("length %" PRIu64 " for chunk %u is larger "
|
||||||
" is larger than max (%u)",
|
"than max (%u)",
|
||||||
s->lengths[i], i, DMG_LENGTHS_MAX);
|
s->lengths[i], i, DMG_LENGTHS_MAX);
|
||||||
ret = -EINVAL;
|
ret = -EINVAL;
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
update_max_chunk_size(s, i, &ds->max_compressed_size,
|
update_max_chunk_size(s, i, &max_compressed_size,
|
||||||
&ds->max_sectors_per_chunk);
|
&max_sectors_per_chunk);
|
||||||
offset += 40;
|
|
||||||
}
|
}
|
||||||
s->n_chunks += chunk_count;
|
s->n_chunks += chunk_count;
|
||||||
return 0;
|
|
||||||
|
|
||||||
fail:
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int dmg_read_resource_fork(BlockDriverState *bs, DmgHeaderState *ds,
|
|
||||||
uint64_t info_begin, uint64_t info_length)
|
|
||||||
{
|
|
||||||
BDRVDMGState *s = bs->opaque;
|
|
||||||
int ret;
|
|
||||||
uint32_t count, rsrc_data_offset;
|
|
||||||
uint8_t *buffer = NULL;
|
|
||||||
uint64_t info_end;
|
|
||||||
uint64_t offset;
|
|
||||||
|
|
||||||
/* read offset from begin of resource fork (info_begin) to resource data */
|
|
||||||
ret = read_uint32(bs, info_begin, &rsrc_data_offset);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
} else if (rsrc_data_offset > info_length) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* read length of resource data */
|
|
||||||
ret = read_uint32(bs, info_begin + 8, &count);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
} else if (count == 0 || rsrc_data_offset + count > info_length) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* begin of resource data (consisting of one or more resources) */
|
|
||||||
offset = info_begin + rsrc_data_offset;
|
|
||||||
|
|
||||||
/* end of resource data (there is possibly a following resource map
|
|
||||||
* which will be ignored). */
|
|
||||||
info_end = offset + count;
|
|
||||||
|
|
||||||
/* read offsets (mish blocks) from one or more resources in resource data */
|
|
||||||
while (offset < info_end) {
|
|
||||||
/* size of following resource */
|
|
||||||
ret = read_uint32(bs, offset, &count);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
} else if (count == 0 || count > info_end - offset) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
offset += 4;
|
|
||||||
|
|
||||||
buffer = g_realloc(buffer, count);
|
|
||||||
ret = bdrv_pread(bs->file->bs, offset, buffer, count);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = dmg_read_mish_block(s, ds, buffer, count);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
/* advance offset by size of resource */
|
|
||||||
offset += count;
|
|
||||||
}
|
|
||||||
ret = 0;
|
|
||||||
|
|
||||||
fail:
|
|
||||||
g_free(buffer);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int dmg_read_plist_xml(BlockDriverState *bs, DmgHeaderState *ds,
|
|
||||||
uint64_t info_begin, uint64_t info_length)
|
|
||||||
{
|
|
||||||
BDRVDMGState *s = bs->opaque;
|
|
||||||
int ret;
|
|
||||||
uint8_t *buffer = NULL;
|
|
||||||
char *data_begin, *data_end;
|
|
||||||
|
|
||||||
/* Have at least some length to avoid NULL for g_malloc. Attempt to set a
|
|
||||||
* safe upper cap on the data length. A test sample had a XML length of
|
|
||||||
* about 1 MiB. */
|
|
||||||
if (info_length == 0 || info_length > 16 * 1024 * 1024) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer = g_malloc(info_length + 1);
|
|
||||||
buffer[info_length] = '\0';
|
|
||||||
ret = bdrv_pread(bs->file->bs, info_begin, buffer, info_length);
|
|
||||||
if (ret != info_length) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* look for <data>...</data>. The data is 284 (0x11c) bytes after base64
|
|
||||||
* decode. The actual data element has 431 (0x1af) bytes which includes tabs
|
|
||||||
* and line feeds. */
|
|
||||||
data_end = (char *)buffer;
|
|
||||||
while ((data_begin = strstr(data_end, "<data>")) != NULL) {
|
|
||||||
guchar *mish;
|
|
||||||
gsize out_len = 0;
|
|
||||||
|
|
||||||
data_begin += 6;
|
|
||||||
data_end = strstr(data_begin, "</data>");
|
|
||||||
/* malformed XML? */
|
|
||||||
if (data_end == NULL) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
*data_end++ = '\0';
|
|
||||||
mish = g_base64_decode(data_begin, &out_len);
|
|
||||||
ret = dmg_read_mish_block(s, ds, mish, (uint32_t)out_len);
|
|
||||||
g_free(mish);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ret = 0;
|
|
||||||
|
|
||||||
fail:
|
|
||||||
g_free(buffer);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int dmg_open(BlockDriverState *bs, QDict *options, int flags,
|
|
||||||
Error **errp)
|
|
||||||
{
|
|
||||||
BDRVDMGState *s = bs->opaque;
|
|
||||||
DmgHeaderState ds;
|
|
||||||
uint64_t rsrc_fork_offset, rsrc_fork_length;
|
|
||||||
uint64_t plist_xml_offset, plist_xml_length;
|
|
||||||
int64_t offset;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
bs->read_only = 1;
|
|
||||||
s->n_chunks = 0;
|
|
||||||
s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
|
|
||||||
/* used by dmg_read_mish_block to keep track of the current I/O position */
|
|
||||||
ds.data_fork_offset = 0;
|
|
||||||
ds.max_compressed_size = 1;
|
|
||||||
ds.max_sectors_per_chunk = 1;
|
|
||||||
|
|
||||||
/* locate the UDIF trailer */
|
|
||||||
offset = dmg_find_koly_offset(bs->file->bs, errp);
|
|
||||||
if (offset < 0) {
|
|
||||||
ret = offset;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* offset of data fork (DataForkOffset) */
|
|
||||||
ret = read_uint64(bs, offset + 0x18, &ds.data_fork_offset);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
} else if (ds.data_fork_offset > offset) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* offset of resource fork (RsrcForkOffset) */
|
|
||||||
ret = read_uint64(bs, offset + 0x28, &rsrc_fork_offset);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
ret = read_uint64(bs, offset + 0x30, &rsrc_fork_length);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
if (rsrc_fork_offset >= offset ||
|
|
||||||
rsrc_fork_length > offset - rsrc_fork_offset) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
/* offset of property list (XMLOffset) */
|
|
||||||
ret = read_uint64(bs, offset + 0xd8, &plist_xml_offset);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
ret = read_uint64(bs, offset + 0xe0, &plist_xml_length);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
if (plist_xml_offset >= offset ||
|
|
||||||
plist_xml_length > offset - plist_xml_offset) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
ret = read_uint64(bs, offset + 0x1ec, (uint64_t *)&bs->total_sectors);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
if (bs->total_sectors < 0) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
if (rsrc_fork_length != 0) {
|
|
||||||
ret = dmg_read_resource_fork(bs, &ds,
|
|
||||||
rsrc_fork_offset, rsrc_fork_length);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
} else if (plist_xml_length != 0) {
|
|
||||||
ret = dmg_read_plist_xml(bs, &ds, plist_xml_offset, plist_xml_length);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* initialize zlib engine */
|
/* initialize zlib engine */
|
||||||
s->compressed_chunk = qemu_try_blockalign(bs->file->bs,
|
s->compressed_chunk = g_malloc(max_compressed_size + 1);
|
||||||
ds.max_compressed_size + 1);
|
s->uncompressed_chunk = g_malloc(512 * max_sectors_per_chunk);
|
||||||
s->uncompressed_chunk = qemu_try_blockalign(bs->file->bs,
|
|
||||||
512 * ds.max_sectors_per_chunk);
|
|
||||||
if (s->compressed_chunk == NULL || s->uncompressed_chunk == NULL) {
|
|
||||||
ret = -ENOMEM;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inflateInit(&s->zstream) != Z_OK) {
|
if (inflateInit(&s->zstream) != Z_OK) {
|
||||||
ret = -EINVAL;
|
ret = -EINVAL;
|
||||||
goto fail;
|
goto fail;
|
||||||
@@ -539,8 +294,8 @@ fail:
|
|||||||
g_free(s->lengths);
|
g_free(s->lengths);
|
||||||
g_free(s->sectors);
|
g_free(s->sectors);
|
||||||
g_free(s->sectorcounts);
|
g_free(s->sectorcounts);
|
||||||
qemu_vfree(s->compressed_chunk);
|
g_free(s->compressed_chunk);
|
||||||
qemu_vfree(s->uncompressed_chunk);
|
g_free(s->uncompressed_chunk);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -579,20 +334,17 @@ static inline int dmg_read_chunk(BlockDriverState *bs, uint64_t sector_num)
|
|||||||
if (!is_sector_in_chunk(s, s->current_chunk, sector_num)) {
|
if (!is_sector_in_chunk(s, s->current_chunk, sector_num)) {
|
||||||
int ret;
|
int ret;
|
||||||
uint32_t chunk = search_chunk(s, sector_num);
|
uint32_t chunk = search_chunk(s, sector_num);
|
||||||
#ifdef CONFIG_BZIP2
|
|
||||||
uint64_t total_out;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (chunk >= s->n_chunks) {
|
if (chunk >= s->n_chunks) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
s->current_chunk = s->n_chunks;
|
s->current_chunk = s->n_chunks;
|
||||||
switch (s->types[chunk]) { /* block entry type */
|
switch (s->types[chunk]) {
|
||||||
case 0x80000005: { /* zlib compressed */
|
case 0x80000005: { /* zlib compressed */
|
||||||
/* we need to buffer, because only the chunk as whole can be
|
/* we need to buffer, because only the chunk as whole can be
|
||||||
* inflated. */
|
* inflated. */
|
||||||
ret = bdrv_pread(bs->file->bs, s->offsets[chunk],
|
ret = bdrv_pread(bs->file, s->offsets[chunk],
|
||||||
s->compressed_chunk, s->lengths[chunk]);
|
s->compressed_chunk, s->lengths[chunk]);
|
||||||
if (ret != s->lengths[chunk]) {
|
if (ret != s->lengths[chunk]) {
|
||||||
return -1;
|
return -1;
|
||||||
@@ -612,44 +364,15 @@ static inline int dmg_read_chunk(BlockDriverState *bs, uint64_t sector_num)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
break; }
|
break; }
|
||||||
#ifdef CONFIG_BZIP2
|
|
||||||
case 0x80000006: /* bzip2 compressed */
|
|
||||||
/* we need to buffer, because only the chunk as whole can be
|
|
||||||
* inflated. */
|
|
||||||
ret = bdrv_pread(bs->file->bs, s->offsets[chunk],
|
|
||||||
s->compressed_chunk, s->lengths[chunk]);
|
|
||||||
if (ret != s->lengths[chunk]) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = BZ2_bzDecompressInit(&s->bzstream, 0, 0);
|
|
||||||
if (ret != BZ_OK) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
s->bzstream.next_in = (char *)s->compressed_chunk;
|
|
||||||
s->bzstream.avail_in = (unsigned int) s->lengths[chunk];
|
|
||||||
s->bzstream.next_out = (char *)s->uncompressed_chunk;
|
|
||||||
s->bzstream.avail_out = (unsigned int) 512 * s->sectorcounts[chunk];
|
|
||||||
ret = BZ2_bzDecompress(&s->bzstream);
|
|
||||||
total_out = ((uint64_t)s->bzstream.total_out_hi32 << 32) +
|
|
||||||
s->bzstream.total_out_lo32;
|
|
||||||
BZ2_bzDecompressEnd(&s->bzstream);
|
|
||||||
if (ret != BZ_STREAM_END ||
|
|
||||||
total_out != 512 * s->sectorcounts[chunk]) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
#endif /* CONFIG_BZIP2 */
|
|
||||||
case 1: /* copy */
|
case 1: /* copy */
|
||||||
ret = bdrv_pread(bs->file->bs, s->offsets[chunk],
|
ret = bdrv_pread(bs->file, s->offsets[chunk],
|
||||||
s->uncompressed_chunk, s->lengths[chunk]);
|
s->uncompressed_chunk, s->lengths[chunk]);
|
||||||
if (ret != s->lengths[chunk]) {
|
if (ret != s->lengths[chunk]) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 2: /* zero */
|
case 2: /* zero */
|
||||||
/* see dmg_read, it is treated specially. No buffer needs to be
|
memset(s->uncompressed_chunk, 0, 512 * s->sectorcounts[chunk]);
|
||||||
* pre-filled, the zeroes can be set directly. */
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
s->current_chunk = chunk;
|
s->current_chunk = chunk;
|
||||||
@@ -668,13 +391,6 @@ static int dmg_read(BlockDriverState *bs, int64_t sector_num,
|
|||||||
if (dmg_read_chunk(bs, sector_num + i) != 0) {
|
if (dmg_read_chunk(bs, sector_num + i) != 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
/* Special case: current chunk is all zeroes. Do not perform a memcpy as
|
|
||||||
* s->uncompressed_chunk may be too small to cover the large all-zeroes
|
|
||||||
* section. dmg_read_chunk is called to find s->current_chunk */
|
|
||||||
if (s->types[s->current_chunk] == 2) { /* all zeroes block entry */
|
|
||||||
memset(buf + i * 512, 0, 512);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
sector_offset_in_chunk = sector_num + i - s->sectors[s->current_chunk];
|
sector_offset_in_chunk = sector_num + i - s->sectors[s->current_chunk];
|
||||||
memcpy(buf + i * 512,
|
memcpy(buf + i * 512,
|
||||||
s->uncompressed_chunk + sector_offset_in_chunk * 512, 512);
|
s->uncompressed_chunk + sector_offset_in_chunk * 512, 512);
|
||||||
@@ -702,8 +418,8 @@ static void dmg_close(BlockDriverState *bs)
|
|||||||
g_free(s->lengths);
|
g_free(s->lengths);
|
||||||
g_free(s->sectors);
|
g_free(s->sectors);
|
||||||
g_free(s->sectorcounts);
|
g_free(s->sectorcounts);
|
||||||
qemu_vfree(s->compressed_chunk);
|
g_free(s->compressed_chunk);
|
||||||
qemu_vfree(s->uncompressed_chunk);
|
g_free(s->uncompressed_chunk);
|
||||||
|
|
||||||
inflateEnd(&s->zstream);
|
inflateEnd(&s->zstream);
|
||||||
}
|
}
|
||||||
|
|||||||
631
block/gluster.c
631
block/gluster.c
@@ -3,27 +3,43 @@
|
|||||||
*
|
*
|
||||||
* Copyright (C) 2012 Bharata B Rao <bharata@linux.vnet.ibm.com>
|
* Copyright (C) 2012 Bharata B Rao <bharata@linux.vnet.ibm.com>
|
||||||
*
|
*
|
||||||
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
* Pipe handling mechanism in AIO implementation is derived from
|
||||||
* See the COPYING file in the top-level directory.
|
* block/rbd.c. Hence,
|
||||||
*
|
*
|
||||||
|
* Copyright (C) 2010-2011 Christian Brunner <chb@muc.de>,
|
||||||
|
* Josh Durgin <josh.durgin@dreamhost.com>
|
||||||
|
*
|
||||||
|
* This work is licensed under the terms of the GNU GPL, version 2. See
|
||||||
|
* the COPYING file in the top-level directory.
|
||||||
|
*
|
||||||
|
* Contributions after 2012-01-13 are licensed under the terms of the
|
||||||
|
* GNU GPL, version 2 or (at your option) any later version.
|
||||||
*/
|
*/
|
||||||
#include <glusterfs/api/glfs.h>
|
#include <glusterfs/api/glfs.h>
|
||||||
#include "block/block_int.h"
|
#include "block/block_int.h"
|
||||||
|
#include "qemu/sockets.h"
|
||||||
#include "qemu/uri.h"
|
#include "qemu/uri.h"
|
||||||
|
|
||||||
typedef struct GlusterAIOCB {
|
typedef struct GlusterAIOCB {
|
||||||
|
BlockDriverAIOCB common;
|
||||||
int64_t size;
|
int64_t size;
|
||||||
int ret;
|
int ret;
|
||||||
|
bool *finished;
|
||||||
QEMUBH *bh;
|
QEMUBH *bh;
|
||||||
Coroutine *coroutine;
|
|
||||||
AioContext *aio_context;
|
|
||||||
} GlusterAIOCB;
|
} GlusterAIOCB;
|
||||||
|
|
||||||
typedef struct BDRVGlusterState {
|
typedef struct BDRVGlusterState {
|
||||||
struct glfs *glfs;
|
struct glfs *glfs;
|
||||||
|
int fds[2];
|
||||||
struct glfs_fd *fd;
|
struct glfs_fd *fd;
|
||||||
|
int qemu_aio_count;
|
||||||
|
int event_reader_pos;
|
||||||
|
GlusterAIOCB *event_acb;
|
||||||
} BDRVGlusterState;
|
} BDRVGlusterState;
|
||||||
|
|
||||||
|
#define GLUSTER_FD_READ 0
|
||||||
|
#define GLUSTER_FD_WRITE 1
|
||||||
|
|
||||||
typedef struct GlusterConf {
|
typedef struct GlusterConf {
|
||||||
char *server;
|
char *server;
|
||||||
int port;
|
int port;
|
||||||
@@ -34,14 +50,12 @@ typedef struct GlusterConf {
|
|||||||
|
|
||||||
static void qemu_gluster_gconf_free(GlusterConf *gconf)
|
static void qemu_gluster_gconf_free(GlusterConf *gconf)
|
||||||
{
|
{
|
||||||
if (gconf) {
|
|
||||||
g_free(gconf->server);
|
g_free(gconf->server);
|
||||||
g_free(gconf->volname);
|
g_free(gconf->volname);
|
||||||
g_free(gconf->image);
|
g_free(gconf->image);
|
||||||
g_free(gconf->transport);
|
g_free(gconf->transport);
|
||||||
g_free(gconf);
|
g_free(gconf);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
static int parse_volume_options(GlusterConf *gconf, char *path)
|
static int parse_volume_options(GlusterConf *gconf, char *path)
|
||||||
{
|
{
|
||||||
@@ -81,7 +95,7 @@ static int parse_volume_options(GlusterConf *gconf, char *path)
|
|||||||
* 'server' specifies the server where the volume file specification for
|
* 'server' specifies the server where the volume file specification for
|
||||||
* the given volume resides. This can be either hostname, ipv4 address
|
* the given volume resides. This can be either hostname, ipv4 address
|
||||||
* or ipv6 address. ipv6 address needs to be within square brackets [ ].
|
* or ipv6 address. ipv6 address needs to be within square brackets [ ].
|
||||||
* If transport type is 'unix', then 'server' field should not be specified.
|
* If transport type is 'unix', then 'server' field should not be specifed.
|
||||||
* The 'socket' field needs to be populated with the path to unix domain
|
* The 'socket' field needs to be populated with the path to unix domain
|
||||||
* socket.
|
* socket.
|
||||||
*
|
*
|
||||||
@@ -118,7 +132,7 @@ static int qemu_gluster_parseuri(GlusterConf *gconf, const char *filename)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* transport */
|
/* transport */
|
||||||
if (!uri->scheme || !strcmp(uri->scheme, "gluster")) {
|
if (!strcmp(uri->scheme, "gluster")) {
|
||||||
gconf->transport = g_strdup("tcp");
|
gconf->transport = g_strdup("tcp");
|
||||||
} else if (!strcmp(uri->scheme, "gluster+tcp")) {
|
} else if (!strcmp(uri->scheme, "gluster+tcp")) {
|
||||||
gconf->transport = g_strdup("tcp");
|
gconf->transport = g_strdup("tcp");
|
||||||
@@ -154,7 +168,7 @@ static int qemu_gluster_parseuri(GlusterConf *gconf, const char *filename)
|
|||||||
}
|
}
|
||||||
gconf->server = g_strdup(qp->p[0].value);
|
gconf->server = g_strdup(qp->p[0].value);
|
||||||
} else {
|
} else {
|
||||||
gconf->server = g_strdup(uri->server ? uri->server : "localhost");
|
gconf->server = g_strdup(uri->server);
|
||||||
gconf->port = uri->port;
|
gconf->port = uri->port;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,8 +180,7 @@ out:
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct glfs *qemu_gluster_init(GlusterConf *gconf, const char *filename,
|
static struct glfs *qemu_gluster_init(GlusterConf *gconf, const char *filename)
|
||||||
Error **errp)
|
|
||||||
{
|
{
|
||||||
struct glfs *glfs = NULL;
|
struct glfs *glfs = NULL;
|
||||||
int ret;
|
int ret;
|
||||||
@@ -175,7 +188,7 @@ static struct glfs *qemu_gluster_init(GlusterConf *gconf, const char *filename,
|
|||||||
|
|
||||||
ret = qemu_gluster_parseuri(gconf, filename);
|
ret = qemu_gluster_parseuri(gconf, filename);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_setg(errp, "Usage: file=gluster[+transport]://[server[:port]]/"
|
error_report("Usage: file=gluster[+transport]://[server[:port]]/"
|
||||||
"volname/image[?socket=...]");
|
"volname/image[?socket=...]");
|
||||||
errno = -ret;
|
errno = -ret;
|
||||||
goto out;
|
goto out;
|
||||||
@@ -203,16 +216,9 @@ static struct glfs *qemu_gluster_init(GlusterConf *gconf, const char *filename,
|
|||||||
|
|
||||||
ret = glfs_init(glfs);
|
ret = glfs_init(glfs);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
error_setg_errno(errp, errno,
|
error_report("Gluster connection failed for server=%s port=%d "
|
||||||
"Gluster connection failed for server=%s port=%d "
|
"volume=%s image=%s transport=%s", gconf->server, gconf->port,
|
||||||
"volume=%s image=%s transport=%s", gconf->server,
|
gconf->volname, gconf->image, gconf->transport);
|
||||||
gconf->port, gconf->volname, gconf->image,
|
|
||||||
gconf->transport);
|
|
||||||
|
|
||||||
/* glfs_init sometimes doesn't set errno although docs suggest that */
|
|
||||||
if (errno == 0)
|
|
||||||
errno = EINVAL;
|
|
||||||
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
return glfs;
|
return glfs;
|
||||||
@@ -226,101 +232,96 @@ out:
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void qemu_gluster_complete_aio(void *opaque)
|
static void qemu_gluster_complete_aio(GlusterAIOCB *acb, BDRVGlusterState *s)
|
||||||
{
|
{
|
||||||
GlusterAIOCB *acb = (GlusterAIOCB *)opaque;
|
int ret;
|
||||||
|
bool *finished = acb->finished;
|
||||||
|
BlockDriverCompletionFunc *cb = acb->common.cb;
|
||||||
|
void *opaque = acb->common.opaque;
|
||||||
|
|
||||||
qemu_bh_delete(acb->bh);
|
if (!acb->ret || acb->ret == acb->size) {
|
||||||
acb->bh = NULL;
|
ret = 0; /* Success */
|
||||||
qemu_coroutine_enter(acb->coroutine, NULL);
|
} else if (acb->ret < 0) {
|
||||||
}
|
ret = acb->ret; /* Read/Write failed */
|
||||||
|
|
||||||
/*
|
|
||||||
* AIO callback routine called from GlusterFS thread.
|
|
||||||
*/
|
|
||||||
static void gluster_finish_aiocb(struct glfs_fd *fd, ssize_t ret, void *arg)
|
|
||||||
{
|
|
||||||
GlusterAIOCB *acb = (GlusterAIOCB *)arg;
|
|
||||||
|
|
||||||
if (!ret || ret == acb->size) {
|
|
||||||
acb->ret = 0; /* Success */
|
|
||||||
} else if (ret < 0) {
|
|
||||||
acb->ret = ret; /* Read/Write failed */
|
|
||||||
} else {
|
} else {
|
||||||
acb->ret = -EIO; /* Partial read/write - fail it */
|
ret = -EIO; /* Partial read/write - fail it */
|
||||||
}
|
}
|
||||||
|
|
||||||
acb->bh = aio_bh_new(acb->aio_context, qemu_gluster_complete_aio, acb);
|
s->qemu_aio_count--;
|
||||||
qemu_bh_schedule(acb->bh);
|
qemu_aio_release(acb);
|
||||||
|
cb(opaque, ret);
|
||||||
|
if (finished) {
|
||||||
|
*finished = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO Convert to fine grained options */
|
static void qemu_gluster_aio_event_reader(void *opaque)
|
||||||
static QemuOptsList runtime_opts = {
|
|
||||||
.name = "gluster",
|
|
||||||
.head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
|
|
||||||
.desc = {
|
|
||||||
{
|
{
|
||||||
.name = "filename",
|
BDRVGlusterState *s = opaque;
|
||||||
.type = QEMU_OPT_STRING,
|
ssize_t ret;
|
||||||
.help = "URL to the gluster image",
|
|
||||||
},
|
|
||||||
{ /* end of list */ }
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
static void qemu_gluster_parse_flags(int bdrv_flags, int *open_flags)
|
do {
|
||||||
|
char *p = (char *)&s->event_acb;
|
||||||
|
|
||||||
|
ret = read(s->fds[GLUSTER_FD_READ], p + s->event_reader_pos,
|
||||||
|
sizeof(s->event_acb) - s->event_reader_pos);
|
||||||
|
if (ret > 0) {
|
||||||
|
s->event_reader_pos += ret;
|
||||||
|
if (s->event_reader_pos == sizeof(s->event_acb)) {
|
||||||
|
s->event_reader_pos = 0;
|
||||||
|
qemu_gluster_complete_aio(s->event_acb, s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (ret < 0 && errno == EINTR);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int qemu_gluster_aio_flush_cb(void *opaque)
|
||||||
{
|
{
|
||||||
assert(open_flags != NULL);
|
BDRVGlusterState *s = opaque;
|
||||||
|
|
||||||
*open_flags |= O_BINARY;
|
return (s->qemu_aio_count > 0);
|
||||||
|
|
||||||
if (bdrv_flags & BDRV_O_RDWR) {
|
|
||||||
*open_flags |= O_RDWR;
|
|
||||||
} else {
|
|
||||||
*open_flags |= O_RDONLY;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((bdrv_flags & BDRV_O_NOCACHE)) {
|
static int qemu_gluster_open(BlockDriverState *bs, const char *filename,
|
||||||
*open_flags |= O_DIRECT;
|
int bdrv_flags)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static int qemu_gluster_open(BlockDriverState *bs, QDict *options,
|
|
||||||
int bdrv_flags, Error **errp)
|
|
||||||
{
|
{
|
||||||
BDRVGlusterState *s = bs->opaque;
|
BDRVGlusterState *s = bs->opaque;
|
||||||
int open_flags = 0;
|
int open_flags = O_BINARY;
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
GlusterConf *gconf = g_new0(GlusterConf, 1);
|
GlusterConf *gconf = g_malloc0(sizeof(GlusterConf));
|
||||||
QemuOpts *opts;
|
|
||||||
Error *local_err = NULL;
|
|
||||||
const char *filename;
|
|
||||||
|
|
||||||
opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
|
s->glfs = qemu_gluster_init(gconf, filename);
|
||||||
qemu_opts_absorb_qdict(opts, options, &local_err);
|
|
||||||
if (local_err) {
|
|
||||||
error_propagate(errp, local_err);
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
filename = qemu_opt_get(opts, "filename");
|
|
||||||
|
|
||||||
s->glfs = qemu_gluster_init(gconf, filename, errp);
|
|
||||||
if (!s->glfs) {
|
if (!s->glfs) {
|
||||||
ret = -errno;
|
ret = -errno;
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
qemu_gluster_parse_flags(bdrv_flags, &open_flags);
|
if (bdrv_flags & BDRV_O_RDWR) {
|
||||||
|
open_flags |= O_RDWR;
|
||||||
|
} else {
|
||||||
|
open_flags |= O_RDONLY;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((bdrv_flags & BDRV_O_NOCACHE)) {
|
||||||
|
open_flags |= O_DIRECT;
|
||||||
|
}
|
||||||
|
|
||||||
s->fd = glfs_open(s->glfs, gconf->image, open_flags);
|
s->fd = glfs_open(s->glfs, gconf->image, open_flags);
|
||||||
if (!s->fd) {
|
if (!s->fd) {
|
||||||
ret = -errno;
|
ret = -errno;
|
||||||
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ret = qemu_pipe(s->fds);
|
||||||
|
if (ret < 0) {
|
||||||
|
ret = -errno;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
fcntl(s->fds[GLUSTER_FD_READ], F_SETFL, O_NONBLOCK);
|
||||||
|
qemu_aio_set_fd_handler(s->fds[GLUSTER_FD_READ],
|
||||||
|
qemu_gluster_aio_event_reader, NULL, qemu_gluster_aio_flush_cb, s);
|
||||||
|
|
||||||
out:
|
out:
|
||||||
qemu_opts_del(opts);
|
|
||||||
qemu_gluster_gconf_free(gconf);
|
qemu_gluster_gconf_free(gconf);
|
||||||
if (!ret) {
|
if (!ret) {
|
||||||
return ret;
|
return ret;
|
||||||
@@ -334,176 +335,26 @@ out:
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef struct BDRVGlusterReopenState {
|
|
||||||
struct glfs *glfs;
|
|
||||||
struct glfs_fd *fd;
|
|
||||||
} BDRVGlusterReopenState;
|
|
||||||
|
|
||||||
|
|
||||||
static int qemu_gluster_reopen_prepare(BDRVReopenState *state,
|
|
||||||
BlockReopenQueue *queue, Error **errp)
|
|
||||||
{
|
|
||||||
int ret = 0;
|
|
||||||
BDRVGlusterReopenState *reop_s;
|
|
||||||
GlusterConf *gconf = NULL;
|
|
||||||
int open_flags = 0;
|
|
||||||
|
|
||||||
assert(state != NULL);
|
|
||||||
assert(state->bs != NULL);
|
|
||||||
|
|
||||||
state->opaque = g_new0(BDRVGlusterReopenState, 1);
|
|
||||||
reop_s = state->opaque;
|
|
||||||
|
|
||||||
qemu_gluster_parse_flags(state->flags, &open_flags);
|
|
||||||
|
|
||||||
gconf = g_new0(GlusterConf, 1);
|
|
||||||
|
|
||||||
reop_s->glfs = qemu_gluster_init(gconf, state->bs->filename, errp);
|
|
||||||
if (reop_s->glfs == NULL) {
|
|
||||||
ret = -errno;
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
reop_s->fd = glfs_open(reop_s->glfs, gconf->image, open_flags);
|
|
||||||
if (reop_s->fd == NULL) {
|
|
||||||
/* reops->glfs will be cleaned up in _abort */
|
|
||||||
ret = -errno;
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
exit:
|
|
||||||
/* state->opaque will be freed in either the _abort or _commit */
|
|
||||||
qemu_gluster_gconf_free(gconf);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void qemu_gluster_reopen_commit(BDRVReopenState *state)
|
|
||||||
{
|
|
||||||
BDRVGlusterReopenState *reop_s = state->opaque;
|
|
||||||
BDRVGlusterState *s = state->bs->opaque;
|
|
||||||
|
|
||||||
|
|
||||||
/* close the old */
|
|
||||||
if (s->fd) {
|
|
||||||
glfs_close(s->fd);
|
|
||||||
}
|
|
||||||
if (s->glfs) {
|
|
||||||
glfs_fini(s->glfs);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* use the newly opened image / connection */
|
|
||||||
s->fd = reop_s->fd;
|
|
||||||
s->glfs = reop_s->glfs;
|
|
||||||
|
|
||||||
g_free(state->opaque);
|
|
||||||
state->opaque = NULL;
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void qemu_gluster_reopen_abort(BDRVReopenState *state)
|
|
||||||
{
|
|
||||||
BDRVGlusterReopenState *reop_s = state->opaque;
|
|
||||||
|
|
||||||
if (reop_s == NULL) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (reop_s->fd) {
|
|
||||||
glfs_close(reop_s->fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (reop_s->glfs) {
|
|
||||||
glfs_fini(reop_s->glfs);
|
|
||||||
}
|
|
||||||
|
|
||||||
g_free(state->opaque);
|
|
||||||
state->opaque = NULL;
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef CONFIG_GLUSTERFS_ZEROFILL
|
|
||||||
static coroutine_fn int qemu_gluster_co_write_zeroes(BlockDriverState *bs,
|
|
||||||
int64_t sector_num, int nb_sectors, BdrvRequestFlags flags)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
GlusterAIOCB acb;
|
|
||||||
BDRVGlusterState *s = bs->opaque;
|
|
||||||
off_t size = nb_sectors * BDRV_SECTOR_SIZE;
|
|
||||||
off_t offset = sector_num * BDRV_SECTOR_SIZE;
|
|
||||||
|
|
||||||
acb.size = size;
|
|
||||||
acb.ret = 0;
|
|
||||||
acb.coroutine = qemu_coroutine_self();
|
|
||||||
acb.aio_context = bdrv_get_aio_context(bs);
|
|
||||||
|
|
||||||
ret = glfs_zerofill_async(s->fd, offset, size, gluster_finish_aiocb, &acb);
|
|
||||||
if (ret < 0) {
|
|
||||||
return -errno;
|
|
||||||
}
|
|
||||||
|
|
||||||
qemu_coroutine_yield();
|
|
||||||
return acb.ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline bool gluster_supports_zerofill(void)
|
|
||||||
{
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int qemu_gluster_zerofill(struct glfs_fd *fd, int64_t offset,
|
|
||||||
int64_t size)
|
|
||||||
{
|
|
||||||
return glfs_zerofill(fd, offset, size);
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
|
||||||
static inline bool gluster_supports_zerofill(void)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int qemu_gluster_zerofill(struct glfs_fd *fd, int64_t offset,
|
|
||||||
int64_t size)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static int qemu_gluster_create(const char *filename,
|
static int qemu_gluster_create(const char *filename,
|
||||||
QemuOpts *opts, Error **errp)
|
QEMUOptionParameter *options)
|
||||||
{
|
{
|
||||||
struct glfs *glfs;
|
struct glfs *glfs;
|
||||||
struct glfs_fd *fd;
|
struct glfs_fd *fd;
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
int prealloc = 0;
|
|
||||||
int64_t total_size = 0;
|
int64_t total_size = 0;
|
||||||
char *tmp = NULL;
|
GlusterConf *gconf = g_malloc0(sizeof(GlusterConf));
|
||||||
GlusterConf *gconf = g_new0(GlusterConf, 1);
|
|
||||||
|
|
||||||
glfs = qemu_gluster_init(gconf, filename, errp);
|
glfs = qemu_gluster_init(gconf, filename);
|
||||||
if (!glfs) {
|
if (!glfs) {
|
||||||
ret = -errno;
|
ret = -errno;
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
|
while (options && options->name) {
|
||||||
BDRV_SECTOR_SIZE);
|
if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
|
||||||
|
total_size = options->value.n / BDRV_SECTOR_SIZE;
|
||||||
tmp = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
|
}
|
||||||
if (!tmp || !strcmp(tmp, "off")) {
|
options++;
|
||||||
prealloc = 0;
|
|
||||||
} else if (!strcmp(tmp, "full") &&
|
|
||||||
gluster_supports_zerofill()) {
|
|
||||||
prealloc = 1;
|
|
||||||
} else {
|
|
||||||
error_setg(errp, "Invalid preallocation mode: '%s'"
|
|
||||||
" or GlusterFS doesn't support zerofill API",
|
|
||||||
tmp);
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto out;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fd = glfs_creat(glfs, gconf->image,
|
fd = glfs_creat(glfs, gconf->image,
|
||||||
@@ -511,20 +362,14 @@ static int qemu_gluster_create(const char *filename,
|
|||||||
if (!fd) {
|
if (!fd) {
|
||||||
ret = -errno;
|
ret = -errno;
|
||||||
} else {
|
} else {
|
||||||
if (!glfs_ftruncate(fd, total_size)) {
|
if (glfs_ftruncate(fd, total_size * BDRV_SECTOR_SIZE) != 0) {
|
||||||
if (prealloc && qemu_gluster_zerofill(fd, 0, total_size)) {
|
|
||||||
ret = -errno;
|
ret = -errno;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
ret = -errno;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (glfs_close(fd) != 0) {
|
if (glfs_close(fd) != 0) {
|
||||||
ret = -errno;
|
ret = -errno;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out:
|
out:
|
||||||
g_free(tmp);
|
|
||||||
qemu_gluster_gconf_free(gconf);
|
qemu_gluster_gconf_free(gconf);
|
||||||
if (glfs) {
|
if (glfs) {
|
||||||
glfs_fini(glfs);
|
glfs_fini(glfs);
|
||||||
@@ -532,106 +377,131 @@ out:
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static coroutine_fn int qemu_gluster_co_rw(BlockDriverState *bs,
|
static void qemu_gluster_aio_cancel(BlockDriverAIOCB *blockacb)
|
||||||
int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, int write)
|
{
|
||||||
|
GlusterAIOCB *acb = (GlusterAIOCB *)blockacb;
|
||||||
|
bool finished = false;
|
||||||
|
|
||||||
|
acb->finished = &finished;
|
||||||
|
while (!finished) {
|
||||||
|
qemu_aio_wait();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static const AIOCBInfo gluster_aiocb_info = {
|
||||||
|
.aiocb_size = sizeof(GlusterAIOCB),
|
||||||
|
.cancel = qemu_gluster_aio_cancel,
|
||||||
|
};
|
||||||
|
|
||||||
|
static void gluster_finish_aiocb(struct glfs_fd *fd, ssize_t ret, void *arg)
|
||||||
|
{
|
||||||
|
GlusterAIOCB *acb = (GlusterAIOCB *)arg;
|
||||||
|
BlockDriverState *bs = acb->common.bs;
|
||||||
|
BDRVGlusterState *s = bs->opaque;
|
||||||
|
int retval;
|
||||||
|
|
||||||
|
acb->ret = ret;
|
||||||
|
retval = qemu_write_full(s->fds[GLUSTER_FD_WRITE], &acb, sizeof(acb));
|
||||||
|
if (retval != sizeof(acb)) {
|
||||||
|
/*
|
||||||
|
* Gluster AIO callback thread failed to notify the waiting
|
||||||
|
* QEMU thread about IO completion.
|
||||||
|
*
|
||||||
|
* Complete this IO request and make the disk inaccessible for
|
||||||
|
* subsequent reads and writes.
|
||||||
|
*/
|
||||||
|
error_report("Gluster failed to notify QEMU about IO completion");
|
||||||
|
|
||||||
|
qemu_mutex_lock_iothread(); /* We are in gluster thread context */
|
||||||
|
acb->common.cb(acb->common.opaque, -EIO);
|
||||||
|
qemu_aio_release(acb);
|
||||||
|
s->qemu_aio_count--;
|
||||||
|
close(s->fds[GLUSTER_FD_READ]);
|
||||||
|
close(s->fds[GLUSTER_FD_WRITE]);
|
||||||
|
qemu_aio_set_fd_handler(s->fds[GLUSTER_FD_READ], NULL, NULL, NULL,
|
||||||
|
NULL);
|
||||||
|
bs->drv = NULL; /* Make the disk inaccessible */
|
||||||
|
qemu_mutex_unlock_iothread();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static BlockDriverAIOCB *qemu_gluster_aio_rw(BlockDriverState *bs,
|
||||||
|
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
||||||
|
BlockDriverCompletionFunc *cb, void *opaque, int write)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
GlusterAIOCB acb;
|
GlusterAIOCB *acb;
|
||||||
BDRVGlusterState *s = bs->opaque;
|
BDRVGlusterState *s = bs->opaque;
|
||||||
size_t size = nb_sectors * BDRV_SECTOR_SIZE;
|
size_t size;
|
||||||
off_t offset = sector_num * BDRV_SECTOR_SIZE;
|
off_t offset;
|
||||||
|
|
||||||
acb.size = size;
|
offset = sector_num * BDRV_SECTOR_SIZE;
|
||||||
acb.ret = 0;
|
size = nb_sectors * BDRV_SECTOR_SIZE;
|
||||||
acb.coroutine = qemu_coroutine_self();
|
s->qemu_aio_count++;
|
||||||
acb.aio_context = bdrv_get_aio_context(bs);
|
|
||||||
|
acb = qemu_aio_get(&gluster_aiocb_info, bs, cb, opaque);
|
||||||
|
acb->size = size;
|
||||||
|
acb->ret = 0;
|
||||||
|
acb->finished = NULL;
|
||||||
|
|
||||||
if (write) {
|
if (write) {
|
||||||
ret = glfs_pwritev_async(s->fd, qiov->iov, qiov->niov, offset, 0,
|
ret = glfs_pwritev_async(s->fd, qiov->iov, qiov->niov, offset, 0,
|
||||||
gluster_finish_aiocb, &acb);
|
&gluster_finish_aiocb, acb);
|
||||||
} else {
|
} else {
|
||||||
ret = glfs_preadv_async(s->fd, qiov->iov, qiov->niov, offset, 0,
|
ret = glfs_preadv_async(s->fd, qiov->iov, qiov->niov, offset, 0,
|
||||||
gluster_finish_aiocb, &acb);
|
&gluster_finish_aiocb, acb);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return -errno;
|
goto out;
|
||||||
|
}
|
||||||
|
return &acb->common;
|
||||||
|
|
||||||
|
out:
|
||||||
|
s->qemu_aio_count--;
|
||||||
|
qemu_aio_release(acb);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
qemu_coroutine_yield();
|
static BlockDriverAIOCB *qemu_gluster_aio_readv(BlockDriverState *bs,
|
||||||
return acb.ret;
|
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
||||||
|
BlockDriverCompletionFunc *cb, void *opaque)
|
||||||
|
{
|
||||||
|
return qemu_gluster_aio_rw(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int qemu_gluster_truncate(BlockDriverState *bs, int64_t offset)
|
static BlockDriverAIOCB *qemu_gluster_aio_writev(BlockDriverState *bs,
|
||||||
|
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
||||||
|
BlockDriverCompletionFunc *cb, void *opaque)
|
||||||
|
{
|
||||||
|
return qemu_gluster_aio_rw(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static BlockDriverAIOCB *qemu_gluster_aio_flush(BlockDriverState *bs,
|
||||||
|
BlockDriverCompletionFunc *cb, void *opaque)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
|
GlusterAIOCB *acb;
|
||||||
BDRVGlusterState *s = bs->opaque;
|
BDRVGlusterState *s = bs->opaque;
|
||||||
|
|
||||||
ret = glfs_ftruncate(s->fd, offset);
|
acb = qemu_aio_get(&gluster_aiocb_info, bs, cb, opaque);
|
||||||
|
acb->size = 0;
|
||||||
|
acb->ret = 0;
|
||||||
|
acb->finished = NULL;
|
||||||
|
s->qemu_aio_count++;
|
||||||
|
|
||||||
|
ret = glfs_fsync_async(s->fd, &gluster_finish_aiocb, acb);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return -errno;
|
goto out;
|
||||||
}
|
}
|
||||||
|
return &acb->common;
|
||||||
|
|
||||||
return 0;
|
out:
|
||||||
|
s->qemu_aio_count--;
|
||||||
|
qemu_aio_release(acb);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static coroutine_fn int qemu_gluster_co_readv(BlockDriverState *bs,
|
|
||||||
int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
|
|
||||||
{
|
|
||||||
return qemu_gluster_co_rw(bs, sector_num, nb_sectors, qiov, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
static coroutine_fn int qemu_gluster_co_writev(BlockDriverState *bs,
|
|
||||||
int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
|
|
||||||
{
|
|
||||||
return qemu_gluster_co_rw(bs, sector_num, nb_sectors, qiov, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
static coroutine_fn int qemu_gluster_co_flush_to_disk(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
GlusterAIOCB acb;
|
|
||||||
BDRVGlusterState *s = bs->opaque;
|
|
||||||
|
|
||||||
acb.size = 0;
|
|
||||||
acb.ret = 0;
|
|
||||||
acb.coroutine = qemu_coroutine_self();
|
|
||||||
acb.aio_context = bdrv_get_aio_context(bs);
|
|
||||||
|
|
||||||
ret = glfs_fsync_async(s->fd, gluster_finish_aiocb, &acb);
|
|
||||||
if (ret < 0) {
|
|
||||||
return -errno;
|
|
||||||
}
|
|
||||||
|
|
||||||
qemu_coroutine_yield();
|
|
||||||
return acb.ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef CONFIG_GLUSTERFS_DISCARD
|
|
||||||
static coroutine_fn int qemu_gluster_co_discard(BlockDriverState *bs,
|
|
||||||
int64_t sector_num, int nb_sectors)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
GlusterAIOCB acb;
|
|
||||||
BDRVGlusterState *s = bs->opaque;
|
|
||||||
size_t size = nb_sectors * BDRV_SECTOR_SIZE;
|
|
||||||
off_t offset = sector_num * BDRV_SECTOR_SIZE;
|
|
||||||
|
|
||||||
acb.size = 0;
|
|
||||||
acb.ret = 0;
|
|
||||||
acb.coroutine = qemu_coroutine_self();
|
|
||||||
acb.aio_context = bdrv_get_aio_context(bs);
|
|
||||||
|
|
||||||
ret = glfs_discard_async(s->fd, offset, size, gluster_finish_aiocb, &acb);
|
|
||||||
if (ret < 0) {
|
|
||||||
return -errno;
|
|
||||||
}
|
|
||||||
|
|
||||||
qemu_coroutine_yield();
|
|
||||||
return acb.ret;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static int64_t qemu_gluster_getlength(BlockDriverState *bs)
|
static int64_t qemu_gluster_getlength(BlockDriverState *bs)
|
||||||
{
|
{
|
||||||
BDRVGlusterState *s = bs->opaque;
|
BDRVGlusterState *s = bs->opaque;
|
||||||
@@ -663,6 +533,10 @@ static void qemu_gluster_close(BlockDriverState *bs)
|
|||||||
{
|
{
|
||||||
BDRVGlusterState *s = bs->opaque;
|
BDRVGlusterState *s = bs->opaque;
|
||||||
|
|
||||||
|
close(s->fds[GLUSTER_FD_READ]);
|
||||||
|
close(s->fds[GLUSTER_FD_WRITE]);
|
||||||
|
qemu_aio_set_fd_handler(s->fds[GLUSTER_FD_READ], NULL, NULL, NULL, NULL);
|
||||||
|
|
||||||
if (s->fd) {
|
if (s->fd) {
|
||||||
glfs_close(s->fd);
|
glfs_close(s->fd);
|
||||||
s->fd = NULL;
|
s->fd = NULL;
|
||||||
@@ -670,136 +544,73 @@ static void qemu_gluster_close(BlockDriverState *bs)
|
|||||||
glfs_fini(s->glfs);
|
glfs_fini(s->glfs);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int qemu_gluster_has_zero_init(BlockDriverState *bs)
|
static QEMUOptionParameter qemu_gluster_create_options[] = {
|
||||||
{
|
|
||||||
/* GlusterFS volume could be backed by a block device */
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static QemuOptsList qemu_gluster_create_opts = {
|
|
||||||
.name = "qemu-gluster-create-opts",
|
|
||||||
.head = QTAILQ_HEAD_INITIALIZER(qemu_gluster_create_opts.head),
|
|
||||||
.desc = {
|
|
||||||
{
|
{
|
||||||
.name = BLOCK_OPT_SIZE,
|
.name = BLOCK_OPT_SIZE,
|
||||||
.type = QEMU_OPT_SIZE,
|
.type = OPT_SIZE,
|
||||||
.help = "Virtual disk size"
|
.help = "Virtual disk size"
|
||||||
},
|
},
|
||||||
{
|
{ NULL }
|
||||||
.name = BLOCK_OPT_PREALLOC,
|
|
||||||
.type = QEMU_OPT_STRING,
|
|
||||||
.help = "Preallocation mode (allowed values: off, full)"
|
|
||||||
},
|
|
||||||
{ /* end of list */ }
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static BlockDriver bdrv_gluster = {
|
static BlockDriver bdrv_gluster = {
|
||||||
.format_name = "gluster",
|
.format_name = "gluster",
|
||||||
.protocol_name = "gluster",
|
.protocol_name = "gluster",
|
||||||
.instance_size = sizeof(BDRVGlusterState),
|
.instance_size = sizeof(BDRVGlusterState),
|
||||||
.bdrv_needs_filename = true,
|
|
||||||
.bdrv_file_open = qemu_gluster_open,
|
.bdrv_file_open = qemu_gluster_open,
|
||||||
.bdrv_reopen_prepare = qemu_gluster_reopen_prepare,
|
|
||||||
.bdrv_reopen_commit = qemu_gluster_reopen_commit,
|
|
||||||
.bdrv_reopen_abort = qemu_gluster_reopen_abort,
|
|
||||||
.bdrv_close = qemu_gluster_close,
|
.bdrv_close = qemu_gluster_close,
|
||||||
.bdrv_create = qemu_gluster_create,
|
.bdrv_create = qemu_gluster_create,
|
||||||
.bdrv_getlength = qemu_gluster_getlength,
|
.bdrv_getlength = qemu_gluster_getlength,
|
||||||
.bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size,
|
.bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size,
|
||||||
.bdrv_truncate = qemu_gluster_truncate,
|
.bdrv_aio_readv = qemu_gluster_aio_readv,
|
||||||
.bdrv_co_readv = qemu_gluster_co_readv,
|
.bdrv_aio_writev = qemu_gluster_aio_writev,
|
||||||
.bdrv_co_writev = qemu_gluster_co_writev,
|
.bdrv_aio_flush = qemu_gluster_aio_flush,
|
||||||
.bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk,
|
.create_options = qemu_gluster_create_options,
|
||||||
.bdrv_has_zero_init = qemu_gluster_has_zero_init,
|
|
||||||
#ifdef CONFIG_GLUSTERFS_DISCARD
|
|
||||||
.bdrv_co_discard = qemu_gluster_co_discard,
|
|
||||||
#endif
|
|
||||||
#ifdef CONFIG_GLUSTERFS_ZEROFILL
|
|
||||||
.bdrv_co_write_zeroes = qemu_gluster_co_write_zeroes,
|
|
||||||
#endif
|
|
||||||
.create_opts = &qemu_gluster_create_opts,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static BlockDriver bdrv_gluster_tcp = {
|
static BlockDriver bdrv_gluster_tcp = {
|
||||||
.format_name = "gluster",
|
.format_name = "gluster",
|
||||||
.protocol_name = "gluster+tcp",
|
.protocol_name = "gluster+tcp",
|
||||||
.instance_size = sizeof(BDRVGlusterState),
|
.instance_size = sizeof(BDRVGlusterState),
|
||||||
.bdrv_needs_filename = true,
|
|
||||||
.bdrv_file_open = qemu_gluster_open,
|
.bdrv_file_open = qemu_gluster_open,
|
||||||
.bdrv_reopen_prepare = qemu_gluster_reopen_prepare,
|
|
||||||
.bdrv_reopen_commit = qemu_gluster_reopen_commit,
|
|
||||||
.bdrv_reopen_abort = qemu_gluster_reopen_abort,
|
|
||||||
.bdrv_close = qemu_gluster_close,
|
.bdrv_close = qemu_gluster_close,
|
||||||
.bdrv_create = qemu_gluster_create,
|
.bdrv_create = qemu_gluster_create,
|
||||||
.bdrv_getlength = qemu_gluster_getlength,
|
.bdrv_getlength = qemu_gluster_getlength,
|
||||||
.bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size,
|
.bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size,
|
||||||
.bdrv_truncate = qemu_gluster_truncate,
|
.bdrv_aio_readv = qemu_gluster_aio_readv,
|
||||||
.bdrv_co_readv = qemu_gluster_co_readv,
|
.bdrv_aio_writev = qemu_gluster_aio_writev,
|
||||||
.bdrv_co_writev = qemu_gluster_co_writev,
|
.bdrv_aio_flush = qemu_gluster_aio_flush,
|
||||||
.bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk,
|
.create_options = qemu_gluster_create_options,
|
||||||
.bdrv_has_zero_init = qemu_gluster_has_zero_init,
|
|
||||||
#ifdef CONFIG_GLUSTERFS_DISCARD
|
|
||||||
.bdrv_co_discard = qemu_gluster_co_discard,
|
|
||||||
#endif
|
|
||||||
#ifdef CONFIG_GLUSTERFS_ZEROFILL
|
|
||||||
.bdrv_co_write_zeroes = qemu_gluster_co_write_zeroes,
|
|
||||||
#endif
|
|
||||||
.create_opts = &qemu_gluster_create_opts,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static BlockDriver bdrv_gluster_unix = {
|
static BlockDriver bdrv_gluster_unix = {
|
||||||
.format_name = "gluster",
|
.format_name = "gluster",
|
||||||
.protocol_name = "gluster+unix",
|
.protocol_name = "gluster+unix",
|
||||||
.instance_size = sizeof(BDRVGlusterState),
|
.instance_size = sizeof(BDRVGlusterState),
|
||||||
.bdrv_needs_filename = true,
|
|
||||||
.bdrv_file_open = qemu_gluster_open,
|
.bdrv_file_open = qemu_gluster_open,
|
||||||
.bdrv_reopen_prepare = qemu_gluster_reopen_prepare,
|
|
||||||
.bdrv_reopen_commit = qemu_gluster_reopen_commit,
|
|
||||||
.bdrv_reopen_abort = qemu_gluster_reopen_abort,
|
|
||||||
.bdrv_close = qemu_gluster_close,
|
.bdrv_close = qemu_gluster_close,
|
||||||
.bdrv_create = qemu_gluster_create,
|
.bdrv_create = qemu_gluster_create,
|
||||||
.bdrv_getlength = qemu_gluster_getlength,
|
.bdrv_getlength = qemu_gluster_getlength,
|
||||||
.bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size,
|
.bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size,
|
||||||
.bdrv_truncate = qemu_gluster_truncate,
|
.bdrv_aio_readv = qemu_gluster_aio_readv,
|
||||||
.bdrv_co_readv = qemu_gluster_co_readv,
|
.bdrv_aio_writev = qemu_gluster_aio_writev,
|
||||||
.bdrv_co_writev = qemu_gluster_co_writev,
|
.bdrv_aio_flush = qemu_gluster_aio_flush,
|
||||||
.bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk,
|
.create_options = qemu_gluster_create_options,
|
||||||
.bdrv_has_zero_init = qemu_gluster_has_zero_init,
|
|
||||||
#ifdef CONFIG_GLUSTERFS_DISCARD
|
|
||||||
.bdrv_co_discard = qemu_gluster_co_discard,
|
|
||||||
#endif
|
|
||||||
#ifdef CONFIG_GLUSTERFS_ZEROFILL
|
|
||||||
.bdrv_co_write_zeroes = qemu_gluster_co_write_zeroes,
|
|
||||||
#endif
|
|
||||||
.create_opts = &qemu_gluster_create_opts,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static BlockDriver bdrv_gluster_rdma = {
|
static BlockDriver bdrv_gluster_rdma = {
|
||||||
.format_name = "gluster",
|
.format_name = "gluster",
|
||||||
.protocol_name = "gluster+rdma",
|
.protocol_name = "gluster+rdma",
|
||||||
.instance_size = sizeof(BDRVGlusterState),
|
.instance_size = sizeof(BDRVGlusterState),
|
||||||
.bdrv_needs_filename = true,
|
|
||||||
.bdrv_file_open = qemu_gluster_open,
|
.bdrv_file_open = qemu_gluster_open,
|
||||||
.bdrv_reopen_prepare = qemu_gluster_reopen_prepare,
|
|
||||||
.bdrv_reopen_commit = qemu_gluster_reopen_commit,
|
|
||||||
.bdrv_reopen_abort = qemu_gluster_reopen_abort,
|
|
||||||
.bdrv_close = qemu_gluster_close,
|
.bdrv_close = qemu_gluster_close,
|
||||||
.bdrv_create = qemu_gluster_create,
|
.bdrv_create = qemu_gluster_create,
|
||||||
.bdrv_getlength = qemu_gluster_getlength,
|
.bdrv_getlength = qemu_gluster_getlength,
|
||||||
.bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size,
|
.bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size,
|
||||||
.bdrv_truncate = qemu_gluster_truncate,
|
.bdrv_aio_readv = qemu_gluster_aio_readv,
|
||||||
.bdrv_co_readv = qemu_gluster_co_readv,
|
.bdrv_aio_writev = qemu_gluster_aio_writev,
|
||||||
.bdrv_co_writev = qemu_gluster_co_writev,
|
.bdrv_aio_flush = qemu_gluster_aio_flush,
|
||||||
.bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk,
|
.create_options = qemu_gluster_create_options,
|
||||||
.bdrv_has_zero_init = qemu_gluster_has_zero_init,
|
|
||||||
#ifdef CONFIG_GLUSTERFS_DISCARD
|
|
||||||
.bdrv_co_discard = qemu_gluster_co_discard,
|
|
||||||
#endif
|
|
||||||
#ifdef CONFIG_GLUSTERFS_ZEROFILL
|
|
||||||
.bdrv_co_write_zeroes = qemu_gluster_co_write_zeroes,
|
|
||||||
#endif
|
|
||||||
.create_opts = &qemu_gluster_create_opts,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static void bdrv_gluster_init(void)
|
static void bdrv_gluster_init(void)
|
||||||
|
|||||||
2767
block/io.c
2767
block/io.c
File diff suppressed because it is too large
Load Diff
1754
block/iscsi.c
1754
block/iscsi.c
File diff suppressed because it is too large
Load Diff
@@ -25,42 +25,23 @@
|
|||||||
*/
|
*/
|
||||||
#define MAX_EVENTS 128
|
#define MAX_EVENTS 128
|
||||||
|
|
||||||
#define MAX_QUEUED_IO 128
|
|
||||||
|
|
||||||
struct qemu_laiocb {
|
struct qemu_laiocb {
|
||||||
BlockAIOCB common;
|
BlockDriverAIOCB common;
|
||||||
struct qemu_laio_state *ctx;
|
struct qemu_laio_state *ctx;
|
||||||
struct iocb iocb;
|
struct iocb iocb;
|
||||||
ssize_t ret;
|
ssize_t ret;
|
||||||
size_t nbytes;
|
size_t nbytes;
|
||||||
QEMUIOVector *qiov;
|
QEMUIOVector *qiov;
|
||||||
bool is_read;
|
bool is_read;
|
||||||
QSIMPLEQ_ENTRY(qemu_laiocb) next;
|
QLIST_ENTRY(qemu_laiocb) node;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
int plugged;
|
|
||||||
unsigned int n;
|
|
||||||
bool blocked;
|
|
||||||
QSIMPLEQ_HEAD(, qemu_laiocb) pending;
|
|
||||||
} LaioQueue;
|
|
||||||
|
|
||||||
struct qemu_laio_state {
|
struct qemu_laio_state {
|
||||||
io_context_t ctx;
|
io_context_t ctx;
|
||||||
EventNotifier e;
|
EventNotifier e;
|
||||||
|
int count;
|
||||||
/* io queue for submit at batch */
|
|
||||||
LaioQueue io_q;
|
|
||||||
|
|
||||||
/* I/O completion processing */
|
|
||||||
QEMUBH *completion_bh;
|
|
||||||
struct io_event events[MAX_EVENTS];
|
|
||||||
int event_idx;
|
|
||||||
int event_max;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static void ioq_submit(struct qemu_laio_state *s);
|
|
||||||
|
|
||||||
static inline ssize_t io_event_ret(struct io_event *ev)
|
static inline ssize_t io_event_ret(struct io_event *ev)
|
||||||
{
|
{
|
||||||
return (ssize_t)(((uint64_t)ev->res2 << 32) | ev->res);
|
return (ssize_t)(((uint64_t)ev->res2 << 32) | ev->res);
|
||||||
@@ -74,6 +55,8 @@ static void qemu_laio_process_completion(struct qemu_laio_state *s,
|
|||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
|
s->count--;
|
||||||
|
|
||||||
ret = laiocb->ret;
|
ret = laiocb->ret;
|
||||||
if (ret != -ECANCELED) {
|
if (ret != -ECANCELED) {
|
||||||
if (ret == laiocb->nbytes) {
|
if (ret == laiocb->nbytes) {
|
||||||
@@ -87,159 +70,84 @@ static void qemu_laio_process_completion(struct qemu_laio_state *s,
|
|||||||
ret = -EINVAL;
|
ret = -EINVAL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
laiocb->common.cb(laiocb->common.opaque, ret);
|
laiocb->common.cb(laiocb->common.opaque, ret);
|
||||||
|
|
||||||
qemu_aio_unref(laiocb);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* The completion BH fetches completed I/O requests and invokes their
|
qemu_aio_release(laiocb);
|
||||||
* callbacks.
|
|
||||||
*
|
|
||||||
* The function is somewhat tricky because it supports nested event loops, for
|
|
||||||
* example when a request callback invokes aio_poll(). In order to do this,
|
|
||||||
* the completion events array and index are kept in qemu_laio_state. The BH
|
|
||||||
* reschedules itself as long as there are completions pending so it will
|
|
||||||
* either be called again in a nested event loop or will be called after all
|
|
||||||
* events have been completed. When there are no events left to complete, the
|
|
||||||
* BH returns without rescheduling.
|
|
||||||
*/
|
|
||||||
static void qemu_laio_completion_bh(void *opaque)
|
|
||||||
{
|
|
||||||
struct qemu_laio_state *s = opaque;
|
|
||||||
|
|
||||||
/* Fetch more completion events when empty */
|
|
||||||
if (s->event_idx == s->event_max) {
|
|
||||||
do {
|
|
||||||
struct timespec ts = { 0 };
|
|
||||||
s->event_max = io_getevents(s->ctx, MAX_EVENTS, MAX_EVENTS,
|
|
||||||
s->events, &ts);
|
|
||||||
} while (s->event_max == -EINTR);
|
|
||||||
|
|
||||||
s->event_idx = 0;
|
|
||||||
if (s->event_max <= 0) {
|
|
||||||
s->event_max = 0;
|
|
||||||
return; /* no more events */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Reschedule so nested event loops see currently pending completions */
|
|
||||||
qemu_bh_schedule(s->completion_bh);
|
|
||||||
|
|
||||||
/* Process completion events */
|
|
||||||
while (s->event_idx < s->event_max) {
|
|
||||||
struct iocb *iocb = s->events[s->event_idx].obj;
|
|
||||||
struct qemu_laiocb *laiocb =
|
|
||||||
container_of(iocb, struct qemu_laiocb, iocb);
|
|
||||||
|
|
||||||
laiocb->ret = io_event_ret(&s->events[s->event_idx]);
|
|
||||||
s->event_idx++;
|
|
||||||
|
|
||||||
qemu_laio_process_completion(s, laiocb);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!s->io_q.plugged && !QSIMPLEQ_EMPTY(&s->io_q.pending)) {
|
|
||||||
ioq_submit(s);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void qemu_laio_completion_cb(EventNotifier *e)
|
static void qemu_laio_completion_cb(EventNotifier *e)
|
||||||
{
|
{
|
||||||
struct qemu_laio_state *s = container_of(e, struct qemu_laio_state, e);
|
struct qemu_laio_state *s = container_of(e, struct qemu_laio_state, e);
|
||||||
|
|
||||||
if (event_notifier_test_and_clear(&s->e)) {
|
while (event_notifier_test_and_clear(&s->e)) {
|
||||||
qemu_bh_schedule(s->completion_bh);
|
struct io_event events[MAX_EVENTS];
|
||||||
|
struct timespec ts = { 0 };
|
||||||
|
int nevents, i;
|
||||||
|
|
||||||
|
do {
|
||||||
|
nevents = io_getevents(s->ctx, MAX_EVENTS, MAX_EVENTS, events, &ts);
|
||||||
|
} while (nevents == -EINTR);
|
||||||
|
|
||||||
|
for (i = 0; i < nevents; i++) {
|
||||||
|
struct iocb *iocb = events[i].obj;
|
||||||
|
struct qemu_laiocb *laiocb =
|
||||||
|
container_of(iocb, struct qemu_laiocb, iocb);
|
||||||
|
|
||||||
|
laiocb->ret = io_event_ret(&events[i]);
|
||||||
|
qemu_laio_process_completion(s, laiocb);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void laio_cancel(BlockAIOCB *blockacb)
|
static int qemu_laio_flush_cb(EventNotifier *e)
|
||||||
|
{
|
||||||
|
struct qemu_laio_state *s = container_of(e, struct qemu_laio_state, e);
|
||||||
|
|
||||||
|
return (s->count > 0) ? 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void laio_cancel(BlockDriverAIOCB *blockacb)
|
||||||
{
|
{
|
||||||
struct qemu_laiocb *laiocb = (struct qemu_laiocb *)blockacb;
|
struct qemu_laiocb *laiocb = (struct qemu_laiocb *)blockacb;
|
||||||
struct io_event event;
|
struct io_event event;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
if (laiocb->ret != -EINPROGRESS) {
|
if (laiocb->ret != -EINPROGRESS)
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
/*
|
||||||
|
* Note that as of Linux 2.6.31 neither the block device code nor any
|
||||||
|
* filesystem implements cancellation of AIO request.
|
||||||
|
* Thus the polling loop below is the normal code path.
|
||||||
|
*/
|
||||||
ret = io_cancel(laiocb->ctx->ctx, &laiocb->iocb, &event);
|
ret = io_cancel(laiocb->ctx->ctx, &laiocb->iocb, &event);
|
||||||
|
if (ret == 0) {
|
||||||
laiocb->ret = -ECANCELED;
|
laiocb->ret = -ECANCELED;
|
||||||
if (ret != 0) {
|
|
||||||
/* iocb is not cancelled, cb will be called by the event loop later */
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
laiocb->common.cb(laiocb->common.opaque, laiocb->ret);
|
/*
|
||||||
|
* We have to wait for the iocb to finish.
|
||||||
|
*
|
||||||
|
* The only way to get the iocb status update is by polling the io context.
|
||||||
|
* We might be able to do this slightly more optimal by removing the
|
||||||
|
* O_NONBLOCK flag.
|
||||||
|
*/
|
||||||
|
while (laiocb->ret == -EINPROGRESS) {
|
||||||
|
qemu_laio_completion_cb(&laiocb->ctx->e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static const AIOCBInfo laio_aiocb_info = {
|
static const AIOCBInfo laio_aiocb_info = {
|
||||||
.aiocb_size = sizeof(struct qemu_laiocb),
|
.aiocb_size = sizeof(struct qemu_laiocb),
|
||||||
.cancel_async = laio_cancel,
|
.cancel = laio_cancel,
|
||||||
};
|
};
|
||||||
|
|
||||||
static void ioq_init(LaioQueue *io_q)
|
BlockDriverAIOCB *laio_submit(BlockDriverState *bs, void *aio_ctx, int fd,
|
||||||
{
|
|
||||||
QSIMPLEQ_INIT(&io_q->pending);
|
|
||||||
io_q->plugged = 0;
|
|
||||||
io_q->n = 0;
|
|
||||||
io_q->blocked = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ioq_submit(struct qemu_laio_state *s)
|
|
||||||
{
|
|
||||||
int ret, len;
|
|
||||||
struct qemu_laiocb *aiocb;
|
|
||||||
struct iocb *iocbs[MAX_QUEUED_IO];
|
|
||||||
QSIMPLEQ_HEAD(, qemu_laiocb) completed;
|
|
||||||
|
|
||||||
do {
|
|
||||||
len = 0;
|
|
||||||
QSIMPLEQ_FOREACH(aiocb, &s->io_q.pending, next) {
|
|
||||||
iocbs[len++] = &aiocb->iocb;
|
|
||||||
if (len == MAX_QUEUED_IO) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = io_submit(s->ctx, len, iocbs);
|
|
||||||
if (ret == -EAGAIN) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (ret < 0) {
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
|
|
||||||
s->io_q.n -= ret;
|
|
||||||
aiocb = container_of(iocbs[ret - 1], struct qemu_laiocb, iocb);
|
|
||||||
QSIMPLEQ_SPLIT_AFTER(&s->io_q.pending, aiocb, next, &completed);
|
|
||||||
} while (ret == len && !QSIMPLEQ_EMPTY(&s->io_q.pending));
|
|
||||||
s->io_q.blocked = (s->io_q.n > 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void laio_io_plug(BlockDriverState *bs, void *aio_ctx)
|
|
||||||
{
|
|
||||||
struct qemu_laio_state *s = aio_ctx;
|
|
||||||
|
|
||||||
s->io_q.plugged++;
|
|
||||||
}
|
|
||||||
|
|
||||||
void laio_io_unplug(BlockDriverState *bs, void *aio_ctx, bool unplug)
|
|
||||||
{
|
|
||||||
struct qemu_laio_state *s = aio_ctx;
|
|
||||||
|
|
||||||
assert(s->io_q.plugged > 0 || !unplug);
|
|
||||||
|
|
||||||
if (unplug && --s->io_q.plugged > 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!s->io_q.blocked && !QSIMPLEQ_EMPTY(&s->io_q.pending)) {
|
|
||||||
ioq_submit(s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
BlockAIOCB *laio_submit(BlockDriverState *bs, void *aio_ctx, int fd,
|
|
||||||
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
||||||
BlockCompletionFunc *cb, void *opaque, int type)
|
BlockDriverCompletionFunc *cb, void *opaque, int type)
|
||||||
{
|
{
|
||||||
struct qemu_laio_state *s = aio_ctx;
|
struct qemu_laio_state *s = aio_ctx;
|
||||||
struct qemu_laiocb *laiocb;
|
struct qemu_laiocb *laiocb;
|
||||||
@@ -269,37 +177,19 @@ BlockAIOCB *laio_submit(BlockDriverState *bs, void *aio_ctx, int fd,
|
|||||||
goto out_free_aiocb;
|
goto out_free_aiocb;
|
||||||
}
|
}
|
||||||
io_set_eventfd(&laiocb->iocb, event_notifier_get_fd(&s->e));
|
io_set_eventfd(&laiocb->iocb, event_notifier_get_fd(&s->e));
|
||||||
|
s->count++;
|
||||||
|
|
||||||
QSIMPLEQ_INSERT_TAIL(&s->io_q.pending, laiocb, next);
|
if (io_submit(s->ctx, 1, &iocbs) < 0)
|
||||||
s->io_q.n++;
|
goto out_dec_count;
|
||||||
if (!s->io_q.blocked &&
|
|
||||||
(!s->io_q.plugged || s->io_q.n >= MAX_QUEUED_IO)) {
|
|
||||||
ioq_submit(s);
|
|
||||||
}
|
|
||||||
return &laiocb->common;
|
return &laiocb->common;
|
||||||
|
|
||||||
|
out_dec_count:
|
||||||
|
s->count--;
|
||||||
out_free_aiocb:
|
out_free_aiocb:
|
||||||
qemu_aio_unref(laiocb);
|
qemu_aio_release(laiocb);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void laio_detach_aio_context(void *s_, AioContext *old_context)
|
|
||||||
{
|
|
||||||
struct qemu_laio_state *s = s_;
|
|
||||||
|
|
||||||
aio_set_event_notifier(old_context, &s->e, false, NULL);
|
|
||||||
qemu_bh_delete(s->completion_bh);
|
|
||||||
}
|
|
||||||
|
|
||||||
void laio_attach_aio_context(void *s_, AioContext *new_context)
|
|
||||||
{
|
|
||||||
struct qemu_laio_state *s = s_;
|
|
||||||
|
|
||||||
s->completion_bh = aio_bh_new(new_context, qemu_laio_completion_bh, s);
|
|
||||||
aio_set_event_notifier(new_context, &s->e, false,
|
|
||||||
qemu_laio_completion_cb);
|
|
||||||
}
|
|
||||||
|
|
||||||
void *laio_init(void)
|
void *laio_init(void)
|
||||||
{
|
{
|
||||||
struct qemu_laio_state *s;
|
struct qemu_laio_state *s;
|
||||||
@@ -313,7 +203,8 @@ void *laio_init(void)
|
|||||||
goto out_close_efd;
|
goto out_close_efd;
|
||||||
}
|
}
|
||||||
|
|
||||||
ioq_init(&s->io_q);
|
qemu_aio_set_event_notifier(&s->e, qemu_laio_completion_cb,
|
||||||
|
qemu_laio_flush_cb);
|
||||||
|
|
||||||
return s;
|
return s;
|
||||||
|
|
||||||
@@ -323,16 +214,3 @@ out_free_state:
|
|||||||
g_free(s);
|
g_free(s);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void laio_cleanup(void *s_)
|
|
||||||
{
|
|
||||||
struct qemu_laio_state *s = s_;
|
|
||||||
|
|
||||||
event_notifier_cleanup(&s->e);
|
|
||||||
|
|
||||||
if (io_destroy(s->ctx) != 0) {
|
|
||||||
fprintf(stderr, "%s: destroy AIO context %p failed\n",
|
|
||||||
__func__, &s->ctx);
|
|
||||||
}
|
|
||||||
g_free(s);
|
|
||||||
}
|
|
||||||
|
|||||||
519
block/mirror.c
519
block/mirror.c
@@ -14,15 +14,11 @@
|
|||||||
#include "trace.h"
|
#include "trace.h"
|
||||||
#include "block/blockjob.h"
|
#include "block/blockjob.h"
|
||||||
#include "block/block_int.h"
|
#include "block/block_int.h"
|
||||||
#include "sysemu/block-backend.h"
|
|
||||||
#include "qapi/qmp/qerror.h"
|
|
||||||
#include "qemu/ratelimit.h"
|
#include "qemu/ratelimit.h"
|
||||||
#include "qemu/bitmap.h"
|
#include "qemu/bitmap.h"
|
||||||
#include "qemu/error-report.h"
|
|
||||||
|
|
||||||
#define SLICE_TIME 100000000ULL /* ns */
|
#define SLICE_TIME 100000000ULL /* ns */
|
||||||
#define MAX_IN_FLIGHT 16
|
#define MAX_IN_FLIGHT 16
|
||||||
#define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
|
|
||||||
|
|
||||||
/* The mirroring buffer is a list of granularity-sized chunks.
|
/* The mirroring buffer is a list of granularity-sized chunks.
|
||||||
* Free chunks are organized in a list.
|
* Free chunks are organized in a list.
|
||||||
@@ -35,23 +31,14 @@ typedef struct MirrorBlockJob {
|
|||||||
BlockJob common;
|
BlockJob common;
|
||||||
RateLimit limit;
|
RateLimit limit;
|
||||||
BlockDriverState *target;
|
BlockDriverState *target;
|
||||||
BlockDriverState *base;
|
MirrorSyncMode mode;
|
||||||
/* The name of the graph node to replace */
|
|
||||||
char *replaces;
|
|
||||||
/* The BDS to replace */
|
|
||||||
BlockDriverState *to_replace;
|
|
||||||
/* Used to block operations on the drive-mirror-replace target */
|
|
||||||
Error *replace_blocker;
|
|
||||||
bool is_none_mode;
|
|
||||||
BlockdevOnError on_source_error, on_target_error;
|
BlockdevOnError on_source_error, on_target_error;
|
||||||
bool synced;
|
bool synced;
|
||||||
bool should_complete;
|
bool should_complete;
|
||||||
int64_t sector_num;
|
int64_t sector_num;
|
||||||
int64_t granularity;
|
int64_t granularity;
|
||||||
size_t buf_size;
|
size_t buf_size;
|
||||||
int64_t bdev_length;
|
|
||||||
unsigned long *cow_bitmap;
|
unsigned long *cow_bitmap;
|
||||||
BdrvDirtyBitmap *dirty_bitmap;
|
|
||||||
HBitmapIter hbi;
|
HBitmapIter hbi;
|
||||||
uint8_t *buf;
|
uint8_t *buf;
|
||||||
QSIMPLEQ_HEAD(, MirrorBuffer) buf_free;
|
QSIMPLEQ_HEAD(, MirrorBuffer) buf_free;
|
||||||
@@ -59,10 +46,7 @@ typedef struct MirrorBlockJob {
|
|||||||
|
|
||||||
unsigned long *in_flight_bitmap;
|
unsigned long *in_flight_bitmap;
|
||||||
int in_flight;
|
int in_flight;
|
||||||
int sectors_in_flight;
|
|
||||||
int ret;
|
int ret;
|
||||||
bool unmap;
|
|
||||||
bool waiting_for_io;
|
|
||||||
} MirrorBlockJob;
|
} MirrorBlockJob;
|
||||||
|
|
||||||
typedef struct MirrorOp {
|
typedef struct MirrorOp {
|
||||||
@@ -95,7 +79,6 @@ static void mirror_iteration_done(MirrorOp *op, int ret)
|
|||||||
trace_mirror_iteration_done(s, op->sector_num, op->nb_sectors, ret);
|
trace_mirror_iteration_done(s, op->sector_num, op->nb_sectors, ret);
|
||||||
|
|
||||||
s->in_flight--;
|
s->in_flight--;
|
||||||
s->sectors_in_flight -= op->nb_sectors;
|
|
||||||
iov = op->qiov.iov;
|
iov = op->qiov.iov;
|
||||||
for (i = 0; i < op->qiov.niov; i++) {
|
for (i = 0; i < op->qiov.niov; i++) {
|
||||||
MirrorBuffer *buf = (MirrorBuffer *) iov[i].iov_base;
|
MirrorBuffer *buf = (MirrorBuffer *) iov[i].iov_base;
|
||||||
@@ -107,31 +90,25 @@ static void mirror_iteration_done(MirrorOp *op, int ret)
|
|||||||
chunk_num = op->sector_num / sectors_per_chunk;
|
chunk_num = op->sector_num / sectors_per_chunk;
|
||||||
nb_chunks = op->nb_sectors / sectors_per_chunk;
|
nb_chunks = op->nb_sectors / sectors_per_chunk;
|
||||||
bitmap_clear(s->in_flight_bitmap, chunk_num, nb_chunks);
|
bitmap_clear(s->in_flight_bitmap, chunk_num, nb_chunks);
|
||||||
if (ret >= 0) {
|
if (s->cow_bitmap && ret >= 0) {
|
||||||
if (s->cow_bitmap) {
|
|
||||||
bitmap_set(s->cow_bitmap, chunk_num, nb_chunks);
|
bitmap_set(s->cow_bitmap, chunk_num, nb_chunks);
|
||||||
}
|
}
|
||||||
s->common.offset += (uint64_t)op->nb_sectors * BDRV_SECTOR_SIZE;
|
|
||||||
}
|
|
||||||
|
|
||||||
qemu_iovec_destroy(&op->qiov);
|
g_slice_free(MirrorOp, op);
|
||||||
g_free(op);
|
|
||||||
|
|
||||||
if (s->waiting_for_io) {
|
|
||||||
qemu_coroutine_enter(s->common.co, NULL);
|
qemu_coroutine_enter(s->common.co, NULL);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
static void mirror_write_complete(void *opaque, int ret)
|
static void mirror_write_complete(void *opaque, int ret)
|
||||||
{
|
{
|
||||||
MirrorOp *op = opaque;
|
MirrorOp *op = opaque;
|
||||||
MirrorBlockJob *s = op->s;
|
MirrorBlockJob *s = op->s;
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
|
BlockDriverState *source = s->common.bs;
|
||||||
BlockErrorAction action;
|
BlockErrorAction action;
|
||||||
|
|
||||||
bdrv_set_dirty_bitmap(s->dirty_bitmap, op->sector_num, op->nb_sectors);
|
bdrv_set_dirty(source, op->sector_num, op->nb_sectors);
|
||||||
action = mirror_error_action(s, false, -ret);
|
action = mirror_error_action(s, false, -ret);
|
||||||
if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) {
|
if (action == BDRV_ACTION_REPORT && s->ret >= 0) {
|
||||||
s->ret = ret;
|
s->ret = ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -143,11 +120,12 @@ static void mirror_read_complete(void *opaque, int ret)
|
|||||||
MirrorOp *op = opaque;
|
MirrorOp *op = opaque;
|
||||||
MirrorBlockJob *s = op->s;
|
MirrorBlockJob *s = op->s;
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
|
BlockDriverState *source = s->common.bs;
|
||||||
BlockErrorAction action;
|
BlockErrorAction action;
|
||||||
|
|
||||||
bdrv_set_dirty_bitmap(s->dirty_bitmap, op->sector_num, op->nb_sectors);
|
bdrv_set_dirty(source, op->sector_num, op->nb_sectors);
|
||||||
action = mirror_error_action(s, true, -ret);
|
action = mirror_error_action(s, true, -ret);
|
||||||
if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) {
|
if (action == BDRV_ACTION_REPORT && s->ret >= 0) {
|
||||||
s->ret = ret;
|
s->ret = ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,30 +136,25 @@ static void mirror_read_complete(void *opaque, int ret)
|
|||||||
mirror_write_complete, op);
|
mirror_write_complete, op);
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
|
static void coroutine_fn mirror_iteration(MirrorBlockJob *s)
|
||||||
{
|
{
|
||||||
BlockDriverState *source = s->common.bs;
|
BlockDriverState *source = s->common.bs;
|
||||||
int nb_sectors, sectors_per_chunk, nb_chunks, max_iov;
|
int nb_sectors, sectors_per_chunk, nb_chunks;
|
||||||
int64_t end, sector_num, next_chunk, next_sector, hbitmap_next_sector;
|
int64_t end, sector_num, next_chunk, next_sector, hbitmap_next_sector;
|
||||||
uint64_t delay_ns = 0;
|
|
||||||
MirrorOp *op;
|
MirrorOp *op;
|
||||||
int pnum;
|
|
||||||
int64_t ret;
|
|
||||||
|
|
||||||
max_iov = MIN(source->bl.max_iov, s->target->bl.max_iov);
|
|
||||||
|
|
||||||
s->sector_num = hbitmap_iter_next(&s->hbi);
|
s->sector_num = hbitmap_iter_next(&s->hbi);
|
||||||
if (s->sector_num < 0) {
|
if (s->sector_num < 0) {
|
||||||
bdrv_dirty_iter_init(s->dirty_bitmap, &s->hbi);
|
bdrv_dirty_iter_init(source, &s->hbi);
|
||||||
s->sector_num = hbitmap_iter_next(&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));
|
||||||
assert(s->sector_num >= 0);
|
assert(s->sector_num >= 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
hbitmap_next_sector = s->sector_num;
|
hbitmap_next_sector = s->sector_num;
|
||||||
sector_num = s->sector_num;
|
sector_num = s->sector_num;
|
||||||
sectors_per_chunk = s->granularity >> BDRV_SECTOR_BITS;
|
sectors_per_chunk = s->granularity >> BDRV_SECTOR_BITS;
|
||||||
end = s->bdev_length / BDRV_SECTOR_SIZE;
|
end = s->common.len >> BDRV_SECTOR_BITS;
|
||||||
|
|
||||||
/* Extend the QEMUIOVector to include all adjacent blocks that will
|
/* Extend the QEMUIOVector to include all adjacent blocks that will
|
||||||
* be copied in this operation.
|
* be copied in this operation.
|
||||||
@@ -204,15 +177,13 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
|
|||||||
/* Wait for I/O to this cluster (from a previous iteration) to be done. */
|
/* Wait for I/O to this cluster (from a previous iteration) to be done. */
|
||||||
while (test_bit(next_chunk, s->in_flight_bitmap)) {
|
while (test_bit(next_chunk, s->in_flight_bitmap)) {
|
||||||
trace_mirror_yield_in_flight(s, sector_num, s->in_flight);
|
trace_mirror_yield_in_flight(s, sector_num, s->in_flight);
|
||||||
s->waiting_for_io = true;
|
|
||||||
qemu_coroutine_yield();
|
qemu_coroutine_yield();
|
||||||
s->waiting_for_io = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
int added_sectors, added_chunks;
|
int added_sectors, added_chunks;
|
||||||
|
|
||||||
if (!bdrv_get_dirty(source, s->dirty_bitmap, next_sector) ||
|
if (!bdrv_get_dirty(source, next_sector) ||
|
||||||
test_bit(next_chunk, s->in_flight_bitmap)) {
|
test_bit(next_chunk, s->in_flight_bitmap)) {
|
||||||
assert(nb_sectors > 0);
|
assert(nb_sectors > 0);
|
||||||
break;
|
break;
|
||||||
@@ -242,18 +213,12 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
|
|||||||
*/
|
*/
|
||||||
while (nb_chunks == 0 && s->buf_free_count < added_chunks) {
|
while (nb_chunks == 0 && s->buf_free_count < added_chunks) {
|
||||||
trace_mirror_yield_buf_busy(s, nb_chunks, s->in_flight);
|
trace_mirror_yield_buf_busy(s, nb_chunks, s->in_flight);
|
||||||
s->waiting_for_io = true;
|
|
||||||
qemu_coroutine_yield();
|
qemu_coroutine_yield();
|
||||||
s->waiting_for_io = false;
|
|
||||||
}
|
}
|
||||||
if (s->buf_free_count < nb_chunks + added_chunks) {
|
if (s->buf_free_count < nb_chunks + added_chunks) {
|
||||||
trace_mirror_break_buf_busy(s, nb_chunks, s->in_flight);
|
trace_mirror_break_buf_busy(s, nb_chunks, s->in_flight);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (max_iov < nb_chunks + added_chunks) {
|
|
||||||
trace_mirror_break_iov_max(s, nb_chunks, added_chunks);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* We have enough free space to copy these sectors. */
|
/* We have enough free space to copy these sectors. */
|
||||||
bitmap_set(s->in_flight_bitmap, next_chunk, added_chunks);
|
bitmap_set(s->in_flight_bitmap, next_chunk, added_chunks);
|
||||||
@@ -262,13 +227,10 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
|
|||||||
nb_chunks += added_chunks;
|
nb_chunks += added_chunks;
|
||||||
next_sector += added_sectors;
|
next_sector += added_sectors;
|
||||||
next_chunk += added_chunks;
|
next_chunk += added_chunks;
|
||||||
if (!s->synced && s->common.speed) {
|
} while (next_sector < end);
|
||||||
delay_ns = ratelimit_calculate_delay(&s->limit, added_sectors);
|
|
||||||
}
|
|
||||||
} while (delay_ns == 0 && next_sector < end);
|
|
||||||
|
|
||||||
/* Allocate a MirrorOp that is used as an AIO callback. */
|
/* Allocate a MirrorOp that is used as an AIO callback. */
|
||||||
op = g_new(MirrorOp, 1);
|
op = g_slice_new(MirrorOp);
|
||||||
op->s = s;
|
op->s = s;
|
||||||
op->sector_num = sector_num;
|
op->sector_num = sector_num;
|
||||||
op->nb_sectors = nb_sectors;
|
op->nb_sectors = nb_sectors;
|
||||||
@@ -280,46 +242,27 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
|
|||||||
next_sector = sector_num;
|
next_sector = sector_num;
|
||||||
while (nb_chunks-- > 0) {
|
while (nb_chunks-- > 0) {
|
||||||
MirrorBuffer *buf = QSIMPLEQ_FIRST(&s->buf_free);
|
MirrorBuffer *buf = QSIMPLEQ_FIRST(&s->buf_free);
|
||||||
size_t remaining = (nb_sectors * BDRV_SECTOR_SIZE) - op->qiov.size;
|
|
||||||
|
|
||||||
QSIMPLEQ_REMOVE_HEAD(&s->buf_free, next);
|
QSIMPLEQ_REMOVE_HEAD(&s->buf_free, next);
|
||||||
s->buf_free_count--;
|
s->buf_free_count--;
|
||||||
qemu_iovec_add(&op->qiov, buf, MIN(s->granularity, remaining));
|
qemu_iovec_add(&op->qiov, buf, s->granularity);
|
||||||
|
|
||||||
/* Advance the HBitmapIter in parallel, so that we do not examine
|
/* Advance the HBitmapIter in parallel, so that we do not examine
|
||||||
* the same sector twice.
|
* the same sector twice.
|
||||||
*/
|
*/
|
||||||
if (next_sector > hbitmap_next_sector
|
if (next_sector > hbitmap_next_sector && bdrv_get_dirty(source, next_sector)) {
|
||||||
&& bdrv_get_dirty(source, s->dirty_bitmap, next_sector)) {
|
|
||||||
hbitmap_next_sector = hbitmap_iter_next(&s->hbi);
|
hbitmap_next_sector = hbitmap_iter_next(&s->hbi);
|
||||||
}
|
}
|
||||||
|
|
||||||
next_sector += sectors_per_chunk;
|
next_sector += sectors_per_chunk;
|
||||||
}
|
}
|
||||||
|
|
||||||
bdrv_reset_dirty_bitmap(s->dirty_bitmap, sector_num, nb_sectors);
|
bdrv_reset_dirty(source, sector_num, nb_sectors);
|
||||||
|
|
||||||
/* Copy the dirty cluster. */
|
/* Copy the dirty cluster. */
|
||||||
s->in_flight++;
|
s->in_flight++;
|
||||||
s->sectors_in_flight += nb_sectors;
|
|
||||||
trace_mirror_one_iteration(s, sector_num, nb_sectors);
|
trace_mirror_one_iteration(s, sector_num, nb_sectors);
|
||||||
|
|
||||||
ret = bdrv_get_block_status_above(source, NULL, sector_num,
|
|
||||||
nb_sectors, &pnum);
|
|
||||||
if (ret < 0 || pnum < nb_sectors ||
|
|
||||||
(ret & BDRV_BLOCK_DATA && !(ret & BDRV_BLOCK_ZERO))) {
|
|
||||||
bdrv_aio_readv(source, sector_num, &op->qiov, nb_sectors,
|
bdrv_aio_readv(source, sector_num, &op->qiov, nb_sectors,
|
||||||
mirror_read_complete, op);
|
mirror_read_complete, op);
|
||||||
} else if (ret & BDRV_BLOCK_ZERO) {
|
|
||||||
bdrv_aio_write_zeroes(s->target, sector_num, op->nb_sectors,
|
|
||||||
s->unmap ? BDRV_REQ_MAY_UNMAP : 0,
|
|
||||||
mirror_write_complete, op);
|
|
||||||
} else {
|
|
||||||
assert(!(ret & BDRV_BLOCK_DATA));
|
|
||||||
bdrv_aio_discard(s->target, sector_num, op->nb_sectors,
|
|
||||||
mirror_write_complete, op);
|
|
||||||
}
|
|
||||||
return delay_ns;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void mirror_free_init(MirrorBlockJob *s)
|
static void mirror_free_init(MirrorBlockJob *s)
|
||||||
@@ -342,80 +285,18 @@ static void mirror_free_init(MirrorBlockJob *s)
|
|||||||
static void mirror_drain(MirrorBlockJob *s)
|
static void mirror_drain(MirrorBlockJob *s)
|
||||||
{
|
{
|
||||||
while (s->in_flight > 0) {
|
while (s->in_flight > 0) {
|
||||||
s->waiting_for_io = true;
|
|
||||||
qemu_coroutine_yield();
|
qemu_coroutine_yield();
|
||||||
s->waiting_for_io = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
int ret;
|
|
||||||
} MirrorExitData;
|
|
||||||
|
|
||||||
static void mirror_exit(BlockJob *job, void *opaque)
|
|
||||||
{
|
|
||||||
MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
|
|
||||||
MirrorExitData *data = opaque;
|
|
||||||
AioContext *replace_aio_context = NULL;
|
|
||||||
BlockDriverState *src = s->common.bs;
|
|
||||||
|
|
||||||
/* Make sure that the source BDS doesn't go away before we called
|
|
||||||
* block_job_completed(). */
|
|
||||||
bdrv_ref(src);
|
|
||||||
|
|
||||||
if (s->to_replace) {
|
|
||||||
replace_aio_context = bdrv_get_aio_context(s->to_replace);
|
|
||||||
aio_context_acquire(replace_aio_context);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (s->should_complete && data->ret == 0) {
|
|
||||||
BlockDriverState *to_replace = s->common.bs;
|
|
||||||
if (s->to_replace) {
|
|
||||||
to_replace = s->to_replace;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* This was checked in mirror_start_job(), but meanwhile one of the
|
|
||||||
* nodes could have been newly attached to a BlockBackend. */
|
|
||||||
if (to_replace->blk && s->target->blk) {
|
|
||||||
error_report("block job: Can't create node with two BlockBackends");
|
|
||||||
data->ret = -EINVAL;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bdrv_get_flags(s->target) != bdrv_get_flags(to_replace)) {
|
|
||||||
bdrv_reopen(s->target, bdrv_get_flags(to_replace), NULL);
|
|
||||||
}
|
|
||||||
bdrv_replace_in_backing_chain(to_replace, s->target);
|
|
||||||
}
|
|
||||||
|
|
||||||
out:
|
|
||||||
if (s->to_replace) {
|
|
||||||
bdrv_op_unblock_all(s->to_replace, s->replace_blocker);
|
|
||||||
error_free(s->replace_blocker);
|
|
||||||
bdrv_unref(s->to_replace);
|
|
||||||
}
|
|
||||||
if (replace_aio_context) {
|
|
||||||
aio_context_release(replace_aio_context);
|
|
||||||
}
|
|
||||||
g_free(s->replaces);
|
|
||||||
bdrv_op_unblock_all(s->target, s->common.blocker);
|
|
||||||
bdrv_unref(s->target);
|
|
||||||
block_job_completed(&s->common, data->ret);
|
|
||||||
g_free(data);
|
|
||||||
bdrv_drained_end(src);
|
|
||||||
bdrv_unref(src);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void coroutine_fn mirror_run(void *opaque)
|
static void coroutine_fn mirror_run(void *opaque)
|
||||||
{
|
{
|
||||||
MirrorBlockJob *s = opaque;
|
MirrorBlockJob *s = opaque;
|
||||||
MirrorExitData *data;
|
|
||||||
BlockDriverState *bs = s->common.bs;
|
BlockDriverState *bs = s->common.bs;
|
||||||
int64_t sector_num, end, length;
|
int64_t sector_num, end, sectors_per_chunk, length;
|
||||||
uint64_t last_pause_ns;
|
uint64_t last_pause_ns;
|
||||||
BlockDriverInfo bdi;
|
BlockDriverInfo bdi;
|
||||||
char backing_filename[2]; /* we only need 2 characters because we are only
|
char backing_filename[1024];
|
||||||
checking for a NULL string */
|
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
int n;
|
int n;
|
||||||
|
|
||||||
@@ -423,22 +304,13 @@ static void coroutine_fn mirror_run(void *opaque)
|
|||||||
goto immediate_exit;
|
goto immediate_exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
s->bdev_length = bdrv_getlength(bs);
|
s->common.len = bdrv_getlength(bs);
|
||||||
if (s->bdev_length < 0) {
|
if (s->common.len <= 0) {
|
||||||
ret = s->bdev_length;
|
block_job_completed(&s->common, s->common.len);
|
||||||
goto immediate_exit;
|
return;
|
||||||
} else if (s->bdev_length == 0) {
|
|
||||||
/* Report BLOCK_JOB_READY and wait for complete. */
|
|
||||||
block_job_event_ready(&s->common);
|
|
||||||
s->synced = true;
|
|
||||||
while (!block_job_is_cancelled(&s->common) && !s->should_complete) {
|
|
||||||
block_job_yield(&s->common);
|
|
||||||
}
|
|
||||||
s->common.cancelled = false;
|
|
||||||
goto immediate_exit;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
length = DIV_ROUND_UP(s->bdev_length, s->granularity);
|
length = (bdrv_getlength(bs) + s->granularity - 1) / s->granularity;
|
||||||
s->in_flight_bitmap = bitmap_new(length);
|
s->in_flight_bitmap = bitmap_new(length);
|
||||||
|
|
||||||
/* If we have no backing file yet in the destination, we cannot let
|
/* If we have no backing file yet in the destination, we cannot let
|
||||||
@@ -447,64 +319,46 @@ static void coroutine_fn mirror_run(void *opaque)
|
|||||||
*/
|
*/
|
||||||
bdrv_get_backing_filename(s->target, backing_filename,
|
bdrv_get_backing_filename(s->target, backing_filename,
|
||||||
sizeof(backing_filename));
|
sizeof(backing_filename));
|
||||||
if (backing_filename[0] && !s->target->backing) {
|
if (backing_filename[0] && !s->target->backing_hd) {
|
||||||
ret = bdrv_get_info(s->target, &bdi);
|
bdrv_get_info(s->target, &bdi);
|
||||||
if (ret < 0) {
|
|
||||||
goto immediate_exit;
|
|
||||||
}
|
|
||||||
if (s->granularity < bdi.cluster_size) {
|
if (s->granularity < bdi.cluster_size) {
|
||||||
s->buf_size = MAX(s->buf_size, bdi.cluster_size);
|
s->buf_size = MAX(s->buf_size, bdi.cluster_size);
|
||||||
s->cow_bitmap = bitmap_new(length);
|
s->cow_bitmap = bitmap_new(length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
end = s->bdev_length / BDRV_SECTOR_SIZE;
|
end = s->common.len >> BDRV_SECTOR_BITS;
|
||||||
s->buf = qemu_try_blockalign(bs, s->buf_size);
|
s->buf = qemu_blockalign(bs, s->buf_size);
|
||||||
if (s->buf == NULL) {
|
sectors_per_chunk = s->granularity >> BDRV_SECTOR_BITS;
|
||||||
ret = -ENOMEM;
|
|
||||||
goto immediate_exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
mirror_free_init(s);
|
mirror_free_init(s);
|
||||||
|
|
||||||
last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
|
if (s->mode != MIRROR_SYNC_MODE_NONE) {
|
||||||
if (!s->is_none_mode) {
|
|
||||||
/* First part, loop on the sectors and initialize the dirty bitmap. */
|
/* First part, loop on the sectors and initialize the dirty bitmap. */
|
||||||
BlockDriverState *base = s->base;
|
BlockDriverState *base;
|
||||||
bool mark_all_dirty = s->base == NULL && !bdrv_has_zero_init(s->target);
|
base = s->mode == MIRROR_SYNC_MODE_FULL ? NULL : bs->backing_hd;
|
||||||
|
|
||||||
for (sector_num = 0; sector_num < end; ) {
|
for (sector_num = 0; sector_num < end; ) {
|
||||||
/* Just to make sure we are not exceeding int limit. */
|
int64_t next = (sector_num | (sectors_per_chunk - 1)) + 1;
|
||||||
int nb_sectors = MIN(INT_MAX >> BDRV_SECTOR_BITS,
|
ret = bdrv_co_is_allocated_above(bs, base,
|
||||||
end - sector_num);
|
sector_num, next - sector_num, &n);
|
||||||
int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
|
|
||||||
|
|
||||||
if (now - last_pause_ns > SLICE_TIME) {
|
|
||||||
last_pause_ns = now;
|
|
||||||
block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (block_job_is_cancelled(&s->common)) {
|
|
||||||
goto immediate_exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = bdrv_is_allocated_above(bs, base, sector_num, nb_sectors, &n);
|
|
||||||
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto immediate_exit;
|
goto immediate_exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(n > 0);
|
assert(n > 0);
|
||||||
if (ret == 1 || mark_all_dirty) {
|
if (ret == 1) {
|
||||||
bdrv_set_dirty_bitmap(s->dirty_bitmap, sector_num, n);
|
bdrv_set_dirty(bs, sector_num, n);
|
||||||
}
|
sector_num = next;
|
||||||
|
} else {
|
||||||
sector_num += n;
|
sector_num += n;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bdrv_dirty_iter_init(s->dirty_bitmap, &s->hbi);
|
bdrv_dirty_iter_init(bs, &s->hbi);
|
||||||
|
last_pause_ns = qemu_get_clock_ns(rt_clock);
|
||||||
for (;;) {
|
for (;;) {
|
||||||
uint64_t delay_ns = 0;
|
uint64_t delay_ns;
|
||||||
int64_t cnt;
|
int64_t cnt;
|
||||||
bool should_complete;
|
bool should_complete;
|
||||||
|
|
||||||
@@ -513,30 +367,23 @@ static void coroutine_fn mirror_run(void *opaque)
|
|||||||
goto immediate_exit;
|
goto immediate_exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
cnt = bdrv_get_dirty_count(s->dirty_bitmap);
|
cnt = bdrv_get_dirty_count(bs);
|
||||||
/* 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
|
|
||||||
* processed; together those are the current total operation length */
|
|
||||||
s->common.len = s->common.offset +
|
|
||||||
(cnt + s->sectors_in_flight) * BDRV_SECTOR_SIZE;
|
|
||||||
|
|
||||||
/* Note that even when no rate limit is applied we need to yield
|
/* 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,
|
* We do so every SLICE_TIME nanoseconds, or when there is an error,
|
||||||
* or when the source is clean, whichever comes first.
|
* or when the source is clean, whichever comes first.
|
||||||
*/
|
*/
|
||||||
if (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - last_pause_ns < SLICE_TIME &&
|
if (qemu_get_clock_ns(rt_clock) - last_pause_ns < SLICE_TIME &&
|
||||||
s->common.iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
|
s->common.iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
|
||||||
if (s->in_flight == MAX_IN_FLIGHT || s->buf_free_count == 0 ||
|
if (s->in_flight == MAX_IN_FLIGHT || s->buf_free_count == 0 ||
|
||||||
(cnt == 0 && s->in_flight > 0)) {
|
(cnt == 0 && s->in_flight > 0)) {
|
||||||
trace_mirror_yield(s, s->in_flight, s->buf_free_count, cnt);
|
trace_mirror_yield(s, s->in_flight, s->buf_free_count, cnt);
|
||||||
s->waiting_for_io = true;
|
|
||||||
qemu_coroutine_yield();
|
qemu_coroutine_yield();
|
||||||
s->waiting_for_io = false;
|
|
||||||
continue;
|
continue;
|
||||||
} else if (cnt != 0) {
|
} else if (cnt != 0) {
|
||||||
delay_ns = mirror_iteration(s);
|
mirror_iteration(s);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -545,8 +392,7 @@ static void coroutine_fn mirror_run(void *opaque)
|
|||||||
trace_mirror_before_flush(s);
|
trace_mirror_before_flush(s);
|
||||||
ret = bdrv_flush(s->target);
|
ret = bdrv_flush(s->target);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
if (mirror_error_action(s, false, -ret) ==
|
if (mirror_error_action(s, false, -ret) == BDRV_ACTION_REPORT) {
|
||||||
BLOCK_ERROR_ACTION_REPORT) {
|
|
||||||
goto immediate_exit;
|
goto immediate_exit;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -555,14 +401,15 @@ static void coroutine_fn mirror_run(void *opaque)
|
|||||||
* report completion. This way, block-job-cancel will leave
|
* report completion. This way, block-job-cancel will leave
|
||||||
* the target in a consistent state.
|
* the target in a consistent state.
|
||||||
*/
|
*/
|
||||||
|
s->common.offset = end * BDRV_SECTOR_SIZE;
|
||||||
if (!s->synced) {
|
if (!s->synced) {
|
||||||
block_job_event_ready(&s->common);
|
block_job_ready(&s->common);
|
||||||
s->synced = true;
|
s->synced = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
should_complete = s->should_complete ||
|
should_complete = s->should_complete ||
|
||||||
block_job_is_cancelled(&s->common);
|
block_job_is_cancelled(&s->common);
|
||||||
cnt = bdrv_get_dirty_count(s->dirty_bitmap);
|
cnt = bdrv_get_dirty_count(bs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -576,20 +423,29 @@ static void coroutine_fn mirror_run(void *opaque)
|
|||||||
* mirror_populate runs.
|
* mirror_populate runs.
|
||||||
*/
|
*/
|
||||||
trace_mirror_before_drain(s, cnt);
|
trace_mirror_before_drain(s, cnt);
|
||||||
bdrv_drain(bs);
|
bdrv_drain_all();
|
||||||
cnt = bdrv_get_dirty_count(s->dirty_bitmap);
|
cnt = bdrv_get_dirty_count(bs);
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
trace_mirror_before_sleep(s, cnt, s->synced, delay_ns);
|
trace_mirror_before_sleep(s, cnt, s->synced);
|
||||||
if (!s->synced) {
|
if (!s->synced) {
|
||||||
block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
|
/* Publish progress */
|
||||||
|
s->common.offset = (end - cnt) * BDRV_SECTOR_SIZE;
|
||||||
|
|
||||||
|
if (s->common.speed) {
|
||||||
|
delay_ns = ratelimit_calculate_delay(&s->limit, sectors_per_chunk);
|
||||||
|
} else {
|
||||||
|
delay_ns = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
block_job_sleep_ns(&s->common, rt_clock, delay_ns);
|
||||||
if (block_job_is_cancelled(&s->common)) {
|
if (block_job_is_cancelled(&s->common)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else if (!should_complete) {
|
} else if (!should_complete) {
|
||||||
delay_ns = (s->in_flight == 0 && cnt == 0 ? SLICE_TIME : 0);
|
delay_ns = (s->in_flight == 0 && cnt == 0 ? SLICE_TIME : 0);
|
||||||
block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
|
block_job_sleep_ns(&s->common, rt_clock, delay_ns);
|
||||||
} else if (cnt == 0) {
|
} else if (cnt == 0) {
|
||||||
/* The two disks are in sync. Exit and report successful
|
/* The two disks are in sync. Exit and report successful
|
||||||
* completion.
|
* completion.
|
||||||
@@ -598,7 +454,7 @@ static void coroutine_fn mirror_run(void *opaque)
|
|||||||
s->common.cancelled = false;
|
s->common.cancelled = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
|
last_pause_ns = qemu_get_clock_ns(rt_clock);
|
||||||
}
|
}
|
||||||
|
|
||||||
immediate_exit:
|
immediate_exit:
|
||||||
@@ -615,17 +471,17 @@ immediate_exit:
|
|||||||
qemu_vfree(s->buf);
|
qemu_vfree(s->buf);
|
||||||
g_free(s->cow_bitmap);
|
g_free(s->cow_bitmap);
|
||||||
g_free(s->in_flight_bitmap);
|
g_free(s->in_flight_bitmap);
|
||||||
bdrv_release_dirty_bitmap(bs, s->dirty_bitmap);
|
bdrv_set_dirty_tracking(bs, 0);
|
||||||
if (s->target->blk) {
|
bdrv_iostatus_disable(s->target);
|
||||||
blk_iostatus_disable(s->target->blk);
|
if (s->should_complete && ret == 0) {
|
||||||
|
if (bdrv_get_flags(s->target) != bdrv_get_flags(s->common.bs)) {
|
||||||
|
bdrv_reopen(s->target, bdrv_get_flags(s->common.bs), NULL);
|
||||||
}
|
}
|
||||||
|
bdrv_swap(s->target, s->common.bs);
|
||||||
data = g_malloc(sizeof(*data));
|
}
|
||||||
data->ret = ret;
|
bdrv_close(s->target);
|
||||||
/* Before we switch to target in mirror_exit, make sure data doesn't
|
bdrv_delete(s->target);
|
||||||
* change. */
|
block_job_completed(&s->common, ret);
|
||||||
bdrv_drained_begin(s->common.bs);
|
|
||||||
block_job_defer_to_main_loop(&s->common, mirror_exit, data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void mirror_set_speed(BlockJob *job, int64_t speed, Error **errp)
|
static void mirror_set_speed(BlockJob *job, int64_t speed, Error **errp)
|
||||||
@@ -633,7 +489,7 @@ static void mirror_set_speed(BlockJob *job, int64_t speed, Error **errp)
|
|||||||
MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
|
MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
|
||||||
|
|
||||||
if (speed < 0) {
|
if (speed < 0) {
|
||||||
error_setg(errp, QERR_INVALID_PARAMETER, "speed");
|
error_set(errp, QERR_INVALID_PARAMETER, "speed");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
|
ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
|
||||||
@@ -643,235 +499,86 @@ static void mirror_iostatus_reset(BlockJob *job)
|
|||||||
{
|
{
|
||||||
MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
|
MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
|
||||||
|
|
||||||
if (s->target->blk) {
|
bdrv_iostatus_reset(s->target);
|
||||||
blk_iostatus_reset(s->target->blk);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void mirror_complete(BlockJob *job, Error **errp)
|
static void mirror_complete(BlockJob *job, Error **errp)
|
||||||
{
|
{
|
||||||
MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
|
MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
|
||||||
Error *local_err = NULL;
|
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
ret = bdrv_open_backing_file(s->target, NULL, "backing", &local_err);
|
ret = bdrv_open_backing_file(s->target);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_propagate(errp, local_err);
|
char backing_filename[PATH_MAX];
|
||||||
|
bdrv_get_full_backing_filename(s->target, backing_filename,
|
||||||
|
sizeof(backing_filename));
|
||||||
|
error_set(errp, QERR_OPEN_FILE_FAILED, backing_filename);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!s->synced) {
|
if (!s->synced) {
|
||||||
error_setg(errp, QERR_BLOCK_JOB_NOT_READY, job->id);
|
error_set(errp, QERR_BLOCK_JOB_NOT_READY, job->bs->device_name);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* check the target bs is not blocked and block all operations on it */
|
|
||||||
if (s->replaces) {
|
|
||||||
AioContext *replace_aio_context;
|
|
||||||
|
|
||||||
s->to_replace = bdrv_find_node(s->replaces);
|
|
||||||
if (!s->to_replace) {
|
|
||||||
error_setg(errp, "Node name '%s' not found", s->replaces);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
replace_aio_context = bdrv_get_aio_context(s->to_replace);
|
|
||||||
aio_context_acquire(replace_aio_context);
|
|
||||||
|
|
||||||
error_setg(&s->replace_blocker,
|
|
||||||
"block device is in use by block-job-complete");
|
|
||||||
bdrv_op_block_all(s->to_replace, s->replace_blocker);
|
|
||||||
bdrv_ref(s->to_replace);
|
|
||||||
|
|
||||||
aio_context_release(replace_aio_context);
|
|
||||||
}
|
|
||||||
|
|
||||||
s->should_complete = true;
|
s->should_complete = true;
|
||||||
block_job_enter(&s->common);
|
block_job_resume(job);
|
||||||
}
|
}
|
||||||
|
|
||||||
static const BlockJobDriver mirror_job_driver = {
|
static BlockJobType mirror_job_type = {
|
||||||
.instance_size = sizeof(MirrorBlockJob),
|
.instance_size = sizeof(MirrorBlockJob),
|
||||||
.job_type = BLOCK_JOB_TYPE_MIRROR,
|
.job_type = "mirror",
|
||||||
.set_speed = mirror_set_speed,
|
.set_speed = mirror_set_speed,
|
||||||
.iostatus_reset= mirror_iostatus_reset,
|
.iostatus_reset= mirror_iostatus_reset,
|
||||||
.complete = mirror_complete,
|
.complete = mirror_complete,
|
||||||
};
|
};
|
||||||
|
|
||||||
static const BlockJobDriver commit_active_job_driver = {
|
void mirror_start(BlockDriverState *bs, BlockDriverState *target,
|
||||||
.instance_size = sizeof(MirrorBlockJob),
|
int64_t speed, int64_t granularity, int64_t buf_size,
|
||||||
.job_type = BLOCK_JOB_TYPE_COMMIT,
|
MirrorSyncMode mode, BlockdevOnError on_source_error,
|
||||||
.set_speed = mirror_set_speed,
|
|
||||||
.iostatus_reset
|
|
||||||
= mirror_iostatus_reset,
|
|
||||||
.complete = mirror_complete,
|
|
||||||
};
|
|
||||||
|
|
||||||
static void mirror_start_job(BlockDriverState *bs, BlockDriverState *target,
|
|
||||||
const char *replaces,
|
|
||||||
int64_t speed, uint32_t granularity,
|
|
||||||
int64_t buf_size,
|
|
||||||
BlockdevOnError on_source_error,
|
|
||||||
BlockdevOnError on_target_error,
|
BlockdevOnError on_target_error,
|
||||||
bool unmap,
|
BlockDriverCompletionFunc *cb,
|
||||||
BlockCompletionFunc *cb,
|
void *opaque, Error **errp)
|
||||||
void *opaque, Error **errp,
|
|
||||||
const BlockJobDriver *driver,
|
|
||||||
bool is_none_mode, BlockDriverState *base)
|
|
||||||
{
|
{
|
||||||
MirrorBlockJob *s;
|
MirrorBlockJob *s;
|
||||||
BlockDriverState *replaced_bs;
|
|
||||||
|
|
||||||
if (granularity == 0) {
|
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);
|
assert ((granularity & (granularity - 1)) == 0);
|
||||||
|
|
||||||
if ((on_source_error == BLOCKDEV_ON_ERROR_STOP ||
|
if ((on_source_error == BLOCKDEV_ON_ERROR_STOP ||
|
||||||
on_source_error == BLOCKDEV_ON_ERROR_ENOSPC) &&
|
on_source_error == BLOCKDEV_ON_ERROR_ENOSPC) &&
|
||||||
(!bs->blk || !blk_iostatus_is_enabled(bs->blk))) {
|
!bdrv_iostatus_is_enabled(bs)) {
|
||||||
error_setg(errp, QERR_INVALID_PARAMETER, "on-source-error");
|
error_set(errp, QERR_INVALID_PARAMETER, "on-source-error");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buf_size < 0) {
|
s = block_job_create(&mirror_job_type, bs, speed, cb, opaque, errp);
|
||||||
error_setg(errp, "Invalid parameter 'buf-size'");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (buf_size == 0) {
|
|
||||||
buf_size = DEFAULT_MIRROR_BUF_SIZE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* We can't support this case as long as the block layer can't handle
|
|
||||||
* multiple BlockBackends per BlockDriverState. */
|
|
||||||
if (replaces) {
|
|
||||||
replaced_bs = bdrv_lookup_bs(replaces, replaces, errp);
|
|
||||||
if (replaced_bs == NULL) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
replaced_bs = bs;
|
|
||||||
}
|
|
||||||
if (replaced_bs->blk && target->blk) {
|
|
||||||
error_setg(errp, "Can't create node with two BlockBackends");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
s = block_job_create(driver, bs, speed, cb, opaque, errp);
|
|
||||||
if (!s) {
|
if (!s) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
s->replaces = g_strdup(replaces);
|
|
||||||
s->on_source_error = on_source_error;
|
s->on_source_error = on_source_error;
|
||||||
s->on_target_error = on_target_error;
|
s->on_target_error = on_target_error;
|
||||||
s->target = target;
|
s->target = target;
|
||||||
s->is_none_mode = is_none_mode;
|
s->mode = mode;
|
||||||
s->base = base;
|
|
||||||
s->granularity = granularity;
|
s->granularity = granularity;
|
||||||
s->buf_size = ROUND_UP(buf_size, granularity);
|
s->buf_size = MAX(buf_size, granularity);
|
||||||
s->unmap = unmap;
|
|
||||||
|
|
||||||
s->dirty_bitmap = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp);
|
|
||||||
if (!s->dirty_bitmap) {
|
|
||||||
g_free(s->replaces);
|
|
||||||
block_job_unref(&s->common);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
bdrv_op_block_all(s->target, s->common.blocker);
|
|
||||||
|
|
||||||
|
bdrv_set_dirty_tracking(bs, granularity);
|
||||||
bdrv_set_enable_write_cache(s->target, true);
|
bdrv_set_enable_write_cache(s->target, true);
|
||||||
if (s->target->blk) {
|
bdrv_set_on_error(s->target, on_target_error, on_target_error);
|
||||||
blk_set_on_error(s->target->blk, on_target_error, on_target_error);
|
bdrv_iostatus_enable(s->target);
|
||||||
blk_iostatus_enable(s->target->blk);
|
|
||||||
}
|
|
||||||
s->common.co = qemu_coroutine_create(mirror_run);
|
s->common.co = qemu_coroutine_create(mirror_run);
|
||||||
trace_mirror_start(bs, s, s->common.co, opaque);
|
trace_mirror_start(bs, s, s->common.co, opaque);
|
||||||
qemu_coroutine_enter(s->common.co, s);
|
qemu_coroutine_enter(s->common.co, s);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mirror_start(BlockDriverState *bs, BlockDriverState *target,
|
|
||||||
const char *replaces,
|
|
||||||
int64_t speed, uint32_t granularity, int64_t buf_size,
|
|
||||||
MirrorSyncMode mode, BlockdevOnError on_source_error,
|
|
||||||
BlockdevOnError on_target_error,
|
|
||||||
bool unmap,
|
|
||||||
BlockCompletionFunc *cb,
|
|
||||||
void *opaque, Error **errp)
|
|
||||||
{
|
|
||||||
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 ? backing_bs(bs) : NULL;
|
|
||||||
mirror_start_job(bs, target, replaces,
|
|
||||||
speed, granularity, buf_size,
|
|
||||||
on_source_error, on_target_error, unmap, cb, opaque, errp,
|
|
||||||
&mirror_job_driver, is_none_mode, base);
|
|
||||||
}
|
|
||||||
|
|
||||||
void commit_active_start(BlockDriverState *bs, BlockDriverState *base,
|
|
||||||
int64_t speed,
|
|
||||||
BlockdevOnError on_error,
|
|
||||||
BlockCompletionFunc *cb,
|
|
||||||
void *opaque, Error **errp)
|
|
||||||
{
|
|
||||||
int64_t length, base_length;
|
|
||||||
int orig_base_flags;
|
|
||||||
int ret;
|
|
||||||
Error *local_err = NULL;
|
|
||||||
|
|
||||||
orig_base_flags = bdrv_get_flags(base);
|
|
||||||
|
|
||||||
if (bdrv_reopen(base, bs->open_flags, errp)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
length = bdrv_getlength(bs);
|
|
||||||
if (length < 0) {
|
|
||||||
error_setg_errno(errp, -length,
|
|
||||||
"Unable to determine length of %s", bs->filename);
|
|
||||||
goto error_restore_flags;
|
|
||||||
}
|
|
||||||
|
|
||||||
base_length = bdrv_getlength(base);
|
|
||||||
if (base_length < 0) {
|
|
||||||
error_setg_errno(errp, -base_length,
|
|
||||||
"Unable to determine length of %s", base->filename);
|
|
||||||
goto error_restore_flags;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (length > base_length) {
|
|
||||||
ret = bdrv_truncate(base, length);
|
|
||||||
if (ret < 0) {
|
|
||||||
error_setg_errno(errp, -ret,
|
|
||||||
"Top image %s is larger than base image %s, and "
|
|
||||||
"resize of base image failed",
|
|
||||||
bs->filename, base->filename);
|
|
||||||
goto error_restore_flags;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bdrv_ref(base);
|
|
||||||
mirror_start_job(bs, base, NULL, speed, 0, 0,
|
|
||||||
on_error, on_error, false, cb, opaque, &local_err,
|
|
||||||
&commit_active_job_driver, false, base);
|
|
||||||
if (local_err) {
|
|
||||||
error_propagate(errp, local_err);
|
|
||||||
goto error_restore_flags;
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
|
|
||||||
error_restore_flags:
|
|
||||||
/* ignore error and errp for bdrv_reopen, because we want to propagate
|
|
||||||
* the original error */
|
|
||||||
bdrv_reopen(base, orig_base_flags, NULL);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,409 +0,0 @@
|
|||||||
/*
|
|
||||||
* QEMU Block driver for NBD
|
|
||||||
*
|
|
||||||
* Copyright (C) 2008 Bull S.A.S.
|
|
||||||
* Author: Laurent Vivier <Laurent.Vivier@bull.net>
|
|
||||||
*
|
|
||||||
* Some parts:
|
|
||||||
* Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
|
|
||||||
*
|
|
||||||
* 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 "nbd-client.h"
|
|
||||||
#include "qemu/sockets.h"
|
|
||||||
|
|
||||||
#define HANDLE_TO_INDEX(bs, handle) ((handle) ^ ((uint64_t)(intptr_t)bs))
|
|
||||||
#define INDEX_TO_HANDLE(bs, index) ((index) ^ ((uint64_t)(intptr_t)bs))
|
|
||||||
|
|
||||||
static void nbd_recv_coroutines_enter_all(NbdClientSession *s)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
for (i = 0; i < MAX_NBD_REQUESTS; i++) {
|
|
||||||
if (s->recv_coroutine[i]) {
|
|
||||||
qemu_coroutine_enter(s->recv_coroutine[i], NULL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nbd_teardown_connection(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
NbdClientSession *client = nbd_get_client_session(bs);
|
|
||||||
|
|
||||||
/* finish any pending coroutines */
|
|
||||||
shutdown(client->sock, 2);
|
|
||||||
nbd_recv_coroutines_enter_all(client);
|
|
||||||
|
|
||||||
nbd_client_detach_aio_context(bs);
|
|
||||||
closesocket(client->sock);
|
|
||||||
client->sock = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nbd_reply_ready(void *opaque)
|
|
||||||
{
|
|
||||||
BlockDriverState *bs = opaque;
|
|
||||||
NbdClientSession *s = nbd_get_client_session(bs);
|
|
||||||
uint64_t i;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
if (s->reply.handle == 0) {
|
|
||||||
/* No reply already in flight. Fetch a header. It is possible
|
|
||||||
* that another thread has done the same thing in parallel, so
|
|
||||||
* the socket is not readable anymore.
|
|
||||||
*/
|
|
||||||
ret = nbd_receive_reply(s->sock, &s->reply);
|
|
||||||
if (ret == -EAGAIN) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (ret < 0) {
|
|
||||||
s->reply.handle = 0;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* There's no need for a mutex on the receive side, because the
|
|
||||||
* handler acts as a synchronization point and ensures that only
|
|
||||||
* one coroutine is called until the reply finishes. */
|
|
||||||
i = HANDLE_TO_INDEX(s, s->reply.handle);
|
|
||||||
if (i >= MAX_NBD_REQUESTS) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (s->recv_coroutine[i]) {
|
|
||||||
qemu_coroutine_enter(s->recv_coroutine[i], NULL);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
fail:
|
|
||||||
nbd_teardown_connection(bs);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nbd_restart_write(void *opaque)
|
|
||||||
{
|
|
||||||
BlockDriverState *bs = opaque;
|
|
||||||
|
|
||||||
qemu_coroutine_enter(nbd_get_client_session(bs)->send_coroutine, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int nbd_co_send_request(BlockDriverState *bs,
|
|
||||||
struct nbd_request *request,
|
|
||||||
QEMUIOVector *qiov, int offset)
|
|
||||||
{
|
|
||||||
NbdClientSession *s = nbd_get_client_session(bs);
|
|
||||||
AioContext *aio_context;
|
|
||||||
int rc, ret, i;
|
|
||||||
|
|
||||||
qemu_co_mutex_lock(&s->send_mutex);
|
|
||||||
|
|
||||||
for (i = 0; i < MAX_NBD_REQUESTS; i++) {
|
|
||||||
if (s->recv_coroutine[i] == NULL) {
|
|
||||||
s->recv_coroutine[i] = qemu_coroutine_self();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(i < MAX_NBD_REQUESTS);
|
|
||||||
request->handle = INDEX_TO_HANDLE(s, i);
|
|
||||||
s->send_coroutine = qemu_coroutine_self();
|
|
||||||
aio_context = bdrv_get_aio_context(bs);
|
|
||||||
|
|
||||||
aio_set_fd_handler(aio_context, s->sock, false,
|
|
||||||
nbd_reply_ready, nbd_restart_write, bs);
|
|
||||||
if (qiov) {
|
|
||||||
if (!s->is_unix) {
|
|
||||||
socket_set_cork(s->sock, 1);
|
|
||||||
}
|
|
||||||
rc = nbd_send_request(s->sock, request);
|
|
||||||
if (rc >= 0) {
|
|
||||||
ret = qemu_co_sendv(s->sock, qiov->iov, qiov->niov,
|
|
||||||
offset, request->len);
|
|
||||||
if (ret != request->len) {
|
|
||||||
rc = -EIO;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!s->is_unix) {
|
|
||||||
socket_set_cork(s->sock, 0);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
rc = nbd_send_request(s->sock, request);
|
|
||||||
}
|
|
||||||
aio_set_fd_handler(aio_context, s->sock, false,
|
|
||||||
nbd_reply_ready, NULL, bs);
|
|
||||||
s->send_coroutine = NULL;
|
|
||||||
qemu_co_mutex_unlock(&s->send_mutex);
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nbd_co_receive_reply(NbdClientSession *s,
|
|
||||||
struct nbd_request *request, struct nbd_reply *reply,
|
|
||||||
QEMUIOVector *qiov, int offset)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
/* Wait until we're woken up by the read handler. TODO: perhaps
|
|
||||||
* peek at the next reply and avoid yielding if it's ours? */
|
|
||||||
qemu_coroutine_yield();
|
|
||||||
*reply = s->reply;
|
|
||||||
if (reply->handle != request->handle) {
|
|
||||||
reply->error = EIO;
|
|
||||||
} else {
|
|
||||||
if (qiov && reply->error == 0) {
|
|
||||||
ret = qemu_co_recvv(s->sock, qiov->iov, qiov->niov,
|
|
||||||
offset, request->len);
|
|
||||||
if (ret != request->len) {
|
|
||||||
reply->error = EIO;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Tell the read handler to read another header. */
|
|
||||||
s->reply.handle = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nbd_coroutine_start(NbdClientSession *s,
|
|
||||||
struct nbd_request *request)
|
|
||||||
{
|
|
||||||
/* Poor man semaphore. The free_sema is locked when no other request
|
|
||||||
* can be accepted, and unlocked after receiving one reply. */
|
|
||||||
if (s->in_flight >= MAX_NBD_REQUESTS - 1) {
|
|
||||||
qemu_co_mutex_lock(&s->free_sema);
|
|
||||||
assert(s->in_flight < MAX_NBD_REQUESTS);
|
|
||||||
}
|
|
||||||
s->in_flight++;
|
|
||||||
|
|
||||||
/* s->recv_coroutine[i] is set as soon as we get the send_lock. */
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nbd_coroutine_end(NbdClientSession *s,
|
|
||||||
struct nbd_request *request)
|
|
||||||
{
|
|
||||||
int i = HANDLE_TO_INDEX(s, request->handle);
|
|
||||||
s->recv_coroutine[i] = NULL;
|
|
||||||
if (s->in_flight-- == MAX_NBD_REQUESTS) {
|
|
||||||
qemu_co_mutex_unlock(&s->free_sema);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static int nbd_co_readv_1(BlockDriverState *bs, int64_t sector_num,
|
|
||||||
int nb_sectors, QEMUIOVector *qiov,
|
|
||||||
int offset)
|
|
||||||
{
|
|
||||||
NbdClientSession *client = nbd_get_client_session(bs);
|
|
||||||
struct nbd_request request = { .type = NBD_CMD_READ };
|
|
||||||
struct nbd_reply reply;
|
|
||||||
ssize_t ret;
|
|
||||||
|
|
||||||
request.from = sector_num * 512;
|
|
||||||
request.len = nb_sectors * 512;
|
|
||||||
|
|
||||||
nbd_coroutine_start(client, &request);
|
|
||||||
ret = nbd_co_send_request(bs, &request, NULL, 0);
|
|
||||||
if (ret < 0) {
|
|
||||||
reply.error = -ret;
|
|
||||||
} else {
|
|
||||||
nbd_co_receive_reply(client, &request, &reply, qiov, offset);
|
|
||||||
}
|
|
||||||
nbd_coroutine_end(client, &request);
|
|
||||||
return -reply.error;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static int nbd_co_writev_1(BlockDriverState *bs, int64_t sector_num,
|
|
||||||
int nb_sectors, QEMUIOVector *qiov,
|
|
||||||
int offset)
|
|
||||||
{
|
|
||||||
NbdClientSession *client = nbd_get_client_session(bs);
|
|
||||||
struct nbd_request request = { .type = NBD_CMD_WRITE };
|
|
||||||
struct nbd_reply reply;
|
|
||||||
ssize_t ret;
|
|
||||||
|
|
||||||
if (!bdrv_enable_write_cache(bs) &&
|
|
||||||
(client->nbdflags & NBD_FLAG_SEND_FUA)) {
|
|
||||||
request.type |= NBD_CMD_FLAG_FUA;
|
|
||||||
}
|
|
||||||
|
|
||||||
request.from = sector_num * 512;
|
|
||||||
request.len = nb_sectors * 512;
|
|
||||||
|
|
||||||
nbd_coroutine_start(client, &request);
|
|
||||||
ret = nbd_co_send_request(bs, &request, qiov, offset);
|
|
||||||
if (ret < 0) {
|
|
||||||
reply.error = -ret;
|
|
||||||
} else {
|
|
||||||
nbd_co_receive_reply(client, &request, &reply, NULL, 0);
|
|
||||||
}
|
|
||||||
nbd_coroutine_end(client, &request);
|
|
||||||
return -reply.error;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* qemu-nbd has a limit of slightly less than 1M per request. Try to
|
|
||||||
* remain aligned to 4K. */
|
|
||||||
#define NBD_MAX_SECTORS 2040
|
|
||||||
|
|
||||||
int nbd_client_co_readv(BlockDriverState *bs, int64_t sector_num,
|
|
||||||
int nb_sectors, QEMUIOVector *qiov)
|
|
||||||
{
|
|
||||||
int offset = 0;
|
|
||||||
int ret;
|
|
||||||
while (nb_sectors > NBD_MAX_SECTORS) {
|
|
||||||
ret = nbd_co_readv_1(bs, sector_num, NBD_MAX_SECTORS, qiov, offset);
|
|
||||||
if (ret < 0) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
offset += NBD_MAX_SECTORS * 512;
|
|
||||||
sector_num += NBD_MAX_SECTORS;
|
|
||||||
nb_sectors -= NBD_MAX_SECTORS;
|
|
||||||
}
|
|
||||||
return nbd_co_readv_1(bs, sector_num, nb_sectors, qiov, offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
int nbd_client_co_writev(BlockDriverState *bs, int64_t sector_num,
|
|
||||||
int nb_sectors, QEMUIOVector *qiov)
|
|
||||||
{
|
|
||||||
int offset = 0;
|
|
||||||
int ret;
|
|
||||||
while (nb_sectors > NBD_MAX_SECTORS) {
|
|
||||||
ret = nbd_co_writev_1(bs, sector_num, NBD_MAX_SECTORS, qiov, offset);
|
|
||||||
if (ret < 0) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
offset += NBD_MAX_SECTORS * 512;
|
|
||||||
sector_num += NBD_MAX_SECTORS;
|
|
||||||
nb_sectors -= NBD_MAX_SECTORS;
|
|
||||||
}
|
|
||||||
return nbd_co_writev_1(bs, sector_num, nb_sectors, qiov, offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
int nbd_client_co_flush(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
NbdClientSession *client = nbd_get_client_session(bs);
|
|
||||||
struct nbd_request request = { .type = NBD_CMD_FLUSH };
|
|
||||||
struct nbd_reply reply;
|
|
||||||
ssize_t ret;
|
|
||||||
|
|
||||||
if (!(client->nbdflags & NBD_FLAG_SEND_FLUSH)) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (client->nbdflags & NBD_FLAG_SEND_FUA) {
|
|
||||||
request.type |= NBD_CMD_FLAG_FUA;
|
|
||||||
}
|
|
||||||
|
|
||||||
request.from = 0;
|
|
||||||
request.len = 0;
|
|
||||||
|
|
||||||
nbd_coroutine_start(client, &request);
|
|
||||||
ret = nbd_co_send_request(bs, &request, NULL, 0);
|
|
||||||
if (ret < 0) {
|
|
||||||
reply.error = -ret;
|
|
||||||
} else {
|
|
||||||
nbd_co_receive_reply(client, &request, &reply, NULL, 0);
|
|
||||||
}
|
|
||||||
nbd_coroutine_end(client, &request);
|
|
||||||
return -reply.error;
|
|
||||||
}
|
|
||||||
|
|
||||||
int nbd_client_co_discard(BlockDriverState *bs, int64_t sector_num,
|
|
||||||
int nb_sectors)
|
|
||||||
{
|
|
||||||
NbdClientSession *client = nbd_get_client_session(bs);
|
|
||||||
struct nbd_request request = { .type = NBD_CMD_TRIM };
|
|
||||||
struct nbd_reply reply;
|
|
||||||
ssize_t ret;
|
|
||||||
|
|
||||||
if (!(client->nbdflags & NBD_FLAG_SEND_TRIM)) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
request.from = sector_num * 512;
|
|
||||||
request.len = nb_sectors * 512;
|
|
||||||
|
|
||||||
nbd_coroutine_start(client, &request);
|
|
||||||
ret = nbd_co_send_request(bs, &request, NULL, 0);
|
|
||||||
if (ret < 0) {
|
|
||||||
reply.error = -ret;
|
|
||||||
} else {
|
|
||||||
nbd_co_receive_reply(client, &request, &reply, NULL, 0);
|
|
||||||
}
|
|
||||||
nbd_coroutine_end(client, &request);
|
|
||||||
return -reply.error;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void nbd_client_detach_aio_context(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
aio_set_fd_handler(bdrv_get_aio_context(bs),
|
|
||||||
nbd_get_client_session(bs)->sock,
|
|
||||||
false, NULL, NULL, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
void nbd_client_attach_aio_context(BlockDriverState *bs,
|
|
||||||
AioContext *new_context)
|
|
||||||
{
|
|
||||||
aio_set_fd_handler(new_context, nbd_get_client_session(bs)->sock,
|
|
||||||
false, nbd_reply_ready, NULL, bs);
|
|
||||||
}
|
|
||||||
|
|
||||||
void nbd_client_close(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
NbdClientSession *client = nbd_get_client_session(bs);
|
|
||||||
struct nbd_request request = {
|
|
||||||
.type = NBD_CMD_DISC,
|
|
||||||
.from = 0,
|
|
||||||
.len = 0
|
|
||||||
};
|
|
||||||
|
|
||||||
if (client->sock == -1) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
nbd_send_request(client->sock, &request);
|
|
||||||
|
|
||||||
nbd_teardown_connection(bs);
|
|
||||||
}
|
|
||||||
|
|
||||||
int nbd_client_init(BlockDriverState *bs, int sock, const char *export,
|
|
||||||
Error **errp)
|
|
||||||
{
|
|
||||||
NbdClientSession *client = nbd_get_client_session(bs);
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
/* NBD handshake */
|
|
||||||
logout("session init %s\n", export);
|
|
||||||
qemu_set_block(sock);
|
|
||||||
ret = nbd_receive_negotiate(sock, export,
|
|
||||||
&client->nbdflags, &client->size, errp);
|
|
||||||
if (ret < 0) {
|
|
||||||
logout("Failed to negotiate with the NBD server\n");
|
|
||||||
closesocket(sock);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
qemu_co_mutex_init(&client->send_mutex);
|
|
||||||
qemu_co_mutex_init(&client->free_sema);
|
|
||||||
client->sock = sock;
|
|
||||||
|
|
||||||
/* Now that we're connected, set the socket to be non-blocking and
|
|
||||||
* kick the reply mechanism. */
|
|
||||||
qemu_set_nonblock(sock);
|
|
||||||
nbd_client_attach_aio_context(bs, bdrv_get_aio_context(bs));
|
|
||||||
|
|
||||||
logout("Established connection with NBD server\n");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
#ifndef NBD_CLIENT_H
|
|
||||||
#define NBD_CLIENT_H
|
|
||||||
|
|
||||||
#include "qemu-common.h"
|
|
||||||
#include "block/nbd.h"
|
|
||||||
#include "block/block_int.h"
|
|
||||||
|
|
||||||
/* #define DEBUG_NBD */
|
|
||||||
|
|
||||||
#if defined(DEBUG_NBD)
|
|
||||||
#define logout(fmt, ...) \
|
|
||||||
fprintf(stderr, "nbd\t%-24s" fmt, __func__, ##__VA_ARGS__)
|
|
||||||
#else
|
|
||||||
#define logout(fmt, ...) ((void)0)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define MAX_NBD_REQUESTS 16
|
|
||||||
|
|
||||||
typedef struct NbdClientSession {
|
|
||||||
int sock;
|
|
||||||
uint32_t nbdflags;
|
|
||||||
off_t size;
|
|
||||||
|
|
||||||
CoMutex send_mutex;
|
|
||||||
CoMutex free_sema;
|
|
||||||
Coroutine *send_coroutine;
|
|
||||||
int in_flight;
|
|
||||||
|
|
||||||
Coroutine *recv_coroutine[MAX_NBD_REQUESTS];
|
|
||||||
struct nbd_reply reply;
|
|
||||||
|
|
||||||
bool is_unix;
|
|
||||||
} NbdClientSession;
|
|
||||||
|
|
||||||
NbdClientSession *nbd_get_client_session(BlockDriverState *bs);
|
|
||||||
|
|
||||||
int nbd_client_init(BlockDriverState *bs, int sock, const char *export_name,
|
|
||||||
Error **errp);
|
|
||||||
void nbd_client_close(BlockDriverState *bs);
|
|
||||||
|
|
||||||
int nbd_client_co_discard(BlockDriverState *bs, int64_t sector_num,
|
|
||||||
int nb_sectors);
|
|
||||||
int nbd_client_co_flush(BlockDriverState *bs);
|
|
||||||
int nbd_client_co_writev(BlockDriverState *bs, int64_t sector_num,
|
|
||||||
int nb_sectors, QEMUIOVector *qiov);
|
|
||||||
int nbd_client_co_readv(BlockDriverState *bs, int64_t sector_num,
|
|
||||||
int nb_sectors, QEMUIOVector *qiov);
|
|
||||||
|
|
||||||
void nbd_client_detach_aio_context(BlockDriverState *bs);
|
|
||||||
void nbd_client_attach_aio_context(BlockDriverState *bs,
|
|
||||||
AioContext *new_context);
|
|
||||||
|
|
||||||
#endif /* NBD_CLIENT_H */
|
|
||||||
579
block/nbd.c
579
block/nbd.c
@@ -26,32 +26,56 @@
|
|||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "block/nbd-client.h"
|
#include "qemu-common.h"
|
||||||
|
#include "block/nbd.h"
|
||||||
#include "qemu/uri.h"
|
#include "qemu/uri.h"
|
||||||
#include "block/block_int.h"
|
#include "block/block_int.h"
|
||||||
#include "qemu/module.h"
|
#include "qemu/module.h"
|
||||||
#include "qemu/sockets.h"
|
#include "qemu/sockets.h"
|
||||||
#include "qapi/qmp/qdict.h"
|
|
||||||
#include "qapi/qmp/qjson.h"
|
|
||||||
#include "qapi/qmp/qint.h"
|
|
||||||
#include "qapi/qmp/qstring.h"
|
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#define EN_OPTSTR ":exportname="
|
#define EN_OPTSTR ":exportname="
|
||||||
|
|
||||||
|
/* #define DEBUG_NBD */
|
||||||
|
|
||||||
|
#if defined(DEBUG_NBD)
|
||||||
|
#define logout(fmt, ...) \
|
||||||
|
fprintf(stderr, "nbd\t%-24s" fmt, __func__, ##__VA_ARGS__)
|
||||||
|
#else
|
||||||
|
#define logout(fmt, ...) ((void)0)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define MAX_NBD_REQUESTS 16
|
||||||
|
#define HANDLE_TO_INDEX(bs, handle) ((handle) ^ ((uint64_t)(intptr_t)bs))
|
||||||
|
#define INDEX_TO_HANDLE(bs, index) ((index) ^ ((uint64_t)(intptr_t)bs))
|
||||||
|
|
||||||
typedef struct BDRVNBDState {
|
typedef struct BDRVNBDState {
|
||||||
NbdClientSession client;
|
int sock;
|
||||||
|
uint32_t nbdflags;
|
||||||
|
off_t size;
|
||||||
|
size_t blocksize;
|
||||||
|
|
||||||
|
CoMutex send_mutex;
|
||||||
|
CoMutex free_sema;
|
||||||
|
Coroutine *send_coroutine;
|
||||||
|
int in_flight;
|
||||||
|
|
||||||
|
Coroutine *recv_coroutine[MAX_NBD_REQUESTS];
|
||||||
|
struct nbd_reply reply;
|
||||||
|
|
||||||
|
int is_unix;
|
||||||
|
char *host_spec;
|
||||||
|
char *export_name; /* An NBD server may export several devices */
|
||||||
} BDRVNBDState;
|
} BDRVNBDState;
|
||||||
|
|
||||||
static int nbd_parse_uri(const char *filename, QDict *options)
|
static int nbd_parse_uri(BDRVNBDState *s, const char *filename)
|
||||||
{
|
{
|
||||||
URI *uri;
|
URI *uri;
|
||||||
const char *p;
|
const char *p;
|
||||||
QueryParams *qp = NULL;
|
QueryParams *qp = NULL;
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
bool is_unix;
|
|
||||||
|
|
||||||
uri = uri_parse(filename);
|
uri = uri_parse(filename);
|
||||||
if (!uri) {
|
if (!uri) {
|
||||||
@@ -60,11 +84,11 @@ static int nbd_parse_uri(const char *filename, QDict *options)
|
|||||||
|
|
||||||
/* transport */
|
/* transport */
|
||||||
if (!strcmp(uri->scheme, "nbd")) {
|
if (!strcmp(uri->scheme, "nbd")) {
|
||||||
is_unix = false;
|
s->is_unix = false;
|
||||||
} else if (!strcmp(uri->scheme, "nbd+tcp")) {
|
} else if (!strcmp(uri->scheme, "nbd+tcp")) {
|
||||||
is_unix = false;
|
s->is_unix = false;
|
||||||
} else if (!strcmp(uri->scheme, "nbd+unix")) {
|
} else if (!strcmp(uri->scheme, "nbd+unix")) {
|
||||||
is_unix = true;
|
s->is_unix = true;
|
||||||
} else {
|
} else {
|
||||||
ret = -EINVAL;
|
ret = -EINVAL;
|
||||||
goto out;
|
goto out;
|
||||||
@@ -73,44 +97,32 @@ static int nbd_parse_uri(const char *filename, QDict *options)
|
|||||||
p = uri->path ? uri->path : "/";
|
p = uri->path ? uri->path : "/";
|
||||||
p += strspn(p, "/");
|
p += strspn(p, "/");
|
||||||
if (p[0]) {
|
if (p[0]) {
|
||||||
qdict_put(options, "export", qstring_from_str(p));
|
s->export_name = g_strdup(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
qp = query_params_parse(uri->query);
|
qp = query_params_parse(uri->query);
|
||||||
if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) {
|
if (qp->n > 1 || (s->is_unix && !qp->n) || (!s->is_unix && qp->n)) {
|
||||||
ret = -EINVAL;
|
ret = -EINVAL;
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_unix) {
|
if (s->is_unix) {
|
||||||
/* nbd+unix:///export?socket=path */
|
/* nbd+unix:///export?socket=path */
|
||||||
if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) {
|
if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) {
|
||||||
ret = -EINVAL;
|
ret = -EINVAL;
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
qdict_put(options, "path", qstring_from_str(qp->p[0].value));
|
s->host_spec = g_strdup(qp->p[0].value);
|
||||||
} else {
|
} else {
|
||||||
QString *host;
|
/* nbd[+tcp]://host:port/export */
|
||||||
/* nbd[+tcp]://host[:port]/export */
|
|
||||||
if (!uri->server) {
|
if (!uri->server) {
|
||||||
ret = -EINVAL;
|
ret = -EINVAL;
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
if (!uri->port) {
|
||||||
/* strip braces from literal IPv6 address */
|
uri->port = NBD_DEFAULT_PORT;
|
||||||
if (uri->server[0] == '[') {
|
|
||||||
host = qstring_from_substr(uri->server, 1,
|
|
||||||
strlen(uri->server) - 2);
|
|
||||||
} else {
|
|
||||||
host = qstring_from_str(uri->server);
|
|
||||||
}
|
|
||||||
|
|
||||||
qdict_put(options, "host", host);
|
|
||||||
if (uri->port) {
|
|
||||||
char* port_str = g_strdup_printf("%d", uri->port);
|
|
||||||
qdict_put(options, "port", qstring_from_str(port_str));
|
|
||||||
g_free(port_str);
|
|
||||||
}
|
}
|
||||||
|
s->host_spec = g_strdup_printf("%s:%d", uri->server, uri->port);
|
||||||
}
|
}
|
||||||
|
|
||||||
out:
|
out:
|
||||||
@@ -121,29 +133,16 @@ out:
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void nbd_parse_filename(const char *filename, QDict *options,
|
static int nbd_config(BDRVNBDState *s, const char *filename)
|
||||||
Error **errp)
|
|
||||||
{
|
{
|
||||||
char *file;
|
char *file;
|
||||||
char *export_name;
|
char *export_name;
|
||||||
const char *host_spec;
|
const char *host_spec;
|
||||||
const char *unixpath;
|
const char *unixpath;
|
||||||
|
int err = -EINVAL;
|
||||||
if (qdict_haskey(options, "host")
|
|
||||||
|| qdict_haskey(options, "port")
|
|
||||||
|| qdict_haskey(options, "path"))
|
|
||||||
{
|
|
||||||
error_setg(errp, "host/port/path and a file name may not be specified "
|
|
||||||
"at the same time");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strstr(filename, "://")) {
|
if (strstr(filename, "://")) {
|
||||||
int ret = nbd_parse_uri(filename, options);
|
return nbd_parse_uri(s, filename);
|
||||||
if (ret < 0) {
|
|
||||||
error_setg(errp, "No valid URL specified");
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
file = g_strdup(filename);
|
file = g_strdup(filename);
|
||||||
@@ -155,290 +154,450 @@ static void nbd_parse_filename(const char *filename, QDict *options,
|
|||||||
}
|
}
|
||||||
export_name[0] = 0; /* truncate 'file' */
|
export_name[0] = 0; /* truncate 'file' */
|
||||||
export_name += strlen(EN_OPTSTR);
|
export_name += strlen(EN_OPTSTR);
|
||||||
|
s->export_name = g_strdup(export_name);
|
||||||
qdict_put(options, "export", qstring_from_str(export_name));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* extract the host_spec - fail if it's not nbd:... */
|
/* extract the host_spec - fail if it's not nbd:... */
|
||||||
if (!strstart(file, "nbd:", &host_spec)) {
|
if (!strstart(file, "nbd:", &host_spec)) {
|
||||||
error_setg(errp, "File name string for NBD must start with 'nbd:'");
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!*host_spec) {
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* are we a UNIX or TCP socket? */
|
/* are we a UNIX or TCP socket? */
|
||||||
if (strstart(host_spec, "unix:", &unixpath)) {
|
if (strstart(host_spec, "unix:", &unixpath)) {
|
||||||
qdict_put(options, "path", qstring_from_str(unixpath));
|
s->is_unix = true;
|
||||||
|
s->host_spec = g_strdup(unixpath);
|
||||||
} else {
|
} else {
|
||||||
InetSocketAddress *addr = NULL;
|
s->is_unix = false;
|
||||||
|
s->host_spec = g_strdup(host_spec);
|
||||||
addr = inet_parse(host_spec, errp);
|
|
||||||
if (!addr) {
|
|
||||||
goto out;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
qdict_put(options, "host", qstring_from_str(addr->host));
|
err = 0;
|
||||||
qdict_put(options, "port", qstring_from_str(addr->port));
|
|
||||||
qapi_free_InetSocketAddress(addr);
|
|
||||||
}
|
|
||||||
|
|
||||||
out:
|
out:
|
||||||
g_free(file);
|
g_free(file);
|
||||||
|
if (err != 0) {
|
||||||
|
g_free(s->export_name);
|
||||||
|
g_free(s->host_spec);
|
||||||
|
}
|
||||||
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
static SocketAddress *nbd_config(BDRVNBDState *s, QDict *options, char **export,
|
static void nbd_coroutine_start(BDRVNBDState *s, struct nbd_request *request)
|
||||||
Error **errp)
|
|
||||||
{
|
{
|
||||||
SocketAddress *saddr;
|
int i;
|
||||||
|
|
||||||
if (qdict_haskey(options, "path") == qdict_haskey(options, "host")) {
|
/* Poor man semaphore. The free_sema is locked when no other request
|
||||||
if (qdict_haskey(options, "path")) {
|
* can be accepted, and unlocked after receiving one reply. */
|
||||||
error_setg(errp, "path and host may not be used at the same time.");
|
if (s->in_flight >= MAX_NBD_REQUESTS - 1) {
|
||||||
} else {
|
qemu_co_mutex_lock(&s->free_sema);
|
||||||
error_setg(errp, "one of path and host must be specified.");
|
assert(s->in_flight < MAX_NBD_REQUESTS);
|
||||||
|
}
|
||||||
|
s->in_flight++;
|
||||||
|
|
||||||
|
for (i = 0; i < MAX_NBD_REQUESTS; i++) {
|
||||||
|
if (s->recv_coroutine[i] == NULL) {
|
||||||
|
s->recv_coroutine[i] = qemu_coroutine_self();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
saddr = g_new0(SocketAddress, 1);
|
assert(i < MAX_NBD_REQUESTS);
|
||||||
|
request->handle = INDEX_TO_HANDLE(s, i);
|
||||||
if (qdict_haskey(options, "path")) {
|
|
||||||
saddr->type = SOCKET_ADDRESS_KIND_UNIX;
|
|
||||||
saddr->u.q_unix = g_new0(UnixSocketAddress, 1);
|
|
||||||
saddr->u.q_unix->path = g_strdup(qdict_get_str(options, "path"));
|
|
||||||
qdict_del(options, "path");
|
|
||||||
} else {
|
|
||||||
saddr->type = SOCKET_ADDRESS_KIND_INET;
|
|
||||||
saddr->u.inet = g_new0(InetSocketAddress, 1);
|
|
||||||
saddr->u.inet->host = g_strdup(qdict_get_str(options, "host"));
|
|
||||||
if (!qdict_get_try_str(options, "port")) {
|
|
||||||
saddr->u.inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
|
|
||||||
} else {
|
|
||||||
saddr->u.inet->port = g_strdup(qdict_get_str(options, "port"));
|
|
||||||
}
|
|
||||||
qdict_del(options, "host");
|
|
||||||
qdict_del(options, "port");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
s->client.is_unix = saddr->type == SOCKET_ADDRESS_KIND_UNIX;
|
static int nbd_have_request(void *opaque)
|
||||||
|
|
||||||
*export = g_strdup(qdict_get_try_str(options, "export"));
|
|
||||||
if (*export) {
|
|
||||||
qdict_del(options, "export");
|
|
||||||
}
|
|
||||||
|
|
||||||
return saddr;
|
|
||||||
}
|
|
||||||
|
|
||||||
NbdClientSession *nbd_get_client_session(BlockDriverState *bs)
|
|
||||||
{
|
{
|
||||||
BDRVNBDState *s = bs->opaque;
|
BDRVNBDState *s = opaque;
|
||||||
return &s->client;
|
|
||||||
|
return s->in_flight > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int nbd_establish_connection(BlockDriverState *bs,
|
static void nbd_reply_ready(void *opaque)
|
||||||
SocketAddress *saddr,
|
{
|
||||||
Error **errp)
|
BDRVNBDState *s = opaque;
|
||||||
|
uint64_t i;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if (s->reply.handle == 0) {
|
||||||
|
/* No reply already in flight. Fetch a header. It is possible
|
||||||
|
* that another thread has done the same thing in parallel, so
|
||||||
|
* the socket is not readable anymore.
|
||||||
|
*/
|
||||||
|
ret = nbd_receive_reply(s->sock, &s->reply);
|
||||||
|
if (ret == -EAGAIN) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (ret < 0) {
|
||||||
|
s->reply.handle = 0;
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* There's no need for a mutex on the receive side, because the
|
||||||
|
* handler acts as a synchronization point and ensures that only
|
||||||
|
* one coroutine is called until the reply finishes. */
|
||||||
|
i = HANDLE_TO_INDEX(s, s->reply.handle);
|
||||||
|
if (i >= MAX_NBD_REQUESTS) {
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (s->recv_coroutine[i]) {
|
||||||
|
qemu_coroutine_enter(s->recv_coroutine[i], NULL);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fail:
|
||||||
|
for (i = 0; i < MAX_NBD_REQUESTS; i++) {
|
||||||
|
if (s->recv_coroutine[i]) {
|
||||||
|
qemu_coroutine_enter(s->recv_coroutine[i], NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void nbd_restart_write(void *opaque)
|
||||||
|
{
|
||||||
|
BDRVNBDState *s = opaque;
|
||||||
|
qemu_coroutine_enter(s->send_coroutine, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int nbd_co_send_request(BDRVNBDState *s, struct nbd_request *request,
|
||||||
|
QEMUIOVector *qiov, int offset)
|
||||||
|
{
|
||||||
|
int rc, ret;
|
||||||
|
|
||||||
|
qemu_co_mutex_lock(&s->send_mutex);
|
||||||
|
s->send_coroutine = qemu_coroutine_self();
|
||||||
|
qemu_aio_set_fd_handler(s->sock, nbd_reply_ready, nbd_restart_write,
|
||||||
|
nbd_have_request, s);
|
||||||
|
rc = nbd_send_request(s->sock, request);
|
||||||
|
if (rc >= 0 && qiov) {
|
||||||
|
ret = qemu_co_sendv(s->sock, qiov->iov, qiov->niov,
|
||||||
|
offset, request->len);
|
||||||
|
if (ret != request->len) {
|
||||||
|
rc = -EIO;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
qemu_aio_set_fd_handler(s->sock, nbd_reply_ready, NULL,
|
||||||
|
nbd_have_request, s);
|
||||||
|
s->send_coroutine = NULL;
|
||||||
|
qemu_co_mutex_unlock(&s->send_mutex);
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void nbd_co_receive_reply(BDRVNBDState *s, struct nbd_request *request,
|
||||||
|
struct nbd_reply *reply,
|
||||||
|
QEMUIOVector *qiov, int offset)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
/* Wait until we're woken up by the read handler. TODO: perhaps
|
||||||
|
* peek at the next reply and avoid yielding if it's ours? */
|
||||||
|
qemu_coroutine_yield();
|
||||||
|
*reply = s->reply;
|
||||||
|
if (reply->handle != request->handle) {
|
||||||
|
reply->error = EIO;
|
||||||
|
} else {
|
||||||
|
if (qiov && reply->error == 0) {
|
||||||
|
ret = qemu_co_recvv(s->sock, qiov->iov, qiov->niov,
|
||||||
|
offset, request->len);
|
||||||
|
if (ret != request->len) {
|
||||||
|
reply->error = EIO;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Tell the read handler to read another header. */
|
||||||
|
s->reply.handle = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void nbd_coroutine_end(BDRVNBDState *s, struct nbd_request *request)
|
||||||
|
{
|
||||||
|
int i = HANDLE_TO_INDEX(s, request->handle);
|
||||||
|
s->recv_coroutine[i] = NULL;
|
||||||
|
if (s->in_flight-- == MAX_NBD_REQUESTS) {
|
||||||
|
qemu_co_mutex_unlock(&s->free_sema);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int nbd_establish_connection(BlockDriverState *bs)
|
||||||
{
|
{
|
||||||
BDRVNBDState *s = bs->opaque;
|
BDRVNBDState *s = bs->opaque;
|
||||||
int sock;
|
int sock;
|
||||||
|
int ret;
|
||||||
|
off_t size;
|
||||||
|
size_t blocksize;
|
||||||
|
|
||||||
sock = socket_connect(saddr, errp, NULL, NULL);
|
if (s->is_unix) {
|
||||||
|
sock = unix_socket_outgoing(s->host_spec);
|
||||||
|
} else {
|
||||||
|
sock = tcp_socket_outgoing_spec(s->host_spec);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Failed to establish connection */
|
||||||
if (sock < 0) {
|
if (sock < 0) {
|
||||||
logout("Failed to establish connection to NBD server\n");
|
logout("Failed to establish connection to NBD server\n");
|
||||||
return -EIO;
|
return -errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!s->client.is_unix) {
|
/* NBD handshake */
|
||||||
socket_set_nodelay(sock);
|
ret = nbd_receive_negotiate(sock, s->export_name, &s->nbdflags, &size,
|
||||||
|
&blocksize);
|
||||||
|
if (ret < 0) {
|
||||||
|
logout("Failed to negotiate with the NBD server\n");
|
||||||
|
closesocket(sock);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
return sock;
|
/* Now that we're connected, set the socket to be non-blocking and
|
||||||
|
* kick the reply mechanism. */
|
||||||
|
qemu_set_nonblock(sock);
|
||||||
|
qemu_aio_set_fd_handler(sock, nbd_reply_ready, NULL,
|
||||||
|
nbd_have_request, s);
|
||||||
|
|
||||||
|
s->sock = sock;
|
||||||
|
s->size = size;
|
||||||
|
s->blocksize = blocksize;
|
||||||
|
|
||||||
|
logout("Established connection with NBD server\n");
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int nbd_open(BlockDriverState *bs, QDict *options, int flags,
|
static void nbd_teardown_connection(BlockDriverState *bs)
|
||||||
Error **errp)
|
|
||||||
{
|
{
|
||||||
BDRVNBDState *s = bs->opaque;
|
BDRVNBDState *s = bs->opaque;
|
||||||
char *export = NULL;
|
struct nbd_request request;
|
||||||
int result, sock;
|
|
||||||
SocketAddress *saddr;
|
request.type = NBD_CMD_DISC;
|
||||||
|
request.from = 0;
|
||||||
|
request.len = 0;
|
||||||
|
nbd_send_request(s->sock, &request);
|
||||||
|
|
||||||
|
qemu_aio_set_fd_handler(s->sock, NULL, NULL, NULL, NULL);
|
||||||
|
closesocket(s->sock);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int nbd_open(BlockDriverState *bs, const char* filename, int flags)
|
||||||
|
{
|
||||||
|
BDRVNBDState *s = bs->opaque;
|
||||||
|
int result;
|
||||||
|
|
||||||
|
qemu_co_mutex_init(&s->send_mutex);
|
||||||
|
qemu_co_mutex_init(&s->free_sema);
|
||||||
|
|
||||||
/* Pop the config into our state object. Exit if invalid. */
|
/* Pop the config into our state object. Exit if invalid. */
|
||||||
saddr = nbd_config(s, options, &export, errp);
|
result = nbd_config(s, filename);
|
||||||
if (!saddr) {
|
if (result != 0) {
|
||||||
return -EINVAL;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* establish TCP connection, return error if it fails
|
/* establish TCP connection, return error if it fails
|
||||||
* TODO: Configurable retry-until-timeout behaviour.
|
* TODO: Configurable retry-until-timeout behaviour.
|
||||||
*/
|
*/
|
||||||
sock = nbd_establish_connection(bs, saddr, errp);
|
result = nbd_establish_connection(bs);
|
||||||
qapi_free_SocketAddress(saddr);
|
|
||||||
if (sock < 0) {
|
|
||||||
g_free(export);
|
|
||||||
return sock;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* NBD handshake */
|
|
||||||
result = nbd_client_init(bs, sock, export, errp);
|
|
||||||
g_free(export);
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int nbd_co_readv_1(BlockDriverState *bs, int64_t sector_num,
|
||||||
|
int nb_sectors, QEMUIOVector *qiov,
|
||||||
|
int offset)
|
||||||
|
{
|
||||||
|
BDRVNBDState *s = bs->opaque;
|
||||||
|
struct nbd_request request;
|
||||||
|
struct nbd_reply reply;
|
||||||
|
ssize_t ret;
|
||||||
|
|
||||||
|
request.type = NBD_CMD_READ;
|
||||||
|
request.from = sector_num * 512;
|
||||||
|
request.len = nb_sectors * 512;
|
||||||
|
|
||||||
|
nbd_coroutine_start(s, &request);
|
||||||
|
ret = nbd_co_send_request(s, &request, NULL, 0);
|
||||||
|
if (ret < 0) {
|
||||||
|
reply.error = -ret;
|
||||||
|
} else {
|
||||||
|
nbd_co_receive_reply(s, &request, &reply, qiov, offset);
|
||||||
|
}
|
||||||
|
nbd_coroutine_end(s, &request);
|
||||||
|
return -reply.error;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static int nbd_co_writev_1(BlockDriverState *bs, int64_t sector_num,
|
||||||
|
int nb_sectors, QEMUIOVector *qiov,
|
||||||
|
int offset)
|
||||||
|
{
|
||||||
|
BDRVNBDState *s = bs->opaque;
|
||||||
|
struct nbd_request request;
|
||||||
|
struct nbd_reply reply;
|
||||||
|
ssize_t ret;
|
||||||
|
|
||||||
|
request.type = NBD_CMD_WRITE;
|
||||||
|
if (!bdrv_enable_write_cache(bs) && (s->nbdflags & NBD_FLAG_SEND_FUA)) {
|
||||||
|
request.type |= NBD_CMD_FLAG_FUA;
|
||||||
|
}
|
||||||
|
|
||||||
|
request.from = sector_num * 512;
|
||||||
|
request.len = nb_sectors * 512;
|
||||||
|
|
||||||
|
nbd_coroutine_start(s, &request);
|
||||||
|
ret = nbd_co_send_request(s, &request, qiov, offset);
|
||||||
|
if (ret < 0) {
|
||||||
|
reply.error = -ret;
|
||||||
|
} else {
|
||||||
|
nbd_co_receive_reply(s, &request, &reply, NULL, 0);
|
||||||
|
}
|
||||||
|
nbd_coroutine_end(s, &request);
|
||||||
|
return -reply.error;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* qemu-nbd has a limit of slightly less than 1M per request. Try to
|
||||||
|
* remain aligned to 4K. */
|
||||||
|
#define NBD_MAX_SECTORS 2040
|
||||||
|
|
||||||
static int nbd_co_readv(BlockDriverState *bs, int64_t sector_num,
|
static int nbd_co_readv(BlockDriverState *bs, int64_t sector_num,
|
||||||
int nb_sectors, QEMUIOVector *qiov)
|
int nb_sectors, QEMUIOVector *qiov)
|
||||||
{
|
{
|
||||||
return nbd_client_co_readv(bs, sector_num, nb_sectors, qiov);
|
int offset = 0;
|
||||||
|
int ret;
|
||||||
|
while (nb_sectors > NBD_MAX_SECTORS) {
|
||||||
|
ret = nbd_co_readv_1(bs, sector_num, NBD_MAX_SECTORS, qiov, offset);
|
||||||
|
if (ret < 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
offset += NBD_MAX_SECTORS * 512;
|
||||||
|
sector_num += NBD_MAX_SECTORS;
|
||||||
|
nb_sectors -= NBD_MAX_SECTORS;
|
||||||
|
}
|
||||||
|
return nbd_co_readv_1(bs, sector_num, nb_sectors, qiov, offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int nbd_co_writev(BlockDriverState *bs, int64_t sector_num,
|
static int nbd_co_writev(BlockDriverState *bs, int64_t sector_num,
|
||||||
int nb_sectors, QEMUIOVector *qiov)
|
int nb_sectors, QEMUIOVector *qiov)
|
||||||
{
|
{
|
||||||
return nbd_client_co_writev(bs, sector_num, nb_sectors, qiov);
|
int offset = 0;
|
||||||
|
int ret;
|
||||||
|
while (nb_sectors > NBD_MAX_SECTORS) {
|
||||||
|
ret = nbd_co_writev_1(bs, sector_num, NBD_MAX_SECTORS, qiov, offset);
|
||||||
|
if (ret < 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
offset += NBD_MAX_SECTORS * 512;
|
||||||
|
sector_num += NBD_MAX_SECTORS;
|
||||||
|
nb_sectors -= NBD_MAX_SECTORS;
|
||||||
|
}
|
||||||
|
return nbd_co_writev_1(bs, sector_num, nb_sectors, qiov, offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int nbd_co_flush(BlockDriverState *bs)
|
static int nbd_co_flush(BlockDriverState *bs)
|
||||||
{
|
{
|
||||||
return nbd_client_co_flush(bs);
|
BDRVNBDState *s = bs->opaque;
|
||||||
|
struct nbd_request request;
|
||||||
|
struct nbd_reply reply;
|
||||||
|
ssize_t ret;
|
||||||
|
|
||||||
|
if (!(s->nbdflags & NBD_FLAG_SEND_FLUSH)) {
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void nbd_refresh_limits(BlockDriverState *bs, Error **errp)
|
request.type = NBD_CMD_FLUSH;
|
||||||
{
|
if (s->nbdflags & NBD_FLAG_SEND_FUA) {
|
||||||
bs->bl.max_discard = UINT32_MAX >> BDRV_SECTOR_BITS;
|
request.type |= NBD_CMD_FLAG_FUA;
|
||||||
bs->bl.max_transfer_length = UINT32_MAX >> BDRV_SECTOR_BITS;
|
}
|
||||||
|
|
||||||
|
request.from = 0;
|
||||||
|
request.len = 0;
|
||||||
|
|
||||||
|
nbd_coroutine_start(s, &request);
|
||||||
|
ret = nbd_co_send_request(s, &request, NULL, 0);
|
||||||
|
if (ret < 0) {
|
||||||
|
reply.error = -ret;
|
||||||
|
} else {
|
||||||
|
nbd_co_receive_reply(s, &request, &reply, NULL, 0);
|
||||||
|
}
|
||||||
|
nbd_coroutine_end(s, &request);
|
||||||
|
return -reply.error;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int nbd_co_discard(BlockDriverState *bs, int64_t sector_num,
|
static int nbd_co_discard(BlockDriverState *bs, int64_t sector_num,
|
||||||
int nb_sectors)
|
int nb_sectors)
|
||||||
{
|
{
|
||||||
return nbd_client_co_discard(bs, sector_num, nb_sectors);
|
BDRVNBDState *s = bs->opaque;
|
||||||
|
struct nbd_request request;
|
||||||
|
struct nbd_reply reply;
|
||||||
|
ssize_t ret;
|
||||||
|
|
||||||
|
if (!(s->nbdflags & NBD_FLAG_SEND_TRIM)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
request.type = NBD_CMD_TRIM;
|
||||||
|
request.from = sector_num * 512;;
|
||||||
|
request.len = nb_sectors * 512;
|
||||||
|
|
||||||
|
nbd_coroutine_start(s, &request);
|
||||||
|
ret = nbd_co_send_request(s, &request, NULL, 0);
|
||||||
|
if (ret < 0) {
|
||||||
|
reply.error = -ret;
|
||||||
|
} else {
|
||||||
|
nbd_co_receive_reply(s, &request, &reply, NULL, 0);
|
||||||
|
}
|
||||||
|
nbd_coroutine_end(s, &request);
|
||||||
|
return -reply.error;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void nbd_close(BlockDriverState *bs)
|
static void nbd_close(BlockDriverState *bs)
|
||||||
{
|
{
|
||||||
nbd_client_close(bs);
|
BDRVNBDState *s = bs->opaque;
|
||||||
|
g_free(s->export_name);
|
||||||
|
g_free(s->host_spec);
|
||||||
|
|
||||||
|
nbd_teardown_connection(bs);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int64_t nbd_getlength(BlockDriverState *bs)
|
static int64_t nbd_getlength(BlockDriverState *bs)
|
||||||
{
|
{
|
||||||
BDRVNBDState *s = bs->opaque;
|
BDRVNBDState *s = bs->opaque;
|
||||||
|
|
||||||
return s->client.size;
|
return s->size;
|
||||||
}
|
|
||||||
|
|
||||||
static void nbd_detach_aio_context(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
nbd_client_detach_aio_context(bs);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nbd_attach_aio_context(BlockDriverState *bs,
|
|
||||||
AioContext *new_context)
|
|
||||||
{
|
|
||||||
nbd_client_attach_aio_context(bs, new_context);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nbd_refresh_filename(BlockDriverState *bs, QDict *options)
|
|
||||||
{
|
|
||||||
QDict *opts = qdict_new();
|
|
||||||
const char *path = qdict_get_try_str(options, "path");
|
|
||||||
const char *host = qdict_get_try_str(options, "host");
|
|
||||||
const char *port = qdict_get_try_str(options, "port");
|
|
||||||
const char *export = qdict_get_try_str(options, "export");
|
|
||||||
|
|
||||||
qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str("nbd")));
|
|
||||||
|
|
||||||
if (path && export) {
|
|
||||||
snprintf(bs->exact_filename, sizeof(bs->exact_filename),
|
|
||||||
"nbd+unix:///%s?socket=%s", export, path);
|
|
||||||
} else if (path && !export) {
|
|
||||||
snprintf(bs->exact_filename, sizeof(bs->exact_filename),
|
|
||||||
"nbd+unix://?socket=%s", path);
|
|
||||||
} else if (!path && export && port) {
|
|
||||||
snprintf(bs->exact_filename, sizeof(bs->exact_filename),
|
|
||||||
"nbd://%s:%s/%s", host, port, export);
|
|
||||||
} else if (!path && export && !port) {
|
|
||||||
snprintf(bs->exact_filename, sizeof(bs->exact_filename),
|
|
||||||
"nbd://%s/%s", host, export);
|
|
||||||
} else if (!path && !export && port) {
|
|
||||||
snprintf(bs->exact_filename, sizeof(bs->exact_filename),
|
|
||||||
"nbd://%s:%s", host, port);
|
|
||||||
} else if (!path && !export && !port) {
|
|
||||||
snprintf(bs->exact_filename, sizeof(bs->exact_filename),
|
|
||||||
"nbd://%s", host);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (path) {
|
|
||||||
qdict_put_obj(opts, "path", QOBJECT(qstring_from_str(path)));
|
|
||||||
} else if (port) {
|
|
||||||
qdict_put_obj(opts, "host", QOBJECT(qstring_from_str(host)));
|
|
||||||
qdict_put_obj(opts, "port", QOBJECT(qstring_from_str(port)));
|
|
||||||
} else {
|
|
||||||
qdict_put_obj(opts, "host", QOBJECT(qstring_from_str(host)));
|
|
||||||
}
|
|
||||||
if (export) {
|
|
||||||
qdict_put_obj(opts, "export", QOBJECT(qstring_from_str(export)));
|
|
||||||
}
|
|
||||||
|
|
||||||
bs->full_open_options = opts;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static BlockDriver bdrv_nbd = {
|
static BlockDriver bdrv_nbd = {
|
||||||
.format_name = "nbd",
|
.format_name = "nbd",
|
||||||
.protocol_name = "nbd",
|
.protocol_name = "nbd",
|
||||||
.instance_size = sizeof(BDRVNBDState),
|
.instance_size = sizeof(BDRVNBDState),
|
||||||
.bdrv_parse_filename = nbd_parse_filename,
|
|
||||||
.bdrv_file_open = nbd_open,
|
.bdrv_file_open = nbd_open,
|
||||||
.bdrv_co_readv = nbd_co_readv,
|
.bdrv_co_readv = nbd_co_readv,
|
||||||
.bdrv_co_writev = nbd_co_writev,
|
.bdrv_co_writev = nbd_co_writev,
|
||||||
.bdrv_close = nbd_close,
|
.bdrv_close = nbd_close,
|
||||||
.bdrv_co_flush_to_os = nbd_co_flush,
|
.bdrv_co_flush_to_os = nbd_co_flush,
|
||||||
.bdrv_co_discard = nbd_co_discard,
|
.bdrv_co_discard = nbd_co_discard,
|
||||||
.bdrv_refresh_limits = nbd_refresh_limits,
|
|
||||||
.bdrv_getlength = nbd_getlength,
|
.bdrv_getlength = nbd_getlength,
|
||||||
.bdrv_detach_aio_context = nbd_detach_aio_context,
|
|
||||||
.bdrv_attach_aio_context = nbd_attach_aio_context,
|
|
||||||
.bdrv_refresh_filename = nbd_refresh_filename,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static BlockDriver bdrv_nbd_tcp = {
|
static BlockDriver bdrv_nbd_tcp = {
|
||||||
.format_name = "nbd",
|
.format_name = "nbd",
|
||||||
.protocol_name = "nbd+tcp",
|
.protocol_name = "nbd+tcp",
|
||||||
.instance_size = sizeof(BDRVNBDState),
|
.instance_size = sizeof(BDRVNBDState),
|
||||||
.bdrv_parse_filename = nbd_parse_filename,
|
|
||||||
.bdrv_file_open = nbd_open,
|
.bdrv_file_open = nbd_open,
|
||||||
.bdrv_co_readv = nbd_co_readv,
|
.bdrv_co_readv = nbd_co_readv,
|
||||||
.bdrv_co_writev = nbd_co_writev,
|
.bdrv_co_writev = nbd_co_writev,
|
||||||
.bdrv_close = nbd_close,
|
.bdrv_close = nbd_close,
|
||||||
.bdrv_co_flush_to_os = nbd_co_flush,
|
.bdrv_co_flush_to_os = nbd_co_flush,
|
||||||
.bdrv_co_discard = nbd_co_discard,
|
.bdrv_co_discard = nbd_co_discard,
|
||||||
.bdrv_refresh_limits = nbd_refresh_limits,
|
|
||||||
.bdrv_getlength = nbd_getlength,
|
.bdrv_getlength = nbd_getlength,
|
||||||
.bdrv_detach_aio_context = nbd_detach_aio_context,
|
|
||||||
.bdrv_attach_aio_context = nbd_attach_aio_context,
|
|
||||||
.bdrv_refresh_filename = nbd_refresh_filename,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static BlockDriver bdrv_nbd_unix = {
|
static BlockDriver bdrv_nbd_unix = {
|
||||||
.format_name = "nbd",
|
.format_name = "nbd",
|
||||||
.protocol_name = "nbd+unix",
|
.protocol_name = "nbd+unix",
|
||||||
.instance_size = sizeof(BDRVNBDState),
|
.instance_size = sizeof(BDRVNBDState),
|
||||||
.bdrv_parse_filename = nbd_parse_filename,
|
|
||||||
.bdrv_file_open = nbd_open,
|
.bdrv_file_open = nbd_open,
|
||||||
.bdrv_co_readv = nbd_co_readv,
|
.bdrv_co_readv = nbd_co_readv,
|
||||||
.bdrv_co_writev = nbd_co_writev,
|
.bdrv_co_writev = nbd_co_writev,
|
||||||
.bdrv_close = nbd_close,
|
.bdrv_close = nbd_close,
|
||||||
.bdrv_co_flush_to_os = nbd_co_flush,
|
.bdrv_co_flush_to_os = nbd_co_flush,
|
||||||
.bdrv_co_discard = nbd_co_discard,
|
.bdrv_co_discard = nbd_co_discard,
|
||||||
.bdrv_refresh_limits = nbd_refresh_limits,
|
|
||||||
.bdrv_getlength = nbd_getlength,
|
.bdrv_getlength = nbd_getlength,
|
||||||
.bdrv_detach_aio_context = nbd_detach_aio_context,
|
|
||||||
.bdrv_attach_aio_context = nbd_attach_aio_context,
|
|
||||||
.bdrv_refresh_filename = nbd_refresh_filename,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static void bdrv_nbd_init(void)
|
static void bdrv_nbd_init(void)
|
||||||
|
|||||||
549
block/nfs.c
549
block/nfs.c
@@ -1,549 +0,0 @@
|
|||||||
/*
|
|
||||||
* QEMU Block driver for native access to files on NFS shares
|
|
||||||
*
|
|
||||||
* Copyright (c) 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
|
|
||||||
* 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 "config-host.h"
|
|
||||||
|
|
||||||
#include <poll.h>
|
|
||||||
#include "qemu-common.h"
|
|
||||||
#include "qemu/config-file.h"
|
|
||||||
#include "qemu/error-report.h"
|
|
||||||
#include "block/block_int.h"
|
|
||||||
#include "trace.h"
|
|
||||||
#include "qemu/iov.h"
|
|
||||||
#include "qemu/uri.h"
|
|
||||||
#include "sysemu/sysemu.h"
|
|
||||||
#include <nfsc/libnfs.h>
|
|
||||||
|
|
||||||
#define QEMU_NFS_MAX_READAHEAD_SIZE 1048576
|
|
||||||
|
|
||||||
typedef struct NFSClient {
|
|
||||||
struct nfs_context *context;
|
|
||||||
struct nfsfh *fh;
|
|
||||||
int events;
|
|
||||||
bool has_zero_init;
|
|
||||||
AioContext *aio_context;
|
|
||||||
blkcnt_t st_blocks;
|
|
||||||
} NFSClient;
|
|
||||||
|
|
||||||
typedef struct NFSRPC {
|
|
||||||
int ret;
|
|
||||||
int complete;
|
|
||||||
QEMUIOVector *iov;
|
|
||||||
struct stat *st;
|
|
||||||
Coroutine *co;
|
|
||||||
QEMUBH *bh;
|
|
||||||
NFSClient *client;
|
|
||||||
} NFSRPC;
|
|
||||||
|
|
||||||
static void nfs_process_read(void *arg);
|
|
||||||
static void nfs_process_write(void *arg);
|
|
||||||
|
|
||||||
static void nfs_set_events(NFSClient *client)
|
|
||||||
{
|
|
||||||
int ev = nfs_which_events(client->context);
|
|
||||||
if (ev != client->events) {
|
|
||||||
aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
|
|
||||||
false,
|
|
||||||
(ev & POLLIN) ? nfs_process_read : NULL,
|
|
||||||
(ev & POLLOUT) ? nfs_process_write : NULL, client);
|
|
||||||
|
|
||||||
}
|
|
||||||
client->events = ev;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nfs_process_read(void *arg)
|
|
||||||
{
|
|
||||||
NFSClient *client = arg;
|
|
||||||
nfs_service(client->context, POLLIN);
|
|
||||||
nfs_set_events(client);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nfs_process_write(void *arg)
|
|
||||||
{
|
|
||||||
NFSClient *client = arg;
|
|
||||||
nfs_service(client->context, POLLOUT);
|
|
||||||
nfs_set_events(client);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nfs_co_init_task(NFSClient *client, NFSRPC *task)
|
|
||||||
{
|
|
||||||
*task = (NFSRPC) {
|
|
||||||
.co = qemu_coroutine_self(),
|
|
||||||
.client = client,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nfs_co_generic_bh_cb(void *opaque)
|
|
||||||
{
|
|
||||||
NFSRPC *task = opaque;
|
|
||||||
task->complete = 1;
|
|
||||||
qemu_bh_delete(task->bh);
|
|
||||||
qemu_coroutine_enter(task->co, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
nfs_co_generic_cb(int ret, struct nfs_context *nfs, void *data,
|
|
||||||
void *private_data)
|
|
||||||
{
|
|
||||||
NFSRPC *task = private_data;
|
|
||||||
task->ret = ret;
|
|
||||||
if (task->ret > 0 && task->iov) {
|
|
||||||
if (task->ret <= task->iov->size) {
|
|
||||||
qemu_iovec_from_buf(task->iov, 0, data, task->ret);
|
|
||||||
} else {
|
|
||||||
task->ret = -EIO;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (task->ret == 0 && task->st) {
|
|
||||||
memcpy(task->st, data, sizeof(struct stat));
|
|
||||||
}
|
|
||||||
if (task->ret < 0) {
|
|
||||||
error_report("NFS Error: %s", nfs_get_error(nfs));
|
|
||||||
}
|
|
||||||
if (task->co) {
|
|
||||||
task->bh = aio_bh_new(task->client->aio_context,
|
|
||||||
nfs_co_generic_bh_cb, task);
|
|
||||||
qemu_bh_schedule(task->bh);
|
|
||||||
} else {
|
|
||||||
task->complete = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static int coroutine_fn nfs_co_readv(BlockDriverState *bs,
|
|
||||||
int64_t sector_num, int nb_sectors,
|
|
||||||
QEMUIOVector *iov)
|
|
||||||
{
|
|
||||||
NFSClient *client = bs->opaque;
|
|
||||||
NFSRPC task;
|
|
||||||
|
|
||||||
nfs_co_init_task(client, &task);
|
|
||||||
task.iov = iov;
|
|
||||||
|
|
||||||
if (nfs_pread_async(client->context, client->fh,
|
|
||||||
sector_num * BDRV_SECTOR_SIZE,
|
|
||||||
nb_sectors * BDRV_SECTOR_SIZE,
|
|
||||||
nfs_co_generic_cb, &task) != 0) {
|
|
||||||
return -ENOMEM;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (!task.complete) {
|
|
||||||
nfs_set_events(client);
|
|
||||||
qemu_coroutine_yield();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (task.ret < 0) {
|
|
||||||
return task.ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* zero pad short reads */
|
|
||||||
if (task.ret < iov->size) {
|
|
||||||
qemu_iovec_memset(iov, task.ret, 0, iov->size - task.ret);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int coroutine_fn nfs_co_writev(BlockDriverState *bs,
|
|
||||||
int64_t sector_num, int nb_sectors,
|
|
||||||
QEMUIOVector *iov)
|
|
||||||
{
|
|
||||||
NFSClient *client = bs->opaque;
|
|
||||||
NFSRPC task;
|
|
||||||
char *buf = NULL;
|
|
||||||
|
|
||||||
nfs_co_init_task(client, &task);
|
|
||||||
|
|
||||||
buf = g_try_malloc(nb_sectors * BDRV_SECTOR_SIZE);
|
|
||||||
if (nb_sectors && buf == NULL) {
|
|
||||||
return -ENOMEM;
|
|
||||||
}
|
|
||||||
|
|
||||||
qemu_iovec_to_buf(iov, 0, buf, nb_sectors * BDRV_SECTOR_SIZE);
|
|
||||||
|
|
||||||
if (nfs_pwrite_async(client->context, client->fh,
|
|
||||||
sector_num * BDRV_SECTOR_SIZE,
|
|
||||||
nb_sectors * BDRV_SECTOR_SIZE,
|
|
||||||
buf, nfs_co_generic_cb, &task) != 0) {
|
|
||||||
g_free(buf);
|
|
||||||
return -ENOMEM;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (!task.complete) {
|
|
||||||
nfs_set_events(client);
|
|
||||||
qemu_coroutine_yield();
|
|
||||||
}
|
|
||||||
|
|
||||||
g_free(buf);
|
|
||||||
|
|
||||||
if (task.ret != nb_sectors * BDRV_SECTOR_SIZE) {
|
|
||||||
return task.ret < 0 ? task.ret : -EIO;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int coroutine_fn nfs_co_flush(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
NFSClient *client = bs->opaque;
|
|
||||||
NFSRPC task;
|
|
||||||
|
|
||||||
nfs_co_init_task(client, &task);
|
|
||||||
|
|
||||||
if (nfs_fsync_async(client->context, client->fh, nfs_co_generic_cb,
|
|
||||||
&task) != 0) {
|
|
||||||
return -ENOMEM;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (!task.complete) {
|
|
||||||
nfs_set_events(client);
|
|
||||||
qemu_coroutine_yield();
|
|
||||||
}
|
|
||||||
|
|
||||||
return task.ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* TODO Convert to fine grained options */
|
|
||||||
static QemuOptsList runtime_opts = {
|
|
||||||
.name = "nfs",
|
|
||||||
.head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
|
|
||||||
.desc = {
|
|
||||||
{
|
|
||||||
.name = "filename",
|
|
||||||
.type = QEMU_OPT_STRING,
|
|
||||||
.help = "URL to the NFS file",
|
|
||||||
},
|
|
||||||
{ /* end of list */ }
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
static void nfs_detach_aio_context(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
NFSClient *client = bs->opaque;
|
|
||||||
|
|
||||||
aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
|
|
||||||
false, NULL, NULL, NULL);
|
|
||||||
client->events = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nfs_attach_aio_context(BlockDriverState *bs,
|
|
||||||
AioContext *new_context)
|
|
||||||
{
|
|
||||||
NFSClient *client = bs->opaque;
|
|
||||||
|
|
||||||
client->aio_context = new_context;
|
|
||||||
nfs_set_events(client);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nfs_client_close(NFSClient *client)
|
|
||||||
{
|
|
||||||
if (client->context) {
|
|
||||||
if (client->fh) {
|
|
||||||
nfs_close(client->context, client->fh);
|
|
||||||
}
|
|
||||||
aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
|
|
||||||
false, NULL, NULL, NULL);
|
|
||||||
nfs_destroy_context(client->context);
|
|
||||||
}
|
|
||||||
memset(client, 0, sizeof(NFSClient));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nfs_file_close(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
NFSClient *client = bs->opaque;
|
|
||||||
nfs_client_close(client);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int64_t nfs_client_open(NFSClient *client, const char *filename,
|
|
||||||
int flags, Error **errp)
|
|
||||||
{
|
|
||||||
int ret = -EINVAL, i;
|
|
||||||
struct stat st;
|
|
||||||
URI *uri;
|
|
||||||
QueryParams *qp = NULL;
|
|
||||||
char *file = NULL, *strp = NULL;
|
|
||||||
|
|
||||||
uri = uri_parse(filename);
|
|
||||||
if (!uri) {
|
|
||||||
error_setg(errp, "Invalid URL specified");
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
if (!uri->server) {
|
|
||||||
error_setg(errp, "Invalid URL specified");
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
strp = strrchr(uri->path, '/');
|
|
||||||
if (strp == NULL) {
|
|
||||||
error_setg(errp, "Invalid URL specified");
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
file = g_strdup(strp);
|
|
||||||
*strp = 0;
|
|
||||||
|
|
||||||
client->context = nfs_init_context();
|
|
||||||
if (client->context == NULL) {
|
|
||||||
error_setg(errp, "Failed to init NFS context");
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
qp = query_params_parse(uri->query);
|
|
||||||
for (i = 0; i < qp->n; i++) {
|
|
||||||
unsigned long long val;
|
|
||||||
if (!qp->p[i].value) {
|
|
||||||
error_setg(errp, "Value for NFS parameter expected: %s",
|
|
||||||
qp->p[i].name);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
if (parse_uint_full(qp->p[i].value, &val, 0)) {
|
|
||||||
error_setg(errp, "Illegal value for NFS parameter: %s",
|
|
||||||
qp->p[i].name);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
if (!strcmp(qp->p[i].name, "uid")) {
|
|
||||||
nfs_set_uid(client->context, val);
|
|
||||||
} else if (!strcmp(qp->p[i].name, "gid")) {
|
|
||||||
nfs_set_gid(client->context, val);
|
|
||||||
} else if (!strcmp(qp->p[i].name, "tcp-syncnt")) {
|
|
||||||
nfs_set_tcp_syncnt(client->context, val);
|
|
||||||
#ifdef LIBNFS_FEATURE_READAHEAD
|
|
||||||
} else if (!strcmp(qp->p[i].name, "readahead")) {
|
|
||||||
if (val > QEMU_NFS_MAX_READAHEAD_SIZE) {
|
|
||||||
error_report("NFS Warning: Truncating NFS readahead"
|
|
||||||
" size to %d", QEMU_NFS_MAX_READAHEAD_SIZE);
|
|
||||||
val = QEMU_NFS_MAX_READAHEAD_SIZE;
|
|
||||||
}
|
|
||||||
nfs_set_readahead(client->context, val);
|
|
||||||
#endif
|
|
||||||
} else {
|
|
||||||
error_setg(errp, "Unknown NFS parameter name: %s",
|
|
||||||
qp->p[i].name);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = nfs_mount(client->context, uri->server, uri->path);
|
|
||||||
if (ret < 0) {
|
|
||||||
error_setg(errp, "Failed to mount nfs share: %s",
|
|
||||||
nfs_get_error(client->context));
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (flags & O_CREAT) {
|
|
||||||
ret = nfs_creat(client->context, file, 0600, &client->fh);
|
|
||||||
if (ret < 0) {
|
|
||||||
error_setg(errp, "Failed to create file: %s",
|
|
||||||
nfs_get_error(client->context));
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ret = nfs_open(client->context, file, flags, &client->fh);
|
|
||||||
if (ret < 0) {
|
|
||||||
error_setg(errp, "Failed to open file : %s",
|
|
||||||
nfs_get_error(client->context));
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = nfs_fstat(client->context, client->fh, &st);
|
|
||||||
if (ret < 0) {
|
|
||||||
error_setg(errp, "Failed to fstat file: %s",
|
|
||||||
nfs_get_error(client->context));
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = DIV_ROUND_UP(st.st_size, BDRV_SECTOR_SIZE);
|
|
||||||
client->st_blocks = st.st_blocks;
|
|
||||||
client->has_zero_init = S_ISREG(st.st_mode);
|
|
||||||
goto out;
|
|
||||||
fail:
|
|
||||||
nfs_client_close(client);
|
|
||||||
out:
|
|
||||||
if (qp) {
|
|
||||||
query_params_free(qp);
|
|
||||||
}
|
|
||||||
uri_free(uri);
|
|
||||||
g_free(file);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags,
|
|
||||||
Error **errp) {
|
|
||||||
NFSClient *client = bs->opaque;
|
|
||||||
int64_t ret;
|
|
||||||
QemuOpts *opts;
|
|
||||||
Error *local_err = NULL;
|
|
||||||
|
|
||||||
client->aio_context = bdrv_get_aio_context(bs);
|
|
||||||
|
|
||||||
opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
|
|
||||||
qemu_opts_absorb_qdict(opts, options, &local_err);
|
|
||||||
if (local_err) {
|
|
||||||
error_propagate(errp, local_err);
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
ret = nfs_client_open(client, qemu_opt_get(opts, "filename"),
|
|
||||||
(flags & BDRV_O_RDWR) ? O_RDWR : O_RDONLY,
|
|
||||||
errp);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
bs->total_sectors = ret;
|
|
||||||
ret = 0;
|
|
||||||
out:
|
|
||||||
qemu_opts_del(opts);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static QemuOptsList nfs_create_opts = {
|
|
||||||
.name = "nfs-create-opts",
|
|
||||||
.head = QTAILQ_HEAD_INITIALIZER(nfs_create_opts.head),
|
|
||||||
.desc = {
|
|
||||||
{
|
|
||||||
.name = BLOCK_OPT_SIZE,
|
|
||||||
.type = QEMU_OPT_SIZE,
|
|
||||||
.help = "Virtual disk size"
|
|
||||||
},
|
|
||||||
{ /* end of list */ }
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
static int nfs_file_create(const char *url, QemuOpts *opts, Error **errp)
|
|
||||||
{
|
|
||||||
int ret = 0;
|
|
||||||
int64_t total_size = 0;
|
|
||||||
NFSClient *client = g_new0(NFSClient, 1);
|
|
||||||
|
|
||||||
client->aio_context = qemu_get_aio_context();
|
|
||||||
|
|
||||||
/* Read out options */
|
|
||||||
total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
|
|
||||||
BDRV_SECTOR_SIZE);
|
|
||||||
|
|
||||||
ret = nfs_client_open(client, url, O_CREAT, errp);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
ret = nfs_ftruncate(client->context, client->fh, total_size);
|
|
||||||
nfs_client_close(client);
|
|
||||||
out:
|
|
||||||
g_free(client);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int nfs_has_zero_init(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
NFSClient *client = bs->opaque;
|
|
||||||
return client->has_zero_init;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int64_t nfs_get_allocated_file_size(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
NFSClient *client = bs->opaque;
|
|
||||||
NFSRPC task = {0};
|
|
||||||
struct stat st;
|
|
||||||
|
|
||||||
if (bdrv_is_read_only(bs) &&
|
|
||||||
!(bs->open_flags & BDRV_O_NOCACHE)) {
|
|
||||||
return client->st_blocks * 512;
|
|
||||||
}
|
|
||||||
|
|
||||||
task.st = &st;
|
|
||||||
if (nfs_fstat_async(client->context, client->fh, nfs_co_generic_cb,
|
|
||||||
&task) != 0) {
|
|
||||||
return -ENOMEM;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (!task.complete) {
|
|
||||||
nfs_set_events(client);
|
|
||||||
aio_poll(client->aio_context, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (task.ret < 0 ? task.ret : st.st_blocks * 512);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int nfs_file_truncate(BlockDriverState *bs, int64_t offset)
|
|
||||||
{
|
|
||||||
NFSClient *client = bs->opaque;
|
|
||||||
return nfs_ftruncate(client->context, client->fh, offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Note that this will not re-establish a connection with the NFS server
|
|
||||||
* - it is effectively a NOP. */
|
|
||||||
static int nfs_reopen_prepare(BDRVReopenState *state,
|
|
||||||
BlockReopenQueue *queue, Error **errp)
|
|
||||||
{
|
|
||||||
NFSClient *client = state->bs->opaque;
|
|
||||||
struct stat st;
|
|
||||||
int ret = 0;
|
|
||||||
|
|
||||||
if (state->flags & BDRV_O_RDWR && bdrv_is_read_only(state->bs)) {
|
|
||||||
error_setg(errp, "Cannot open a read-only mount as read-write");
|
|
||||||
return -EACCES;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Update cache for read-only reopens */
|
|
||||||
if (!(state->flags & BDRV_O_RDWR)) {
|
|
||||||
ret = nfs_fstat(client->context, client->fh, &st);
|
|
||||||
if (ret < 0) {
|
|
||||||
error_setg(errp, "Failed to fstat file: %s",
|
|
||||||
nfs_get_error(client->context));
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
client->st_blocks = st.st_blocks;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static BlockDriver bdrv_nfs = {
|
|
||||||
.format_name = "nfs",
|
|
||||||
.protocol_name = "nfs",
|
|
||||||
|
|
||||||
.instance_size = sizeof(NFSClient),
|
|
||||||
.bdrv_needs_filename = true,
|
|
||||||
.create_opts = &nfs_create_opts,
|
|
||||||
|
|
||||||
.bdrv_has_zero_init = nfs_has_zero_init,
|
|
||||||
.bdrv_get_allocated_file_size = nfs_get_allocated_file_size,
|
|
||||||
.bdrv_truncate = nfs_file_truncate,
|
|
||||||
|
|
||||||
.bdrv_file_open = nfs_file_open,
|
|
||||||
.bdrv_close = nfs_file_close,
|
|
||||||
.bdrv_create = nfs_file_create,
|
|
||||||
.bdrv_reopen_prepare = nfs_reopen_prepare,
|
|
||||||
|
|
||||||
.bdrv_co_readv = nfs_co_readv,
|
|
||||||
.bdrv_co_writev = nfs_co_writev,
|
|
||||||
.bdrv_co_flush_to_disk = nfs_co_flush,
|
|
||||||
|
|
||||||
.bdrv_detach_aio_context = nfs_detach_aio_context,
|
|
||||||
.bdrv_attach_aio_context = nfs_attach_aio_context,
|
|
||||||
};
|
|
||||||
|
|
||||||
static void nfs_block_init(void)
|
|
||||||
{
|
|
||||||
bdrv_register(&bdrv_nfs);
|
|
||||||
}
|
|
||||||
|
|
||||||
block_init(nfs_block_init);
|
|
||||||
222
block/null.c
222
block/null.c
@@ -1,222 +0,0 @@
|
|||||||
/*
|
|
||||||
* Null block driver
|
|
||||||
*
|
|
||||||
* Authors:
|
|
||||||
* Fam Zheng <famz@redhat.com>
|
|
||||||
*
|
|
||||||
* Copyright (C) 2014 Red Hat, Inc.
|
|
||||||
*
|
|
||||||
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
||||||
* See the COPYING file in the top-level directory.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#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 = {
|
|
||||||
.name = "null",
|
|
||||||
.head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
|
|
||||||
.desc = {
|
|
||||||
{
|
|
||||||
.name = "filename",
|
|
||||||
.type = QEMU_OPT_STRING,
|
|
||||||
.help = "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
.name = BLOCK_OPT_SIZE,
|
|
||||||
.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 */ }
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
static int null_file_open(BlockDriverState *bs, QDict *options, int flags,
|
|
||||||
Error **errp)
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void null_close(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
static int64_t null_getlength(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
BDRVNullState *s = bs->opaque;
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
static coroutine_fn int null_co_writev(BlockDriverState *bs,
|
|
||||||
int64_t sector_num, int nb_sectors,
|
|
||||||
QEMUIOVector *qiov)
|
|
||||||
{
|
|
||||||
return null_co_common(bs);
|
|
||||||
}
|
|
||||||
|
|
||||||
static coroutine_fn int null_co_flush(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
return null_co_common(bs);
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
BlockAIOCB common;
|
|
||||||
QEMUBH *bh;
|
|
||||||
QEMUTimer timer;
|
|
||||||
} NullAIOCB;
|
|
||||||
|
|
||||||
static const AIOCBInfo null_aiocb_info = {
|
|
||||||
.aiocb_size = sizeof(NullAIOCB),
|
|
||||||
};
|
|
||||||
|
|
||||||
static void null_bh_cb(void *opaque)
|
|
||||||
{
|
|
||||||
NullAIOCB *acb = opaque;
|
|
||||||
acb->common.cb(acb->common.opaque, 0);
|
|
||||||
qemu_bh_delete(acb->bh);
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
return &acb->common;
|
|
||||||
}
|
|
||||||
|
|
||||||
static BlockAIOCB *null_aio_readv(BlockDriverState *bs,
|
|
||||||
int64_t sector_num, QEMUIOVector *qiov,
|
|
||||||
int nb_sectors,
|
|
||||||
BlockCompletionFunc *cb,
|
|
||||||
void *opaque)
|
|
||||||
{
|
|
||||||
return null_aio_common(bs, cb, opaque);
|
|
||||||
}
|
|
||||||
|
|
||||||
static BlockAIOCB *null_aio_writev(BlockDriverState *bs,
|
|
||||||
int64_t sector_num, QEMUIOVector *qiov,
|
|
||||||
int nb_sectors,
|
|
||||||
BlockCompletionFunc *cb,
|
|
||||||
void *opaque)
|
|
||||||
{
|
|
||||||
return null_aio_common(bs, cb, opaque);
|
|
||||||
}
|
|
||||||
|
|
||||||
static BlockAIOCB *null_aio_flush(BlockDriverState *bs,
|
|
||||||
BlockCompletionFunc *cb,
|
|
||||||
void *opaque)
|
|
||||||
{
|
|
||||||
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",
|
|
||||||
.instance_size = sizeof(BDRVNullState),
|
|
||||||
|
|
||||||
.bdrv_file_open = null_file_open,
|
|
||||||
.bdrv_close = null_close,
|
|
||||||
.bdrv_getlength = null_getlength,
|
|
||||||
|
|
||||||
.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 = {
|
|
||||||
.format_name = "null-aio",
|
|
||||||
.protocol_name = "null-aio",
|
|
||||||
.instance_size = sizeof(BDRVNullState),
|
|
||||||
|
|
||||||
.bdrv_file_open = null_file_open,
|
|
||||||
.bdrv_close = null_close,
|
|
||||||
.bdrv_getlength = null_getlength,
|
|
||||||
|
|
||||||
.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)
|
|
||||||
{
|
|
||||||
bdrv_register(&bdrv_null_co);
|
|
||||||
bdrv_register(&bdrv_null_aio);
|
|
||||||
}
|
|
||||||
|
|
||||||
block_init(bdrv_null_init);
|
|
||||||
@@ -2,12 +2,8 @@
|
|||||||
* Block driver for Parallels disk image format
|
* Block driver for Parallels disk image format
|
||||||
*
|
*
|
||||||
* Copyright (c) 2007 Alex Beregszaszi
|
* 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
|
* This code is based on comparing different disk images created by Parallels.
|
||||||
* by Parallels. Currently it is based on opened OpenVZ sources
|
|
||||||
* available at
|
|
||||||
* http://git.openvz.org/?p=ploop;a=summary
|
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@@ -30,726 +26,158 @@
|
|||||||
#include "qemu-common.h"
|
#include "qemu-common.h"
|
||||||
#include "block/block_int.h"
|
#include "block/block_int.h"
|
||||||
#include "qemu/module.h"
|
#include "qemu/module.h"
|
||||||
#include "qemu/bitmap.h"
|
|
||||||
#include "qapi/util.h"
|
|
||||||
|
|
||||||
/**************************************************************/
|
/**************************************************************/
|
||||||
|
|
||||||
#define HEADER_MAGIC "WithoutFreeSpace"
|
#define HEADER_MAGIC "WithoutFreeSpace"
|
||||||
#define HEADER_MAGIC2 "WithouFreSpacExt"
|
|
||||||
#define HEADER_VERSION 2
|
#define HEADER_VERSION 2
|
||||||
#define HEADER_INUSE_MAGIC (0x746F6E59)
|
#define HEADER_SIZE 64
|
||||||
|
|
||||||
#define DEFAULT_CLUSTER_SIZE 1048576 /* 1 MiB */
|
|
||||||
|
|
||||||
|
|
||||||
// always little-endian
|
// always little-endian
|
||||||
typedef struct ParallelsHeader {
|
struct parallels_header {
|
||||||
char magic[16]; // "WithoutFreeSpace"
|
char magic[16]; // "WithoutFreeSpace"
|
||||||
uint32_t version;
|
uint32_t version;
|
||||||
uint32_t heads;
|
uint32_t heads;
|
||||||
uint32_t cylinders;
|
uint32_t cylinders;
|
||||||
uint32_t tracks;
|
uint32_t tracks;
|
||||||
uint32_t bat_entries;
|
uint32_t catalog_entries;
|
||||||
uint64_t nb_sectors;
|
uint32_t nb_sectors;
|
||||||
uint32_t inuse;
|
char padding[24];
|
||||||
uint32_t data_off;
|
} QEMU_PACKED;
|
||||||
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,
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct BDRVParallelsState {
|
typedef struct BDRVParallelsState {
|
||||||
/** Locking is conservative, the lock protects
|
|
||||||
* - image file extending (truncate, fallocate)
|
|
||||||
* - any access to block allocation table
|
|
||||||
*/
|
|
||||||
CoMutex lock;
|
CoMutex lock;
|
||||||
|
|
||||||
ParallelsHeader *header;
|
uint32_t *catalog_bitmap;
|
||||||
uint32_t header_size;
|
unsigned int catalog_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;
|
|
||||||
|
|
||||||
unsigned int tracks;
|
unsigned int tracks;
|
||||||
|
|
||||||
unsigned int off_multiplier;
|
|
||||||
} BDRVParallelsState;
|
} BDRVParallelsState;
|
||||||
|
|
||||||
|
static int parallels_probe(const uint8_t *buf, int buf_size, const char *filename)
|
||||||
#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,
|
const struct parallels_header *ph = (const void *)buf;
|
||||||
.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 */ },
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
|
if (buf_size < HEADER_SIZE)
|
||||||
|
return 0;
|
||||||
|
|
||||||
static int64_t bat2sect(BDRVParallelsState *s, uint32_t idx)
|
if (!memcmp(ph->magic, HEADER_MAGIC, 16) &&
|
||||||
{
|
(le32_to_cpu(ph->version) == HEADER_VERSION))
|
||||||
return (uint64_t)le32_to_cpu(s->bat_bitmap[idx]) * s->off_multiplier;
|
return 100;
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint32_t bat_entry_off(uint32_t idx)
|
static int parallels_open(BlockDriverState *bs, int flags)
|
||||||
{
|
{
|
||||||
return sizeof(ParallelsHeader) + sizeof(uint32_t) * idx;
|
BDRVParallelsState *s = bs->opaque;
|
||||||
|
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) {
|
||||||
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int64_t seek_to_sector(BDRVParallelsState *s, int64_t sector_num)
|
if (memcmp(ph.magic, HEADER_MAGIC, 16) ||
|
||||||
|
(le32_to_cpu(ph.version) != HEADER_VERSION)) {
|
||||||
|
ret = -EMEDIUMTYPE;
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
bs->total_sectors = le32_to_cpu(ph.nb_sectors);
|
||||||
|
|
||||||
|
s->tracks = le32_to_cpu(ph.tracks);
|
||||||
|
if (s->tracks == 0) {
|
||||||
|
qerror_report(ERROR_CLASS_GENERIC_ERROR,
|
||||||
|
"Invalid image: Zero sectors per track");
|
||||||
|
ret = -EINVAL;
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
s->catalog_size = le32_to_cpu(ph.catalog_entries);
|
||||||
|
if (s->catalog_size > INT_MAX / 4) {
|
||||||
|
qerror_report(ERROR_CLASS_GENERIC_ERROR, "Catalog too large");
|
||||||
|
ret = -EFBIG;
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
s->catalog_bitmap = g_malloc(s->catalog_size * 4);
|
||||||
|
|
||||||
|
ret = bdrv_pread(bs->file, 64, s->catalog_bitmap, s->catalog_size * 4);
|
||||||
|
if (ret < 0) {
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < s->catalog_size; i++)
|
||||||
|
le32_to_cpus(&s->catalog_bitmap[i]);
|
||||||
|
|
||||||
|
qemu_co_mutex_init(&s->lock);
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
fail:
|
||||||
|
g_free(s->catalog_bitmap);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num)
|
||||||
{
|
{
|
||||||
|
BDRVParallelsState *s = bs->opaque;
|
||||||
uint32_t index, offset;
|
uint32_t index, offset;
|
||||||
|
|
||||||
index = sector_num / s->tracks;
|
index = sector_num / s->tracks;
|
||||||
offset = sector_num % s->tracks;
|
offset = sector_num % s->tracks;
|
||||||
|
|
||||||
/* not allocated */
|
/* not allocated */
|
||||||
if ((index >= s->bat_size) || (s->bat_bitmap[index] == 0)) {
|
if ((index > s->catalog_size) || (s->catalog_bitmap[index] == 0))
|
||||||
return -1;
|
return -1;
|
||||||
}
|
return (uint64_t)(s->catalog_bitmap[index] + offset) * 512;
|
||||||
return bat2sect(s, index) + offset;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int cluster_remainder(BDRVParallelsState *s, int64_t sector_num,
|
static int parallels_read(BlockDriverState *bs, int64_t sector_num,
|
||||||
int nb_sectors)
|
uint8_t *buf, 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->bs) >> BDRV_SECTOR_BITS) {
|
|
||||||
int ret;
|
|
||||||
space += s->prealloc_size;
|
|
||||||
if (s->prealloc_mode == PRL_PREALLOC_MODE_FALLOCATE) {
|
|
||||||
ret = bdrv_write_zeroes(bs->file->bs, s->data_end, space, 0);
|
|
||||||
} else {
|
|
||||||
ret = bdrv_truncate(bs->file->bs,
|
|
||||||
(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 + i) / 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->bs, 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) {
|
|
||||||
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) {
|
while (nb_sectors > 0) {
|
||||||
int64_t position;
|
int64_t position = seek_to_sector(bs, sector_num);
|
||||||
int n, nbytes;
|
if (position >= 0) {
|
||||||
|
if (bdrv_pread(bs->file, position, buf, 512) != 512)
|
||||||
qemu_co_mutex_lock(&s->lock);
|
return -1;
|
||||||
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->bs, 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 {
|
} else {
|
||||||
qemu_iovec_reset(&hd_qiov);
|
memset(buf, 0, 512);
|
||||||
qemu_iovec_concat(&hd_qiov, qiov, bytes_done, nbytes);
|
|
||||||
|
|
||||||
ret = bdrv_co_readv(bs->file->bs, position, n, &hd_qiov);
|
|
||||||
if (ret < 0) {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
nb_sectors--;
|
||||||
|
sector_num++;
|
||||||
|
buf += 512;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
nb_sectors -= n;
|
static coroutine_fn int parallels_co_read(BlockDriverState *bs, int64_t sector_num,
|
||||||
sector_num += n;
|
uint8_t *buf, int nb_sectors)
|
||||||
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;
|
int ret;
|
||||||
uint32_t i;
|
|
||||||
bool flush_bat = false;
|
|
||||||
int cluster_size = s->tracks << BDRV_SECTOR_BITS;
|
|
||||||
|
|
||||||
size = bdrv_getlength(bs->file->bs);
|
|
||||||
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->bs, 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->bs, 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, &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)) {
|
|
||||||
return 100;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int parallels_update_header(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
BDRVParallelsState *s = bs->opaque;
|
BDRVParallelsState *s = bs->opaque;
|
||||||
unsigned size = MAX(bdrv_opt_mem_align(bs->file->bs),
|
qemu_co_mutex_lock(&s->lock);
|
||||||
sizeof(ParallelsHeader));
|
ret = parallels_read(bs, sector_num, buf, nb_sectors);
|
||||||
|
qemu_co_mutex_unlock(&s->lock);
|
||||||
if (size > s->header_size) {
|
|
||||||
size = s->header_size;
|
|
||||||
}
|
|
||||||
return bdrv_pwrite_sync(bs->file->bs, 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;
|
|
||||||
|
|
||||||
ret = bdrv_pread(bs->file->bs, 0, &ph, sizeof(ph));
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
bs->total_sectors = le64_to_cpu(ph.nb_sectors);
|
|
||||||
|
|
||||||
if (le32_to_cpu(ph.version) != HEADER_VERSION) {
|
|
||||||
goto fail_format;
|
|
||||||
}
|
|
||||||
if (!memcmp(ph.magic, HEADER_MAGIC, 16)) {
|
|
||||||
s->off_multiplier = 1;
|
|
||||||
bs->total_sectors = 0xffffffff & bs->total_sectors;
|
|
||||||
} else if (!memcmp(ph.magic, HEADER_MAGIC2, 16)) {
|
|
||||||
s->off_multiplier = le32_to_cpu(ph.tracks);
|
|
||||||
} else {
|
|
||||||
goto fail_format;
|
|
||||||
}
|
|
||||||
|
|
||||||
s->tracks = le32_to_cpu(ph.tracks);
|
|
||||||
if (s->tracks == 0) {
|
|
||||||
error_setg(errp, "Invalid image: Zero sectors per track");
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
if (s->tracks > INT32_MAX/513) {
|
|
||||||
error_setg(errp, "Invalid image: Too big cluster");
|
|
||||||
ret = -EFBIG;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
s->bat_size = le32_to_cpu(ph.bat_entries);
|
|
||||||
if (s->bat_size > INT_MAX / sizeof(uint32_t)) {
|
|
||||||
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->bs));
|
|
||||||
s->header = qemu_try_blockalign(bs->file->bs, s->header_size);
|
|
||||||
if (s->header == 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->bs, 0, s->header, s->header_size);
|
|
||||||
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->bs) ||
|
|
||||||
bdrv_truncate(bs->file->bs, bdrv_getlength(bs->file->bs)) != 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));
|
|
||||||
|
|
||||||
qemu_co_mutex_init(&s->lock);
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
fail_format:
|
|
||||||
error_setg(errp, "Image not in Parallels format");
|
|
||||||
ret = -EINVAL;
|
|
||||||
fail:
|
|
||||||
qemu_vfree(s->header);
|
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
fail_options:
|
|
||||||
error_propagate(errp, local_err);
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void parallels_close(BlockDriverState *bs)
|
static void parallels_close(BlockDriverState *bs)
|
||||||
{
|
{
|
||||||
BDRVParallelsState *s = bs->opaque;
|
BDRVParallelsState *s = bs->opaque;
|
||||||
|
g_free(s->catalog_bitmap);
|
||||||
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->bs, s->data_end << BDRV_SECTOR_BITS);
|
|
||||||
}
|
|
||||||
|
|
||||||
g_free(s->bat_dirty_bmap);
|
|
||||||
qemu_vfree(s->header);
|
|
||||||
}
|
|
||||||
|
|
||||||
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 = {
|
static BlockDriver bdrv_parallels = {
|
||||||
.format_name = "parallels",
|
.format_name = "parallels",
|
||||||
.instance_size = sizeof(BDRVParallelsState),
|
.instance_size = sizeof(BDRVParallelsState),
|
||||||
.bdrv_probe = parallels_probe,
|
.bdrv_probe = parallels_probe,
|
||||||
.bdrv_open = parallels_open,
|
.bdrv_open = parallels_open,
|
||||||
|
.bdrv_read = parallels_co_read,
|
||||||
.bdrv_close = parallels_close,
|
.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)
|
static void bdrv_parallels_init(void)
|
||||||
|
|||||||
724
block/qapi.c
724
block/qapi.c
@@ -1,724 +0,0 @@
|
|||||||
/*
|
|
||||||
* Block layer qmp and info dump related functions
|
|
||||||
*
|
|
||||||
* Copyright (c) 2003-2008 Fabrice Bellard
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
||||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#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"
|
|
||||||
#include "qapi/qmp-output-visitor.h"
|
|
||||||
#include "qapi/qmp/types.h"
|
|
||||||
#include "sysemu/block-backend.h"
|
|
||||||
|
|
||||||
BlockDeviceInfo *bdrv_block_device_info(BlockDriverState *bs, Error **errp)
|
|
||||||
{
|
|
||||||
ImageInfo **p_image_info;
|
|
||||||
BlockDriverState *bs0;
|
|
||||||
BlockDeviceInfo *info = g_malloc0(sizeof(*info));
|
|
||||||
|
|
||||||
info->file = g_strdup(bs->filename);
|
|
||||||
info->ro = bs->read_only;
|
|
||||||
info->drv = g_strdup(bs->drv->format_name);
|
|
||||||
info->encrypted = bs->encrypted;
|
|
||||||
info->encryption_key_missing = bdrv_key_required(bs);
|
|
||||||
|
|
||||||
info->cache = g_new(BlockdevCacheInfo, 1);
|
|
||||||
*info->cache = (BlockdevCacheInfo) {
|
|
||||||
.writeback = bdrv_enable_write_cache(bs),
|
|
||||||
.direct = !!(bs->open_flags & BDRV_O_NOCACHE),
|
|
||||||
.no_flush = !!(bs->open_flags & BDRV_O_NO_FLUSH),
|
|
||||||
};
|
|
||||||
|
|
||||||
if (bs->node_name[0]) {
|
|
||||||
info->has_node_name = true;
|
|
||||||
info->node_name = g_strdup(bs->node_name);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bs->backing_file[0]) {
|
|
||||||
info->has_backing_file = true;
|
|
||||||
info->backing_file = g_strdup(bs->backing_file);
|
|
||||||
}
|
|
||||||
|
|
||||||
info->backing_file_depth = bdrv_get_backing_file_depth(bs);
|
|
||||||
info->detect_zeroes = bs->detect_zeroes;
|
|
||||||
|
|
||||||
if (bs->throttle_state) {
|
|
||||||
ThrottleConfig cfg;
|
|
||||||
|
|
||||||
throttle_group_get_config(bs, &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;
|
|
||||||
|
|
||||||
info->iops = cfg.buckets[THROTTLE_OPS_TOTAL].avg;
|
|
||||||
info->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg;
|
|
||||||
info->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg;
|
|
||||||
|
|
||||||
info->has_bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max;
|
|
||||||
info->bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max;
|
|
||||||
info->has_bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max;
|
|
||||||
info->bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max;
|
|
||||||
info->has_bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max;
|
|
||||||
info->bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max;
|
|
||||||
|
|
||||||
info->has_iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max;
|
|
||||||
info->iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max;
|
|
||||||
info->has_iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
|
|
||||||
info->iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
|
|
||||||
info->has_iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
|
|
||||||
info->iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
|
|
||||||
|
|
||||||
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) {
|
|
||||||
bs0 = bs0->backing->bs;
|
|
||||||
(*p_image_info)->has_backing_image = true;
|
|
||||||
p_image_info = &((*p_image_info)->backing_image);
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return info;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Returns 0 on success, with *p_list either set to describe snapshot
|
|
||||||
* information, or NULL because there are no snapshots. Returns -errno on
|
|
||||||
* error, with *p_list untouched.
|
|
||||||
*/
|
|
||||||
int bdrv_query_snapshot_info_list(BlockDriverState *bs,
|
|
||||||
SnapshotInfoList **p_list,
|
|
||||||
Error **errp)
|
|
||||||
{
|
|
||||||
int i, sn_count;
|
|
||||||
QEMUSnapshotInfo *sn_tab = NULL;
|
|
||||||
SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL;
|
|
||||||
SnapshotInfo *info;
|
|
||||||
|
|
||||||
sn_count = bdrv_snapshot_list(bs, &sn_tab);
|
|
||||||
if (sn_count < 0) {
|
|
||||||
const char *dev = bdrv_get_device_name(bs);
|
|
||||||
switch (sn_count) {
|
|
||||||
case -ENOMEDIUM:
|
|
||||||
error_setg(errp, "Device '%s' is not inserted", dev);
|
|
||||||
break;
|
|
||||||
case -ENOTSUP:
|
|
||||||
error_setg(errp,
|
|
||||||
"Device '%s' does not support internal snapshots",
|
|
||||||
dev);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
error_setg_errno(errp, -sn_count,
|
|
||||||
"Can't list snapshots of device '%s'", dev);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return sn_count;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < sn_count; i++) {
|
|
||||||
info = g_new0(SnapshotInfo, 1);
|
|
||||||
info->id = g_strdup(sn_tab[i].id_str);
|
|
||||||
info->name = g_strdup(sn_tab[i].name);
|
|
||||||
info->vm_state_size = sn_tab[i].vm_state_size;
|
|
||||||
info->date_sec = sn_tab[i].date_sec;
|
|
||||||
info->date_nsec = sn_tab[i].date_nsec;
|
|
||||||
info->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000;
|
|
||||||
info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
|
|
||||||
|
|
||||||
info_list = g_new0(SnapshotInfoList, 1);
|
|
||||||
info_list->value = info;
|
|
||||||
|
|
||||||
/* XXX: waiting for the qapi to support qemu-queue.h types */
|
|
||||||
if (!cur_item) {
|
|
||||||
head = cur_item = info_list;
|
|
||||||
} else {
|
|
||||||
cur_item->next = info_list;
|
|
||||||
cur_item = info_list;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
g_free(sn_tab);
|
|
||||||
*p_list = head;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* bdrv_query_image_info:
|
|
||||||
* @bs: block device to examine
|
|
||||||
* @p_info: location to store image information
|
|
||||||
* @errp: location to store error information
|
|
||||||
*
|
|
||||||
* Store "flat" image information in @p_info.
|
|
||||||
*
|
|
||||||
* "Flat" means it does *not* query backing image information,
|
|
||||||
* i.e. (*pinfo)->has_backing_image will be set to false and
|
|
||||||
* (*pinfo)->backing_image to NULL even when the image does in fact have
|
|
||||||
* a backing image.
|
|
||||||
*
|
|
||||||
* @p_info will be set only on success. On error, store error in @errp.
|
|
||||||
*/
|
|
||||||
void bdrv_query_image_info(BlockDriverState *bs,
|
|
||||||
ImageInfo **p_info,
|
|
||||||
Error **errp)
|
|
||||||
{
|
|
||||||
int64_t size;
|
|
||||||
const char *backing_filename;
|
|
||||||
BlockDriverInfo bdi;
|
|
||||||
int ret;
|
|
||||||
Error *err = NULL;
|
|
||||||
ImageInfo *info;
|
|
||||||
|
|
||||||
size = bdrv_getlength(bs);
|
|
||||||
if (size < 0) {
|
|
||||||
error_setg_errno(errp, -size, "Can't get size of device '%s'",
|
|
||||||
bdrv_get_device_name(bs));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
info = g_new0(ImageInfo, 1);
|
|
||||||
info->filename = g_strdup(bs->filename);
|
|
||||||
info->format = g_strdup(bdrv_get_format_name(bs));
|
|
||||||
info->virtual_size = size;
|
|
||||||
info->actual_size = bdrv_get_allocated_file_size(bs);
|
|
||||||
info->has_actual_size = info->actual_size >= 0;
|
|
||||||
if (bdrv_is_encrypted(bs)) {
|
|
||||||
info->encrypted = true;
|
|
||||||
info->has_encrypted = true;
|
|
||||||
}
|
|
||||||
if (bdrv_get_info(bs, &bdi) >= 0) {
|
|
||||||
if (bdi.cluster_size != 0) {
|
|
||||||
info->cluster_size = bdi.cluster_size;
|
|
||||||
info->has_cluster_size = true;
|
|
||||||
}
|
|
||||||
info->dirty_flag = bdi.is_dirty;
|
|
||||||
info->has_dirty_flag = true;
|
|
||||||
}
|
|
||||||
info->format_specific = bdrv_get_specific_info(bs);
|
|
||||||
info->has_format_specific = info->format_specific != NULL;
|
|
||||||
|
|
||||||
backing_filename = bs->backing_file;
|
|
||||||
if (backing_filename[0] != '\0') {
|
|
||||||
char *backing_filename2 = g_malloc0(PATH_MAX);
|
|
||||||
info->backing_filename = g_strdup(backing_filename);
|
|
||||||
info->has_backing_filename = true;
|
|
||||||
bdrv_get_full_backing_filename(bs, backing_filename2, PATH_MAX, &err);
|
|
||||||
if (err) {
|
|
||||||
/* Can't reconstruct the full backing filename, so we must omit
|
|
||||||
* this field and apply a Best Effort to this query. */
|
|
||||||
g_free(backing_filename2);
|
|
||||||
backing_filename2 = NULL;
|
|
||||||
error_free(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Always report the full_backing_filename if present, even if it's the
|
|
||||||
* same as backing_filename. That they are same is useful info. */
|
|
||||||
if (backing_filename2) {
|
|
||||||
info->full_backing_filename = g_strdup(backing_filename2);
|
|
||||||
info->has_full_backing_filename = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bs->backing_format[0]) {
|
|
||||||
info->backing_filename_format = g_strdup(bs->backing_format);
|
|
||||||
info->has_backing_filename_format = true;
|
|
||||||
}
|
|
||||||
g_free(backing_filename2);
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
|
|
||||||
switch (ret) {
|
|
||||||
case 0:
|
|
||||||
if (info->snapshots) {
|
|
||||||
info->has_snapshots = true;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
/* recoverable error */
|
|
||||||
case -ENOMEDIUM:
|
|
||||||
case -ENOTSUP:
|
|
||||||
error_free(err);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
error_propagate(errp, err);
|
|
||||||
qapi_free_ImageInfo(info);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
*p_info = info;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* @p_info will be set only on success. */
|
|
||||||
static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info,
|
|
||||||
Error **errp)
|
|
||||||
{
|
|
||||||
BlockInfo *info = g_malloc0(sizeof(*info));
|
|
||||||
BlockDriverState *bs = blk_bs(blk);
|
|
||||||
info->device = g_strdup(blk_name(blk));
|
|
||||||
info->type = g_strdup("unknown");
|
|
||||||
info->locked = blk_dev_is_medium_locked(blk);
|
|
||||||
info->removable = blk_dev_has_removable_media(blk);
|
|
||||||
|
|
||||||
if (blk_dev_has_removable_media(blk)) {
|
|
||||||
info->has_tray_open = true;
|
|
||||||
info->tray_open = blk_dev_is_tray_open(blk);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (blk_iostatus_is_enabled(blk)) {
|
|
||||||
info->has_io_status = true;
|
|
||||||
info->io_status = blk_iostatus(blk);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bs && !QLIST_EMPTY(&bs->dirty_bitmaps)) {
|
|
||||||
info->has_dirty_bitmaps = true;
|
|
||||||
info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bs && bs->drv) {
|
|
||||||
info->has_inserted = true;
|
|
||||||
info->inserted = bdrv_block_device_info(bs, errp);
|
|
||||||
if (info->inserted == NULL) {
|
|
||||||
goto err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
*p_info = info;
|
|
||||||
return;
|
|
||||||
|
|
||||||
err:
|
|
||||||
qapi_free_BlockInfo(info);
|
|
||||||
}
|
|
||||||
|
|
||||||
static BlockStats *bdrv_query_stats(const BlockDriverState *bs,
|
|
||||||
bool query_backing)
|
|
||||||
{
|
|
||||||
BlockStats *s;
|
|
||||||
|
|
||||||
s = g_malloc0(sizeof(*s));
|
|
||||||
|
|
||||||
if (bdrv_get_device_name(bs)[0]) {
|
|
||||||
s->has_device = true;
|
|
||||||
s->device = g_strdup(bdrv_get_device_name(bs));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bdrv_get_node_name(bs)[0]) {
|
|
||||||
s->has_node_name = true;
|
|
||||||
s->node_name = g_strdup(bdrv_get_node_name(bs));
|
|
||||||
}
|
|
||||||
|
|
||||||
s->stats = g_malloc0(sizeof(*s->stats));
|
|
||||||
if (bs->blk) {
|
|
||||||
BlockAcctStats *stats = blk_get_stats(bs->blk);
|
|
||||||
BlockAcctTimedStats *ts = NULL;
|
|
||||||
|
|
||||||
s->stats->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ];
|
|
||||||
s->stats->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE];
|
|
||||||
s->stats->rd_operations = stats->nr_ops[BLOCK_ACCT_READ];
|
|
||||||
s->stats->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE];
|
|
||||||
|
|
||||||
s->stats->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ];
|
|
||||||
s->stats->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE];
|
|
||||||
s->stats->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH];
|
|
||||||
|
|
||||||
s->stats->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ];
|
|
||||||
s->stats->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE];
|
|
||||||
s->stats->invalid_flush_operations =
|
|
||||||
stats->invalid_ops[BLOCK_ACCT_FLUSH];
|
|
||||||
|
|
||||||
s->stats->rd_merged = stats->merged[BLOCK_ACCT_READ];
|
|
||||||
s->stats->wr_merged = stats->merged[BLOCK_ACCT_WRITE];
|
|
||||||
s->stats->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH];
|
|
||||||
s->stats->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE];
|
|
||||||
s->stats->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ];
|
|
||||||
s->stats->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH];
|
|
||||||
|
|
||||||
s->stats->has_idle_time_ns = stats->last_access_time_ns > 0;
|
|
||||||
if (s->stats->has_idle_time_ns) {
|
|
||||||
s->stats->idle_time_ns = block_acct_idle_time_ns(stats);
|
|
||||||
}
|
|
||||||
|
|
||||||
s->stats->account_invalid = stats->account_invalid;
|
|
||||||
s->stats->account_failed = stats->account_failed;
|
|
||||||
|
|
||||||
while ((ts = block_acct_interval_next(stats, ts))) {
|
|
||||||
BlockDeviceTimedStatsList *timed_stats =
|
|
||||||
g_malloc0(sizeof(*timed_stats));
|
|
||||||
BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats));
|
|
||||||
timed_stats->next = s->stats->timed_stats;
|
|
||||||
timed_stats->value = dev_stats;
|
|
||||||
s->stats->timed_stats = timed_stats;
|
|
||||||
|
|
||||||
TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ];
|
|
||||||
TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE];
|
|
||||||
TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH];
|
|
||||||
|
|
||||||
dev_stats->interval_length = ts->interval_length;
|
|
||||||
|
|
||||||
dev_stats->min_rd_latency_ns = timed_average_min(rd);
|
|
||||||
dev_stats->max_rd_latency_ns = timed_average_max(rd);
|
|
||||||
dev_stats->avg_rd_latency_ns = timed_average_avg(rd);
|
|
||||||
|
|
||||||
dev_stats->min_wr_latency_ns = timed_average_min(wr);
|
|
||||||
dev_stats->max_wr_latency_ns = timed_average_max(wr);
|
|
||||||
dev_stats->avg_wr_latency_ns = timed_average_avg(wr);
|
|
||||||
|
|
||||||
dev_stats->min_flush_latency_ns = timed_average_min(fl);
|
|
||||||
dev_stats->max_flush_latency_ns = timed_average_max(fl);
|
|
||||||
dev_stats->avg_flush_latency_ns = timed_average_avg(fl);
|
|
||||||
|
|
||||||
dev_stats->avg_rd_queue_depth =
|
|
||||||
block_acct_queue_depth(ts, BLOCK_ACCT_READ);
|
|
||||||
dev_stats->avg_wr_queue_depth =
|
|
||||||
block_acct_queue_depth(ts, BLOCK_ACCT_WRITE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
s->stats->wr_highest_offset = bs->wr_highest_offset;
|
|
||||||
|
|
||||||
if (bs->file) {
|
|
||||||
s->has_parent = true;
|
|
||||||
s->parent = bdrv_query_stats(bs->file->bs, query_backing);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (query_backing && bs->backing) {
|
|
||||||
s->has_backing = true;
|
|
||||||
s->backing = bdrv_query_stats(bs->backing->bs, query_backing);
|
|
||||||
}
|
|
||||||
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
BlockInfoList *qmp_query_block(Error **errp)
|
|
||||||
{
|
|
||||||
BlockInfoList *head = NULL, **p_next = &head;
|
|
||||||
BlockBackend *blk;
|
|
||||||
Error *local_err = NULL;
|
|
||||||
|
|
||||||
for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
|
|
||||||
BlockInfoList *info = g_malloc0(sizeof(*info));
|
|
||||||
bdrv_query_info(blk, &info->value, &local_err);
|
|
||||||
if (local_err) {
|
|
||||||
error_propagate(errp, local_err);
|
|
||||||
g_free(info);
|
|
||||||
qapi_free_BlockInfoList(head);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
*p_next = info;
|
|
||||||
p_next = &info->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
return head;
|
|
||||||
}
|
|
||||||
|
|
||||||
BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
|
|
||||||
bool query_nodes,
|
|
||||||
Error **errp)
|
|
||||||
{
|
|
||||||
BlockStatsList *head = NULL, **p_next = &head;
|
|
||||||
BlockDriverState *bs = NULL;
|
|
||||||
|
|
||||||
/* Just to be safe if query_nodes is not always initialized */
|
|
||||||
query_nodes = has_query_nodes && query_nodes;
|
|
||||||
|
|
||||||
while ((bs = query_nodes ? bdrv_next_node(bs) : bdrv_next(bs))) {
|
|
||||||
BlockStatsList *info = g_malloc0(sizeof(*info));
|
|
||||||
AioContext *ctx = bdrv_get_aio_context(bs);
|
|
||||||
|
|
||||||
aio_context_acquire(ctx);
|
|
||||||
info->value = bdrv_query_stats(bs, !query_nodes);
|
|
||||||
aio_context_release(ctx);
|
|
||||||
|
|
||||||
*p_next = info;
|
|
||||||
p_next = &info->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
return head;
|
|
||||||
}
|
|
||||||
|
|
||||||
#define NB_SUFFIXES 4
|
|
||||||
|
|
||||||
static char *get_human_readable_size(char *buf, int buf_size, int64_t size)
|
|
||||||
{
|
|
||||||
static const char suffixes[NB_SUFFIXES] = {'K', 'M', 'G', 'T'};
|
|
||||||
int64_t base;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
if (size <= 999) {
|
|
||||||
snprintf(buf, buf_size, "%" PRId64, size);
|
|
||||||
} else {
|
|
||||||
base = 1024;
|
|
||||||
for (i = 0; i < NB_SUFFIXES; i++) {
|
|
||||||
if (size < (10 * base)) {
|
|
||||||
snprintf(buf, buf_size, "%0.1f%c",
|
|
||||||
(double)size / base,
|
|
||||||
suffixes[i]);
|
|
||||||
break;
|
|
||||||
} else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
|
|
||||||
snprintf(buf, buf_size, "%" PRId64 "%c",
|
|
||||||
((size + (base >> 1)) / base),
|
|
||||||
suffixes[i]);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
base = base * 1024;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
void bdrv_snapshot_dump(fprintf_function func_fprintf, void *f,
|
|
||||||
QEMUSnapshotInfo *sn)
|
|
||||||
{
|
|
||||||
char buf1[128], date_buf[128], clock_buf[128];
|
|
||||||
struct tm tm;
|
|
||||||
time_t ti;
|
|
||||||
int64_t secs;
|
|
||||||
|
|
||||||
if (!sn) {
|
|
||||||
func_fprintf(f,
|
|
||||||
"%-10s%-20s%7s%20s%15s",
|
|
||||||
"ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
|
|
||||||
} else {
|
|
||||||
ti = sn->date_sec;
|
|
||||||
localtime_r(&ti, &tm);
|
|
||||||
strftime(date_buf, sizeof(date_buf),
|
|
||||||
"%Y-%m-%d %H:%M:%S", &tm);
|
|
||||||
secs = sn->vm_clock_nsec / 1000000000;
|
|
||||||
snprintf(clock_buf, sizeof(clock_buf),
|
|
||||||
"%02d:%02d:%02d.%03d",
|
|
||||||
(int)(secs / 3600),
|
|
||||||
(int)((secs / 60) % 60),
|
|
||||||
(int)(secs % 60),
|
|
||||||
(int)((sn->vm_clock_nsec / 1000000) % 1000));
|
|
||||||
func_fprintf(f,
|
|
||||||
"%-10s%-20s%7s%20s%15s",
|
|
||||||
sn->id_str, sn->name,
|
|
||||||
get_human_readable_size(buf1, sizeof(buf1),
|
|
||||||
sn->vm_state_size),
|
|
||||||
date_buf,
|
|
||||||
clock_buf);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
|
|
||||||
QDict *dict);
|
|
||||||
static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
|
|
||||||
QList *list);
|
|
||||||
|
|
||||||
static void dump_qobject(fprintf_function func_fprintf, void *f,
|
|
||||||
int comp_indent, QObject *obj)
|
|
||||||
{
|
|
||||||
switch (qobject_type(obj)) {
|
|
||||||
case QTYPE_QINT: {
|
|
||||||
QInt *value = qobject_to_qint(obj);
|
|
||||||
func_fprintf(f, "%" PRId64, qint_get_int(value));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case QTYPE_QSTRING: {
|
|
||||||
QString *value = qobject_to_qstring(obj);
|
|
||||||
func_fprintf(f, "%s", qstring_get_str(value));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case QTYPE_QDICT: {
|
|
||||||
QDict *value = qobject_to_qdict(obj);
|
|
||||||
dump_qdict(func_fprintf, f, comp_indent, value);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case QTYPE_QLIST: {
|
|
||||||
QList *value = qobject_to_qlist(obj);
|
|
||||||
dump_qlist(func_fprintf, f, comp_indent, value);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case QTYPE_QFLOAT: {
|
|
||||||
QFloat *value = qobject_to_qfloat(obj);
|
|
||||||
func_fprintf(f, "%g", qfloat_get_double(value));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case QTYPE_QBOOL: {
|
|
||||||
QBool *value = qobject_to_qbool(obj);
|
|
||||||
func_fprintf(f, "%s", qbool_get_bool(value) ? "true" : "false");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
|
|
||||||
QList *list)
|
|
||||||
{
|
|
||||||
const QListEntry *entry;
|
|
||||||
int i = 0;
|
|
||||||
|
|
||||||
for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
|
|
||||||
QType type = qobject_type(entry->value);
|
|
||||||
bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
|
|
||||||
const char *format = composite ? "%*s[%i]:\n" : "%*s[%i]: ";
|
|
||||||
|
|
||||||
func_fprintf(f, format, indentation * 4, "", i);
|
|
||||||
dump_qobject(func_fprintf, f, indentation + 1, entry->value);
|
|
||||||
if (!composite) {
|
|
||||||
func_fprintf(f, "\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
|
|
||||||
QDict *dict)
|
|
||||||
{
|
|
||||||
const QDictEntry *entry;
|
|
||||||
|
|
||||||
for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
|
|
||||||
QType type = qobject_type(entry->value);
|
|
||||||
bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
|
|
||||||
const char *format = composite ? "%*s%s:\n" : "%*s%s: ";
|
|
||||||
char key[strlen(entry->key) + 1];
|
|
||||||
int i;
|
|
||||||
|
|
||||||
/* replace dashes with spaces in key (variable) names */
|
|
||||||
for (i = 0; entry->key[i]; i++) {
|
|
||||||
key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
|
|
||||||
}
|
|
||||||
key[i] = 0;
|
|
||||||
|
|
||||||
func_fprintf(f, format, indentation * 4, "", key);
|
|
||||||
dump_qobject(func_fprintf, f, indentation + 1, entry->value);
|
|
||||||
if (!composite) {
|
|
||||||
func_fprintf(f, "\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f,
|
|
||||||
ImageInfoSpecific *info_spec)
|
|
||||||
{
|
|
||||||
QmpOutputVisitor *ov = qmp_output_visitor_new();
|
|
||||||
QObject *obj, *data;
|
|
||||||
|
|
||||||
visit_type_ImageInfoSpecific(qmp_output_get_visitor(ov), &info_spec, NULL,
|
|
||||||
&error_abort);
|
|
||||||
obj = qmp_output_get_qobject(ov);
|
|
||||||
assert(qobject_type(obj) == QTYPE_QDICT);
|
|
||||||
data = qdict_get(qobject_to_qdict(obj), "data");
|
|
||||||
dump_qobject(func_fprintf, f, 1, data);
|
|
||||||
qmp_output_visitor_cleanup(ov);
|
|
||||||
}
|
|
||||||
|
|
||||||
void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
|
|
||||||
ImageInfo *info)
|
|
||||||
{
|
|
||||||
char size_buf[128], dsize_buf[128];
|
|
||||||
if (!info->has_actual_size) {
|
|
||||||
snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
|
|
||||||
} else {
|
|
||||||
get_human_readable_size(dsize_buf, sizeof(dsize_buf),
|
|
||||||
info->actual_size);
|
|
||||||
}
|
|
||||||
get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
|
|
||||||
func_fprintf(f,
|
|
||||||
"image: %s\n"
|
|
||||||
"file format: %s\n"
|
|
||||||
"virtual size: %s (%" PRId64 " bytes)\n"
|
|
||||||
"disk size: %s\n",
|
|
||||||
info->filename, info->format, size_buf,
|
|
||||||
info->virtual_size,
|
|
||||||
dsize_buf);
|
|
||||||
|
|
||||||
if (info->has_encrypted && info->encrypted) {
|
|
||||||
func_fprintf(f, "encrypted: yes\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (info->has_cluster_size) {
|
|
||||||
func_fprintf(f, "cluster_size: %" PRId64 "\n",
|
|
||||||
info->cluster_size);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (info->has_dirty_flag && info->dirty_flag) {
|
|
||||||
func_fprintf(f, "cleanly shut down: no\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (info->has_backing_filename) {
|
|
||||||
func_fprintf(f, "backing file: %s", info->backing_filename);
|
|
||||||
if (!info->has_full_backing_filename) {
|
|
||||||
func_fprintf(f, " (cannot determine actual path)");
|
|
||||||
} else if (strcmp(info->backing_filename,
|
|
||||||
info->full_backing_filename) != 0) {
|
|
||||||
func_fprintf(f, " (actual path: %s)", info->full_backing_filename);
|
|
||||||
}
|
|
||||||
func_fprintf(f, "\n");
|
|
||||||
if (info->has_backing_filename_format) {
|
|
||||||
func_fprintf(f, "backing file format: %s\n",
|
|
||||||
info->backing_filename_format);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (info->has_snapshots) {
|
|
||||||
SnapshotInfoList *elem;
|
|
||||||
|
|
||||||
func_fprintf(f, "Snapshot list:\n");
|
|
||||||
bdrv_snapshot_dump(func_fprintf, f, NULL);
|
|
||||||
func_fprintf(f, "\n");
|
|
||||||
|
|
||||||
/* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
|
|
||||||
* we convert to the block layer's native QEMUSnapshotInfo for now.
|
|
||||||
*/
|
|
||||||
for (elem = info->snapshots; elem; elem = elem->next) {
|
|
||||||
QEMUSnapshotInfo sn = {
|
|
||||||
.vm_state_size = elem->value->vm_state_size,
|
|
||||||
.date_sec = elem->value->date_sec,
|
|
||||||
.date_nsec = elem->value->date_nsec,
|
|
||||||
.vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
|
|
||||||
elem->value->vm_clock_nsec,
|
|
||||||
};
|
|
||||||
|
|
||||||
pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
|
|
||||||
pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
|
|
||||||
bdrv_snapshot_dump(func_fprintf, f, &sn);
|
|
||||||
func_fprintf(f, "\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (info->has_format_specific) {
|
|
||||||
func_fprintf(f, "Format specific information:\n");
|
|
||||||
bdrv_image_info_specific_dump(func_fprintf, f, info->format_specific);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
317
block/qcow.c
317
block/qcow.c
@@ -25,8 +25,7 @@
|
|||||||
#include "block/block_int.h"
|
#include "block/block_int.h"
|
||||||
#include "qemu/module.h"
|
#include "qemu/module.h"
|
||||||
#include <zlib.h>
|
#include <zlib.h>
|
||||||
#include "qapi/qmp/qerror.h"
|
#include "block/aes.h"
|
||||||
#include "crypto/cipher.h"
|
|
||||||
#include "migration/migration.h"
|
#include "migration/migration.h"
|
||||||
|
|
||||||
/**************************************************************/
|
/**************************************************************/
|
||||||
@@ -49,10 +48,9 @@ typedef struct QCowHeader {
|
|||||||
uint64_t size; /* in bytes */
|
uint64_t size; /* in bytes */
|
||||||
uint8_t cluster_bits;
|
uint8_t cluster_bits;
|
||||||
uint8_t l2_bits;
|
uint8_t l2_bits;
|
||||||
uint16_t padding;
|
|
||||||
uint32_t crypt_method;
|
uint32_t crypt_method;
|
||||||
uint64_t l1_table_offset;
|
uint64_t l1_table_offset;
|
||||||
} QEMU_PACKED QCowHeader;
|
} QCowHeader;
|
||||||
|
|
||||||
#define L2_CACHE_SIZE 16
|
#define L2_CACHE_SIZE 16
|
||||||
|
|
||||||
@@ -72,8 +70,10 @@ typedef struct BDRVQcowState {
|
|||||||
uint8_t *cluster_cache;
|
uint8_t *cluster_cache;
|
||||||
uint8_t *cluster_data;
|
uint8_t *cluster_data;
|
||||||
uint64_t cluster_cache_offset;
|
uint64_t cluster_cache_offset;
|
||||||
QCryptoCipher *cipher; /* NULL if no key yet */
|
uint32_t crypt_method; /* current crypt method, 0 if no key yet */
|
||||||
uint32_t crypt_method_header;
|
uint32_t crypt_method_header;
|
||||||
|
AES_KEY aes_encrypt_key;
|
||||||
|
AES_KEY aes_decrypt_key;
|
||||||
CoMutex lock;
|
CoMutex lock;
|
||||||
Error *migration_blocker;
|
Error *migration_blocker;
|
||||||
} BDRVQcowState;
|
} BDRVQcowState;
|
||||||
@@ -92,15 +92,13 @@ static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int qcow_open(BlockDriverState *bs, QDict *options, int flags,
|
static int qcow_open(BlockDriverState *bs, int flags)
|
||||||
Error **errp)
|
|
||||||
{
|
{
|
||||||
BDRVQcowState *s = bs->opaque;
|
BDRVQcowState *s = bs->opaque;
|
||||||
unsigned int len, i, shift;
|
int len, i, shift, ret;
|
||||||
int ret;
|
|
||||||
QCowHeader header;
|
QCowHeader header;
|
||||||
|
|
||||||
ret = bdrv_pread(bs->file->bs, 0, &header, sizeof(header));
|
ret = bdrv_pread(bs->file, 0, &header, sizeof(header));
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
@@ -114,27 +112,27 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags,
|
|||||||
be64_to_cpus(&header.l1_table_offset);
|
be64_to_cpus(&header.l1_table_offset);
|
||||||
|
|
||||||
if (header.magic != QCOW_MAGIC) {
|
if (header.magic != QCOW_MAGIC) {
|
||||||
error_setg(errp, "Image not in qcow format");
|
ret = -EMEDIUMTYPE;
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
if (header.version != QCOW_VERSION) {
|
if (header.version != QCOW_VERSION) {
|
||||||
char version[64];
|
char version[64];
|
||||||
snprintf(version, sizeof(version), "QCOW version %" PRIu32,
|
snprintf(version, sizeof(version), "QCOW version %d", header.version);
|
||||||
header.version);
|
qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
|
||||||
error_setg(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
|
bs->device_name, "qcow", version);
|
||||||
bdrv_get_device_or_node_name(bs), "qcow", version);
|
|
||||||
ret = -ENOTSUP;
|
ret = -ENOTSUP;
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (header.size <= 1) {
|
if (header.size <= 1) {
|
||||||
error_setg(errp, "Image size is too small (must be at least 2 bytes)");
|
qerror_report(ERROR_CLASS_GENERIC_ERROR,
|
||||||
|
"Image size is too small (must be at least 2 bytes)");
|
||||||
ret = -EINVAL;
|
ret = -EINVAL;
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
if (header.cluster_bits < 9 || header.cluster_bits > 16) {
|
if (header.cluster_bits < 9 || header.cluster_bits > 16) {
|
||||||
error_setg(errp, "Cluster size must be between 512 and 64k");
|
qerror_report(ERROR_CLASS_GENERIC_ERROR,
|
||||||
|
"Cluster size must be between 512 and 64k");
|
||||||
ret = -EINVAL;
|
ret = -EINVAL;
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
@@ -142,18 +140,13 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags,
|
|||||||
/* l2_bits specifies number of entries; storing a uint64_t in each entry,
|
/* l2_bits specifies number of entries; storing a uint64_t in each entry,
|
||||||
* so bytes = num_entries << 3. */
|
* so bytes = num_entries << 3. */
|
||||||
if (header.l2_bits < 9 - 3 || header.l2_bits > 16 - 3) {
|
if (header.l2_bits < 9 - 3 || header.l2_bits > 16 - 3) {
|
||||||
error_setg(errp, "L2 table size must be between 512 and 64k");
|
qerror_report(ERROR_CLASS_GENERIC_ERROR,
|
||||||
|
"L2 table size must be between 512 and 64k");
|
||||||
ret = -EINVAL;
|
ret = -EINVAL;
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (header.crypt_method > QCOW_CRYPT_AES) {
|
if (header.crypt_method > QCOW_CRYPT_AES) {
|
||||||
error_setg(errp, "invalid encryption method in qcow header");
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
if (!qcrypto_cipher_supports(QCRYPTO_CIPHER_ALG_AES_128)) {
|
|
||||||
error_setg(errp, "AES cipher not available");
|
|
||||||
ret = -EINVAL;
|
ret = -EINVAL;
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
@@ -172,13 +165,13 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags,
|
|||||||
/* read the level 1 table */
|
/* read the level 1 table */
|
||||||
shift = s->cluster_bits + s->l2_bits;
|
shift = s->cluster_bits + s->l2_bits;
|
||||||
if (header.size > UINT64_MAX - (1LL << shift)) {
|
if (header.size > UINT64_MAX - (1LL << shift)) {
|
||||||
error_setg(errp, "Image too large");
|
qerror_report(ERROR_CLASS_GENERIC_ERROR, "Image too large");
|
||||||
ret = -EINVAL;
|
ret = -EINVAL;
|
||||||
goto fail;
|
goto fail;
|
||||||
} else {
|
} else {
|
||||||
uint64_t l1_size = (header.size + (1LL << shift) - 1) >> shift;
|
uint64_t l1_size = (header.size + (1LL << shift) - 1) >> shift;
|
||||||
if (l1_size > INT_MAX / sizeof(uint64_t)) {
|
if (l1_size > INT_MAX / sizeof(uint64_t)) {
|
||||||
error_setg(errp, "Image too large");
|
qerror_report(ERROR_CLASS_GENERIC_ERROR, "Image too large");
|
||||||
ret = -EINVAL;
|
ret = -EINVAL;
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
@@ -186,14 +179,9 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags,
|
|||||||
}
|
}
|
||||||
|
|
||||||
s->l1_table_offset = header.l1_table_offset;
|
s->l1_table_offset = header.l1_table_offset;
|
||||||
s->l1_table = g_try_new(uint64_t, s->l1_size);
|
s->l1_table = g_malloc(s->l1_size * sizeof(uint64_t));
|
||||||
if (s->l1_table == NULL) {
|
|
||||||
error_setg(errp, "Could not allocate memory for L1 table");
|
|
||||||
ret = -ENOMEM;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = bdrv_pread(bs->file->bs, s->l1_table_offset, s->l1_table,
|
ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table,
|
||||||
s->l1_size * sizeof(uint64_t));
|
s->l1_size * sizeof(uint64_t));
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto fail;
|
goto fail;
|
||||||
@@ -202,16 +190,8 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags,
|
|||||||
for(i = 0;i < s->l1_size; i++) {
|
for(i = 0;i < s->l1_size; i++) {
|
||||||
be64_to_cpus(&s->l1_table[i]);
|
be64_to_cpus(&s->l1_table[i]);
|
||||||
}
|
}
|
||||||
|
/* alloc L2 cache */
|
||||||
/* alloc L2 cache (max. 64k * 16 * 8 = 8 MB) */
|
s->l2_cache = g_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
|
||||||
s->l2_cache =
|
|
||||||
qemu_try_blockalign(bs->file->bs,
|
|
||||||
s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
|
|
||||||
if (s->l2_cache == NULL) {
|
|
||||||
error_setg(errp, "Could not allocate L2 table cache");
|
|
||||||
ret = -ENOMEM;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
s->cluster_cache = g_malloc(s->cluster_size);
|
s->cluster_cache = g_malloc(s->cluster_size);
|
||||||
s->cluster_data = g_malloc(s->cluster_size);
|
s->cluster_data = g_malloc(s->cluster_size);
|
||||||
s->cluster_cache_offset = -1;
|
s->cluster_cache_offset = -1;
|
||||||
@@ -219,12 +199,10 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags,
|
|||||||
/* read the backing file name */
|
/* read the backing file name */
|
||||||
if (header.backing_file_offset != 0) {
|
if (header.backing_file_offset != 0) {
|
||||||
len = header.backing_file_size;
|
len = header.backing_file_size;
|
||||||
if (len > 1023 || len >= sizeof(bs->backing_file)) {
|
if (len > 1023) {
|
||||||
error_setg(errp, "Backing file name too long");
|
len = 1023;
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
}
|
||||||
ret = bdrv_pread(bs->file->bs, header.backing_file_offset,
|
ret = bdrv_pread(bs->file, header.backing_file_offset,
|
||||||
bs->backing_file, len);
|
bs->backing_file, len);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto fail;
|
goto fail;
|
||||||
@@ -233,9 +211,9 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Disable migration when qcow images are used */
|
/* Disable migration when qcow images are used */
|
||||||
error_setg(&s->migration_blocker, "The qcow format used by node '%s' "
|
error_set(&s->migration_blocker,
|
||||||
"does not support live migration",
|
QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
|
||||||
bdrv_get_device_or_node_name(bs));
|
"qcow", bs->device_name, "live migration");
|
||||||
migrate_add_blocker(s->migration_blocker);
|
migrate_add_blocker(s->migration_blocker);
|
||||||
|
|
||||||
qemu_co_mutex_init(&s->lock);
|
qemu_co_mutex_init(&s->lock);
|
||||||
@@ -243,7 +221,7 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags,
|
|||||||
|
|
||||||
fail:
|
fail:
|
||||||
g_free(s->l1_table);
|
g_free(s->l1_table);
|
||||||
qemu_vfree(s->l2_cache);
|
g_free(s->l2_cache);
|
||||||
g_free(s->cluster_cache);
|
g_free(s->cluster_cache);
|
||||||
g_free(s->cluster_data);
|
g_free(s->cluster_data);
|
||||||
return ret;
|
return ret;
|
||||||
@@ -263,7 +241,6 @@ static int qcow_set_key(BlockDriverState *bs, const char *key)
|
|||||||
BDRVQcowState *s = bs->opaque;
|
BDRVQcowState *s = bs->opaque;
|
||||||
uint8_t keybuf[16];
|
uint8_t keybuf[16];
|
||||||
int len, i;
|
int len, i;
|
||||||
Error *err;
|
|
||||||
|
|
||||||
memset(keybuf, 0, 16);
|
memset(keybuf, 0, 16);
|
||||||
len = strlen(key);
|
len = strlen(key);
|
||||||
@@ -274,68 +251,38 @@ static int qcow_set_key(BlockDriverState *bs, const char *key)
|
|||||||
for(i = 0;i < len;i++) {
|
for(i = 0;i < len;i++) {
|
||||||
keybuf[i] = key[i];
|
keybuf[i] = key[i];
|
||||||
}
|
}
|
||||||
assert(bs->encrypted);
|
s->crypt_method = s->crypt_method_header;
|
||||||
|
|
||||||
qcrypto_cipher_free(s->cipher);
|
if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
|
||||||
s->cipher = qcrypto_cipher_new(
|
return -1;
|
||||||
QCRYPTO_CIPHER_ALG_AES_128,
|
if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
|
||||||
QCRYPTO_CIPHER_MODE_CBC,
|
|
||||||
keybuf, G_N_ELEMENTS(keybuf),
|
|
||||||
&err);
|
|
||||||
|
|
||||||
if (!s->cipher) {
|
|
||||||
/* XXX would be nice if errors in this method could
|
|
||||||
* be properly propagate to the caller. Would need
|
|
||||||
* the bdrv_set_key() API signature to be fixed. */
|
|
||||||
error_free(err);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* The crypt function is compatible with the linux cryptoloop
|
/* The crypt function is compatible with the linux cryptoloop
|
||||||
algorithm for < 4 GB images. NOTE: out_buf == in_buf is
|
algorithm for < 4 GB images. NOTE: out_buf == in_buf is
|
||||||
supported */
|
supported */
|
||||||
static int encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
|
static void encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
|
||||||
uint8_t *out_buf, const uint8_t *in_buf,
|
uint8_t *out_buf, const uint8_t *in_buf,
|
||||||
int nb_sectors, bool enc, Error **errp)
|
int nb_sectors, int enc,
|
||||||
|
const AES_KEY *key)
|
||||||
{
|
{
|
||||||
union {
|
union {
|
||||||
uint64_t ll[2];
|
uint64_t ll[2];
|
||||||
uint8_t b[16];
|
uint8_t b[16];
|
||||||
} ivec;
|
} ivec;
|
||||||
int i;
|
int i;
|
||||||
int ret;
|
|
||||||
|
|
||||||
for(i = 0; i < nb_sectors; i++) {
|
for(i = 0; i < nb_sectors; i++) {
|
||||||
ivec.ll[0] = cpu_to_le64(sector_num);
|
ivec.ll[0] = cpu_to_le64(sector_num);
|
||||||
ivec.ll[1] = 0;
|
ivec.ll[1] = 0;
|
||||||
if (qcrypto_cipher_setiv(s->cipher,
|
AES_cbc_encrypt(in_buf, out_buf, 512, key,
|
||||||
ivec.b, G_N_ELEMENTS(ivec.b),
|
ivec.b, enc);
|
||||||
errp) < 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
if (enc) {
|
|
||||||
ret = qcrypto_cipher_encrypt(s->cipher,
|
|
||||||
in_buf,
|
|
||||||
out_buf,
|
|
||||||
512,
|
|
||||||
errp);
|
|
||||||
} else {
|
|
||||||
ret = qcrypto_cipher_decrypt(s->cipher,
|
|
||||||
in_buf,
|
|
||||||
out_buf,
|
|
||||||
512,
|
|
||||||
errp);
|
|
||||||
}
|
|
||||||
if (ret < 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
sector_num++;
|
sector_num++;
|
||||||
in_buf += 512;
|
in_buf += 512;
|
||||||
out_buf += 512;
|
out_buf += 512;
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 'allocate' is:
|
/* 'allocate' is:
|
||||||
@@ -369,13 +316,13 @@ static uint64_t get_cluster_offset(BlockDriverState *bs,
|
|||||||
if (!allocate)
|
if (!allocate)
|
||||||
return 0;
|
return 0;
|
||||||
/* allocate a new l2 entry */
|
/* allocate a new l2 entry */
|
||||||
l2_offset = bdrv_getlength(bs->file->bs);
|
l2_offset = bdrv_getlength(bs->file);
|
||||||
/* round to cluster size */
|
/* round to cluster size */
|
||||||
l2_offset = (l2_offset + s->cluster_size - 1) & ~(s->cluster_size - 1);
|
l2_offset = (l2_offset + s->cluster_size - 1) & ~(s->cluster_size - 1);
|
||||||
/* update the L1 entry */
|
/* update the L1 entry */
|
||||||
s->l1_table[l1_index] = l2_offset;
|
s->l1_table[l1_index] = l2_offset;
|
||||||
tmp = cpu_to_be64(l2_offset);
|
tmp = cpu_to_be64(l2_offset);
|
||||||
if (bdrv_pwrite_sync(bs->file->bs,
|
if (bdrv_pwrite_sync(bs->file,
|
||||||
s->l1_table_offset + l1_index * sizeof(tmp),
|
s->l1_table_offset + l1_index * sizeof(tmp),
|
||||||
&tmp, sizeof(tmp)) < 0)
|
&tmp, sizeof(tmp)) < 0)
|
||||||
return 0;
|
return 0;
|
||||||
@@ -405,12 +352,11 @@ static uint64_t get_cluster_offset(BlockDriverState *bs,
|
|||||||
l2_table = s->l2_cache + (min_index << s->l2_bits);
|
l2_table = s->l2_cache + (min_index << s->l2_bits);
|
||||||
if (new_l2_table) {
|
if (new_l2_table) {
|
||||||
memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
|
memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
|
||||||
if (bdrv_pwrite_sync(bs->file->bs, l2_offset, l2_table,
|
if (bdrv_pwrite_sync(bs->file, l2_offset, l2_table,
|
||||||
s->l2_size * sizeof(uint64_t)) < 0)
|
s->l2_size * sizeof(uint64_t)) < 0)
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
if (bdrv_pread(bs->file->bs, l2_offset, l2_table,
|
if (bdrv_pread(bs->file, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) !=
|
||||||
s->l2_size * sizeof(uint64_t)) !=
|
|
||||||
s->l2_size * sizeof(uint64_t))
|
s->l2_size * sizeof(uint64_t))
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -431,42 +377,34 @@ static uint64_t get_cluster_offset(BlockDriverState *bs,
|
|||||||
overwritten */
|
overwritten */
|
||||||
if (decompress_cluster(bs, cluster_offset) < 0)
|
if (decompress_cluster(bs, cluster_offset) < 0)
|
||||||
return 0;
|
return 0;
|
||||||
cluster_offset = bdrv_getlength(bs->file->bs);
|
cluster_offset = bdrv_getlength(bs->file);
|
||||||
cluster_offset = (cluster_offset + s->cluster_size - 1) &
|
cluster_offset = (cluster_offset + s->cluster_size - 1) &
|
||||||
~(s->cluster_size - 1);
|
~(s->cluster_size - 1);
|
||||||
/* write the cluster content */
|
/* write the cluster content */
|
||||||
if (bdrv_pwrite(bs->file->bs, cluster_offset, s->cluster_cache,
|
if (bdrv_pwrite(bs->file, cluster_offset, s->cluster_cache, s->cluster_size) !=
|
||||||
s->cluster_size) !=
|
|
||||||
s->cluster_size)
|
s->cluster_size)
|
||||||
return -1;
|
return -1;
|
||||||
} else {
|
} else {
|
||||||
cluster_offset = bdrv_getlength(bs->file->bs);
|
cluster_offset = bdrv_getlength(bs->file);
|
||||||
if (allocate == 1) {
|
if (allocate == 1) {
|
||||||
/* round to cluster size */
|
/* round to cluster size */
|
||||||
cluster_offset = (cluster_offset + s->cluster_size - 1) &
|
cluster_offset = (cluster_offset + s->cluster_size - 1) &
|
||||||
~(s->cluster_size - 1);
|
~(s->cluster_size - 1);
|
||||||
bdrv_truncate(bs->file->bs, cluster_offset + s->cluster_size);
|
bdrv_truncate(bs->file, cluster_offset + s->cluster_size);
|
||||||
/* if encrypted, we must initialize the cluster
|
/* if encrypted, we must initialize the cluster
|
||||||
content which won't be written */
|
content which won't be written */
|
||||||
if (bs->encrypted &&
|
if (s->crypt_method &&
|
||||||
(n_end - n_start) < s->cluster_sectors) {
|
(n_end - n_start) < s->cluster_sectors) {
|
||||||
uint64_t start_sect;
|
uint64_t start_sect;
|
||||||
assert(s->cipher);
|
|
||||||
start_sect = (offset & ~(s->cluster_size - 1)) >> 9;
|
start_sect = (offset & ~(s->cluster_size - 1)) >> 9;
|
||||||
memset(s->cluster_data + 512, 0x00, 512);
|
memset(s->cluster_data + 512, 0x00, 512);
|
||||||
for(i = 0; i < s->cluster_sectors; i++) {
|
for(i = 0; i < s->cluster_sectors; i++) {
|
||||||
if (i < n_start || i >= n_end) {
|
if (i < n_start || i >= n_end) {
|
||||||
Error *err = NULL;
|
encrypt_sectors(s, start_sect + i,
|
||||||
if (encrypt_sectors(s, start_sect + i,
|
|
||||||
s->cluster_data,
|
s->cluster_data,
|
||||||
s->cluster_data + 512, 1,
|
s->cluster_data + 512, 1, 1,
|
||||||
true, &err) < 0) {
|
&s->aes_encrypt_key);
|
||||||
error_free(err);
|
if (bdrv_pwrite(bs->file, cluster_offset + i * 512,
|
||||||
errno = EIO;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
if (bdrv_pwrite(bs->file->bs,
|
|
||||||
cluster_offset + i * 512,
|
|
||||||
s->cluster_data, 512) != 512)
|
s->cluster_data, 512) != 512)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -480,14 +418,14 @@ static uint64_t get_cluster_offset(BlockDriverState *bs,
|
|||||||
/* update L2 table */
|
/* update L2 table */
|
||||||
tmp = cpu_to_be64(cluster_offset);
|
tmp = cpu_to_be64(cluster_offset);
|
||||||
l2_table[l2_index] = tmp;
|
l2_table[l2_index] = tmp;
|
||||||
if (bdrv_pwrite_sync(bs->file->bs, l2_offset + l2_index * sizeof(tmp),
|
if (bdrv_pwrite_sync(bs->file, l2_offset + l2_index * sizeof(tmp),
|
||||||
&tmp, sizeof(tmp)) < 0)
|
&tmp, sizeof(tmp)) < 0)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return cluster_offset;
|
return cluster_offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int64_t coroutine_fn qcow_co_get_block_status(BlockDriverState *bs,
|
static int coroutine_fn qcow_co_is_allocated(BlockDriverState *bs,
|
||||||
int64_t sector_num, int nb_sectors, int *pnum)
|
int64_t sector_num, int nb_sectors, int *pnum)
|
||||||
{
|
{
|
||||||
BDRVQcowState *s = bs->opaque;
|
BDRVQcowState *s = bs->opaque;
|
||||||
@@ -502,14 +440,7 @@ static int64_t coroutine_fn qcow_co_get_block_status(BlockDriverState *bs,
|
|||||||
if (n > nb_sectors)
|
if (n > nb_sectors)
|
||||||
n = nb_sectors;
|
n = nb_sectors;
|
||||||
*pnum = n;
|
*pnum = n;
|
||||||
if (!cluster_offset) {
|
return (cluster_offset != 0);
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if ((cluster_offset & QCOW_OFLAG_COMPRESSED) || s->cipher) {
|
|
||||||
return BDRV_BLOCK_DATA;
|
|
||||||
}
|
|
||||||
cluster_offset |= (index_in_cluster << BDRV_SECTOR_BITS);
|
|
||||||
return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | cluster_offset;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
|
static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
|
||||||
@@ -549,7 +480,7 @@ static int decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset)
|
|||||||
if (s->cluster_cache_offset != coffset) {
|
if (s->cluster_cache_offset != coffset) {
|
||||||
csize = cluster_offset >> (63 - s->cluster_bits);
|
csize = cluster_offset >> (63 - s->cluster_bits);
|
||||||
csize &= (s->cluster_size - 1);
|
csize &= (s->cluster_size - 1);
|
||||||
ret = bdrv_pread(bs->file->bs, coffset, s->cluster_data, csize);
|
ret = bdrv_pread(bs->file, coffset, s->cluster_data, csize);
|
||||||
if (ret != csize)
|
if (ret != csize)
|
||||||
return -1;
|
return -1;
|
||||||
if (decompress_buffer(s->cluster_cache, s->cluster_size,
|
if (decompress_buffer(s->cluster_cache, s->cluster_size,
|
||||||
@@ -572,13 +503,9 @@ static coroutine_fn int qcow_co_readv(BlockDriverState *bs, int64_t sector_num,
|
|||||||
QEMUIOVector hd_qiov;
|
QEMUIOVector hd_qiov;
|
||||||
uint8_t *buf;
|
uint8_t *buf;
|
||||||
void *orig_buf;
|
void *orig_buf;
|
||||||
Error *err = NULL;
|
|
||||||
|
|
||||||
if (qiov->niov > 1) {
|
if (qiov->niov > 1) {
|
||||||
buf = orig_buf = qemu_try_blockalign(bs, qiov->size);
|
buf = orig_buf = qemu_blockalign(bs, qiov->size);
|
||||||
if (buf == NULL) {
|
|
||||||
return -ENOMEM;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
orig_buf = NULL;
|
orig_buf = NULL;
|
||||||
buf = (uint8_t *)qiov->iov->iov_base;
|
buf = (uint8_t *)qiov->iov->iov_base;
|
||||||
@@ -597,13 +524,13 @@ static coroutine_fn int qcow_co_readv(BlockDriverState *bs, int64_t sector_num,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!cluster_offset) {
|
if (!cluster_offset) {
|
||||||
if (bs->backing) {
|
if (bs->backing_hd) {
|
||||||
/* read from the base image */
|
/* read from the base image */
|
||||||
hd_iov.iov_base = (void *)buf;
|
hd_iov.iov_base = (void *)buf;
|
||||||
hd_iov.iov_len = n * 512;
|
hd_iov.iov_len = n * 512;
|
||||||
qemu_iovec_init_external(&hd_qiov, &hd_iov, 1);
|
qemu_iovec_init_external(&hd_qiov, &hd_iov, 1);
|
||||||
qemu_co_mutex_unlock(&s->lock);
|
qemu_co_mutex_unlock(&s->lock);
|
||||||
ret = bdrv_co_readv(bs->backing->bs, sector_num,
|
ret = bdrv_co_readv(bs->backing_hd, sector_num,
|
||||||
n, &hd_qiov);
|
n, &hd_qiov);
|
||||||
qemu_co_mutex_lock(&s->lock);
|
qemu_co_mutex_lock(&s->lock);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
@@ -628,19 +555,17 @@ static coroutine_fn int qcow_co_readv(BlockDriverState *bs, int64_t sector_num,
|
|||||||
hd_iov.iov_len = n * 512;
|
hd_iov.iov_len = n * 512;
|
||||||
qemu_iovec_init_external(&hd_qiov, &hd_iov, 1);
|
qemu_iovec_init_external(&hd_qiov, &hd_iov, 1);
|
||||||
qemu_co_mutex_unlock(&s->lock);
|
qemu_co_mutex_unlock(&s->lock);
|
||||||
ret = bdrv_co_readv(bs->file->bs,
|
ret = bdrv_co_readv(bs->file,
|
||||||
(cluster_offset >> 9) + index_in_cluster,
|
(cluster_offset >> 9) + index_in_cluster,
|
||||||
n, &hd_qiov);
|
n, &hd_qiov);
|
||||||
qemu_co_mutex_lock(&s->lock);
|
qemu_co_mutex_lock(&s->lock);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (bs->encrypted) {
|
if (s->crypt_method) {
|
||||||
assert(s->cipher);
|
encrypt_sectors(s, sector_num, buf, buf,
|
||||||
if (encrypt_sectors(s, sector_num, buf, buf,
|
n, 0,
|
||||||
n, false, &err) < 0) {
|
&s->aes_decrypt_key);
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ret = 0;
|
ret = 0;
|
||||||
@@ -661,7 +586,6 @@ done:
|
|||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
fail:
|
fail:
|
||||||
error_free(err);
|
|
||||||
ret = -EIO;
|
ret = -EIO;
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
@@ -683,10 +607,7 @@ static coroutine_fn int qcow_co_writev(BlockDriverState *bs, int64_t sector_num,
|
|||||||
s->cluster_cache_offset = -1; /* disable compressed cache */
|
s->cluster_cache_offset = -1; /* disable compressed cache */
|
||||||
|
|
||||||
if (qiov->niov > 1) {
|
if (qiov->niov > 1) {
|
||||||
buf = orig_buf = qemu_try_blockalign(bs, qiov->size);
|
buf = orig_buf = qemu_blockalign(bs, qiov->size);
|
||||||
if (buf == NULL) {
|
|
||||||
return -ENOMEM;
|
|
||||||
}
|
|
||||||
qemu_iovec_to_buf(qiov, 0, buf, qiov->size);
|
qemu_iovec_to_buf(qiov, 0, buf, qiov->size);
|
||||||
} else {
|
} else {
|
||||||
orig_buf = NULL;
|
orig_buf = NULL;
|
||||||
@@ -709,18 +630,12 @@ static coroutine_fn int qcow_co_writev(BlockDriverState *bs, int64_t sector_num,
|
|||||||
ret = -EIO;
|
ret = -EIO;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (bs->encrypted) {
|
if (s->crypt_method) {
|
||||||
Error *err = NULL;
|
|
||||||
assert(s->cipher);
|
|
||||||
if (!cluster_data) {
|
if (!cluster_data) {
|
||||||
cluster_data = g_malloc0(s->cluster_size);
|
cluster_data = g_malloc0(s->cluster_size);
|
||||||
}
|
}
|
||||||
if (encrypt_sectors(s, sector_num, cluster_data, buf,
|
encrypt_sectors(s, sector_num, cluster_data, buf,
|
||||||
n, true, &err) < 0) {
|
n, 1, &s->aes_encrypt_key);
|
||||||
error_free(err);
|
|
||||||
ret = -EIO;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
src_buf = cluster_data;
|
src_buf = cluster_data;
|
||||||
} else {
|
} else {
|
||||||
src_buf = buf;
|
src_buf = buf;
|
||||||
@@ -730,7 +645,7 @@ static coroutine_fn int qcow_co_writev(BlockDriverState *bs, int64_t sector_num,
|
|||||||
hd_iov.iov_len = n * 512;
|
hd_iov.iov_len = n * 512;
|
||||||
qemu_iovec_init_external(&hd_qiov, &hd_iov, 1);
|
qemu_iovec_init_external(&hd_qiov, &hd_iov, 1);
|
||||||
qemu_co_mutex_unlock(&s->lock);
|
qemu_co_mutex_unlock(&s->lock);
|
||||||
ret = bdrv_co_writev(bs->file->bs,
|
ret = bdrv_co_writev(bs->file,
|
||||||
(cluster_offset >> 9) + index_in_cluster,
|
(cluster_offset >> 9) + index_in_cluster,
|
||||||
n, &hd_qiov);
|
n, &hd_qiov);
|
||||||
qemu_co_mutex_lock(&s->lock);
|
qemu_co_mutex_lock(&s->lock);
|
||||||
@@ -757,10 +672,8 @@ static void qcow_close(BlockDriverState *bs)
|
|||||||
{
|
{
|
||||||
BDRVQcowState *s = bs->opaque;
|
BDRVQcowState *s = bs->opaque;
|
||||||
|
|
||||||
qcrypto_cipher_free(s->cipher);
|
|
||||||
s->cipher = NULL;
|
|
||||||
g_free(s->l1_table);
|
g_free(s->l1_table);
|
||||||
qemu_vfree(s->l2_cache);
|
g_free(s->l2_cache);
|
||||||
g_free(s->cluster_cache);
|
g_free(s->cluster_cache);
|
||||||
g_free(s->cluster_data);
|
g_free(s->cluster_data);
|
||||||
|
|
||||||
@@ -768,38 +681,37 @@ static void qcow_close(BlockDriverState *bs)
|
|||||||
error_free(s->migration_blocker);
|
error_free(s->migration_blocker);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int qcow_create(const char *filename, QemuOpts *opts, Error **errp)
|
static int qcow_create(const char *filename, QEMUOptionParameter *options)
|
||||||
{
|
{
|
||||||
int header_size, backing_filename_len, l1_size, shift, i;
|
int header_size, backing_filename_len, l1_size, shift, i;
|
||||||
QCowHeader header;
|
QCowHeader header;
|
||||||
uint8_t *tmp;
|
uint8_t *tmp;
|
||||||
int64_t total_size = 0;
|
int64_t total_size = 0;
|
||||||
char *backing_file = NULL;
|
const char *backing_file = NULL;
|
||||||
int flags = 0;
|
int flags = 0;
|
||||||
Error *local_err = NULL;
|
|
||||||
int ret;
|
int ret;
|
||||||
BlockDriverState *qcow_bs;
|
BlockDriverState *qcow_bs;
|
||||||
|
|
||||||
/* Read out options */
|
/* Read out options */
|
||||||
total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
|
while (options && options->name) {
|
||||||
BDRV_SECTOR_SIZE);
|
if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
|
||||||
backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
|
total_size = options->value.n / 512;
|
||||||
if (qemu_opt_get_bool_del(opts, BLOCK_OPT_ENCRYPT, false)) {
|
} else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
|
||||||
flags |= BLOCK_FLAG_ENCRYPT;
|
backing_file = options->value.s;
|
||||||
|
} else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) {
|
||||||
|
flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0;
|
||||||
|
}
|
||||||
|
options++;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = bdrv_create_file(filename, opts, &local_err);
|
ret = bdrv_create_file(filename, options);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_propagate(errp, local_err);
|
return ret;
|
||||||
goto cleanup;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
qcow_bs = NULL;
|
ret = bdrv_file_open(&qcow_bs, filename, BDRV_O_RDWR);
|
||||||
ret = bdrv_open(&qcow_bs, filename, NULL, NULL,
|
|
||||||
BDRV_O_RDWR | BDRV_O_PROTOCOL, &local_err);
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_propagate(errp, local_err);
|
return ret;
|
||||||
goto cleanup;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = bdrv_truncate(qcow_bs, 0);
|
ret = bdrv_truncate(qcow_bs, 0);
|
||||||
@@ -810,7 +722,7 @@ static int qcow_create(const char *filename, QemuOpts *opts, Error **errp)
|
|||||||
memset(&header, 0, sizeof(header));
|
memset(&header, 0, sizeof(header));
|
||||||
header.magic = cpu_to_be32(QCOW_MAGIC);
|
header.magic = cpu_to_be32(QCOW_MAGIC);
|
||||||
header.version = cpu_to_be32(QCOW_VERSION);
|
header.version = cpu_to_be32(QCOW_VERSION);
|
||||||
header.size = cpu_to_be64(total_size);
|
header.size = cpu_to_be64(total_size * 512);
|
||||||
header_size = sizeof(header);
|
header_size = sizeof(header);
|
||||||
backing_filename_len = 0;
|
backing_filename_len = 0;
|
||||||
if (backing_file) {
|
if (backing_file) {
|
||||||
@@ -824,7 +736,7 @@ static int qcow_create(const char *filename, QemuOpts *opts, Error **errp)
|
|||||||
backing_file = NULL;
|
backing_file = NULL;
|
||||||
}
|
}
|
||||||
header.cluster_bits = 9; /* 512 byte cluster to avoid copying
|
header.cluster_bits = 9; /* 512 byte cluster to avoid copying
|
||||||
unmodified sectors */
|
unmodifyed sectors */
|
||||||
header.l2_bits = 12; /* 32 KB L2 tables */
|
header.l2_bits = 12; /* 32 KB L2 tables */
|
||||||
} else {
|
} else {
|
||||||
header.cluster_bits = 12; /* 4 KB clusters */
|
header.cluster_bits = 12; /* 4 KB clusters */
|
||||||
@@ -832,7 +744,7 @@ static int qcow_create(const char *filename, QemuOpts *opts, Error **errp)
|
|||||||
}
|
}
|
||||||
header_size = (header_size + 7) & ~7;
|
header_size = (header_size + 7) & ~7;
|
||||||
shift = header.cluster_bits + header.l2_bits;
|
shift = header.cluster_bits + header.l2_bits;
|
||||||
l1_size = (total_size + (1LL << shift) - 1) >> shift;
|
l1_size = ((total_size * 512) + (1LL << shift) - 1) >> shift;
|
||||||
|
|
||||||
header.l1_table_offset = cpu_to_be64(header_size);
|
header.l1_table_offset = cpu_to_be64(header_size);
|
||||||
if (flags & BLOCK_FLAG_ENCRYPT) {
|
if (flags & BLOCK_FLAG_ENCRYPT) {
|
||||||
@@ -869,9 +781,7 @@ static int qcow_create(const char *filename, QemuOpts *opts, Error **errp)
|
|||||||
g_free(tmp);
|
g_free(tmp);
|
||||||
ret = 0;
|
ret = 0;
|
||||||
exit:
|
exit:
|
||||||
bdrv_unref(qcow_bs);
|
bdrv_delete(qcow_bs);
|
||||||
cleanup:
|
|
||||||
g_free(backing_file);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -882,10 +792,10 @@ static int qcow_make_empty(BlockDriverState *bs)
|
|||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
memset(s->l1_table, 0, l1_length);
|
memset(s->l1_table, 0, l1_length);
|
||||||
if (bdrv_pwrite_sync(bs->file->bs, s->l1_table_offset, s->l1_table,
|
if (bdrv_pwrite_sync(bs->file, s->l1_table_offset, s->l1_table,
|
||||||
l1_length) < 0)
|
l1_length) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
ret = bdrv_truncate(bs->file->bs, s->l1_table_offset + l1_length);
|
ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
@@ -907,21 +817,8 @@ static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
|
|||||||
uint8_t *out_buf;
|
uint8_t *out_buf;
|
||||||
uint64_t cluster_offset;
|
uint64_t cluster_offset;
|
||||||
|
|
||||||
if (nb_sectors != s->cluster_sectors) {
|
if (nb_sectors != s->cluster_sectors)
|
||||||
ret = -EINVAL;
|
return -EINVAL;
|
||||||
|
|
||||||
/* Zero-pad last write if image size is not cluster aligned */
|
|
||||||
if (sector_num + nb_sectors == bs->total_sectors &&
|
|
||||||
nb_sectors < s->cluster_sectors) {
|
|
||||||
uint8_t *pad_buf = qemu_blockalign(bs, s->cluster_size);
|
|
||||||
memset(pad_buf, 0, s->cluster_size);
|
|
||||||
memcpy(pad_buf, buf, nb_sectors * BDRV_SECTOR_SIZE);
|
|
||||||
ret = qcow_write_compressed(bs, sector_num,
|
|
||||||
pad_buf, s->cluster_sectors);
|
|
||||||
qemu_vfree(pad_buf);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
out_buf = g_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
|
out_buf = g_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
|
||||||
|
|
||||||
@@ -965,7 +862,7 @@ static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
|
|||||||
}
|
}
|
||||||
|
|
||||||
cluster_offset &= s->cluster_offset_mask;
|
cluster_offset &= s->cluster_offset_mask;
|
||||||
ret = bdrv_pwrite(bs->file->bs, cluster_offset, out_buf, out_len);
|
ret = bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
@@ -984,28 +881,24 @@ static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static QemuOptsList qcow_create_opts = {
|
|
||||||
.name = "qcow-create-opts",
|
static QEMUOptionParameter qcow_create_options[] = {
|
||||||
.head = QTAILQ_HEAD_INITIALIZER(qcow_create_opts.head),
|
|
||||||
.desc = {
|
|
||||||
{
|
{
|
||||||
.name = BLOCK_OPT_SIZE,
|
.name = BLOCK_OPT_SIZE,
|
||||||
.type = QEMU_OPT_SIZE,
|
.type = OPT_SIZE,
|
||||||
.help = "Virtual disk size"
|
.help = "Virtual disk size"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = BLOCK_OPT_BACKING_FILE,
|
.name = BLOCK_OPT_BACKING_FILE,
|
||||||
.type = QEMU_OPT_STRING,
|
.type = OPT_STRING,
|
||||||
.help = "File name of a base image"
|
.help = "File name of a base image"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = BLOCK_OPT_ENCRYPT,
|
.name = BLOCK_OPT_ENCRYPT,
|
||||||
.type = QEMU_OPT_BOOL,
|
.type = OPT_FLAG,
|
||||||
.help = "Encrypt the image",
|
.help = "Encrypt the image"
|
||||||
.def_value_str = "off"
|
|
||||||
},
|
},
|
||||||
{ /* end of list */ }
|
{ NULL }
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static BlockDriver bdrv_qcow = {
|
static BlockDriver bdrv_qcow = {
|
||||||
@@ -1016,19 +909,17 @@ static BlockDriver bdrv_qcow = {
|
|||||||
.bdrv_close = qcow_close,
|
.bdrv_close = qcow_close,
|
||||||
.bdrv_reopen_prepare = qcow_reopen_prepare,
|
.bdrv_reopen_prepare = qcow_reopen_prepare,
|
||||||
.bdrv_create = qcow_create,
|
.bdrv_create = qcow_create,
|
||||||
.bdrv_has_zero_init = bdrv_has_zero_init_1,
|
|
||||||
.supports_backing = true,
|
|
||||||
|
|
||||||
.bdrv_co_readv = qcow_co_readv,
|
.bdrv_co_readv = qcow_co_readv,
|
||||||
.bdrv_co_writev = qcow_co_writev,
|
.bdrv_co_writev = qcow_co_writev,
|
||||||
.bdrv_co_get_block_status = qcow_co_get_block_status,
|
.bdrv_co_is_allocated = qcow_co_is_allocated,
|
||||||
|
|
||||||
.bdrv_set_key = qcow_set_key,
|
.bdrv_set_key = qcow_set_key,
|
||||||
.bdrv_make_empty = qcow_make_empty,
|
.bdrv_make_empty = qcow_make_empty,
|
||||||
.bdrv_write_compressed = qcow_write_compressed,
|
.bdrv_write_compressed = qcow_write_compressed,
|
||||||
.bdrv_get_info = qcow_get_info,
|
.bdrv_get_info = qcow_get_info,
|
||||||
|
|
||||||
.create_opts = &qcow_create_opts,
|
.create_options = qcow_create_options,
|
||||||
};
|
};
|
||||||
|
|
||||||
static void bdrv_qcow_init(void)
|
static void bdrv_qcow_init(void)
|
||||||
|
|||||||
@@ -22,24 +22,17 @@
|
|||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* Needed for CONFIG_MADVISE */
|
|
||||||
#include "config-host.h"
|
|
||||||
|
|
||||||
#if defined(CONFIG_MADVISE) || defined(CONFIG_POSIX_MADVISE)
|
|
||||||
#include <sys/mman.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "block/block_int.h"
|
#include "block/block_int.h"
|
||||||
#include "qemu-common.h"
|
#include "qemu-common.h"
|
||||||
#include "qemu/osdep.h"
|
|
||||||
#include "qcow2.h"
|
#include "qcow2.h"
|
||||||
#include "trace.h"
|
#include "trace.h"
|
||||||
|
|
||||||
typedef struct Qcow2CachedTable {
|
typedef struct Qcow2CachedTable {
|
||||||
|
void* table;
|
||||||
int64_t offset;
|
int64_t offset;
|
||||||
uint64_t lru_counter;
|
|
||||||
int ref;
|
|
||||||
bool dirty;
|
bool dirty;
|
||||||
|
int cache_hits;
|
||||||
|
int ref;
|
||||||
} Qcow2CachedTable;
|
} Qcow2CachedTable;
|
||||||
|
|
||||||
struct Qcow2Cache {
|
struct Qcow2Cache {
|
||||||
@@ -47,94 +40,20 @@ struct Qcow2Cache {
|
|||||||
struct Qcow2Cache* depends;
|
struct Qcow2Cache* depends;
|
||||||
int size;
|
int size;
|
||||||
bool depends_on_flush;
|
bool depends_on_flush;
|
||||||
void *table_array;
|
|
||||||
uint64_t lru_counter;
|
|
||||||
uint64_t cache_clean_lru_counter;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static inline void *qcow2_cache_get_table_addr(BlockDriverState *bs,
|
|
||||||
Qcow2Cache *c, int table)
|
|
||||||
{
|
|
||||||
BDRVQcow2State *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)
|
|
||||||
{
|
|
||||||
BDRVQcow2State *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;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void qcow2_cache_table_release(BlockDriverState *bs, Qcow2Cache *c,
|
|
||||||
int i, int num_tables)
|
|
||||||
{
|
|
||||||
#if QEMU_MADV_DONTNEED != QEMU_MADV_INVALID
|
|
||||||
BDRVQcow2State *s = bs->opaque;
|
|
||||||
void *t = qcow2_cache_get_table_addr(bs, c, i);
|
|
||||||
int align = getpagesize();
|
|
||||||
size_t mem_size = (size_t) s->cluster_size * num_tables;
|
|
||||||
size_t offset = QEMU_ALIGN_UP((uintptr_t) t, align) - (uintptr_t) t;
|
|
||||||
size_t length = QEMU_ALIGN_DOWN(mem_size - offset, align);
|
|
||||||
if (length > 0) {
|
|
||||||
qemu_madvise((uint8_t *) t + offset, length, QEMU_MADV_DONTNEED);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline bool can_clean_entry(Qcow2Cache *c, int i)
|
|
||||||
{
|
|
||||||
Qcow2CachedTable *t = &c->entries[i];
|
|
||||||
return t->ref == 0 && !t->dirty && t->offset != 0 &&
|
|
||||||
t->lru_counter <= c->cache_clean_lru_counter;
|
|
||||||
}
|
|
||||||
|
|
||||||
void qcow2_cache_clean_unused(BlockDriverState *bs, Qcow2Cache *c)
|
|
||||||
{
|
|
||||||
int i = 0;
|
|
||||||
while (i < c->size) {
|
|
||||||
int to_clean = 0;
|
|
||||||
|
|
||||||
/* Skip the entries that we don't need to clean */
|
|
||||||
while (i < c->size && !can_clean_entry(c, i)) {
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* And count how many we can clean in a row */
|
|
||||||
while (i < c->size && can_clean_entry(c, i)) {
|
|
||||||
c->entries[i].offset = 0;
|
|
||||||
c->entries[i].lru_counter = 0;
|
|
||||||
i++;
|
|
||||||
to_clean++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (to_clean > 0) {
|
|
||||||
qcow2_cache_table_release(bs, c, i - to_clean, to_clean);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
c->cache_clean_lru_counter = c->lru_counter;
|
|
||||||
}
|
|
||||||
|
|
||||||
Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables)
|
Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables)
|
||||||
{
|
{
|
||||||
BDRVQcow2State *s = bs->opaque;
|
BDRVQcowState *s = bs->opaque;
|
||||||
Qcow2Cache *c;
|
Qcow2Cache *c;
|
||||||
|
int i;
|
||||||
|
|
||||||
c = g_new0(Qcow2Cache, 1);
|
c = g_malloc0(sizeof(*c));
|
||||||
c->size = num_tables;
|
c->size = num_tables;
|
||||||
c->entries = g_try_new0(Qcow2CachedTable, num_tables);
|
c->entries = g_malloc0(sizeof(*c->entries) * num_tables);
|
||||||
c->table_array = qemu_try_blockalign(bs->file->bs,
|
|
||||||
(size_t) num_tables * s->cluster_size);
|
|
||||||
|
|
||||||
if (!c->entries || !c->table_array) {
|
for (i = 0; i < c->size; i++) {
|
||||||
qemu_vfree(c->table_array);
|
c->entries[i].table = qemu_blockalign(bs, s->cluster_size);
|
||||||
g_free(c->entries);
|
|
||||||
g_free(c);
|
|
||||||
c = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return c;
|
return c;
|
||||||
@@ -146,9 +65,9 @@ int qcow2_cache_destroy(BlockDriverState *bs, Qcow2Cache *c)
|
|||||||
|
|
||||||
for (i = 0; i < c->size; i++) {
|
for (i = 0; i < c->size; i++) {
|
||||||
assert(c->entries[i].ref == 0);
|
assert(c->entries[i].ref == 0);
|
||||||
|
qemu_vfree(c->entries[i].table);
|
||||||
}
|
}
|
||||||
|
|
||||||
qemu_vfree(c->table_array);
|
|
||||||
g_free(c->entries);
|
g_free(c->entries);
|
||||||
g_free(c);
|
g_free(c);
|
||||||
|
|
||||||
@@ -172,7 +91,7 @@ static int qcow2_cache_flush_dependency(BlockDriverState *bs, Qcow2Cache *c)
|
|||||||
|
|
||||||
static int qcow2_cache_entry_flush(BlockDriverState *bs, Qcow2Cache *c, int i)
|
static int qcow2_cache_entry_flush(BlockDriverState *bs, Qcow2Cache *c, int i)
|
||||||
{
|
{
|
||||||
BDRVQcow2State *s = bs->opaque;
|
BDRVQcowState *s = bs->opaque;
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
if (!c->entries[i].dirty || !c->entries[i].offset) {
|
if (!c->entries[i].dirty || !c->entries[i].offset) {
|
||||||
@@ -185,7 +104,7 @@ static int qcow2_cache_entry_flush(BlockDriverState *bs, Qcow2Cache *c, int i)
|
|||||||
if (c->depends) {
|
if (c->depends) {
|
||||||
ret = qcow2_cache_flush_dependency(bs, c);
|
ret = qcow2_cache_flush_dependency(bs, c);
|
||||||
} else if (c->depends_on_flush) {
|
} else if (c->depends_on_flush) {
|
||||||
ret = bdrv_flush(bs->file->bs);
|
ret = bdrv_flush(bs->file);
|
||||||
if (ret >= 0) {
|
if (ret >= 0) {
|
||||||
c->depends_on_flush = false;
|
c->depends_on_flush = false;
|
||||||
}
|
}
|
||||||
@@ -195,29 +114,14 @@ static int qcow2_cache_entry_flush(BlockDriverState *bs, Qcow2Cache *c, int i)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c == s->refcount_block_cache) {
|
|
||||||
ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_REFCOUNT_BLOCK,
|
|
||||||
c->entries[i].offset, s->cluster_size);
|
|
||||||
} else if (c == s->l2_table_cache) {
|
|
||||||
ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L2,
|
|
||||||
c->entries[i].offset, s->cluster_size);
|
|
||||||
} else {
|
|
||||||
ret = qcow2_pre_write_overlap_check(bs, 0,
|
|
||||||
c->entries[i].offset, s->cluster_size);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ret < 0) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c == s->refcount_block_cache) {
|
if (c == s->refcount_block_cache) {
|
||||||
BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_UPDATE_PART);
|
BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_UPDATE_PART);
|
||||||
} else if (c == s->l2_table_cache) {
|
} else if (c == s->l2_table_cache) {
|
||||||
BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE);
|
BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE);
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = bdrv_pwrite(bs->file->bs, c->entries[i].offset,
|
ret = bdrv_pwrite(bs->file, c->entries[i].offset, c->entries[i].table,
|
||||||
qcow2_cache_get_table_addr(bs, c, i), s->cluster_size);
|
s->cluster_size);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -229,7 +133,7 @@ static int qcow2_cache_entry_flush(BlockDriverState *bs, Qcow2Cache *c, int i)
|
|||||||
|
|
||||||
int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c)
|
int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c)
|
||||||
{
|
{
|
||||||
BDRVQcow2State *s = bs->opaque;
|
BDRVQcowState *s = bs->opaque;
|
||||||
int result = 0;
|
int result = 0;
|
||||||
int ret;
|
int ret;
|
||||||
int i;
|
int i;
|
||||||
@@ -244,7 +148,7 @@ int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (result == 0) {
|
if (result == 0) {
|
||||||
ret = bdrv_flush(bs->file->bs);
|
ret = bdrv_flush(bs->file);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
result = ret;
|
result = ret;
|
||||||
}
|
}
|
||||||
@@ -281,67 +185,60 @@ void qcow2_cache_depends_on_flush(Qcow2Cache *c)
|
|||||||
c->depends_on_flush = true;
|
c->depends_on_flush = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int qcow2_cache_empty(BlockDriverState *bs, Qcow2Cache *c)
|
static int qcow2_cache_find_entry_to_replace(Qcow2Cache *c)
|
||||||
{
|
{
|
||||||
int ret, i;
|
int i;
|
||||||
|
int min_count = INT_MAX;
|
||||||
|
int min_index = -1;
|
||||||
|
|
||||||
ret = qcow2_cache_flush(bs, c);
|
|
||||||
if (ret < 0) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < c->size; i++) {
|
for (i = 0; i < c->size; i++) {
|
||||||
assert(c->entries[i].ref == 0);
|
if (c->entries[i].ref) {
|
||||||
c->entries[i].offset = 0;
|
continue;
|
||||||
c->entries[i].lru_counter = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
qcow2_cache_table_release(bs, c, 0, c->size);
|
if (c->entries[i].cache_hits < min_count) {
|
||||||
|
min_index = i;
|
||||||
|
min_count = c->entries[i].cache_hits;
|
||||||
|
}
|
||||||
|
|
||||||
c->lru_counter = 0;
|
/* Give newer hits priority */
|
||||||
|
/* TODO Check how to optimize the replacement strategy */
|
||||||
|
c->entries[i].cache_hits /= 2;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
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,
|
static int qcow2_cache_do_get(BlockDriverState *bs, Qcow2Cache *c,
|
||||||
uint64_t offset, void **table, bool read_from_disk)
|
uint64_t offset, void **table, bool read_from_disk)
|
||||||
{
|
{
|
||||||
BDRVQcow2State *s = bs->opaque;
|
BDRVQcowState *s = bs->opaque;
|
||||||
int i;
|
int i;
|
||||||
int ret;
|
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,
|
trace_qcow2_cache_get(qemu_coroutine_self(), c == s->l2_table_cache,
|
||||||
offset, read_from_disk);
|
offset, read_from_disk);
|
||||||
|
|
||||||
/* Check if the table is already cached */
|
/* Check if the table is already cached */
|
||||||
i = lookup_index = (offset / s->cluster_size * 4) % c->size;
|
for (i = 0; i < c->size; i++) {
|
||||||
do {
|
if (c->entries[i].offset == offset) {
|
||||||
const Qcow2CachedTable *t = &c->entries[i];
|
|
||||||
if (t->offset == offset) {
|
|
||||||
goto found;
|
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 */
|
/* If not, write a table back and replace it */
|
||||||
i = min_lru_index;
|
i = qcow2_cache_find_entry_to_replace(c);
|
||||||
trace_qcow2_cache_get_replace_entry(qemu_coroutine_self(),
|
trace_qcow2_cache_get_replace_entry(qemu_coroutine_self(),
|
||||||
c == s->l2_table_cache, i);
|
c == s->l2_table_cache, i);
|
||||||
|
if (i < 0) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
ret = qcow2_cache_entry_flush(bs, c, i);
|
ret = qcow2_cache_entry_flush(bs, c, i);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
@@ -356,20 +253,22 @@ static int qcow2_cache_do_get(BlockDriverState *bs, Qcow2Cache *c,
|
|||||||
BLKDBG_EVENT(bs->file, BLKDBG_L2_LOAD);
|
BLKDBG_EVENT(bs->file, BLKDBG_L2_LOAD);
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = bdrv_pread(bs->file->bs, offset,
|
ret = bdrv_pread(bs->file, offset, c->entries[i].table, s->cluster_size);
|
||||||
qcow2_cache_get_table_addr(bs, c, i),
|
|
||||||
s->cluster_size);
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return ret;
|
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;
|
c->entries[i].offset = offset;
|
||||||
|
|
||||||
/* And return the right table */
|
/* And return the right table */
|
||||||
found:
|
found:
|
||||||
|
c->entries[i].cache_hits++;
|
||||||
c->entries[i].ref++;
|
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(),
|
trace_qcow2_cache_get_done(qemu_coroutine_self(),
|
||||||
c == s->l2_table_cache, i);
|
c == s->l2_table_cache, i);
|
||||||
@@ -389,24 +288,36 @@ int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
|
|||||||
return qcow2_cache_do_get(bs, c, offset, table, false);
|
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--;
|
c->entries[i].ref--;
|
||||||
*table = NULL;
|
*table = NULL;
|
||||||
|
|
||||||
if (c->entries[i].ref == 0) {
|
|
||||||
c->entries[i].lru_counter = ++c->lru_counter;
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(c->entries[i].ref >= 0);
|
assert(c->entries[i].ref >= 0);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void qcow2_cache_entry_mark_dirty(BlockDriverState *bs, Qcow2Cache *c,
|
void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table)
|
||||||
void *table)
|
|
||||||
{
|
{
|
||||||
int i = qcow2_cache_get_table_idx(bs, c, table);
|
int i;
|
||||||
assert(c->entries[i].offset != 0);
|
|
||||||
|
for (i = 0; i < c->size; i++) {
|
||||||
|
if (c->entries[i].table == table) {
|
||||||
|
goto found;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
abort();
|
||||||
|
|
||||||
|
found:
|
||||||
c->entries[i].dirty = true;
|
c->entries[i].dirty = true;
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -25,11 +25,10 @@
|
|||||||
#include "qemu-common.h"
|
#include "qemu-common.h"
|
||||||
#include "block/block_int.h"
|
#include "block/block_int.h"
|
||||||
#include "block/qcow2.h"
|
#include "block/qcow2.h"
|
||||||
#include "qemu/error-report.h"
|
|
||||||
|
|
||||||
void qcow2_free_snapshots(BlockDriverState *bs)
|
void qcow2_free_snapshots(BlockDriverState *bs)
|
||||||
{
|
{
|
||||||
BDRVQcow2State *s = bs->opaque;
|
BDRVQcowState *s = bs->opaque;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for(i = 0; i < s->nb_snapshots; i++) {
|
for(i = 0; i < s->nb_snapshots; i++) {
|
||||||
@@ -43,7 +42,7 @@ void qcow2_free_snapshots(BlockDriverState *bs)
|
|||||||
|
|
||||||
int qcow2_read_snapshots(BlockDriverState *bs)
|
int qcow2_read_snapshots(BlockDriverState *bs)
|
||||||
{
|
{
|
||||||
BDRVQcow2State *s = bs->opaque;
|
BDRVQcowState *s = bs->opaque;
|
||||||
QCowSnapshotHeader h;
|
QCowSnapshotHeader h;
|
||||||
QCowSnapshotExtraData extra;
|
QCowSnapshotExtraData extra;
|
||||||
QCowSnapshot *sn;
|
QCowSnapshot *sn;
|
||||||
@@ -59,12 +58,12 @@ int qcow2_read_snapshots(BlockDriverState *bs)
|
|||||||
}
|
}
|
||||||
|
|
||||||
offset = s->snapshots_offset;
|
offset = s->snapshots_offset;
|
||||||
s->snapshots = g_new0(QCowSnapshot, s->nb_snapshots);
|
s->snapshots = g_malloc0(s->nb_snapshots * sizeof(QCowSnapshot));
|
||||||
|
|
||||||
for(i = 0; i < s->nb_snapshots; i++) {
|
for(i = 0; i < s->nb_snapshots; i++) {
|
||||||
/* Read statically sized part of the snapshot header */
|
/* Read statically sized part of the snapshot header */
|
||||||
offset = align_offset(offset, 8);
|
offset = align_offset(offset, 8);
|
||||||
ret = bdrv_pread(bs->file->bs, offset, &h, sizeof(h));
|
ret = bdrv_pread(bs->file, offset, &h, sizeof(h));
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
@@ -83,7 +82,7 @@ int qcow2_read_snapshots(BlockDriverState *bs)
|
|||||||
name_size = be16_to_cpu(h.name_size);
|
name_size = be16_to_cpu(h.name_size);
|
||||||
|
|
||||||
/* Read extra data */
|
/* Read extra data */
|
||||||
ret = bdrv_pread(bs->file->bs, offset, &extra,
|
ret = bdrv_pread(bs->file, offset, &extra,
|
||||||
MIN(sizeof(extra), extra_data_size));
|
MIN(sizeof(extra), extra_data_size));
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto fail;
|
goto fail;
|
||||||
@@ -102,7 +101,7 @@ int qcow2_read_snapshots(BlockDriverState *bs)
|
|||||||
|
|
||||||
/* Read snapshot ID */
|
/* Read snapshot ID */
|
||||||
sn->id_str = g_malloc(id_str_size + 1);
|
sn->id_str = g_malloc(id_str_size + 1);
|
||||||
ret = bdrv_pread(bs->file->bs, offset, sn->id_str, id_str_size);
|
ret = bdrv_pread(bs->file, offset, sn->id_str, id_str_size);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
@@ -111,7 +110,7 @@ int qcow2_read_snapshots(BlockDriverState *bs)
|
|||||||
|
|
||||||
/* Read snapshot name */
|
/* Read snapshot name */
|
||||||
sn->name = g_malloc(name_size + 1);
|
sn->name = g_malloc(name_size + 1);
|
||||||
ret = bdrv_pread(bs->file->bs, offset, sn->name, name_size);
|
ret = bdrv_pread(bs->file, offset, sn->name, name_size);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
@@ -136,7 +135,7 @@ fail:
|
|||||||
/* add at the end of the file a new list of snapshots */
|
/* add at the end of the file a new list of snapshots */
|
||||||
static int qcow2_write_snapshots(BlockDriverState *bs)
|
static int qcow2_write_snapshots(BlockDriverState *bs)
|
||||||
{
|
{
|
||||||
BDRVQcow2State *s = bs->opaque;
|
BDRVQcowState *s = bs->opaque;
|
||||||
QCowSnapshot *sn;
|
QCowSnapshot *sn;
|
||||||
QCowSnapshotHeader h;
|
QCowSnapshotHeader h;
|
||||||
QCowSnapshotExtraData extra;
|
QCowSnapshotExtraData extra;
|
||||||
@@ -171,22 +170,13 @@ static int qcow2_write_snapshots(BlockDriverState *bs)
|
|||||||
snapshots_offset = qcow2_alloc_clusters(bs, snapshots_size);
|
snapshots_offset = qcow2_alloc_clusters(bs, snapshots_size);
|
||||||
offset = snapshots_offset;
|
offset = snapshots_offset;
|
||||||
if (offset < 0) {
|
if (offset < 0) {
|
||||||
ret = offset;
|
return offset;
|
||||||
goto fail;
|
|
||||||
}
|
}
|
||||||
ret = bdrv_flush(bs);
|
ret = bdrv_flush(bs);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto fail;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* The snapshot list position has not yet been updated, so these clusters
|
|
||||||
* must indeed be completely free */
|
|
||||||
ret = qcow2_pre_write_overlap_check(bs, 0, offset, snapshots_size);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* Write all snapshots to the new list */
|
/* Write all snapshots to the new list */
|
||||||
for(i = 0; i < s->nb_snapshots; i++) {
|
for(i = 0; i < s->nb_snapshots; i++) {
|
||||||
sn = s->snapshots + i;
|
sn = s->snapshots + i;
|
||||||
@@ -209,30 +199,29 @@ static int qcow2_write_snapshots(BlockDriverState *bs)
|
|||||||
|
|
||||||
id_str_size = strlen(sn->id_str);
|
id_str_size = strlen(sn->id_str);
|
||||||
name_size = strlen(sn->name);
|
name_size = strlen(sn->name);
|
||||||
assert(id_str_size <= UINT16_MAX && name_size <= UINT16_MAX);
|
|
||||||
h.id_str_size = cpu_to_be16(id_str_size);
|
h.id_str_size = cpu_to_be16(id_str_size);
|
||||||
h.name_size = cpu_to_be16(name_size);
|
h.name_size = cpu_to_be16(name_size);
|
||||||
offset = align_offset(offset, 8);
|
offset = align_offset(offset, 8);
|
||||||
|
|
||||||
ret = bdrv_pwrite(bs->file->bs, offset, &h, sizeof(h));
|
ret = bdrv_pwrite(bs->file, offset, &h, sizeof(h));
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
offset += sizeof(h);
|
offset += sizeof(h);
|
||||||
|
|
||||||
ret = bdrv_pwrite(bs->file->bs, offset, &extra, sizeof(extra));
|
ret = bdrv_pwrite(bs->file, offset, &extra, sizeof(extra));
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
offset += sizeof(extra);
|
offset += sizeof(extra);
|
||||||
|
|
||||||
ret = bdrv_pwrite(bs->file->bs, offset, sn->id_str, id_str_size);
|
ret = bdrv_pwrite(bs->file, offset, sn->id_str, id_str_size);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
offset += id_str_size;
|
offset += id_str_size;
|
||||||
|
|
||||||
ret = bdrv_pwrite(bs->file->bs, offset, sn->name, name_size);
|
ret = bdrv_pwrite(bs->file, offset, sn->name, name_size);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
@@ -254,34 +243,28 @@ static int qcow2_write_snapshots(BlockDriverState *bs)
|
|||||||
header_data.nb_snapshots = cpu_to_be32(s->nb_snapshots);
|
header_data.nb_snapshots = cpu_to_be32(s->nb_snapshots);
|
||||||
header_data.snapshots_offset = cpu_to_be64(snapshots_offset);
|
header_data.snapshots_offset = cpu_to_be64(snapshots_offset);
|
||||||
|
|
||||||
ret = bdrv_pwrite_sync(bs->file->bs, offsetof(QCowHeader, nb_snapshots),
|
ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, nb_snapshots),
|
||||||
&header_data, sizeof(header_data));
|
&header_data, sizeof(header_data));
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* free the old snapshot table */
|
/* free the old snapshot table */
|
||||||
qcow2_free_clusters(bs, s->snapshots_offset, s->snapshots_size,
|
qcow2_free_clusters(bs, s->snapshots_offset, s->snapshots_size);
|
||||||
QCOW2_DISCARD_SNAPSHOT);
|
|
||||||
s->snapshots_offset = snapshots_offset;
|
s->snapshots_offset = snapshots_offset;
|
||||||
s->snapshots_size = snapshots_size;
|
s->snapshots_size = snapshots_size;
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
fail:
|
fail:
|
||||||
if (snapshots_offset > 0) {
|
|
||||||
qcow2_free_clusters(bs, snapshots_offset, snapshots_size,
|
|
||||||
QCOW2_DISCARD_ALWAYS);
|
|
||||||
}
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void find_new_snapshot_id(BlockDriverState *bs,
|
static void find_new_snapshot_id(BlockDriverState *bs,
|
||||||
char *id_str, int id_str_size)
|
char *id_str, int id_str_size)
|
||||||
{
|
{
|
||||||
BDRVQcow2State *s = bs->opaque;
|
BDRVQcowState *s = bs->opaque;
|
||||||
QCowSnapshot *sn;
|
QCowSnapshot *sn;
|
||||||
int i;
|
int i, id, id_max = 0;
|
||||||
unsigned long id, id_max = 0;
|
|
||||||
|
|
||||||
for(i = 0; i < s->nb_snapshots; i++) {
|
for(i = 0; i < s->nb_snapshots; i++) {
|
||||||
sn = s->snapshots + i;
|
sn = s->snapshots + i;
|
||||||
@@ -289,56 +272,40 @@ static void find_new_snapshot_id(BlockDriverState *bs,
|
|||||||
if (id > id_max)
|
if (id > id_max)
|
||||||
id_max = id;
|
id_max = id;
|
||||||
}
|
}
|
||||||
snprintf(id_str, id_str_size, "%lu", id_max + 1);
|
snprintf(id_str, id_str_size, "%d", id_max + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int find_snapshot_by_id_and_name(BlockDriverState *bs,
|
static int find_snapshot_by_id(BlockDriverState *bs, const char *id_str)
|
||||||
const char *id,
|
|
||||||
const char *name)
|
|
||||||
{
|
{
|
||||||
BDRVQcow2State *s = bs->opaque;
|
BDRVQcowState *s = bs->opaque;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (id && name) {
|
|
||||||
for(i = 0; i < s->nb_snapshots; i++) {
|
for(i = 0; i < s->nb_snapshots; i++) {
|
||||||
if (!strcmp(s->snapshots[i].id_str, id) &&
|
if (!strcmp(s->snapshots[i].id_str, id_str))
|
||||||
!strcmp(s->snapshots[i].name, name)) {
|
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} else if (id) {
|
|
||||||
for (i = 0; i < s->nb_snapshots; i++) {
|
|
||||||
if (!strcmp(s->snapshots[i].id_str, id)) {
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (name) {
|
|
||||||
for (i = 0; i < s->nb_snapshots; i++) {
|
|
||||||
if (!strcmp(s->snapshots[i].name, name)) {
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int find_snapshot_by_id_or_name(BlockDriverState *bs,
|
static int find_snapshot_by_id_or_name(BlockDriverState *bs, const char *name)
|
||||||
const char *id_or_name)
|
|
||||||
{
|
{
|
||||||
int ret;
|
BDRVQcowState *s = bs->opaque;
|
||||||
|
int i, ret;
|
||||||
|
|
||||||
ret = find_snapshot_by_id_and_name(bs, id_or_name, NULL);
|
ret = find_snapshot_by_id(bs, name);
|
||||||
if (ret >= 0) {
|
if (ret >= 0)
|
||||||
return ret;
|
return ret;
|
||||||
|
for(i = 0; i < s->nb_snapshots; i++) {
|
||||||
|
if (!strcmp(s->snapshots[i].name, name))
|
||||||
|
return i;
|
||||||
}
|
}
|
||||||
return find_snapshot_by_id_and_name(bs, NULL, id_or_name);
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if no id is provided, a new one is constructed */
|
/* if no id is provided, a new one is constructed */
|
||||||
int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
|
int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
|
||||||
{
|
{
|
||||||
BDRVQcow2State *s = bs->opaque;
|
BDRVQcowState *s = bs->opaque;
|
||||||
QCowSnapshot *new_snapshot_list = NULL;
|
QCowSnapshot *new_snapshot_list = NULL;
|
||||||
QCowSnapshot *old_snapshot_list = NULL;
|
QCowSnapshot *old_snapshot_list = NULL;
|
||||||
QCowSnapshot sn1, *sn = &sn1;
|
QCowSnapshot sn1, *sn = &sn1;
|
||||||
@@ -352,11 +319,13 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
|
|||||||
|
|
||||||
memset(sn, 0, sizeof(*sn));
|
memset(sn, 0, sizeof(*sn));
|
||||||
|
|
||||||
/* Generate an ID */
|
/* 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));
|
find_new_snapshot_id(bs, sn_info->id_str, sizeof(sn_info->id_str));
|
||||||
|
}
|
||||||
|
|
||||||
/* Check that the ID is unique */
|
/* Check that the ID is unique */
|
||||||
if (find_snapshot_by_id_and_name(bs, sn_info->id_str, NULL) >= 0) {
|
if (find_snapshot_by_id(bs, sn_info->id_str) >= 0) {
|
||||||
return -EEXIST;
|
return -EEXIST;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -380,23 +349,12 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
|
|||||||
sn->l1_table_offset = l1_table_offset;
|
sn->l1_table_offset = l1_table_offset;
|
||||||
sn->l1_size = s->l1_size;
|
sn->l1_size = s->l1_size;
|
||||||
|
|
||||||
l1_table = g_try_new(uint64_t, s->l1_size);
|
l1_table = g_malloc(s->l1_size * sizeof(uint64_t));
|
||||||
if (s->l1_size && l1_table == NULL) {
|
|
||||||
ret = -ENOMEM;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
for(i = 0; i < s->l1_size; i++) {
|
for(i = 0; i < s->l1_size; i++) {
|
||||||
l1_table[i] = cpu_to_be64(s->l1_table[i]);
|
l1_table[i] = cpu_to_be64(s->l1_table[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = qcow2_pre_write_overlap_check(bs, 0, sn->l1_table_offset,
|
ret = bdrv_pwrite(bs->file, sn->l1_table_offset, l1_table,
|
||||||
s->l1_size * sizeof(uint64_t));
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = bdrv_pwrite(bs->file->bs, sn->l1_table_offset, l1_table,
|
|
||||||
s->l1_size * sizeof(uint64_t));
|
s->l1_size * sizeof(uint64_t));
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto fail;
|
goto fail;
|
||||||
@@ -415,8 +373,13 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
|
|||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ret = bdrv_flush(bs);
|
||||||
|
if (ret < 0) {
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
/* Append the new snapshot to the snapshot list */
|
/* Append the new snapshot to the snapshot list */
|
||||||
new_snapshot_list = g_new(QCowSnapshot, s->nb_snapshots + 1);
|
new_snapshot_list = g_malloc((s->nb_snapshots + 1) * sizeof(QCowSnapshot));
|
||||||
if (s->snapshots) {
|
if (s->snapshots) {
|
||||||
memcpy(new_snapshot_list, s->snapshots,
|
memcpy(new_snapshot_list, s->snapshots,
|
||||||
s->nb_snapshots * sizeof(QCowSnapshot));
|
s->nb_snapshots * sizeof(QCowSnapshot));
|
||||||
@@ -429,19 +392,11 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
|
|||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
g_free(s->snapshots);
|
g_free(s->snapshots);
|
||||||
s->snapshots = old_snapshot_list;
|
s->snapshots = old_snapshot_list;
|
||||||
s->nb_snapshots--;
|
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
g_free(old_snapshot_list);
|
g_free(old_snapshot_list);
|
||||||
|
|
||||||
/* The VM state isn't needed any more in the active L1 table; in fact, it
|
|
||||||
* hurts by causing expensive COW for the next snapshot. */
|
|
||||||
qcow2_discard_clusters(bs, qcow2_vm_state_offset(s),
|
|
||||||
align_offset(sn->vm_state_size, s->cluster_size)
|
|
||||||
>> BDRV_SECTOR_BITS,
|
|
||||||
QCOW2_DISCARD_NEVER, false);
|
|
||||||
|
|
||||||
#ifdef DEBUG_ALLOC
|
#ifdef DEBUG_ALLOC
|
||||||
{
|
{
|
||||||
BdrvCheckResult result = {0};
|
BdrvCheckResult result = {0};
|
||||||
@@ -461,7 +416,7 @@ fail:
|
|||||||
/* copy the snapshot 'snapshot_name' into the current disk image */
|
/* copy the snapshot 'snapshot_name' into the current disk image */
|
||||||
int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
|
int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
|
||||||
{
|
{
|
||||||
BDRVQcow2State *s = bs->opaque;
|
BDRVQcowState *s = bs->opaque;
|
||||||
QCowSnapshot *sn;
|
QCowSnapshot *sn;
|
||||||
int i, snapshot_index;
|
int i, snapshot_index;
|
||||||
int cur_l1_bytes, sn_l1_bytes;
|
int cur_l1_bytes, sn_l1_bytes;
|
||||||
@@ -503,14 +458,9 @@ int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
|
|||||||
* Decrease the refcount referenced by the old one only when the L1
|
* Decrease the refcount referenced by the old one only when the L1
|
||||||
* table is overwritten.
|
* table is overwritten.
|
||||||
*/
|
*/
|
||||||
sn_l1_table = g_try_malloc0(cur_l1_bytes);
|
sn_l1_table = g_malloc0(cur_l1_bytes);
|
||||||
if (cur_l1_bytes && sn_l1_table == NULL) {
|
|
||||||
ret = -ENOMEM;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = bdrv_pread(bs->file->bs, sn->l1_table_offset,
|
ret = bdrv_pread(bs->file, sn->l1_table_offset, sn_l1_table, sn_l1_bytes);
|
||||||
sn_l1_table, sn_l1_bytes);
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
@@ -521,13 +471,7 @@ int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
|
|||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L1,
|
ret = bdrv_pwrite_sync(bs->file, s->l1_table_offset, sn_l1_table,
|
||||||
s->l1_table_offset, cur_l1_bytes);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = bdrv_pwrite_sync(bs->file->bs, s->l1_table_offset, sn_l1_table,
|
|
||||||
cur_l1_bytes);
|
cur_l1_bytes);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto fail;
|
goto fail;
|
||||||
@@ -583,19 +527,15 @@ fail:
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int qcow2_snapshot_delete(BlockDriverState *bs,
|
int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
|
||||||
const char *snapshot_id,
|
|
||||||
const char *name,
|
|
||||||
Error **errp)
|
|
||||||
{
|
{
|
||||||
BDRVQcow2State *s = bs->opaque;
|
BDRVQcowState *s = bs->opaque;
|
||||||
QCowSnapshot sn;
|
QCowSnapshot sn;
|
||||||
int snapshot_index, ret;
|
int snapshot_index, ret;
|
||||||
|
|
||||||
/* Search the snapshot */
|
/* Search the snapshot */
|
||||||
snapshot_index = find_snapshot_by_id_and_name(bs, snapshot_id, name);
|
snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id);
|
||||||
if (snapshot_index < 0) {
|
if (snapshot_index < 0) {
|
||||||
error_setg(errp, "Can't find the snapshot");
|
|
||||||
return -ENOENT;
|
return -ENOENT;
|
||||||
}
|
}
|
||||||
sn = s->snapshots[snapshot_index];
|
sn = s->snapshots[snapshot_index];
|
||||||
@@ -607,8 +547,6 @@ int qcow2_snapshot_delete(BlockDriverState *bs,
|
|||||||
s->nb_snapshots--;
|
s->nb_snapshots--;
|
||||||
ret = qcow2_write_snapshots(bs);
|
ret = qcow2_write_snapshots(bs);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_setg_errno(errp, -ret,
|
|
||||||
"Failed to remove snapshot from snapshot list");
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -626,17 +564,13 @@ int qcow2_snapshot_delete(BlockDriverState *bs,
|
|||||||
ret = qcow2_update_snapshot_refcount(bs, sn.l1_table_offset,
|
ret = qcow2_update_snapshot_refcount(bs, sn.l1_table_offset,
|
||||||
sn.l1_size, -1);
|
sn.l1_size, -1);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_setg_errno(errp, -ret, "Failed to free the cluster and L1 table");
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
qcow2_free_clusters(bs, sn.l1_table_offset, sn.l1_size * sizeof(uint64_t),
|
qcow2_free_clusters(bs, sn.l1_table_offset, sn.l1_size * sizeof(uint64_t));
|
||||||
QCOW2_DISCARD_SNAPSHOT);
|
|
||||||
|
|
||||||
/* must update the copied flag on the current cluster offsets */
|
/* must update the copied flag on the current cluster offsets */
|
||||||
ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
|
ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_setg_errno(errp, -ret,
|
|
||||||
"Failed to update snapshot status in disk");
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -651,7 +585,7 @@ int qcow2_snapshot_delete(BlockDriverState *bs,
|
|||||||
|
|
||||||
int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
|
int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
|
||||||
{
|
{
|
||||||
BDRVQcow2State *s = bs->opaque;
|
BDRVQcowState *s = bs->opaque;
|
||||||
QEMUSnapshotInfo *sn_tab, *sn_info;
|
QEMUSnapshotInfo *sn_tab, *sn_info;
|
||||||
QCowSnapshot *sn;
|
QCowSnapshot *sn;
|
||||||
int i;
|
int i;
|
||||||
@@ -661,7 +595,7 @@ int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
|
|||||||
return s->nb_snapshots;
|
return s->nb_snapshots;
|
||||||
}
|
}
|
||||||
|
|
||||||
sn_tab = g_new0(QEMUSnapshotInfo, s->nb_snapshots);
|
sn_tab = g_malloc0(s->nb_snapshots * sizeof(QEMUSnapshotInfo));
|
||||||
for(i = 0; i < s->nb_snapshots; i++) {
|
for(i = 0; i < s->nb_snapshots; i++) {
|
||||||
sn_info = sn_tab + i;
|
sn_info = sn_tab + i;
|
||||||
sn = s->snapshots + i;
|
sn = s->snapshots + i;
|
||||||
@@ -678,13 +612,10 @@ int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
|
|||||||
return s->nb_snapshots;
|
return s->nb_snapshots;
|
||||||
}
|
}
|
||||||
|
|
||||||
int qcow2_snapshot_load_tmp(BlockDriverState *bs,
|
int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name)
|
||||||
const char *snapshot_id,
|
|
||||||
const char *name,
|
|
||||||
Error **errp)
|
|
||||||
{
|
{
|
||||||
int i, snapshot_index;
|
int i, snapshot_index;
|
||||||
BDRVQcow2State *s = bs->opaque;
|
BDRVQcowState *s = bs->opaque;
|
||||||
QCowSnapshot *sn;
|
QCowSnapshot *sn;
|
||||||
uint64_t *new_l1_table;
|
uint64_t *new_l1_table;
|
||||||
int new_l1_bytes;
|
int new_l1_bytes;
|
||||||
@@ -693,36 +624,28 @@ int qcow2_snapshot_load_tmp(BlockDriverState *bs,
|
|||||||
assert(bs->read_only);
|
assert(bs->read_only);
|
||||||
|
|
||||||
/* Search the snapshot */
|
/* Search the snapshot */
|
||||||
snapshot_index = find_snapshot_by_id_and_name(bs, snapshot_id, name);
|
snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_name);
|
||||||
if (snapshot_index < 0) {
|
if (snapshot_index < 0) {
|
||||||
error_setg(errp,
|
|
||||||
"Can't find snapshot");
|
|
||||||
return -ENOENT;
|
return -ENOENT;
|
||||||
}
|
}
|
||||||
sn = &s->snapshots[snapshot_index];
|
sn = &s->snapshots[snapshot_index];
|
||||||
|
|
||||||
/* Allocate and read in the snapshot's L1 table */
|
/* Allocate and read in the snapshot's L1 table */
|
||||||
if (sn->l1_size > QCOW_MAX_L1_SIZE / sizeof(uint64_t)) {
|
if (sn->l1_size > QCOW_MAX_L1_SIZE) {
|
||||||
error_setg(errp, "Snapshot L1 table too large");
|
error_report("Snapshot L1 table too large");
|
||||||
return -EFBIG;
|
return -EFBIG;
|
||||||
}
|
}
|
||||||
new_l1_bytes = sn->l1_size * sizeof(uint64_t);
|
new_l1_bytes = sn->l1_size * sizeof(uint64_t);
|
||||||
new_l1_table = qemu_try_blockalign(bs->file->bs,
|
new_l1_table = g_malloc0(align_offset(new_l1_bytes, 512));
|
||||||
align_offset(new_l1_bytes, 512));
|
|
||||||
if (new_l1_table == NULL) {
|
|
||||||
return -ENOMEM;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = bdrv_pread(bs->file->bs, sn->l1_table_offset,
|
ret = bdrv_pread(bs->file, sn->l1_table_offset, new_l1_table, new_l1_bytes);
|
||||||
new_l1_table, new_l1_bytes);
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_setg(errp, "Failed to read l1 table for snapshot");
|
g_free(new_l1_table);
|
||||||
qemu_vfree(new_l1_table);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Switch the L1 table */
|
/* Switch the L1 table */
|
||||||
qemu_vfree(s->l1_table);
|
g_free(s->l1_table);
|
||||||
|
|
||||||
s->l1_size = sn->l1_size;
|
s->l1_size = sn->l1_size;
|
||||||
s->l1_table_offset = sn->l1_table_offset;
|
s->l1_table_offset = sn->l1_table_offset;
|
||||||
|
|||||||
2162
block/qcow2.c
2162
block/qcow2.c
File diff suppressed because it is too large
Load Diff
275
block/qcow2.h
275
block/qcow2.h
@@ -25,8 +25,8 @@
|
|||||||
#ifndef BLOCK_QCOW2_H
|
#ifndef BLOCK_QCOW2_H
|
||||||
#define BLOCK_QCOW2_H
|
#define BLOCK_QCOW2_H
|
||||||
|
|
||||||
#include "crypto/cipher.h"
|
#include "block/aes.h"
|
||||||
#include "qemu/coroutine.h"
|
#include "block/coroutine.h"
|
||||||
|
|
||||||
//#define DEBUG_ALLOC
|
//#define DEBUG_ALLOC
|
||||||
//#define DEBUG_ALLOC2
|
//#define DEBUG_ALLOC2
|
||||||
@@ -53,51 +53,24 @@
|
|||||||
#define QCOW_MAX_SNAPSHOTS_SIZE (1024 * QCOW_MAX_SNAPSHOTS)
|
#define QCOW_MAX_SNAPSHOTS_SIZE (1024 * QCOW_MAX_SNAPSHOTS)
|
||||||
|
|
||||||
/* indicate that the refcount of the referenced cluster is exactly one. */
|
/* indicate that the refcount of the referenced cluster is exactly one. */
|
||||||
#define QCOW_OFLAG_COPIED (1ULL << 63)
|
#define QCOW_OFLAG_COPIED (1LL << 63)
|
||||||
/* indicate that the cluster is compressed (they never have the copied flag) */
|
/* indicate that the cluster is compressed (they never have the copied flag) */
|
||||||
#define QCOW_OFLAG_COMPRESSED (1ULL << 62)
|
#define QCOW_OFLAG_COMPRESSED (1LL << 62)
|
||||||
/* The cluster reads as all zeros */
|
/* The cluster reads as all zeros */
|
||||||
#define QCOW_OFLAG_ZERO (1ULL << 0)
|
#define QCOW_OFLAG_ZERO (1LL << 0)
|
||||||
|
|
||||||
|
#define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
|
||||||
|
|
||||||
#define MIN_CLUSTER_BITS 9
|
#define MIN_CLUSTER_BITS 9
|
||||||
#define MAX_CLUSTER_BITS 21
|
#define MAX_CLUSTER_BITS 21
|
||||||
|
|
||||||
/* Must be at least 2 to cover COW */
|
#define L2_CACHE_SIZE 16
|
||||||
#define MIN_L2_CACHE_SIZE 2 /* clusters */
|
|
||||||
|
|
||||||
/* Must be at least 4 to cover all cases of refcount table growth */
|
/* Must be at least 4 to cover all cases of refcount table growth */
|
||||||
#define MIN_REFCOUNT_CACHE_SIZE 4 /* clusters */
|
#define REFCOUNT_CACHE_SIZE 4
|
||||||
|
|
||||||
/* 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
|
|
||||||
* clusters */
|
|
||||||
#define DEFAULT_L2_REFCOUNT_SIZE_RATIO 4
|
|
||||||
|
|
||||||
#define DEFAULT_CLUSTER_SIZE 65536
|
#define DEFAULT_CLUSTER_SIZE 65536
|
||||||
|
|
||||||
|
|
||||||
#define QCOW2_OPT_LAZY_REFCOUNTS "lazy-refcounts"
|
|
||||||
#define QCOW2_OPT_DISCARD_REQUEST "pass-discard-request"
|
|
||||||
#define QCOW2_OPT_DISCARD_SNAPSHOT "pass-discard-snapshot"
|
|
||||||
#define QCOW2_OPT_DISCARD_OTHER "pass-discard-other"
|
|
||||||
#define QCOW2_OPT_OVERLAP "overlap-check"
|
|
||||||
#define QCOW2_OPT_OVERLAP_TEMPLATE "overlap-check.template"
|
|
||||||
#define QCOW2_OPT_OVERLAP_MAIN_HEADER "overlap-check.main-header"
|
|
||||||
#define QCOW2_OPT_OVERLAP_ACTIVE_L1 "overlap-check.active-l1"
|
|
||||||
#define QCOW2_OPT_OVERLAP_ACTIVE_L2 "overlap-check.active-l2"
|
|
||||||
#define QCOW2_OPT_OVERLAP_REFCOUNT_TABLE "overlap-check.refcount-table"
|
|
||||||
#define QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK "overlap-check.refcount-block"
|
|
||||||
#define QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE "overlap-check.snapshot-table"
|
|
||||||
#define QCOW2_OPT_OVERLAP_INACTIVE_L1 "overlap-check.inactive-l1"
|
|
||||||
#define QCOW2_OPT_OVERLAP_INACTIVE_L2 "overlap-check.inactive-l2"
|
|
||||||
#define QCOW2_OPT_CACHE_SIZE "cache-size"
|
|
||||||
#define QCOW2_OPT_L2_CACHE_SIZE "l2-cache-size"
|
|
||||||
#define QCOW2_OPT_REFCOUNT_CACHE_SIZE "refcount-cache-size"
|
|
||||||
#define QCOW2_OPT_CACHE_CLEAN_INTERVAL "cache-clean-interval"
|
|
||||||
|
|
||||||
typedef struct QCowHeader {
|
typedef struct QCowHeader {
|
||||||
uint32_t magic;
|
uint32_t magic;
|
||||||
uint32_t version;
|
uint32_t version;
|
||||||
@@ -120,7 +93,7 @@ typedef struct QCowHeader {
|
|||||||
|
|
||||||
uint32_t refcount_order;
|
uint32_t refcount_order;
|
||||||
uint32_t header_length;
|
uint32_t header_length;
|
||||||
} QEMU_PACKED QCowHeader;
|
} QCowHeader;
|
||||||
|
|
||||||
typedef struct QEMU_PACKED QCowSnapshotHeader {
|
typedef struct QEMU_PACKED QCowSnapshotHeader {
|
||||||
/* header is 8 byte aligned */
|
/* header is 8 byte aligned */
|
||||||
@@ -179,12 +152,9 @@ enum {
|
|||||||
/* Incompatible feature bits */
|
/* Incompatible feature bits */
|
||||||
enum {
|
enum {
|
||||||
QCOW2_INCOMPAT_DIRTY_BITNR = 0,
|
QCOW2_INCOMPAT_DIRTY_BITNR = 0,
|
||||||
QCOW2_INCOMPAT_CORRUPT_BITNR = 1,
|
|
||||||
QCOW2_INCOMPAT_DIRTY = 1 << QCOW2_INCOMPAT_DIRTY_BITNR,
|
QCOW2_INCOMPAT_DIRTY = 1 << QCOW2_INCOMPAT_DIRTY_BITNR,
|
||||||
QCOW2_INCOMPAT_CORRUPT = 1 << QCOW2_INCOMPAT_CORRUPT_BITNR,
|
|
||||||
|
|
||||||
QCOW2_INCOMPAT_MASK = QCOW2_INCOMPAT_DIRTY
|
QCOW2_INCOMPAT_MASK = QCOW2_INCOMPAT_DIRTY,
|
||||||
| QCOW2_INCOMPAT_CORRUPT,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Compatible feature bits */
|
/* Compatible feature bits */
|
||||||
@@ -195,34 +165,13 @@ enum {
|
|||||||
QCOW2_COMPAT_FEAT_MASK = QCOW2_COMPAT_LAZY_REFCOUNTS,
|
QCOW2_COMPAT_FEAT_MASK = QCOW2_COMPAT_LAZY_REFCOUNTS,
|
||||||
};
|
};
|
||||||
|
|
||||||
enum qcow2_discard_type {
|
|
||||||
QCOW2_DISCARD_NEVER = 0,
|
|
||||||
QCOW2_DISCARD_ALWAYS,
|
|
||||||
QCOW2_DISCARD_REQUEST,
|
|
||||||
QCOW2_DISCARD_SNAPSHOT,
|
|
||||||
QCOW2_DISCARD_OTHER,
|
|
||||||
QCOW2_DISCARD_MAX
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct Qcow2Feature {
|
typedef struct Qcow2Feature {
|
||||||
uint8_t type;
|
uint8_t type;
|
||||||
uint8_t bit;
|
uint8_t bit;
|
||||||
char name[46];
|
char name[46];
|
||||||
} QEMU_PACKED Qcow2Feature;
|
} QEMU_PACKED Qcow2Feature;
|
||||||
|
|
||||||
typedef struct Qcow2DiscardRegion {
|
typedef struct BDRVQcowState {
|
||||||
BlockDriverState *bs;
|
|
||||||
uint64_t offset;
|
|
||||||
uint64_t bytes;
|
|
||||||
QTAILQ_ENTRY(Qcow2DiscardRegion) next;
|
|
||||||
} Qcow2DiscardRegion;
|
|
||||||
|
|
||||||
typedef uint64_t Qcow2GetRefcountFunc(const void *refcount_array,
|
|
||||||
uint64_t index);
|
|
||||||
typedef void Qcow2SetRefcountFunc(void *refcount_array,
|
|
||||||
uint64_t index, uint64_t value);
|
|
||||||
|
|
||||||
typedef struct BDRVQcow2State {
|
|
||||||
int cluster_bits;
|
int cluster_bits;
|
||||||
int cluster_size;
|
int cluster_size;
|
||||||
int cluster_sectors;
|
int cluster_sectors;
|
||||||
@@ -230,8 +179,6 @@ typedef struct BDRVQcow2State {
|
|||||||
int l2_size;
|
int l2_size;
|
||||||
int l1_size;
|
int l1_size;
|
||||||
int l1_vm_state_index;
|
int l1_vm_state_index;
|
||||||
int refcount_block_bits;
|
|
||||||
int refcount_block_size;
|
|
||||||
int csize_shift;
|
int csize_shift;
|
||||||
int csize_mask;
|
int csize_mask;
|
||||||
uint64_t cluster_offset_mask;
|
uint64_t cluster_offset_mask;
|
||||||
@@ -240,8 +187,6 @@ typedef struct BDRVQcow2State {
|
|||||||
|
|
||||||
Qcow2Cache* l2_table_cache;
|
Qcow2Cache* l2_table_cache;
|
||||||
Qcow2Cache* refcount_block_cache;
|
Qcow2Cache* refcount_block_cache;
|
||||||
QEMUTimer *cache_clean_timer;
|
|
||||||
unsigned cache_clean_interval;
|
|
||||||
|
|
||||||
uint8_t *cluster_cache;
|
uint8_t *cluster_cache;
|
||||||
uint8_t *cluster_data;
|
uint8_t *cluster_data;
|
||||||
@@ -256,8 +201,10 @@ typedef struct BDRVQcow2State {
|
|||||||
|
|
||||||
CoMutex lock;
|
CoMutex lock;
|
||||||
|
|
||||||
QCryptoCipher *cipher; /* current cipher, NULL if no key yet */
|
uint32_t crypt_method; /* current crypt method, 0 if no key yet */
|
||||||
uint32_t crypt_method_header;
|
uint32_t crypt_method_header;
|
||||||
|
AES_KEY aes_encrypt_key;
|
||||||
|
AES_KEY aes_decrypt_key;
|
||||||
uint64_t snapshots_offset;
|
uint64_t snapshots_offset;
|
||||||
int snapshots_size;
|
int snapshots_size;
|
||||||
unsigned int nb_snapshots;
|
unsigned int nb_snapshots;
|
||||||
@@ -265,18 +212,6 @@ typedef struct BDRVQcow2State {
|
|||||||
|
|
||||||
int flags;
|
int flags;
|
||||||
int qcow_version;
|
int qcow_version;
|
||||||
bool use_lazy_refcounts;
|
|
||||||
int refcount_order;
|
|
||||||
int refcount_bits;
|
|
||||||
uint64_t refcount_max;
|
|
||||||
|
|
||||||
Qcow2GetRefcountFunc *get_refcount;
|
|
||||||
Qcow2SetRefcountFunc *set_refcount;
|
|
||||||
|
|
||||||
bool discard_passthrough[QCOW2_DISCARD_MAX];
|
|
||||||
|
|
||||||
int overlap_check; /* bitmask of Qcow2MetadataOverlap values */
|
|
||||||
bool signaled_corruption;
|
|
||||||
|
|
||||||
uint64_t incompatible_features;
|
uint64_t incompatible_features;
|
||||||
uint64_t compatible_features;
|
uint64_t compatible_features;
|
||||||
@@ -285,15 +220,20 @@ typedef struct BDRVQcow2State {
|
|||||||
size_t unknown_header_fields_size;
|
size_t unknown_header_fields_size;
|
||||||
void* unknown_header_fields;
|
void* unknown_header_fields;
|
||||||
QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext;
|
QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext;
|
||||||
QTAILQ_HEAD (, Qcow2DiscardRegion) discards;
|
} BDRVQcowState;
|
||||||
bool cache_discards;
|
|
||||||
|
|
||||||
/* Backing file path and format as stored in the image (this is not the
|
/* XXX: use std qcow open function ? */
|
||||||
* effective path/format, which may be the result of a runtime option
|
typedef struct QCowCreateState {
|
||||||
* override) */
|
int cluster_size;
|
||||||
char *image_backing_file;
|
int cluster_bits;
|
||||||
char *image_backing_format;
|
uint16_t *refcount_block;
|
||||||
} BDRVQcow2State;
|
uint64_t *refcount_table;
|
||||||
|
int64_t l1_table_offset;
|
||||||
|
int64_t refcount_table_offset;
|
||||||
|
int64_t refcount_block_offset;
|
||||||
|
} QCowCreateState;
|
||||||
|
|
||||||
|
struct QCowAIOCB;
|
||||||
|
|
||||||
typedef struct Qcow2COWRegion {
|
typedef struct Qcow2COWRegion {
|
||||||
/**
|
/**
|
||||||
@@ -345,9 +285,6 @@ typedef struct QCowL2Meta
|
|||||||
*/
|
*/
|
||||||
Qcow2COWRegion cow_end;
|
Qcow2COWRegion cow_end;
|
||||||
|
|
||||||
/** Pointer to next L2Meta of the same write request */
|
|
||||||
struct QCowL2Meta *next;
|
|
||||||
|
|
||||||
QLIST_ENTRY(QCowL2Meta) next_in_flight;
|
QLIST_ENTRY(QCowL2Meta) next_in_flight;
|
||||||
} QCowL2Meta;
|
} QCowL2Meta;
|
||||||
|
|
||||||
@@ -358,89 +295,30 @@ enum {
|
|||||||
QCOW2_CLUSTER_ZERO
|
QCOW2_CLUSTER_ZERO
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef enum QCow2MetadataOverlap {
|
#define L1E_OFFSET_MASK 0x00ffffffffffff00ULL
|
||||||
QCOW2_OL_MAIN_HEADER_BITNR = 0,
|
#define L2E_OFFSET_MASK 0x00ffffffffffff00ULL
|
||||||
QCOW2_OL_ACTIVE_L1_BITNR = 1,
|
|
||||||
QCOW2_OL_ACTIVE_L2_BITNR = 2,
|
|
||||||
QCOW2_OL_REFCOUNT_TABLE_BITNR = 3,
|
|
||||||
QCOW2_OL_REFCOUNT_BLOCK_BITNR = 4,
|
|
||||||
QCOW2_OL_SNAPSHOT_TABLE_BITNR = 5,
|
|
||||||
QCOW2_OL_INACTIVE_L1_BITNR = 6,
|
|
||||||
QCOW2_OL_INACTIVE_L2_BITNR = 7,
|
|
||||||
|
|
||||||
QCOW2_OL_MAX_BITNR = 8,
|
|
||||||
|
|
||||||
QCOW2_OL_NONE = 0,
|
|
||||||
QCOW2_OL_MAIN_HEADER = (1 << QCOW2_OL_MAIN_HEADER_BITNR),
|
|
||||||
QCOW2_OL_ACTIVE_L1 = (1 << QCOW2_OL_ACTIVE_L1_BITNR),
|
|
||||||
QCOW2_OL_ACTIVE_L2 = (1 << QCOW2_OL_ACTIVE_L2_BITNR),
|
|
||||||
QCOW2_OL_REFCOUNT_TABLE = (1 << QCOW2_OL_REFCOUNT_TABLE_BITNR),
|
|
||||||
QCOW2_OL_REFCOUNT_BLOCK = (1 << QCOW2_OL_REFCOUNT_BLOCK_BITNR),
|
|
||||||
QCOW2_OL_SNAPSHOT_TABLE = (1 << QCOW2_OL_SNAPSHOT_TABLE_BITNR),
|
|
||||||
QCOW2_OL_INACTIVE_L1 = (1 << QCOW2_OL_INACTIVE_L1_BITNR),
|
|
||||||
/* NOTE: Checking overlaps with inactive L2 tables will result in bdrv
|
|
||||||
* reads. */
|
|
||||||
QCOW2_OL_INACTIVE_L2 = (1 << QCOW2_OL_INACTIVE_L2_BITNR),
|
|
||||||
} QCow2MetadataOverlap;
|
|
||||||
|
|
||||||
/* Perform all overlap checks which can be done in constant time */
|
|
||||||
#define QCOW2_OL_CONSTANT \
|
|
||||||
(QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 | QCOW2_OL_REFCOUNT_TABLE | \
|
|
||||||
QCOW2_OL_SNAPSHOT_TABLE)
|
|
||||||
|
|
||||||
/* Perform all overlap checks which don't require disk access */
|
|
||||||
#define QCOW2_OL_CACHED \
|
|
||||||
(QCOW2_OL_CONSTANT | QCOW2_OL_ACTIVE_L2 | QCOW2_OL_REFCOUNT_BLOCK | \
|
|
||||||
QCOW2_OL_INACTIVE_L1)
|
|
||||||
|
|
||||||
/* Perform all overlap checks */
|
|
||||||
#define QCOW2_OL_ALL \
|
|
||||||
(QCOW2_OL_CACHED | QCOW2_OL_INACTIVE_L2)
|
|
||||||
|
|
||||||
#define L1E_OFFSET_MASK 0x00fffffffffffe00ULL
|
|
||||||
#define L2E_OFFSET_MASK 0x00fffffffffffe00ULL
|
|
||||||
#define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL
|
#define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL
|
||||||
|
|
||||||
#define REFT_OFFSET_MASK 0xfffffffffffffe00ULL
|
#define REFT_OFFSET_MASK 0xffffffffffffff00ULL
|
||||||
|
|
||||||
static inline int64_t start_of_cluster(BDRVQcow2State *s, int64_t offset)
|
static inline int size_to_clusters(BDRVQcowState *s, int64_t size)
|
||||||
{
|
|
||||||
return offset & ~(s->cluster_size - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int64_t offset_into_cluster(BDRVQcow2State *s, int64_t offset)
|
|
||||||
{
|
|
||||||
return offset & (s->cluster_size - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline uint64_t size_to_clusters(BDRVQcow2State *s, uint64_t size)
|
|
||||||
{
|
{
|
||||||
return (size + (s->cluster_size - 1)) >> s->cluster_bits;
|
return (size + (s->cluster_size - 1)) >> s->cluster_bits;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int64_t size_to_l1(BDRVQcow2State *s, int64_t size)
|
static inline int64_t size_to_l1(BDRVQcowState *s, int64_t size)
|
||||||
{
|
{
|
||||||
int shift = s->cluster_bits + s->l2_bits;
|
int shift = s->cluster_bits + s->l2_bits;
|
||||||
return (size + (1ULL << shift) - 1) >> shift;
|
return (size + (1ULL << shift) - 1) >> shift;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int offset_to_l2_index(BDRVQcow2State *s, int64_t offset)
|
|
||||||
{
|
|
||||||
return (offset >> s->cluster_bits) & (s->l2_size - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int64_t align_offset(int64_t offset, int n)
|
static inline int64_t align_offset(int64_t offset, int n)
|
||||||
{
|
{
|
||||||
offset = (offset + n - 1) & ~(n - 1);
|
offset = (offset + n - 1) & ~(n - 1);
|
||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int64_t qcow2_vm_state_offset(BDRVQcow2State *s)
|
static inline uint64_t qcow2_max_refcount_clusters(BDRVQcowState *s)
|
||||||
{
|
|
||||||
return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline uint64_t qcow2_max_refcount_clusters(BDRVQcow2State *s)
|
|
||||||
{
|
{
|
||||||
return QCOW_MAX_REFTABLE_SIZE >> s->cluster_bits;
|
return QCOW_MAX_REFTABLE_SIZE >> s->cluster_bits;
|
||||||
}
|
}
|
||||||
@@ -459,27 +337,11 @@ static inline int qcow2_get_cluster_type(uint64_t l2_entry)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Check whether refcounts are eager or lazy */
|
/* Check whether refcounts are eager or lazy */
|
||||||
static inline bool qcow2_need_accurate_refcounts(BDRVQcow2State *s)
|
static inline bool qcow2_need_accurate_refcounts(BDRVQcowState *s)
|
||||||
{
|
{
|
||||||
return !(s->incompatible_features & QCOW2_INCOMPAT_DIRTY);
|
return !(s->incompatible_features & QCOW2_INCOMPAT_DIRTY);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline uint64_t l2meta_cow_start(QCowL2Meta *m)
|
|
||||||
{
|
|
||||||
return m->offset + m->cow_start.offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline uint64_t l2meta_cow_end(QCowL2Meta *m)
|
|
||||||
{
|
|
||||||
return m->offset + m->cow_end.offset
|
|
||||||
+ (m->cow_end.nb_sectors << BDRV_SECTOR_BITS);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline uint64_t refcount_diff(uint64_t r1, uint64_t r2)
|
|
||||||
{
|
|
||||||
return r1 > r2 ? r1 - r2 : r2 - r1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// FIXME Need qcow2_ prefix to global functions
|
// FIXME Need qcow2_ prefix to global functions
|
||||||
|
|
||||||
/* qcow2.c functions */
|
/* qcow2.c functions */
|
||||||
@@ -487,34 +349,20 @@ int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
|
|||||||
int64_t sector_num, int nb_sectors);
|
int64_t sector_num, int nb_sectors);
|
||||||
|
|
||||||
int qcow2_mark_dirty(BlockDriverState *bs);
|
int qcow2_mark_dirty(BlockDriverState *bs);
|
||||||
int qcow2_mark_corrupt(BlockDriverState *bs);
|
|
||||||
int qcow2_mark_consistent(BlockDriverState *bs);
|
|
||||||
int qcow2_update_header(BlockDriverState *bs);
|
int qcow2_update_header(BlockDriverState *bs);
|
||||||
|
|
||||||
void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset,
|
|
||||||
int64_t size, const char *message_format, ...)
|
|
||||||
GCC_FMT_ATTR(5, 6);
|
|
||||||
|
|
||||||
/* qcow2-refcount.c functions */
|
/* qcow2-refcount.c functions */
|
||||||
int qcow2_refcount_init(BlockDriverState *bs);
|
int qcow2_refcount_init(BlockDriverState *bs);
|
||||||
void qcow2_refcount_close(BlockDriverState *bs);
|
void qcow2_refcount_close(BlockDriverState *bs);
|
||||||
|
|
||||||
int qcow2_get_refcount(BlockDriverState *bs, int64_t cluster_index,
|
|
||||||
uint64_t *refcount);
|
|
||||||
|
|
||||||
int qcow2_update_cluster_refcount(BlockDriverState *bs, int64_t cluster_index,
|
|
||||||
uint64_t addend, bool decrease,
|
|
||||||
enum qcow2_discard_type type);
|
|
||||||
|
|
||||||
int64_t qcow2_alloc_clusters(BlockDriverState *bs, uint64_t size);
|
int64_t qcow2_alloc_clusters(BlockDriverState *bs, uint64_t size);
|
||||||
int64_t qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
|
int qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
|
||||||
int64_t nb_clusters);
|
int nb_clusters);
|
||||||
int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
|
int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
|
||||||
void qcow2_free_clusters(BlockDriverState *bs,
|
void qcow2_free_clusters(BlockDriverState *bs,
|
||||||
int64_t offset, int64_t size,
|
int64_t offset, int64_t size);
|
||||||
enum qcow2_discard_type type);
|
void qcow2_free_any_clusters(BlockDriverState *bs,
|
||||||
void qcow2_free_any_clusters(BlockDriverState *bs, uint64_t l2_entry,
|
uint64_t cluster_offset, int nb_clusters);
|
||||||
int nb_clusters, enum qcow2_discard_type type);
|
|
||||||
|
|
||||||
int qcow2_update_snapshot_refcount(BlockDriverState *bs,
|
int qcow2_update_snapshot_refcount(BlockDriverState *bs,
|
||||||
int64_t l1_table_offset, int l1_size, int addend);
|
int64_t l1_table_offset, int l1_size, int addend);
|
||||||
@@ -522,56 +370,35 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
|
|||||||
int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
|
int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
|
||||||
BdrvCheckMode fix);
|
BdrvCheckMode fix);
|
||||||
|
|
||||||
void qcow2_process_discards(BlockDriverState *bs, int ret);
|
|
||||||
|
|
||||||
int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,
|
|
||||||
int64_t size);
|
|
||||||
int qcow2_pre_write_overlap_check(BlockDriverState *bs, int ign, int64_t offset,
|
|
||||||
int64_t size);
|
|
||||||
|
|
||||||
int qcow2_change_refcount_order(BlockDriverState *bs, int refcount_order,
|
|
||||||
BlockDriverAmendStatusCB *status_cb,
|
|
||||||
void *cb_opaque, Error **errp);
|
|
||||||
|
|
||||||
/* qcow2-cluster.c functions */
|
/* qcow2-cluster.c functions */
|
||||||
int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,
|
int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,
|
||||||
bool exact_size);
|
bool exact_size);
|
||||||
int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index);
|
|
||||||
void qcow2_l2_cache_reset(BlockDriverState *bs);
|
void qcow2_l2_cache_reset(BlockDriverState *bs);
|
||||||
int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
|
int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
|
||||||
int qcow2_encrypt_sectors(BDRVQcow2State *s, int64_t sector_num,
|
void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
|
||||||
uint8_t *out_buf, const uint8_t *in_buf,
|
uint8_t *out_buf, const uint8_t *in_buf,
|
||||||
int nb_sectors, bool enc, Error **errp);
|
int nb_sectors, int enc,
|
||||||
|
const AES_KEY *key);
|
||||||
|
|
||||||
int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
|
int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
|
||||||
int *num, uint64_t *cluster_offset);
|
int *num, uint64_t *cluster_offset);
|
||||||
int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
|
int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
|
||||||
int *num, uint64_t *host_offset, QCowL2Meta **m);
|
int n_start, int n_end, int *num, uint64_t *host_offset, QCowL2Meta **m);
|
||||||
uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
|
uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
|
||||||
uint64_t offset,
|
uint64_t offset,
|
||||||
int compressed_size);
|
int compressed_size);
|
||||||
|
|
||||||
int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
|
int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
|
||||||
int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
|
int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
|
||||||
int nb_sectors, enum qcow2_discard_type type, bool full_discard);
|
int nb_sectors);
|
||||||
int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors);
|
int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors);
|
||||||
|
|
||||||
int qcow2_expand_zero_clusters(BlockDriverState *bs,
|
|
||||||
BlockDriverAmendStatusCB *status_cb,
|
|
||||||
void *cb_opaque);
|
|
||||||
|
|
||||||
/* qcow2-snapshot.c functions */
|
/* qcow2-snapshot.c functions */
|
||||||
int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
|
int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
|
||||||
int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
|
int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
|
||||||
int qcow2_snapshot_delete(BlockDriverState *bs,
|
int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
|
||||||
const char *snapshot_id,
|
|
||||||
const char *name,
|
|
||||||
Error **errp);
|
|
||||||
int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
|
int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
|
||||||
int qcow2_snapshot_load_tmp(BlockDriverState *bs,
|
int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name);
|
||||||
const char *snapshot_id,
|
|
||||||
const char *name,
|
|
||||||
Error **errp);
|
|
||||||
|
|
||||||
void qcow2_free_snapshots(BlockDriverState *bs);
|
void qcow2_free_snapshots(BlockDriverState *bs);
|
||||||
int qcow2_read_snapshots(BlockDriverState *bs);
|
int qcow2_read_snapshots(BlockDriverState *bs);
|
||||||
@@ -580,20 +407,16 @@ int qcow2_read_snapshots(BlockDriverState *bs);
|
|||||||
Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables);
|
Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables);
|
||||||
int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c);
|
int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c);
|
||||||
|
|
||||||
void qcow2_cache_entry_mark_dirty(BlockDriverState *bs, Qcow2Cache *c,
|
void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table);
|
||||||
void *table);
|
|
||||||
int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
|
int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
|
||||||
int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
|
int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
|
||||||
Qcow2Cache *dependency);
|
Qcow2Cache *dependency);
|
||||||
void qcow2_cache_depends_on_flush(Qcow2Cache *c);
|
void qcow2_cache_depends_on_flush(Qcow2Cache *c);
|
||||||
|
|
||||||
void qcow2_cache_clean_unused(BlockDriverState *bs, Qcow2Cache *c);
|
|
||||||
int qcow2_cache_empty(BlockDriverState *bs, Qcow2Cache *c);
|
|
||||||
|
|
||||||
int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
|
int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
|
||||||
void **table);
|
void **table);
|
||||||
int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
|
int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
|
||||||
void **table);
|
void **table);
|
||||||
void qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table);
|
int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -227,10 +227,8 @@ int qed_check(BDRVQEDState *s, BdrvCheckResult *result, bool fix)
|
|||||||
};
|
};
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
check.used_clusters = g_try_new0(uint32_t, (check.nclusters + 31) / 32);
|
check.used_clusters = g_malloc0(((check.nclusters + 31) / 32) *
|
||||||
if (check.nclusters && check.used_clusters == NULL) {
|
sizeof(check.used_clusters[0]));
|
||||||
return -ENOMEM;
|
|
||||||
}
|
|
||||||
|
|
||||||
check.result->bfi.total_clusters =
|
check.result->bfi.total_clusters =
|
||||||
(s->header.image_size + s->header.cluster_size - 1) /
|
(s->header.image_size + s->header.cluster_size - 1) /
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
#include "qed.h"
|
#include "qed.h"
|
||||||
|
|
||||||
void *gencb_alloc(size_t len, BlockCompletionFunc *cb, void *opaque)
|
void *gencb_alloc(size_t len, BlockDriverCompletionFunc *cb, void *opaque)
|
||||||
{
|
{
|
||||||
GenericCB *gencb = g_malloc(len);
|
GenericCB *gencb = g_malloc(len);
|
||||||
gencb->cb = cb;
|
gencb->cb = cb;
|
||||||
@@ -24,7 +24,7 @@ void *gencb_alloc(size_t len, BlockCompletionFunc *cb, void *opaque)
|
|||||||
void gencb_complete(void *opaque, int ret)
|
void gencb_complete(void *opaque, int ret)
|
||||||
{
|
{
|
||||||
GenericCB *gencb = opaque;
|
GenericCB *gencb = opaque;
|
||||||
BlockCompletionFunc *cb = gencb->cb;
|
BlockDriverCompletionFunc *cb = gencb->cb;
|
||||||
void *user_opaque = gencb->opaque;
|
void *user_opaque = gencb->opaque;
|
||||||
|
|
||||||
g_free(gencb);
|
g_free(gencb);
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user