blockdev.c 113 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745
  1. /*
  2. * QEMU host block devices
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or
  7. * later. See the COPYING file in the top-level directory.
  8. *
  9. * This file incorporates work covered by the following copyright and
  10. * permission notice:
  11. *
  12. * Copyright (c) 2003-2008 Fabrice Bellard
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining a copy
  15. * of this software and associated documentation files (the "Software"), to deal
  16. * in the Software without restriction, including without limitation the rights
  17. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  18. * copies of the Software, and to permit persons to whom the Software is
  19. * furnished to do so, subject to the following conditions:
  20. *
  21. * The above copyright notice and this permission notice shall be included in
  22. * all copies or substantial portions of the Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  27. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  29. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  30. * THE SOFTWARE.
  31. */
  32. #include "qemu/osdep.h"
  33. #include "sysemu/block-backend.h"
  34. #include "sysemu/blockdev.h"
  35. #include "hw/block/block.h"
  36. #include "block/blockjob.h"
  37. #include "block/qdict.h"
  38. #include "block/throttle-groups.h"
  39. #include "monitor/monitor.h"
  40. #include "qemu/error-report.h"
  41. #include "qemu/option.h"
  42. #include "qemu/qemu-print.h"
  43. #include "qemu/config-file.h"
  44. #include "qapi/qapi-commands-block.h"
  45. #include "qapi/qapi-commands-transaction.h"
  46. #include "qapi/qapi-visit-block-core.h"
  47. #include "qapi/qmp/qdict.h"
  48. #include "qapi/qmp/qnum.h"
  49. #include "qapi/qmp/qstring.h"
  50. #include "qapi/error.h"
  51. #include "qapi/qmp/qerror.h"
  52. #include "qapi/qmp/qlist.h"
  53. #include "qapi/qobject-output-visitor.h"
  54. #include "sysemu/sysemu.h"
  55. #include "sysemu/iothread.h"
  56. #include "block/block_int.h"
  57. #include "block/trace.h"
  58. #include "sysemu/arch_init.h"
  59. #include "sysemu/qtest.h"
  60. #include "sysemu/runstate.h"
  61. #include "qemu/cutils.h"
  62. #include "qemu/help_option.h"
  63. #include "qemu/main-loop.h"
  64. #include "qemu/throttle-options.h"
  65. QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states =
  66. QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states);
  67. void bdrv_set_monitor_owned(BlockDriverState *bs)
  68. {
  69. QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list);
  70. }
  71. static const char *const if_name[IF_COUNT] = {
  72. [IF_NONE] = "none",
  73. [IF_IDE] = "ide",
  74. [IF_SCSI] = "scsi",
  75. [IF_FLOPPY] = "floppy",
  76. [IF_PFLASH] = "pflash",
  77. [IF_MTD] = "mtd",
  78. [IF_SD] = "sd",
  79. [IF_VIRTIO] = "virtio",
  80. [IF_XEN] = "xen",
  81. };
  82. static int if_max_devs[IF_COUNT] = {
  83. /*
  84. * Do not change these numbers! They govern how drive option
  85. * index maps to unit and bus. That mapping is ABI.
  86. *
  87. * All controllers used to implement if=T drives need to support
  88. * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
  89. * Otherwise, some index values map to "impossible" bus, unit
  90. * values.
  91. *
  92. * For instance, if you change [IF_SCSI] to 255, -drive
  93. * if=scsi,index=12 no longer means bus=1,unit=5, but
  94. * bus=0,unit=12. With an lsi53c895a controller (7 units max),
  95. * the drive can't be set up. Regression.
  96. */
  97. [IF_IDE] = 2,
  98. [IF_SCSI] = 7,
  99. };
  100. /**
  101. * Boards may call this to offer board-by-board overrides
  102. * of the default, global values.
  103. */
  104. void override_max_devs(BlockInterfaceType type, int max_devs)
  105. {
  106. BlockBackend *blk;
  107. DriveInfo *dinfo;
  108. if (max_devs <= 0) {
  109. return;
  110. }
  111. for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
  112. dinfo = blk_legacy_dinfo(blk);
  113. if (dinfo->type == type) {
  114. fprintf(stderr, "Cannot override units-per-bus property of"
  115. " the %s interface, because a drive of that type has"
  116. " already been added.\n", if_name[type]);
  117. g_assert_not_reached();
  118. }
  119. }
  120. if_max_devs[type] = max_devs;
  121. }
  122. /*
  123. * We automatically delete the drive when a device using it gets
  124. * unplugged. Questionable feature, but we can't just drop it.
  125. * Device models call blockdev_mark_auto_del() to schedule the
  126. * automatic deletion, and generic qdev code calls blockdev_auto_del()
  127. * when deletion is actually safe.
  128. */
  129. void blockdev_mark_auto_del(BlockBackend *blk)
  130. {
  131. DriveInfo *dinfo = blk_legacy_dinfo(blk);
  132. BlockJob *job;
  133. if (!dinfo) {
  134. return;
  135. }
  136. for (job = block_job_next(NULL); job; job = block_job_next(job)) {
  137. if (block_job_has_bdrv(job, blk_bs(blk))) {
  138. AioContext *aio_context = job->job.aio_context;
  139. aio_context_acquire(aio_context);
  140. job_cancel(&job->job, false);
  141. aio_context_release(aio_context);
  142. }
  143. }
  144. dinfo->auto_del = 1;
  145. }
  146. void blockdev_auto_del(BlockBackend *blk)
  147. {
  148. DriveInfo *dinfo = blk_legacy_dinfo(blk);
  149. if (dinfo && dinfo->auto_del) {
  150. monitor_remove_blk(blk);
  151. blk_unref(blk);
  152. }
  153. }
  154. /**
  155. * Returns the current mapping of how many units per bus
  156. * a particular interface can support.
  157. *
  158. * A positive integer indicates n units per bus.
  159. * 0 implies the mapping has not been established.
  160. * -1 indicates an invalid BlockInterfaceType was given.
  161. */
  162. int drive_get_max_devs(BlockInterfaceType type)
  163. {
  164. if (type >= IF_IDE && type < IF_COUNT) {
  165. return if_max_devs[type];
  166. }
  167. return -1;
  168. }
  169. static int drive_index_to_bus_id(BlockInterfaceType type, int index)
  170. {
  171. int max_devs = if_max_devs[type];
  172. return max_devs ? index / max_devs : 0;
  173. }
  174. static int drive_index_to_unit_id(BlockInterfaceType type, int index)
  175. {
  176. int max_devs = if_max_devs[type];
  177. return max_devs ? index % max_devs : index;
  178. }
  179. QemuOpts *drive_def(const char *optstr)
  180. {
  181. return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false);
  182. }
  183. QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
  184. const char *optstr)
  185. {
  186. QemuOpts *opts;
  187. opts = drive_def(optstr);
  188. if (!opts) {
  189. return NULL;
  190. }
  191. if (type != IF_DEFAULT) {
  192. qemu_opt_set(opts, "if", if_name[type], &error_abort);
  193. }
  194. if (index >= 0) {
  195. qemu_opt_set_number(opts, "index", index, &error_abort);
  196. }
  197. if (file)
  198. qemu_opt_set(opts, "file", file, &error_abort);
  199. return opts;
  200. }
  201. DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
  202. {
  203. BlockBackend *blk;
  204. DriveInfo *dinfo;
  205. for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
  206. dinfo = blk_legacy_dinfo(blk);
  207. if (dinfo && dinfo->type == type
  208. && dinfo->bus == bus && dinfo->unit == unit) {
  209. return dinfo;
  210. }
  211. }
  212. return NULL;
  213. }
  214. void drive_mark_claimed_by_board(void)
  215. {
  216. BlockBackend *blk;
  217. DriveInfo *dinfo;
  218. for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
  219. dinfo = blk_legacy_dinfo(blk);
  220. if (dinfo && blk_get_attached_dev(blk)) {
  221. dinfo->claimed_by_board = true;
  222. }
  223. }
  224. }
  225. void drive_check_orphaned(void)
  226. {
  227. BlockBackend *blk;
  228. DriveInfo *dinfo;
  229. Location loc;
  230. bool orphans = false;
  231. for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
  232. dinfo = blk_legacy_dinfo(blk);
  233. if (dinfo->is_default || dinfo->type == IF_NONE) {
  234. continue;
  235. }
  236. if (!blk_get_attached_dev(blk)) {
  237. loc_push_none(&loc);
  238. qemu_opts_loc_restore(dinfo->opts);
  239. error_report("machine type does not support"
  240. " if=%s,bus=%d,unit=%d",
  241. if_name[dinfo->type], dinfo->bus, dinfo->unit);
  242. loc_pop(&loc);
  243. orphans = true;
  244. continue;
  245. }
  246. if (!dinfo->claimed_by_board && dinfo->type != IF_VIRTIO) {
  247. loc_push_none(&loc);
  248. qemu_opts_loc_restore(dinfo->opts);
  249. warn_report("bogus if=%s is deprecated, use if=none",
  250. if_name[dinfo->type]);
  251. loc_pop(&loc);
  252. }
  253. }
  254. if (orphans) {
  255. exit(1);
  256. }
  257. }
  258. DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
  259. {
  260. return drive_get(type,
  261. drive_index_to_bus_id(type, index),
  262. drive_index_to_unit_id(type, index));
  263. }
  264. int drive_get_max_bus(BlockInterfaceType type)
  265. {
  266. int max_bus;
  267. BlockBackend *blk;
  268. DriveInfo *dinfo;
  269. max_bus = -1;
  270. for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
  271. dinfo = blk_legacy_dinfo(blk);
  272. if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
  273. max_bus = dinfo->bus;
  274. }
  275. }
  276. return max_bus;
  277. }
  278. /* Get a block device. This should only be used for single-drive devices
  279. (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
  280. appropriate bus. */
  281. DriveInfo *drive_get_next(BlockInterfaceType type)
  282. {
  283. static int next_block_unit[IF_COUNT];
  284. return drive_get(type, 0, next_block_unit[type]++);
  285. }
  286. static void bdrv_format_print(void *opaque, const char *name)
  287. {
  288. qemu_printf(" %s", name);
  289. }
  290. typedef struct {
  291. QEMUBH *bh;
  292. BlockDriverState *bs;
  293. } BDRVPutRefBH;
  294. static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
  295. {
  296. if (!strcmp(buf, "ignore")) {
  297. return BLOCKDEV_ON_ERROR_IGNORE;
  298. } else if (!is_read && !strcmp(buf, "enospc")) {
  299. return BLOCKDEV_ON_ERROR_ENOSPC;
  300. } else if (!strcmp(buf, "stop")) {
  301. return BLOCKDEV_ON_ERROR_STOP;
  302. } else if (!strcmp(buf, "report")) {
  303. return BLOCKDEV_ON_ERROR_REPORT;
  304. } else {
  305. error_setg(errp, "'%s' invalid %s error action",
  306. buf, is_read ? "read" : "write");
  307. return -1;
  308. }
  309. }
  310. static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals,
  311. Error **errp)
  312. {
  313. const QListEntry *entry;
  314. for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) {
  315. switch (qobject_type(entry->value)) {
  316. case QTYPE_QSTRING: {
  317. unsigned long long length;
  318. const char *str = qstring_get_str(qobject_to(QString,
  319. entry->value));
  320. if (parse_uint_full(str, &length, 10) == 0 &&
  321. length > 0 && length <= UINT_MAX) {
  322. block_acct_add_interval(stats, (unsigned) length);
  323. } else {
  324. error_setg(errp, "Invalid interval length: %s", str);
  325. return false;
  326. }
  327. break;
  328. }
  329. case QTYPE_QNUM: {
  330. int64_t length = qnum_get_int(qobject_to(QNum, entry->value));
  331. if (length > 0 && length <= UINT_MAX) {
  332. block_acct_add_interval(stats, (unsigned) length);
  333. } else {
  334. error_setg(errp, "Invalid interval length: %" PRId64, length);
  335. return false;
  336. }
  337. break;
  338. }
  339. default:
  340. error_setg(errp, "The specification of stats-intervals is invalid");
  341. return false;
  342. }
  343. }
  344. return true;
  345. }
  346. typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
  347. /* All parameters but @opts are optional and may be set to NULL. */
  348. static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
  349. const char **throttling_group, ThrottleConfig *throttle_cfg,
  350. BlockdevDetectZeroesOptions *detect_zeroes, Error **errp)
  351. {
  352. Error *local_error = NULL;
  353. const char *aio;
  354. if (bdrv_flags) {
  355. if (qemu_opt_get_bool(opts, "copy-on-read", false)) {
  356. *bdrv_flags |= BDRV_O_COPY_ON_READ;
  357. }
  358. if ((aio = qemu_opt_get(opts, "aio")) != NULL) {
  359. if (bdrv_parse_aio(aio, bdrv_flags) < 0) {
  360. error_setg(errp, "invalid aio option");
  361. return;
  362. }
  363. }
  364. }
  365. /* disk I/O throttling */
  366. if (throttling_group) {
  367. *throttling_group = qemu_opt_get(opts, "throttling.group");
  368. }
  369. if (throttle_cfg) {
  370. throttle_config_init(throttle_cfg);
  371. throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg =
  372. qemu_opt_get_number(opts, "throttling.bps-total", 0);
  373. throttle_cfg->buckets[THROTTLE_BPS_READ].avg =
  374. qemu_opt_get_number(opts, "throttling.bps-read", 0);
  375. throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg =
  376. qemu_opt_get_number(opts, "throttling.bps-write", 0);
  377. throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg =
  378. qemu_opt_get_number(opts, "throttling.iops-total", 0);
  379. throttle_cfg->buckets[THROTTLE_OPS_READ].avg =
  380. qemu_opt_get_number(opts, "throttling.iops-read", 0);
  381. throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg =
  382. qemu_opt_get_number(opts, "throttling.iops-write", 0);
  383. throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max =
  384. qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
  385. throttle_cfg->buckets[THROTTLE_BPS_READ].max =
  386. qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
  387. throttle_cfg->buckets[THROTTLE_BPS_WRITE].max =
  388. qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
  389. throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max =
  390. qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
  391. throttle_cfg->buckets[THROTTLE_OPS_READ].max =
  392. qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
  393. throttle_cfg->buckets[THROTTLE_OPS_WRITE].max =
  394. qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
  395. throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length =
  396. qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1);
  397. throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length =
  398. qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1);
  399. throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length =
  400. qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1);
  401. throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length =
  402. qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1);
  403. throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length =
  404. qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1);
  405. throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length =
  406. qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1);
  407. throttle_cfg->op_size =
  408. qemu_opt_get_number(opts, "throttling.iops-size", 0);
  409. if (!throttle_is_valid(throttle_cfg, errp)) {
  410. return;
  411. }
  412. }
  413. if (detect_zeroes) {
  414. *detect_zeroes =
  415. qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
  416. qemu_opt_get(opts, "detect-zeroes"),
  417. BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
  418. &local_error);
  419. if (local_error) {
  420. error_propagate(errp, local_error);
  421. return;
  422. }
  423. }
  424. }
  425. /* Takes the ownership of bs_opts */
  426. static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
  427. Error **errp)
  428. {
  429. const char *buf;
  430. int bdrv_flags = 0;
  431. int on_read_error, on_write_error;
  432. bool account_invalid, account_failed;
  433. bool writethrough, read_only;
  434. BlockBackend *blk;
  435. BlockDriverState *bs;
  436. ThrottleConfig cfg;
  437. int snapshot = 0;
  438. Error *error = NULL;
  439. QemuOpts *opts;
  440. QDict *interval_dict = NULL;
  441. QList *interval_list = NULL;
  442. const char *id;
  443. BlockdevDetectZeroesOptions detect_zeroes =
  444. BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
  445. const char *throttling_group = NULL;
  446. /* Check common options by copying from bs_opts to opts, all other options
  447. * stay in bs_opts for processing by bdrv_open(). */
  448. id = qdict_get_try_str(bs_opts, "id");
  449. opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, errp);
  450. if (!opts) {
  451. goto err_no_opts;
  452. }
  453. if (!qemu_opts_absorb_qdict(opts, bs_opts, errp)) {
  454. goto early_err;
  455. }
  456. if (id) {
  457. qdict_del(bs_opts, "id");
  458. }
  459. /* extract parameters */
  460. snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
  461. account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true);
  462. account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true);
  463. writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true);
  464. id = qemu_opts_id(opts);
  465. qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals.");
  466. qdict_array_split(interval_dict, &interval_list);
  467. if (qdict_size(interval_dict) != 0) {
  468. error_setg(errp, "Invalid option stats-intervals.%s",
  469. qdict_first(interval_dict)->key);
  470. goto early_err;
  471. }
  472. extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg,
  473. &detect_zeroes, &error);
  474. if (error) {
  475. error_propagate(errp, error);
  476. goto early_err;
  477. }
  478. if ((buf = qemu_opt_get(opts, "format")) != NULL) {
  479. if (is_help_option(buf)) {
  480. qemu_printf("Supported formats:");
  481. bdrv_iterate_format(bdrv_format_print, NULL, false);
  482. qemu_printf("\nSupported formats (read-only):");
  483. bdrv_iterate_format(bdrv_format_print, NULL, true);
  484. qemu_printf("\n");
  485. goto early_err;
  486. }
  487. if (qdict_haskey(bs_opts, "driver")) {
  488. error_setg(errp, "Cannot specify both 'driver' and 'format'");
  489. goto early_err;
  490. }
  491. qdict_put_str(bs_opts, "driver", buf);
  492. }
  493. on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
  494. if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
  495. on_write_error = parse_block_error_action(buf, 0, &error);
  496. if (error) {
  497. error_propagate(errp, error);
  498. goto early_err;
  499. }
  500. }
  501. on_read_error = BLOCKDEV_ON_ERROR_REPORT;
  502. if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
  503. on_read_error = parse_block_error_action(buf, 1, &error);
  504. if (error) {
  505. error_propagate(errp, error);
  506. goto early_err;
  507. }
  508. }
  509. if (snapshot) {
  510. bdrv_flags |= BDRV_O_SNAPSHOT;
  511. }
  512. read_only = qemu_opt_get_bool(opts, BDRV_OPT_READ_ONLY, false);
  513. /* init */
  514. if ((!file || !*file) && !qdict_size(bs_opts)) {
  515. BlockBackendRootState *blk_rs;
  516. blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
  517. blk_rs = blk_get_root_state(blk);
  518. blk_rs->open_flags = bdrv_flags;
  519. blk_rs->read_only = read_only;
  520. blk_rs->detect_zeroes = detect_zeroes;
  521. qobject_unref(bs_opts);
  522. } else {
  523. if (file && !*file) {
  524. file = NULL;
  525. }
  526. /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
  527. * with other callers) rather than what we want as the real defaults.
  528. * Apply the defaults here instead. */
  529. qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
  530. qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
  531. qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY,
  532. read_only ? "on" : "off");
  533. qdict_set_default_str(bs_opts, BDRV_OPT_AUTO_READ_ONLY, "on");
  534. assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0);
  535. if (runstate_check(RUN_STATE_INMIGRATE)) {
  536. bdrv_flags |= BDRV_O_INACTIVE;
  537. }
  538. blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp);
  539. if (!blk) {
  540. goto err_no_bs_opts;
  541. }
  542. bs = blk_bs(blk);
  543. bs->detect_zeroes = detect_zeroes;
  544. block_acct_setup(blk_get_stats(blk), account_invalid, account_failed);
  545. if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) {
  546. blk_unref(blk);
  547. blk = NULL;
  548. goto err_no_bs_opts;
  549. }
  550. }
  551. /* disk I/O throttling */
  552. if (throttle_enabled(&cfg)) {
  553. if (!throttling_group) {
  554. throttling_group = id;
  555. }
  556. blk_io_limits_enable(blk, throttling_group);
  557. blk_set_io_limits(blk, &cfg);
  558. }
  559. blk_set_enable_write_cache(blk, !writethrough);
  560. blk_set_on_error(blk, on_read_error, on_write_error);
  561. if (!monitor_add_blk(blk, id, errp)) {
  562. blk_unref(blk);
  563. blk = NULL;
  564. goto err_no_bs_opts;
  565. }
  566. err_no_bs_opts:
  567. qemu_opts_del(opts);
  568. qobject_unref(interval_dict);
  569. qobject_unref(interval_list);
  570. return blk;
  571. early_err:
  572. qemu_opts_del(opts);
  573. qobject_unref(interval_dict);
  574. qobject_unref(interval_list);
  575. err_no_opts:
  576. qobject_unref(bs_opts);
  577. return NULL;
  578. }
  579. /* Takes the ownership of bs_opts */
  580. BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)
  581. {
  582. int bdrv_flags = 0;
  583. /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
  584. * with other callers) rather than what we want as the real defaults.
  585. * Apply the defaults here instead. */
  586. qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off");
  587. qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off");
  588. qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, "off");
  589. if (runstate_check(RUN_STATE_INMIGRATE)) {
  590. bdrv_flags |= BDRV_O_INACTIVE;
  591. }
  592. return bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp);
  593. }
  594. void blockdev_close_all_bdrv_states(void)
  595. {
  596. BlockDriverState *bs, *next_bs;
  597. QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) {
  598. AioContext *ctx = bdrv_get_aio_context(bs);
  599. aio_context_acquire(ctx);
  600. bdrv_unref(bs);
  601. aio_context_release(ctx);
  602. }
  603. }
  604. /* Iterates over the list of monitor-owned BlockDriverStates */
  605. BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs)
  606. {
  607. return bs ? QTAILQ_NEXT(bs, monitor_list)
  608. : QTAILQ_FIRST(&monitor_bdrv_states);
  609. }
  610. static bool qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
  611. Error **errp)
  612. {
  613. const char *value;
  614. value = qemu_opt_get(opts, from);
  615. if (value) {
  616. if (qemu_opt_find(opts, to)) {
  617. error_setg(errp, "'%s' and its alias '%s' can't be used at the "
  618. "same time", to, from);
  619. return false;
  620. }
  621. }
  622. /* rename all items in opts */
  623. while ((value = qemu_opt_get(opts, from))) {
  624. qemu_opt_set(opts, to, value, &error_abort);
  625. qemu_opt_unset(opts, from);
  626. }
  627. return true;
  628. }
  629. QemuOptsList qemu_legacy_drive_opts = {
  630. .name = "drive",
  631. .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
  632. .desc = {
  633. {
  634. .name = "bus",
  635. .type = QEMU_OPT_NUMBER,
  636. .help = "bus number",
  637. },{
  638. .name = "unit",
  639. .type = QEMU_OPT_NUMBER,
  640. .help = "unit number (i.e. lun for scsi)",
  641. },{
  642. .name = "index",
  643. .type = QEMU_OPT_NUMBER,
  644. .help = "index number",
  645. },{
  646. .name = "media",
  647. .type = QEMU_OPT_STRING,
  648. .help = "media type (disk, cdrom)",
  649. },{
  650. .name = "if",
  651. .type = QEMU_OPT_STRING,
  652. .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
  653. },{
  654. .name = "file",
  655. .type = QEMU_OPT_STRING,
  656. .help = "file name",
  657. },
  658. /* Options that are passed on, but have special semantics with -drive */
  659. {
  660. .name = BDRV_OPT_READ_ONLY,
  661. .type = QEMU_OPT_BOOL,
  662. .help = "open drive file as read-only",
  663. },{
  664. .name = "rerror",
  665. .type = QEMU_OPT_STRING,
  666. .help = "read error action",
  667. },{
  668. .name = "werror",
  669. .type = QEMU_OPT_STRING,
  670. .help = "write error action",
  671. },{
  672. .name = "copy-on-read",
  673. .type = QEMU_OPT_BOOL,
  674. .help = "copy read data from backing file into image file",
  675. },
  676. { /* end of list */ }
  677. },
  678. };
  679. DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type,
  680. Error **errp)
  681. {
  682. const char *value;
  683. BlockBackend *blk;
  684. DriveInfo *dinfo = NULL;
  685. QDict *bs_opts;
  686. QemuOpts *legacy_opts;
  687. DriveMediaType media = MEDIA_DISK;
  688. BlockInterfaceType type;
  689. int max_devs, bus_id, unit_id, index;
  690. const char *werror, *rerror;
  691. bool read_only = false;
  692. bool copy_on_read;
  693. const char *filename;
  694. int i;
  695. /* Change legacy command line options into QMP ones */
  696. static const struct {
  697. const char *from;
  698. const char *to;
  699. } opt_renames[] = {
  700. { "iops", "throttling.iops-total" },
  701. { "iops_rd", "throttling.iops-read" },
  702. { "iops_wr", "throttling.iops-write" },
  703. { "bps", "throttling.bps-total" },
  704. { "bps_rd", "throttling.bps-read" },
  705. { "bps_wr", "throttling.bps-write" },
  706. { "iops_max", "throttling.iops-total-max" },
  707. { "iops_rd_max", "throttling.iops-read-max" },
  708. { "iops_wr_max", "throttling.iops-write-max" },
  709. { "bps_max", "throttling.bps-total-max" },
  710. { "bps_rd_max", "throttling.bps-read-max" },
  711. { "bps_wr_max", "throttling.bps-write-max" },
  712. { "iops_size", "throttling.iops-size" },
  713. { "group", "throttling.group" },
  714. { "readonly", BDRV_OPT_READ_ONLY },
  715. };
  716. for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
  717. if (!qemu_opt_rename(all_opts, opt_renames[i].from,
  718. opt_renames[i].to, errp)) {
  719. return NULL;
  720. }
  721. }
  722. value = qemu_opt_get(all_opts, "cache");
  723. if (value) {
  724. int flags = 0;
  725. bool writethrough;
  726. if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) {
  727. error_setg(errp, "invalid cache option");
  728. return NULL;
  729. }
  730. /* Specific options take precedence */
  731. if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) {
  732. qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB,
  733. !writethrough, &error_abort);
  734. }
  735. if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) {
  736. qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT,
  737. !!(flags & BDRV_O_NOCACHE), &error_abort);
  738. }
  739. if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) {
  740. qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH,
  741. !!(flags & BDRV_O_NO_FLUSH), &error_abort);
  742. }
  743. qemu_opt_unset(all_opts, "cache");
  744. }
  745. /* Get a QDict for processing the options */
  746. bs_opts = qdict_new();
  747. qemu_opts_to_qdict(all_opts, bs_opts);
  748. legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
  749. &error_abort);
  750. if (!qemu_opts_absorb_qdict(legacy_opts, bs_opts, errp)) {
  751. goto fail;
  752. }
  753. /* Media type */
  754. value = qemu_opt_get(legacy_opts, "media");
  755. if (value) {
  756. if (!strcmp(value, "disk")) {
  757. media = MEDIA_DISK;
  758. } else if (!strcmp(value, "cdrom")) {
  759. media = MEDIA_CDROM;
  760. read_only = true;
  761. } else {
  762. error_setg(errp, "'%s' invalid media", value);
  763. goto fail;
  764. }
  765. }
  766. /* copy-on-read is disabled with a warning for read-only devices */
  767. read_only |= qemu_opt_get_bool(legacy_opts, BDRV_OPT_READ_ONLY, false);
  768. copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
  769. if (read_only && copy_on_read) {
  770. warn_report("disabling copy-on-read on read-only drive");
  771. copy_on_read = false;
  772. }
  773. qdict_put_str(bs_opts, BDRV_OPT_READ_ONLY, read_only ? "on" : "off");
  774. qdict_put_str(bs_opts, "copy-on-read", copy_on_read ? "on" : "off");
  775. /* Controller type */
  776. value = qemu_opt_get(legacy_opts, "if");
  777. if (value) {
  778. for (type = 0;
  779. type < IF_COUNT && strcmp(value, if_name[type]);
  780. type++) {
  781. }
  782. if (type == IF_COUNT) {
  783. error_setg(errp, "unsupported bus type '%s'", value);
  784. goto fail;
  785. }
  786. } else {
  787. type = block_default_type;
  788. }
  789. /* Device address specified by bus/unit or index.
  790. * If none was specified, try to find the first free one. */
  791. bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
  792. unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
  793. index = qemu_opt_get_number(legacy_opts, "index", -1);
  794. max_devs = if_max_devs[type];
  795. if (index != -1) {
  796. if (bus_id != 0 || unit_id != -1) {
  797. error_setg(errp, "index cannot be used with bus and unit");
  798. goto fail;
  799. }
  800. bus_id = drive_index_to_bus_id(type, index);
  801. unit_id = drive_index_to_unit_id(type, index);
  802. }
  803. if (unit_id == -1) {
  804. unit_id = 0;
  805. while (drive_get(type, bus_id, unit_id) != NULL) {
  806. unit_id++;
  807. if (max_devs && unit_id >= max_devs) {
  808. unit_id -= max_devs;
  809. bus_id++;
  810. }
  811. }
  812. }
  813. if (max_devs && unit_id >= max_devs) {
  814. error_setg(errp, "unit %d too big (max is %d)", unit_id, max_devs - 1);
  815. goto fail;
  816. }
  817. if (drive_get(type, bus_id, unit_id) != NULL) {
  818. error_setg(errp, "drive with bus=%d, unit=%d (index=%d) exists",
  819. bus_id, unit_id, index);
  820. goto fail;
  821. }
  822. /* no id supplied -> create one */
  823. if (qemu_opts_id(all_opts) == NULL) {
  824. char *new_id;
  825. const char *mediastr = "";
  826. if (type == IF_IDE || type == IF_SCSI) {
  827. mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
  828. }
  829. if (max_devs) {
  830. new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
  831. mediastr, unit_id);
  832. } else {
  833. new_id = g_strdup_printf("%s%s%i", if_name[type],
  834. mediastr, unit_id);
  835. }
  836. qdict_put_str(bs_opts, "id", new_id);
  837. g_free(new_id);
  838. }
  839. /* Add virtio block device */
  840. if (type == IF_VIRTIO) {
  841. QemuOpts *devopts;
  842. devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
  843. &error_abort);
  844. if (arch_type == QEMU_ARCH_S390X) {
  845. qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort);
  846. } else {
  847. qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort);
  848. }
  849. qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"),
  850. &error_abort);
  851. }
  852. filename = qemu_opt_get(legacy_opts, "file");
  853. /* Check werror/rerror compatibility with if=... */
  854. werror = qemu_opt_get(legacy_opts, "werror");
  855. if (werror != NULL) {
  856. if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
  857. type != IF_NONE) {
  858. error_setg(errp, "werror is not supported by this bus type");
  859. goto fail;
  860. }
  861. qdict_put_str(bs_opts, "werror", werror);
  862. }
  863. rerror = qemu_opt_get(legacy_opts, "rerror");
  864. if (rerror != NULL) {
  865. if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
  866. type != IF_NONE) {
  867. error_setg(errp, "rerror is not supported by this bus type");
  868. goto fail;
  869. }
  870. qdict_put_str(bs_opts, "rerror", rerror);
  871. }
  872. /* Actual block device init: Functionality shared with blockdev-add */
  873. blk = blockdev_init(filename, bs_opts, errp);
  874. bs_opts = NULL;
  875. if (!blk) {
  876. goto fail;
  877. }
  878. /* Create legacy DriveInfo */
  879. dinfo = g_malloc0(sizeof(*dinfo));
  880. dinfo->opts = all_opts;
  881. dinfo->type = type;
  882. dinfo->bus = bus_id;
  883. dinfo->unit = unit_id;
  884. blk_set_legacy_dinfo(blk, dinfo);
  885. switch(type) {
  886. case IF_IDE:
  887. case IF_SCSI:
  888. case IF_XEN:
  889. case IF_NONE:
  890. dinfo->media_cd = media == MEDIA_CDROM;
  891. break;
  892. default:
  893. break;
  894. }
  895. fail:
  896. qemu_opts_del(legacy_opts);
  897. qobject_unref(bs_opts);
  898. return dinfo;
  899. }
  900. static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp)
  901. {
  902. BlockDriverState *bs;
  903. bs = bdrv_lookup_bs(name, name, errp);
  904. if (bs == NULL) {
  905. return NULL;
  906. }
  907. if (!bdrv_is_root_node(bs)) {
  908. error_setg(errp, "Need a root block node");
  909. return NULL;
  910. }
  911. if (!bdrv_is_inserted(bs)) {
  912. error_setg(errp, "Device has no medium");
  913. return NULL;
  914. }
  915. return bs;
  916. }
  917. static void blockdev_do_action(TransactionAction *action, Error **errp)
  918. {
  919. TransactionActionList list;
  920. list.value = action;
  921. list.next = NULL;
  922. qmp_transaction(&list, false, NULL, errp);
  923. }
  924. void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
  925. bool has_node_name, const char *node_name,
  926. const char *snapshot_file,
  927. bool has_snapshot_node_name,
  928. const char *snapshot_node_name,
  929. bool has_format, const char *format,
  930. bool has_mode, NewImageMode mode, Error **errp)
  931. {
  932. BlockdevSnapshotSync snapshot = {
  933. .has_device = has_device,
  934. .device = (char *) device,
  935. .has_node_name = has_node_name,
  936. .node_name = (char *) node_name,
  937. .snapshot_file = (char *) snapshot_file,
  938. .has_snapshot_node_name = has_snapshot_node_name,
  939. .snapshot_node_name = (char *) snapshot_node_name,
  940. .has_format = has_format,
  941. .format = (char *) format,
  942. .has_mode = has_mode,
  943. .mode = mode,
  944. };
  945. TransactionAction action = {
  946. .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
  947. .u.blockdev_snapshot_sync.data = &snapshot,
  948. };
  949. blockdev_do_action(&action, errp);
  950. }
  951. void qmp_blockdev_snapshot(const char *node, const char *overlay,
  952. Error **errp)
  953. {
  954. BlockdevSnapshot snapshot_data = {
  955. .node = (char *) node,
  956. .overlay = (char *) overlay
  957. };
  958. TransactionAction action = {
  959. .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT,
  960. .u.blockdev_snapshot.data = &snapshot_data,
  961. };
  962. blockdev_do_action(&action, errp);
  963. }
  964. void qmp_blockdev_snapshot_internal_sync(const char *device,
  965. const char *name,
  966. Error **errp)
  967. {
  968. BlockdevSnapshotInternal snapshot = {
  969. .device = (char *) device,
  970. .name = (char *) name
  971. };
  972. TransactionAction action = {
  973. .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
  974. .u.blockdev_snapshot_internal_sync.data = &snapshot,
  975. };
  976. blockdev_do_action(&action, errp);
  977. }
  978. SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
  979. bool has_id,
  980. const char *id,
  981. bool has_name,
  982. const char *name,
  983. Error **errp)
  984. {
  985. BlockDriverState *bs;
  986. AioContext *aio_context;
  987. QEMUSnapshotInfo sn;
  988. Error *local_err = NULL;
  989. SnapshotInfo *info = NULL;
  990. int ret;
  991. bs = qmp_get_root_bs(device, errp);
  992. if (!bs) {
  993. return NULL;
  994. }
  995. aio_context = bdrv_get_aio_context(bs);
  996. aio_context_acquire(aio_context);
  997. if (!has_id) {
  998. id = NULL;
  999. }
  1000. if (!has_name) {
  1001. name = NULL;
  1002. }
  1003. if (!id && !name) {
  1004. error_setg(errp, "Name or id must be provided");
  1005. goto out_aio_context;
  1006. }
  1007. if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) {
  1008. goto out_aio_context;
  1009. }
  1010. ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
  1011. if (local_err) {
  1012. error_propagate(errp, local_err);
  1013. goto out_aio_context;
  1014. }
  1015. if (!ret) {
  1016. error_setg(errp,
  1017. "Snapshot with id '%s' and name '%s' does not exist on "
  1018. "device '%s'",
  1019. STR_OR_NULL(id), STR_OR_NULL(name), device);
  1020. goto out_aio_context;
  1021. }
  1022. bdrv_snapshot_delete(bs, id, name, &local_err);
  1023. if (local_err) {
  1024. error_propagate(errp, local_err);
  1025. goto out_aio_context;
  1026. }
  1027. aio_context_release(aio_context);
  1028. info = g_new0(SnapshotInfo, 1);
  1029. info->id = g_strdup(sn.id_str);
  1030. info->name = g_strdup(sn.name);
  1031. info->date_nsec = sn.date_nsec;
  1032. info->date_sec = sn.date_sec;
  1033. info->vm_state_size = sn.vm_state_size;
  1034. info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
  1035. info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
  1036. return info;
  1037. out_aio_context:
  1038. aio_context_release(aio_context);
  1039. return NULL;
  1040. }
  1041. /* New and old BlockDriverState structs for atomic group operations */
  1042. typedef struct BlkActionState BlkActionState;
  1043. /**
  1044. * BlkActionOps:
  1045. * Table of operations that define an Action.
  1046. *
  1047. * @instance_size: Size of state struct, in bytes.
  1048. * @prepare: Prepare the work, must NOT be NULL.
  1049. * @commit: Commit the changes, can be NULL.
  1050. * @abort: Abort the changes on fail, can be NULL.
  1051. * @clean: Clean up resources after all transaction actions have called
  1052. * commit() or abort(). Can be NULL.
  1053. *
  1054. * Only prepare() may fail. In a single transaction, only one of commit() or
  1055. * abort() will be called. clean() will always be called if it is present.
  1056. */
  1057. typedef struct BlkActionOps {
  1058. size_t instance_size;
  1059. void (*prepare)(BlkActionState *common, Error **errp);
  1060. void (*commit)(BlkActionState *common);
  1061. void (*abort)(BlkActionState *common);
  1062. void (*clean)(BlkActionState *common);
  1063. } BlkActionOps;
  1064. /**
  1065. * BlkActionState:
  1066. * Describes one Action's state within a Transaction.
  1067. *
  1068. * @action: QAPI-defined enum identifying which Action to perform.
  1069. * @ops: Table of ActionOps this Action can perform.
  1070. * @block_job_txn: Transaction which this action belongs to.
  1071. * @entry: List membership for all Actions in this Transaction.
  1072. *
  1073. * This structure must be arranged as first member in a subclassed type,
  1074. * assuming that the compiler will also arrange it to the same offsets as the
  1075. * base class.
  1076. */
  1077. struct BlkActionState {
  1078. TransactionAction *action;
  1079. const BlkActionOps *ops;
  1080. JobTxn *block_job_txn;
  1081. TransactionProperties *txn_props;
  1082. QTAILQ_ENTRY(BlkActionState) entry;
  1083. };
  1084. /* internal snapshot private data */
  1085. typedef struct InternalSnapshotState {
  1086. BlkActionState common;
  1087. BlockDriverState *bs;
  1088. QEMUSnapshotInfo sn;
  1089. bool created;
  1090. } InternalSnapshotState;
  1091. static int action_check_completion_mode(BlkActionState *s, Error **errp)
  1092. {
  1093. if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
  1094. error_setg(errp,
  1095. "Action '%s' does not support Transaction property "
  1096. "completion-mode = %s",
  1097. TransactionActionKind_str(s->action->type),
  1098. ActionCompletionMode_str(s->txn_props->completion_mode));
  1099. return -1;
  1100. }
  1101. return 0;
  1102. }
  1103. static void internal_snapshot_prepare(BlkActionState *common,
  1104. Error **errp)
  1105. {
  1106. Error *local_err = NULL;
  1107. const char *device;
  1108. const char *name;
  1109. BlockDriverState *bs;
  1110. QEMUSnapshotInfo old_sn, *sn;
  1111. bool ret;
  1112. qemu_timeval tv;
  1113. BlockdevSnapshotInternal *internal;
  1114. InternalSnapshotState *state;
  1115. AioContext *aio_context;
  1116. int ret1;
  1117. g_assert(common->action->type ==
  1118. TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
  1119. internal = common->action->u.blockdev_snapshot_internal_sync.data;
  1120. state = DO_UPCAST(InternalSnapshotState, common, common);
  1121. /* 1. parse input */
  1122. device = internal->device;
  1123. name = internal->name;
  1124. /* 2. check for validation */
  1125. if (action_check_completion_mode(common, errp) < 0) {
  1126. return;
  1127. }
  1128. bs = qmp_get_root_bs(device, errp);
  1129. if (!bs) {
  1130. return;
  1131. }
  1132. aio_context = bdrv_get_aio_context(bs);
  1133. aio_context_acquire(aio_context);
  1134. state->bs = bs;
  1135. /* Paired with .clean() */
  1136. bdrv_drained_begin(bs);
  1137. if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) {
  1138. goto out;
  1139. }
  1140. if (bdrv_is_read_only(bs)) {
  1141. error_setg(errp, "Device '%s' is read only", device);
  1142. goto out;
  1143. }
  1144. if (!bdrv_can_snapshot(bs)) {
  1145. error_setg(errp, "Block format '%s' used by device '%s' "
  1146. "does not support internal snapshots",
  1147. bs->drv->format_name, device);
  1148. goto out;
  1149. }
  1150. if (!strlen(name)) {
  1151. error_setg(errp, "Name is empty");
  1152. goto out;
  1153. }
  1154. /* check whether a snapshot with name exist */
  1155. ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
  1156. &local_err);
  1157. if (local_err) {
  1158. error_propagate(errp, local_err);
  1159. goto out;
  1160. } else if (ret) {
  1161. error_setg(errp,
  1162. "Snapshot with name '%s' already exists on device '%s'",
  1163. name, device);
  1164. goto out;
  1165. }
  1166. /* 3. take the snapshot */
  1167. sn = &state->sn;
  1168. pstrcpy(sn->name, sizeof(sn->name), name);
  1169. qemu_gettimeofday(&tv);
  1170. sn->date_sec = tv.tv_sec;
  1171. sn->date_nsec = tv.tv_usec * 1000;
  1172. sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  1173. ret1 = bdrv_snapshot_create(bs, sn);
  1174. if (ret1 < 0) {
  1175. error_setg_errno(errp, -ret1,
  1176. "Failed to create snapshot '%s' on device '%s'",
  1177. name, device);
  1178. goto out;
  1179. }
  1180. /* 4. succeed, mark a snapshot is created */
  1181. state->created = true;
  1182. out:
  1183. aio_context_release(aio_context);
  1184. }
  1185. static void internal_snapshot_abort(BlkActionState *common)
  1186. {
  1187. InternalSnapshotState *state =
  1188. DO_UPCAST(InternalSnapshotState, common, common);
  1189. BlockDriverState *bs = state->bs;
  1190. QEMUSnapshotInfo *sn = &state->sn;
  1191. AioContext *aio_context;
  1192. Error *local_error = NULL;
  1193. if (!state->created) {
  1194. return;
  1195. }
  1196. aio_context = bdrv_get_aio_context(state->bs);
  1197. aio_context_acquire(aio_context);
  1198. if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
  1199. error_reportf_err(local_error,
  1200. "Failed to delete snapshot with id '%s' and "
  1201. "name '%s' on device '%s' in abort: ",
  1202. sn->id_str, sn->name,
  1203. bdrv_get_device_name(bs));
  1204. }
  1205. aio_context_release(aio_context);
  1206. }
  1207. static void internal_snapshot_clean(BlkActionState *common)
  1208. {
  1209. InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState,
  1210. common, common);
  1211. AioContext *aio_context;
  1212. if (!state->bs) {
  1213. return;
  1214. }
  1215. aio_context = bdrv_get_aio_context(state->bs);
  1216. aio_context_acquire(aio_context);
  1217. bdrv_drained_end(state->bs);
  1218. aio_context_release(aio_context);
  1219. }
  1220. /* external snapshot private data */
  1221. typedef struct ExternalSnapshotState {
  1222. BlkActionState common;
  1223. BlockDriverState *old_bs;
  1224. BlockDriverState *new_bs;
  1225. bool overlay_appended;
  1226. } ExternalSnapshotState;
  1227. static void external_snapshot_prepare(BlkActionState *common,
  1228. Error **errp)
  1229. {
  1230. int flags = 0;
  1231. QDict *options = NULL;
  1232. Error *local_err = NULL;
  1233. /* Device and node name of the image to generate the snapshot from */
  1234. const char *device;
  1235. const char *node_name;
  1236. /* Reference to the new image (for 'blockdev-snapshot') */
  1237. const char *snapshot_ref;
  1238. /* File name of the new image (for 'blockdev-snapshot-sync') */
  1239. const char *new_image_file;
  1240. ExternalSnapshotState *state =
  1241. DO_UPCAST(ExternalSnapshotState, common, common);
  1242. TransactionAction *action = common->action;
  1243. AioContext *aio_context;
  1244. uint64_t perm, shared;
  1245. /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
  1246. * purpose but a different set of parameters */
  1247. switch (action->type) {
  1248. case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT:
  1249. {
  1250. BlockdevSnapshot *s = action->u.blockdev_snapshot.data;
  1251. device = s->node;
  1252. node_name = s->node;
  1253. new_image_file = NULL;
  1254. snapshot_ref = s->overlay;
  1255. }
  1256. break;
  1257. case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
  1258. {
  1259. BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
  1260. device = s->has_device ? s->device : NULL;
  1261. node_name = s->has_node_name ? s->node_name : NULL;
  1262. new_image_file = s->snapshot_file;
  1263. snapshot_ref = NULL;
  1264. }
  1265. break;
  1266. default:
  1267. g_assert_not_reached();
  1268. }
  1269. /* start processing */
  1270. if (action_check_completion_mode(common, errp) < 0) {
  1271. return;
  1272. }
  1273. state->old_bs = bdrv_lookup_bs(device, node_name, errp);
  1274. if (!state->old_bs) {
  1275. return;
  1276. }
  1277. aio_context = bdrv_get_aio_context(state->old_bs);
  1278. aio_context_acquire(aio_context);
  1279. /* Paired with .clean() */
  1280. bdrv_drained_begin(state->old_bs);
  1281. if (!bdrv_is_inserted(state->old_bs)) {
  1282. error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
  1283. goto out;
  1284. }
  1285. if (bdrv_op_is_blocked(state->old_bs,
  1286. BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
  1287. goto out;
  1288. }
  1289. if (!bdrv_is_read_only(state->old_bs)) {
  1290. if (bdrv_flush(state->old_bs)) {
  1291. error_setg(errp, QERR_IO_ERROR);
  1292. goto out;
  1293. }
  1294. }
  1295. if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) {
  1296. BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data;
  1297. const char *format = s->has_format ? s->format : "qcow2";
  1298. enum NewImageMode mode;
  1299. const char *snapshot_node_name =
  1300. s->has_snapshot_node_name ? s->snapshot_node_name : NULL;
  1301. if (node_name && !snapshot_node_name) {
  1302. error_setg(errp, "New overlay node name missing");
  1303. goto out;
  1304. }
  1305. if (snapshot_node_name &&
  1306. bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) {
  1307. error_setg(errp, "New overlay node name already in use");
  1308. goto out;
  1309. }
  1310. flags = state->old_bs->open_flags;
  1311. flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_COPY_ON_READ);
  1312. flags |= BDRV_O_NO_BACKING;
  1313. /* create new image w/backing file */
  1314. mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
  1315. if (mode != NEW_IMAGE_MODE_EXISTING) {
  1316. int64_t size = bdrv_getlength(state->old_bs);
  1317. if (size < 0) {
  1318. error_setg_errno(errp, -size, "bdrv_getlength failed");
  1319. goto out;
  1320. }
  1321. bdrv_refresh_filename(state->old_bs);
  1322. bdrv_img_create(new_image_file, format,
  1323. state->old_bs->filename,
  1324. state->old_bs->drv->format_name,
  1325. NULL, size, flags, false, &local_err);
  1326. if (local_err) {
  1327. error_propagate(errp, local_err);
  1328. goto out;
  1329. }
  1330. }
  1331. options = qdict_new();
  1332. if (snapshot_node_name) {
  1333. qdict_put_str(options, "node-name", snapshot_node_name);
  1334. }
  1335. qdict_put_str(options, "driver", format);
  1336. }
  1337. state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags,
  1338. errp);
  1339. /* We will manually add the backing_hd field to the bs later */
  1340. if (!state->new_bs) {
  1341. goto out;
  1342. }
  1343. /*
  1344. * Allow attaching a backing file to an overlay that's already in use only
  1345. * if the parents don't assume that they are already seeing a valid image.
  1346. * (Specifically, allow it as a mirror target, which is write-only access.)
  1347. */
  1348. bdrv_get_cumulative_perm(state->new_bs, &perm, &shared);
  1349. if (perm & BLK_PERM_CONSISTENT_READ) {
  1350. error_setg(errp, "The overlay is already in use");
  1351. goto out;
  1352. }
  1353. if (state->new_bs->backing != NULL) {
  1354. error_setg(errp, "The overlay already has a backing image");
  1355. goto out;
  1356. }
  1357. if (!state->new_bs->drv->supports_backing) {
  1358. error_setg(errp, "The overlay does not support backing images");
  1359. goto out;
  1360. }
  1361. /* This removes our old bs and adds the new bs. This is an operation that
  1362. * can fail, so we need to do it in .prepare; undoing it for abort is
  1363. * always possible. */
  1364. bdrv_ref(state->new_bs);
  1365. bdrv_append(state->new_bs, state->old_bs, &local_err);
  1366. if (local_err) {
  1367. error_propagate(errp, local_err);
  1368. goto out;
  1369. }
  1370. state->overlay_appended = true;
  1371. out:
  1372. aio_context_release(aio_context);
  1373. }
  1374. static void external_snapshot_commit(BlkActionState *common)
  1375. {
  1376. ExternalSnapshotState *state =
  1377. DO_UPCAST(ExternalSnapshotState, common, common);
  1378. AioContext *aio_context;
  1379. aio_context = bdrv_get_aio_context(state->old_bs);
  1380. aio_context_acquire(aio_context);
  1381. /* We don't need (or want) to use the transactional
  1382. * bdrv_reopen_multiple() across all the entries at once, because we
  1383. * don't want to abort all of them if one of them fails the reopen */
  1384. if (!atomic_read(&state->old_bs->copy_on_read)) {
  1385. bdrv_reopen_set_read_only(state->old_bs, true, NULL);
  1386. }
  1387. aio_context_release(aio_context);
  1388. }
  1389. static void external_snapshot_abort(BlkActionState *common)
  1390. {
  1391. ExternalSnapshotState *state =
  1392. DO_UPCAST(ExternalSnapshotState, common, common);
  1393. if (state->new_bs) {
  1394. if (state->overlay_appended) {
  1395. AioContext *aio_context;
  1396. AioContext *tmp_context;
  1397. int ret;
  1398. aio_context = bdrv_get_aio_context(state->old_bs);
  1399. aio_context_acquire(aio_context);
  1400. bdrv_ref(state->old_bs); /* we can't let bdrv_set_backind_hd()
  1401. close state->old_bs; we need it */
  1402. bdrv_set_backing_hd(state->new_bs, NULL, &error_abort);
  1403. /*
  1404. * The call to bdrv_set_backing_hd() above returns state->old_bs to
  1405. * the main AioContext. As we're still going to be using it, return
  1406. * it to the AioContext it was before.
  1407. */
  1408. tmp_context = bdrv_get_aio_context(state->old_bs);
  1409. if (aio_context != tmp_context) {
  1410. aio_context_release(aio_context);
  1411. aio_context_acquire(tmp_context);
  1412. ret = bdrv_try_set_aio_context(state->old_bs,
  1413. aio_context, NULL);
  1414. assert(ret == 0);
  1415. aio_context_release(tmp_context);
  1416. aio_context_acquire(aio_context);
  1417. }
  1418. bdrv_replace_node(state->new_bs, state->old_bs, &error_abort);
  1419. bdrv_unref(state->old_bs); /* bdrv_replace_node() ref'ed old_bs */
  1420. aio_context_release(aio_context);
  1421. }
  1422. }
  1423. }
  1424. static void external_snapshot_clean(BlkActionState *common)
  1425. {
  1426. ExternalSnapshotState *state =
  1427. DO_UPCAST(ExternalSnapshotState, common, common);
  1428. AioContext *aio_context;
  1429. if (!state->old_bs) {
  1430. return;
  1431. }
  1432. aio_context = bdrv_get_aio_context(state->old_bs);
  1433. aio_context_acquire(aio_context);
  1434. bdrv_drained_end(state->old_bs);
  1435. bdrv_unref(state->new_bs);
  1436. aio_context_release(aio_context);
  1437. }
  1438. typedef struct DriveBackupState {
  1439. BlkActionState common;
  1440. BlockDriverState *bs;
  1441. BlockJob *job;
  1442. } DriveBackupState;
  1443. static BlockJob *do_backup_common(BackupCommon *backup,
  1444. BlockDriverState *bs,
  1445. BlockDriverState *target_bs,
  1446. AioContext *aio_context,
  1447. JobTxn *txn, Error **errp);
  1448. static void drive_backup_prepare(BlkActionState *common, Error **errp)
  1449. {
  1450. DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
  1451. DriveBackup *backup;
  1452. BlockDriverState *bs;
  1453. BlockDriverState *target_bs;
  1454. BlockDriverState *source = NULL;
  1455. AioContext *aio_context;
  1456. AioContext *old_context;
  1457. QDict *options;
  1458. Error *local_err = NULL;
  1459. int flags;
  1460. int64_t size;
  1461. bool set_backing_hd = false;
  1462. int ret;
  1463. assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
  1464. backup = common->action->u.drive_backup.data;
  1465. if (!backup->has_mode) {
  1466. backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
  1467. }
  1468. bs = bdrv_lookup_bs(backup->device, backup->device, errp);
  1469. if (!bs) {
  1470. return;
  1471. }
  1472. if (!bs->drv) {
  1473. error_setg(errp, "Device has no medium");
  1474. return;
  1475. }
  1476. aio_context = bdrv_get_aio_context(bs);
  1477. aio_context_acquire(aio_context);
  1478. /* Paired with .clean() */
  1479. bdrv_drained_begin(bs);
  1480. if (!backup->has_format) {
  1481. backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ?
  1482. NULL : (char *) bs->drv->format_name;
  1483. }
  1484. /* Early check to avoid creating target */
  1485. if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
  1486. goto out;
  1487. }
  1488. flags = bs->open_flags | BDRV_O_RDWR;
  1489. /*
  1490. * See if we have a backing HD we can use to create our new image
  1491. * on top of.
  1492. */
  1493. if (backup->sync == MIRROR_SYNC_MODE_TOP) {
  1494. source = backing_bs(bs);
  1495. if (!source) {
  1496. backup->sync = MIRROR_SYNC_MODE_FULL;
  1497. }
  1498. }
  1499. if (backup->sync == MIRROR_SYNC_MODE_NONE) {
  1500. source = bs;
  1501. flags |= BDRV_O_NO_BACKING;
  1502. set_backing_hd = true;
  1503. }
  1504. size = bdrv_getlength(bs);
  1505. if (size < 0) {
  1506. error_setg_errno(errp, -size, "bdrv_getlength failed");
  1507. goto out;
  1508. }
  1509. if (backup->mode != NEW_IMAGE_MODE_EXISTING) {
  1510. assert(backup->format);
  1511. if (source) {
  1512. bdrv_refresh_filename(source);
  1513. bdrv_img_create(backup->target, backup->format, source->filename,
  1514. source->drv->format_name, NULL,
  1515. size, flags, false, &local_err);
  1516. } else {
  1517. bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL,
  1518. size, flags, false, &local_err);
  1519. }
  1520. }
  1521. if (local_err) {
  1522. error_propagate(errp, local_err);
  1523. goto out;
  1524. }
  1525. options = qdict_new();
  1526. qdict_put_str(options, "discard", "unmap");
  1527. qdict_put_str(options, "detect-zeroes", "unmap");
  1528. if (backup->format) {
  1529. qdict_put_str(options, "driver", backup->format);
  1530. }
  1531. target_bs = bdrv_open(backup->target, NULL, options, flags, errp);
  1532. if (!target_bs) {
  1533. goto out;
  1534. }
  1535. /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
  1536. old_context = bdrv_get_aio_context(target_bs);
  1537. aio_context_release(aio_context);
  1538. aio_context_acquire(old_context);
  1539. ret = bdrv_try_set_aio_context(target_bs, aio_context, errp);
  1540. if (ret < 0) {
  1541. bdrv_unref(target_bs);
  1542. aio_context_release(old_context);
  1543. return;
  1544. }
  1545. aio_context_release(old_context);
  1546. aio_context_acquire(aio_context);
  1547. if (set_backing_hd) {
  1548. bdrv_set_backing_hd(target_bs, source, &local_err);
  1549. if (local_err) {
  1550. goto unref;
  1551. }
  1552. }
  1553. state->bs = bs;
  1554. state->job = do_backup_common(qapi_DriveBackup_base(backup),
  1555. bs, target_bs, aio_context,
  1556. common->block_job_txn, errp);
  1557. unref:
  1558. bdrv_unref(target_bs);
  1559. out:
  1560. aio_context_release(aio_context);
  1561. }
  1562. static void drive_backup_commit(BlkActionState *common)
  1563. {
  1564. DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
  1565. AioContext *aio_context;
  1566. aio_context = bdrv_get_aio_context(state->bs);
  1567. aio_context_acquire(aio_context);
  1568. assert(state->job);
  1569. job_start(&state->job->job);
  1570. aio_context_release(aio_context);
  1571. }
  1572. static void drive_backup_abort(BlkActionState *common)
  1573. {
  1574. DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
  1575. if (state->job) {
  1576. AioContext *aio_context;
  1577. aio_context = bdrv_get_aio_context(state->bs);
  1578. aio_context_acquire(aio_context);
  1579. job_cancel_sync(&state->job->job);
  1580. aio_context_release(aio_context);
  1581. }
  1582. }
  1583. static void drive_backup_clean(BlkActionState *common)
  1584. {
  1585. DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
  1586. AioContext *aio_context;
  1587. if (!state->bs) {
  1588. return;
  1589. }
  1590. aio_context = bdrv_get_aio_context(state->bs);
  1591. aio_context_acquire(aio_context);
  1592. bdrv_drained_end(state->bs);
  1593. aio_context_release(aio_context);
  1594. }
  1595. typedef struct BlockdevBackupState {
  1596. BlkActionState common;
  1597. BlockDriverState *bs;
  1598. BlockJob *job;
  1599. } BlockdevBackupState;
  1600. static void blockdev_backup_prepare(BlkActionState *common, Error **errp)
  1601. {
  1602. BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
  1603. BlockdevBackup *backup;
  1604. BlockDriverState *bs;
  1605. BlockDriverState *target_bs;
  1606. AioContext *aio_context;
  1607. AioContext *old_context;
  1608. int ret;
  1609. assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP);
  1610. backup = common->action->u.blockdev_backup.data;
  1611. bs = bdrv_lookup_bs(backup->device, backup->device, errp);
  1612. if (!bs) {
  1613. return;
  1614. }
  1615. target_bs = bdrv_lookup_bs(backup->target, backup->target, errp);
  1616. if (!target_bs) {
  1617. return;
  1618. }
  1619. /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
  1620. aio_context = bdrv_get_aio_context(bs);
  1621. old_context = bdrv_get_aio_context(target_bs);
  1622. aio_context_acquire(old_context);
  1623. ret = bdrv_try_set_aio_context(target_bs, aio_context, errp);
  1624. if (ret < 0) {
  1625. aio_context_release(old_context);
  1626. return;
  1627. }
  1628. aio_context_release(old_context);
  1629. aio_context_acquire(aio_context);
  1630. state->bs = bs;
  1631. /* Paired with .clean() */
  1632. bdrv_drained_begin(state->bs);
  1633. state->job = do_backup_common(qapi_BlockdevBackup_base(backup),
  1634. bs, target_bs, aio_context,
  1635. common->block_job_txn, errp);
  1636. aio_context_release(aio_context);
  1637. }
  1638. static void blockdev_backup_commit(BlkActionState *common)
  1639. {
  1640. BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
  1641. AioContext *aio_context;
  1642. aio_context = bdrv_get_aio_context(state->bs);
  1643. aio_context_acquire(aio_context);
  1644. assert(state->job);
  1645. job_start(&state->job->job);
  1646. aio_context_release(aio_context);
  1647. }
  1648. static void blockdev_backup_abort(BlkActionState *common)
  1649. {
  1650. BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
  1651. if (state->job) {
  1652. AioContext *aio_context;
  1653. aio_context = bdrv_get_aio_context(state->bs);
  1654. aio_context_acquire(aio_context);
  1655. job_cancel_sync(&state->job->job);
  1656. aio_context_release(aio_context);
  1657. }
  1658. }
  1659. static void blockdev_backup_clean(BlkActionState *common)
  1660. {
  1661. BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common);
  1662. AioContext *aio_context;
  1663. if (!state->bs) {
  1664. return;
  1665. }
  1666. aio_context = bdrv_get_aio_context(state->bs);
  1667. aio_context_acquire(aio_context);
  1668. bdrv_drained_end(state->bs);
  1669. aio_context_release(aio_context);
  1670. }
  1671. typedef struct BlockDirtyBitmapState {
  1672. BlkActionState common;
  1673. BdrvDirtyBitmap *bitmap;
  1674. BlockDriverState *bs;
  1675. HBitmap *backup;
  1676. bool prepared;
  1677. bool was_enabled;
  1678. } BlockDirtyBitmapState;
  1679. static void block_dirty_bitmap_add_prepare(BlkActionState *common,
  1680. Error **errp)
  1681. {
  1682. Error *local_err = NULL;
  1683. BlockDirtyBitmapAdd *action;
  1684. BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
  1685. common, common);
  1686. if (action_check_completion_mode(common, errp) < 0) {
  1687. return;
  1688. }
  1689. action = common->action->u.block_dirty_bitmap_add.data;
  1690. /* AIO context taken and released within qmp_block_dirty_bitmap_add */
  1691. qmp_block_dirty_bitmap_add(action->node, action->name,
  1692. action->has_granularity, action->granularity,
  1693. action->has_persistent, action->persistent,
  1694. action->has_disabled, action->disabled,
  1695. &local_err);
  1696. if (!local_err) {
  1697. state->prepared = true;
  1698. } else {
  1699. error_propagate(errp, local_err);
  1700. }
  1701. }
  1702. static void block_dirty_bitmap_add_abort(BlkActionState *common)
  1703. {
  1704. BlockDirtyBitmapAdd *action;
  1705. BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
  1706. common, common);
  1707. action = common->action->u.block_dirty_bitmap_add.data;
  1708. /* Should not be able to fail: IF the bitmap was added via .prepare(),
  1709. * then the node reference and bitmap name must have been valid.
  1710. */
  1711. if (state->prepared) {
  1712. qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort);
  1713. }
  1714. }
  1715. static void block_dirty_bitmap_clear_prepare(BlkActionState *common,
  1716. Error **errp)
  1717. {
  1718. BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
  1719. common, common);
  1720. BlockDirtyBitmap *action;
  1721. if (action_check_completion_mode(common, errp) < 0) {
  1722. return;
  1723. }
  1724. action = common->action->u.block_dirty_bitmap_clear.data;
  1725. state->bitmap = block_dirty_bitmap_lookup(action->node,
  1726. action->name,
  1727. &state->bs,
  1728. errp);
  1729. if (!state->bitmap) {
  1730. return;
  1731. }
  1732. if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_DEFAULT, errp)) {
  1733. return;
  1734. }
  1735. bdrv_clear_dirty_bitmap(state->bitmap, &state->backup);
  1736. }
  1737. static void block_dirty_bitmap_restore(BlkActionState *common)
  1738. {
  1739. BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
  1740. common, common);
  1741. if (state->backup) {
  1742. bdrv_restore_dirty_bitmap(state->bitmap, state->backup);
  1743. }
  1744. }
  1745. static void block_dirty_bitmap_free_backup(BlkActionState *common)
  1746. {
  1747. BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
  1748. common, common);
  1749. hbitmap_free(state->backup);
  1750. }
  1751. static void block_dirty_bitmap_enable_prepare(BlkActionState *common,
  1752. Error **errp)
  1753. {
  1754. BlockDirtyBitmap *action;
  1755. BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
  1756. common, common);
  1757. if (action_check_completion_mode(common, errp) < 0) {
  1758. return;
  1759. }
  1760. action = common->action->u.block_dirty_bitmap_enable.data;
  1761. state->bitmap = block_dirty_bitmap_lookup(action->node,
  1762. action->name,
  1763. NULL,
  1764. errp);
  1765. if (!state->bitmap) {
  1766. return;
  1767. }
  1768. if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
  1769. return;
  1770. }
  1771. state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap);
  1772. bdrv_enable_dirty_bitmap(state->bitmap);
  1773. }
  1774. static void block_dirty_bitmap_enable_abort(BlkActionState *common)
  1775. {
  1776. BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
  1777. common, common);
  1778. if (!state->was_enabled) {
  1779. bdrv_disable_dirty_bitmap(state->bitmap);
  1780. }
  1781. }
  1782. static void block_dirty_bitmap_disable_prepare(BlkActionState *common,
  1783. Error **errp)
  1784. {
  1785. BlockDirtyBitmap *action;
  1786. BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
  1787. common, common);
  1788. if (action_check_completion_mode(common, errp) < 0) {
  1789. return;
  1790. }
  1791. action = common->action->u.block_dirty_bitmap_disable.data;
  1792. state->bitmap = block_dirty_bitmap_lookup(action->node,
  1793. action->name,
  1794. NULL,
  1795. errp);
  1796. if (!state->bitmap) {
  1797. return;
  1798. }
  1799. if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_ALLOW_RO, errp)) {
  1800. return;
  1801. }
  1802. state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap);
  1803. bdrv_disable_dirty_bitmap(state->bitmap);
  1804. }
  1805. static void block_dirty_bitmap_disable_abort(BlkActionState *common)
  1806. {
  1807. BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
  1808. common, common);
  1809. if (state->was_enabled) {
  1810. bdrv_enable_dirty_bitmap(state->bitmap);
  1811. }
  1812. }
  1813. static void block_dirty_bitmap_merge_prepare(BlkActionState *common,
  1814. Error **errp)
  1815. {
  1816. BlockDirtyBitmapMerge *action;
  1817. BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
  1818. common, common);
  1819. if (action_check_completion_mode(common, errp) < 0) {
  1820. return;
  1821. }
  1822. action = common->action->u.block_dirty_bitmap_merge.data;
  1823. state->bitmap = block_dirty_bitmap_merge(action->node, action->target,
  1824. action->bitmaps, &state->backup,
  1825. errp);
  1826. }
  1827. static void block_dirty_bitmap_remove_prepare(BlkActionState *common,
  1828. Error **errp)
  1829. {
  1830. BlockDirtyBitmap *action;
  1831. BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
  1832. common, common);
  1833. if (action_check_completion_mode(common, errp) < 0) {
  1834. return;
  1835. }
  1836. action = common->action->u.block_dirty_bitmap_remove.data;
  1837. state->bitmap = block_dirty_bitmap_remove(action->node, action->name,
  1838. false, &state->bs, errp);
  1839. if (state->bitmap) {
  1840. bdrv_dirty_bitmap_skip_store(state->bitmap, true);
  1841. bdrv_dirty_bitmap_set_busy(state->bitmap, true);
  1842. }
  1843. }
  1844. static void block_dirty_bitmap_remove_abort(BlkActionState *common)
  1845. {
  1846. BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
  1847. common, common);
  1848. if (state->bitmap) {
  1849. bdrv_dirty_bitmap_skip_store(state->bitmap, false);
  1850. bdrv_dirty_bitmap_set_busy(state->bitmap, false);
  1851. }
  1852. }
  1853. static void block_dirty_bitmap_remove_commit(BlkActionState *common)
  1854. {
  1855. BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState,
  1856. common, common);
  1857. bdrv_dirty_bitmap_set_busy(state->bitmap, false);
  1858. bdrv_release_dirty_bitmap(state->bitmap);
  1859. }
  1860. static void abort_prepare(BlkActionState *common, Error **errp)
  1861. {
  1862. error_setg(errp, "Transaction aborted using Abort action");
  1863. }
  1864. static void abort_commit(BlkActionState *common)
  1865. {
  1866. g_assert_not_reached(); /* this action never succeeds */
  1867. }
  1868. static const BlkActionOps actions[] = {
  1869. [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = {
  1870. .instance_size = sizeof(ExternalSnapshotState),
  1871. .prepare = external_snapshot_prepare,
  1872. .commit = external_snapshot_commit,
  1873. .abort = external_snapshot_abort,
  1874. .clean = external_snapshot_clean,
  1875. },
  1876. [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
  1877. .instance_size = sizeof(ExternalSnapshotState),
  1878. .prepare = external_snapshot_prepare,
  1879. .commit = external_snapshot_commit,
  1880. .abort = external_snapshot_abort,
  1881. .clean = external_snapshot_clean,
  1882. },
  1883. [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
  1884. .instance_size = sizeof(DriveBackupState),
  1885. .prepare = drive_backup_prepare,
  1886. .commit = drive_backup_commit,
  1887. .abort = drive_backup_abort,
  1888. .clean = drive_backup_clean,
  1889. },
  1890. [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = {
  1891. .instance_size = sizeof(BlockdevBackupState),
  1892. .prepare = blockdev_backup_prepare,
  1893. .commit = blockdev_backup_commit,
  1894. .abort = blockdev_backup_abort,
  1895. .clean = blockdev_backup_clean,
  1896. },
  1897. [TRANSACTION_ACTION_KIND_ABORT] = {
  1898. .instance_size = sizeof(BlkActionState),
  1899. .prepare = abort_prepare,
  1900. .commit = abort_commit,
  1901. },
  1902. [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
  1903. .instance_size = sizeof(InternalSnapshotState),
  1904. .prepare = internal_snapshot_prepare,
  1905. .abort = internal_snapshot_abort,
  1906. .clean = internal_snapshot_clean,
  1907. },
  1908. [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = {
  1909. .instance_size = sizeof(BlockDirtyBitmapState),
  1910. .prepare = block_dirty_bitmap_add_prepare,
  1911. .abort = block_dirty_bitmap_add_abort,
  1912. },
  1913. [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = {
  1914. .instance_size = sizeof(BlockDirtyBitmapState),
  1915. .prepare = block_dirty_bitmap_clear_prepare,
  1916. .commit = block_dirty_bitmap_free_backup,
  1917. .abort = block_dirty_bitmap_restore,
  1918. },
  1919. [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ENABLE] = {
  1920. .instance_size = sizeof(BlockDirtyBitmapState),
  1921. .prepare = block_dirty_bitmap_enable_prepare,
  1922. .abort = block_dirty_bitmap_enable_abort,
  1923. },
  1924. [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_DISABLE] = {
  1925. .instance_size = sizeof(BlockDirtyBitmapState),
  1926. .prepare = block_dirty_bitmap_disable_prepare,
  1927. .abort = block_dirty_bitmap_disable_abort,
  1928. },
  1929. [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_MERGE] = {
  1930. .instance_size = sizeof(BlockDirtyBitmapState),
  1931. .prepare = block_dirty_bitmap_merge_prepare,
  1932. .commit = block_dirty_bitmap_free_backup,
  1933. .abort = block_dirty_bitmap_restore,
  1934. },
  1935. [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_REMOVE] = {
  1936. .instance_size = sizeof(BlockDirtyBitmapState),
  1937. .prepare = block_dirty_bitmap_remove_prepare,
  1938. .commit = block_dirty_bitmap_remove_commit,
  1939. .abort = block_dirty_bitmap_remove_abort,
  1940. },
  1941. /* Where are transactions for MIRROR, COMMIT and STREAM?
  1942. * Although these blockjobs use transaction callbacks like the backup job,
  1943. * these jobs do not necessarily adhere to transaction semantics.
  1944. * These jobs may not fully undo all of their actions on abort, nor do they
  1945. * necessarily work in transactions with more than one job in them.
  1946. */
  1947. };
  1948. /**
  1949. * Allocate a TransactionProperties structure if necessary, and fill
  1950. * that structure with desired defaults if they are unset.
  1951. */
  1952. static TransactionProperties *get_transaction_properties(
  1953. TransactionProperties *props)
  1954. {
  1955. if (!props) {
  1956. props = g_new0(TransactionProperties, 1);
  1957. }
  1958. if (!props->has_completion_mode) {
  1959. props->has_completion_mode = true;
  1960. props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL;
  1961. }
  1962. return props;
  1963. }
  1964. /*
  1965. * 'Atomic' group operations. The operations are performed as a set, and if
  1966. * any fail then we roll back all operations in the group.
  1967. */
  1968. void qmp_transaction(TransactionActionList *dev_list,
  1969. bool has_props,
  1970. struct TransactionProperties *props,
  1971. Error **errp)
  1972. {
  1973. TransactionActionList *dev_entry = dev_list;
  1974. JobTxn *block_job_txn = NULL;
  1975. BlkActionState *state, *next;
  1976. Error *local_err = NULL;
  1977. QTAILQ_HEAD(, BlkActionState) snap_bdrv_states;
  1978. QTAILQ_INIT(&snap_bdrv_states);
  1979. /* Does this transaction get canceled as a group on failure?
  1980. * If not, we don't really need to make a JobTxn.
  1981. */
  1982. props = get_transaction_properties(props);
  1983. if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) {
  1984. block_job_txn = job_txn_new();
  1985. }
  1986. /* drain all i/o before any operations */
  1987. bdrv_drain_all();
  1988. /* We don't do anything in this loop that commits us to the operations */
  1989. while (NULL != dev_entry) {
  1990. TransactionAction *dev_info = NULL;
  1991. const BlkActionOps *ops;
  1992. dev_info = dev_entry->value;
  1993. dev_entry = dev_entry->next;
  1994. assert(dev_info->type < ARRAY_SIZE(actions));
  1995. ops = &actions[dev_info->type];
  1996. assert(ops->instance_size > 0);
  1997. state = g_malloc0(ops->instance_size);
  1998. state->ops = ops;
  1999. state->action = dev_info;
  2000. state->block_job_txn = block_job_txn;
  2001. state->txn_props = props;
  2002. QTAILQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
  2003. state->ops->prepare(state, &local_err);
  2004. if (local_err) {
  2005. error_propagate(errp, local_err);
  2006. goto delete_and_fail;
  2007. }
  2008. }
  2009. QTAILQ_FOREACH(state, &snap_bdrv_states, entry) {
  2010. if (state->ops->commit) {
  2011. state->ops->commit(state);
  2012. }
  2013. }
  2014. /* success */
  2015. goto exit;
  2016. delete_and_fail:
  2017. /* failure, and it is all-or-none; roll back all operations */
  2018. QTAILQ_FOREACH_REVERSE(state, &snap_bdrv_states, entry) {
  2019. if (state->ops->abort) {
  2020. state->ops->abort(state);
  2021. }
  2022. }
  2023. exit:
  2024. QTAILQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
  2025. if (state->ops->clean) {
  2026. state->ops->clean(state);
  2027. }
  2028. g_free(state);
  2029. }
  2030. if (!has_props) {
  2031. qapi_free_TransactionProperties(props);
  2032. }
  2033. job_txn_unref(block_job_txn);
  2034. }
  2035. void qmp_block_passwd(bool has_device, const char *device,
  2036. bool has_node_name, const char *node_name,
  2037. const char *password, Error **errp)
  2038. {
  2039. error_setg(errp,
  2040. "Setting block passwords directly is no longer supported");
  2041. }
  2042. BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bitmap_sha256(const char *node,
  2043. const char *name,
  2044. Error **errp)
  2045. {
  2046. BdrvDirtyBitmap *bitmap;
  2047. BlockDriverState *bs;
  2048. BlockDirtyBitmapSha256 *ret = NULL;
  2049. char *sha256;
  2050. bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp);
  2051. if (!bitmap || !bs) {
  2052. return NULL;
  2053. }
  2054. sha256 = bdrv_dirty_bitmap_sha256(bitmap, errp);
  2055. if (sha256 == NULL) {
  2056. return NULL;
  2057. }
  2058. ret = g_new(BlockDirtyBitmapSha256, 1);
  2059. ret->sha256 = sha256;
  2060. return ret;
  2061. }
  2062. void qmp_block_resize(bool has_device, const char *device,
  2063. bool has_node_name, const char *node_name,
  2064. int64_t size, Error **errp)
  2065. {
  2066. Error *local_err = NULL;
  2067. BlockBackend *blk = NULL;
  2068. BlockDriverState *bs;
  2069. AioContext *aio_context;
  2070. bs = bdrv_lookup_bs(has_device ? device : NULL,
  2071. has_node_name ? node_name : NULL,
  2072. &local_err);
  2073. if (local_err) {
  2074. error_propagate(errp, local_err);
  2075. return;
  2076. }
  2077. aio_context = bdrv_get_aio_context(bs);
  2078. aio_context_acquire(aio_context);
  2079. if (size < 0) {
  2080. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
  2081. goto out;
  2082. }
  2083. if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
  2084. error_setg(errp, QERR_DEVICE_IN_USE, device);
  2085. goto out;
  2086. }
  2087. blk = blk_new_with_bs(bs, BLK_PERM_RESIZE, BLK_PERM_ALL, errp);
  2088. if (!blk) {
  2089. goto out;
  2090. }
  2091. bdrv_drained_begin(bs);
  2092. blk_truncate(blk, size, false, PREALLOC_MODE_OFF, 0, errp);
  2093. bdrv_drained_end(bs);
  2094. out:
  2095. blk_unref(blk);
  2096. aio_context_release(aio_context);
  2097. }
  2098. void qmp_block_stream(bool has_job_id, const char *job_id, const char *device,
  2099. bool has_base, const char *base,
  2100. bool has_base_node, const char *base_node,
  2101. bool has_backing_file, const char *backing_file,
  2102. bool has_speed, int64_t speed,
  2103. bool has_on_error, BlockdevOnError on_error,
  2104. bool has_auto_finalize, bool auto_finalize,
  2105. bool has_auto_dismiss, bool auto_dismiss,
  2106. Error **errp)
  2107. {
  2108. BlockDriverState *bs, *iter;
  2109. BlockDriverState *base_bs = NULL;
  2110. AioContext *aio_context;
  2111. Error *local_err = NULL;
  2112. const char *base_name = NULL;
  2113. int job_flags = JOB_DEFAULT;
  2114. if (!has_on_error) {
  2115. on_error = BLOCKDEV_ON_ERROR_REPORT;
  2116. }
  2117. bs = bdrv_lookup_bs(device, device, errp);
  2118. if (!bs) {
  2119. return;
  2120. }
  2121. aio_context = bdrv_get_aio_context(bs);
  2122. aio_context_acquire(aio_context);
  2123. if (has_base && has_base_node) {
  2124. error_setg(errp, "'base' and 'base-node' cannot be specified "
  2125. "at the same time");
  2126. goto out;
  2127. }
  2128. if (has_base) {
  2129. base_bs = bdrv_find_backing_image(bs, base);
  2130. if (base_bs == NULL) {
  2131. error_setg(errp, QERR_BASE_NOT_FOUND, base);
  2132. goto out;
  2133. }
  2134. assert(bdrv_get_aio_context(base_bs) == aio_context);
  2135. base_name = base;
  2136. }
  2137. if (has_base_node) {
  2138. base_bs = bdrv_lookup_bs(NULL, base_node, errp);
  2139. if (!base_bs) {
  2140. goto out;
  2141. }
  2142. if (bs == base_bs || !bdrv_chain_contains(bs, base_bs)) {
  2143. error_setg(errp, "Node '%s' is not a backing image of '%s'",
  2144. base_node, device);
  2145. goto out;
  2146. }
  2147. assert(bdrv_get_aio_context(base_bs) == aio_context);
  2148. bdrv_refresh_filename(base_bs);
  2149. base_name = base_bs->filename;
  2150. }
  2151. /* Check for op blockers in the whole chain between bs and base */
  2152. for (iter = bs; iter && iter != base_bs; iter = backing_bs(iter)) {
  2153. if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_STREAM, errp)) {
  2154. goto out;
  2155. }
  2156. }
  2157. /* if we are streaming the entire chain, the result will have no backing
  2158. * file, and specifying one is therefore an error */
  2159. if (base_bs == NULL && has_backing_file) {
  2160. error_setg(errp, "backing file specified, but streaming the "
  2161. "entire chain");
  2162. goto out;
  2163. }
  2164. /* backing_file string overrides base bs filename */
  2165. base_name = has_backing_file ? backing_file : base_name;
  2166. if (has_auto_finalize && !auto_finalize) {
  2167. job_flags |= JOB_MANUAL_FINALIZE;
  2168. }
  2169. if (has_auto_dismiss && !auto_dismiss) {
  2170. job_flags |= JOB_MANUAL_DISMISS;
  2171. }
  2172. stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name,
  2173. job_flags, has_speed ? speed : 0, on_error, &local_err);
  2174. if (local_err) {
  2175. error_propagate(errp, local_err);
  2176. goto out;
  2177. }
  2178. trace_qmp_block_stream(bs);
  2179. out:
  2180. aio_context_release(aio_context);
  2181. }
  2182. void qmp_block_commit(bool has_job_id, const char *job_id, const char *device,
  2183. bool has_base_node, const char *base_node,
  2184. bool has_base, const char *base,
  2185. bool has_top_node, const char *top_node,
  2186. bool has_top, const char *top,
  2187. bool has_backing_file, const char *backing_file,
  2188. bool has_speed, int64_t speed,
  2189. bool has_on_error, BlockdevOnError on_error,
  2190. bool has_filter_node_name, const char *filter_node_name,
  2191. bool has_auto_finalize, bool auto_finalize,
  2192. bool has_auto_dismiss, bool auto_dismiss,
  2193. Error **errp)
  2194. {
  2195. BlockDriverState *bs;
  2196. BlockDriverState *iter;
  2197. BlockDriverState *base_bs, *top_bs;
  2198. AioContext *aio_context;
  2199. Error *local_err = NULL;
  2200. int job_flags = JOB_DEFAULT;
  2201. if (!has_speed) {
  2202. speed = 0;
  2203. }
  2204. if (!has_on_error) {
  2205. on_error = BLOCKDEV_ON_ERROR_REPORT;
  2206. }
  2207. if (!has_filter_node_name) {
  2208. filter_node_name = NULL;
  2209. }
  2210. if (has_auto_finalize && !auto_finalize) {
  2211. job_flags |= JOB_MANUAL_FINALIZE;
  2212. }
  2213. if (has_auto_dismiss && !auto_dismiss) {
  2214. job_flags |= JOB_MANUAL_DISMISS;
  2215. }
  2216. /* Important Note:
  2217. * libvirt relies on the DeviceNotFound error class in order to probe for
  2218. * live commit feature versions; for this to work, we must make sure to
  2219. * perform the device lookup before any generic errors that may occur in a
  2220. * scenario in which all optional arguments are omitted. */
  2221. bs = qmp_get_root_bs(device, &local_err);
  2222. if (!bs) {
  2223. bs = bdrv_lookup_bs(device, device, NULL);
  2224. if (!bs) {
  2225. error_free(local_err);
  2226. error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
  2227. "Device '%s' not found", device);
  2228. } else {
  2229. error_propagate(errp, local_err);
  2230. }
  2231. return;
  2232. }
  2233. aio_context = bdrv_get_aio_context(bs);
  2234. aio_context_acquire(aio_context);
  2235. if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) {
  2236. goto out;
  2237. }
  2238. /* default top_bs is the active layer */
  2239. top_bs = bs;
  2240. if (has_top_node && has_top) {
  2241. error_setg(errp, "'top-node' and 'top' are mutually exclusive");
  2242. goto out;
  2243. } else if (has_top_node) {
  2244. top_bs = bdrv_lookup_bs(NULL, top_node, errp);
  2245. if (top_bs == NULL) {
  2246. goto out;
  2247. }
  2248. if (!bdrv_chain_contains(bs, top_bs)) {
  2249. error_setg(errp, "'%s' is not in this backing file chain",
  2250. top_node);
  2251. goto out;
  2252. }
  2253. } else if (has_top && top) {
  2254. /* This strcmp() is just a shortcut, there is no need to
  2255. * refresh @bs's filename. If it mismatches,
  2256. * bdrv_find_backing_image() will do the refresh and may still
  2257. * return @bs. */
  2258. if (strcmp(bs->filename, top) != 0) {
  2259. top_bs = bdrv_find_backing_image(bs, top);
  2260. }
  2261. }
  2262. if (top_bs == NULL) {
  2263. error_setg(errp, "Top image file %s not found", top ? top : "NULL");
  2264. goto out;
  2265. }
  2266. assert(bdrv_get_aio_context(top_bs) == aio_context);
  2267. if (has_base_node && has_base) {
  2268. error_setg(errp, "'base-node' and 'base' are mutually exclusive");
  2269. goto out;
  2270. } else if (has_base_node) {
  2271. base_bs = bdrv_lookup_bs(NULL, base_node, errp);
  2272. if (base_bs == NULL) {
  2273. goto out;
  2274. }
  2275. if (!bdrv_chain_contains(top_bs, base_bs)) {
  2276. error_setg(errp, "'%s' is not in this backing file chain",
  2277. base_node);
  2278. goto out;
  2279. }
  2280. } else if (has_base && base) {
  2281. base_bs = bdrv_find_backing_image(top_bs, base);
  2282. } else {
  2283. base_bs = bdrv_find_base(top_bs);
  2284. }
  2285. if (base_bs == NULL) {
  2286. error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
  2287. goto out;
  2288. }
  2289. assert(bdrv_get_aio_context(base_bs) == aio_context);
  2290. for (iter = top_bs; iter != backing_bs(base_bs); iter = backing_bs(iter)) {
  2291. if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
  2292. goto out;
  2293. }
  2294. }
  2295. /* Do not allow attempts to commit an image into itself */
  2296. if (top_bs == base_bs) {
  2297. error_setg(errp, "cannot commit an image into itself");
  2298. goto out;
  2299. }
  2300. if (top_bs == bs) {
  2301. if (has_backing_file) {
  2302. error_setg(errp, "'backing-file' specified,"
  2303. " but 'top' is the active layer");
  2304. goto out;
  2305. }
  2306. commit_active_start(has_job_id ? job_id : NULL, bs, base_bs,
  2307. job_flags, speed, on_error,
  2308. filter_node_name, NULL, NULL, false, &local_err);
  2309. } else {
  2310. BlockDriverState *overlay_bs = bdrv_find_overlay(bs, top_bs);
  2311. if (bdrv_op_is_blocked(overlay_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) {
  2312. goto out;
  2313. }
  2314. commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, job_flags,
  2315. speed, on_error, has_backing_file ? backing_file : NULL,
  2316. filter_node_name, &local_err);
  2317. }
  2318. if (local_err != NULL) {
  2319. error_propagate(errp, local_err);
  2320. goto out;
  2321. }
  2322. out:
  2323. aio_context_release(aio_context);
  2324. }
  2325. /* Common QMP interface for drive-backup and blockdev-backup */
  2326. static BlockJob *do_backup_common(BackupCommon *backup,
  2327. BlockDriverState *bs,
  2328. BlockDriverState *target_bs,
  2329. AioContext *aio_context,
  2330. JobTxn *txn, Error **errp)
  2331. {
  2332. BlockJob *job = NULL;
  2333. BdrvDirtyBitmap *bmap = NULL;
  2334. int job_flags = JOB_DEFAULT;
  2335. if (!backup->has_speed) {
  2336. backup->speed = 0;
  2337. }
  2338. if (!backup->has_on_source_error) {
  2339. backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT;
  2340. }
  2341. if (!backup->has_on_target_error) {
  2342. backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT;
  2343. }
  2344. if (!backup->has_job_id) {
  2345. backup->job_id = NULL;
  2346. }
  2347. if (!backup->has_auto_finalize) {
  2348. backup->auto_finalize = true;
  2349. }
  2350. if (!backup->has_auto_dismiss) {
  2351. backup->auto_dismiss = true;
  2352. }
  2353. if (!backup->has_compress) {
  2354. backup->compress = false;
  2355. }
  2356. if ((backup->sync == MIRROR_SYNC_MODE_BITMAP) ||
  2357. (backup->sync == MIRROR_SYNC_MODE_INCREMENTAL)) {
  2358. /* done before desugaring 'incremental' to print the right message */
  2359. if (!backup->has_bitmap) {
  2360. error_setg(errp, "must provide a valid bitmap name for "
  2361. "'%s' sync mode", MirrorSyncMode_str(backup->sync));
  2362. return NULL;
  2363. }
  2364. }
  2365. if (backup->sync == MIRROR_SYNC_MODE_INCREMENTAL) {
  2366. if (backup->has_bitmap_mode &&
  2367. backup->bitmap_mode != BITMAP_SYNC_MODE_ON_SUCCESS) {
  2368. error_setg(errp, "Bitmap sync mode must be '%s' "
  2369. "when using sync mode '%s'",
  2370. BitmapSyncMode_str(BITMAP_SYNC_MODE_ON_SUCCESS),
  2371. MirrorSyncMode_str(backup->sync));
  2372. return NULL;
  2373. }
  2374. backup->has_bitmap_mode = true;
  2375. backup->sync = MIRROR_SYNC_MODE_BITMAP;
  2376. backup->bitmap_mode = BITMAP_SYNC_MODE_ON_SUCCESS;
  2377. }
  2378. if (backup->has_bitmap) {
  2379. bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap);
  2380. if (!bmap) {
  2381. error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap);
  2382. return NULL;
  2383. }
  2384. if (!backup->has_bitmap_mode) {
  2385. error_setg(errp, "Bitmap sync mode must be given "
  2386. "when providing a bitmap");
  2387. return NULL;
  2388. }
  2389. if (bdrv_dirty_bitmap_check(bmap, BDRV_BITMAP_ALLOW_RO, errp)) {
  2390. return NULL;
  2391. }
  2392. /* This does not produce a useful bitmap artifact: */
  2393. if (backup->sync == MIRROR_SYNC_MODE_NONE) {
  2394. error_setg(errp, "sync mode '%s' does not produce meaningful bitmap"
  2395. " outputs", MirrorSyncMode_str(backup->sync));
  2396. return NULL;
  2397. }
  2398. /* If the bitmap isn't used for input or output, this is useless: */
  2399. if (backup->bitmap_mode == BITMAP_SYNC_MODE_NEVER &&
  2400. backup->sync != MIRROR_SYNC_MODE_BITMAP) {
  2401. error_setg(errp, "Bitmap sync mode '%s' has no meaningful effect"
  2402. " when combined with sync mode '%s'",
  2403. BitmapSyncMode_str(backup->bitmap_mode),
  2404. MirrorSyncMode_str(backup->sync));
  2405. return NULL;
  2406. }
  2407. }
  2408. if (!backup->has_bitmap && backup->has_bitmap_mode) {
  2409. error_setg(errp, "Cannot specify bitmap sync mode without a bitmap");
  2410. return NULL;
  2411. }
  2412. if (!backup->auto_finalize) {
  2413. job_flags |= JOB_MANUAL_FINALIZE;
  2414. }
  2415. if (!backup->auto_dismiss) {
  2416. job_flags |= JOB_MANUAL_DISMISS;
  2417. }
  2418. job = backup_job_create(backup->job_id, bs, target_bs, backup->speed,
  2419. backup->sync, bmap, backup->bitmap_mode,
  2420. backup->compress,
  2421. backup->filter_node_name,
  2422. backup->on_source_error,
  2423. backup->on_target_error,
  2424. job_flags, NULL, NULL, txn, errp);
  2425. return job;
  2426. }
  2427. void qmp_drive_backup(DriveBackup *backup, Error **errp)
  2428. {
  2429. TransactionAction action = {
  2430. .type = TRANSACTION_ACTION_KIND_DRIVE_BACKUP,
  2431. .u.drive_backup.data = backup,
  2432. };
  2433. blockdev_do_action(&action, errp);
  2434. }
  2435. BlockDeviceInfoList *qmp_query_named_block_nodes(bool has_flat,
  2436. bool flat,
  2437. Error **errp)
  2438. {
  2439. bool return_flat = has_flat && flat;
  2440. return bdrv_named_nodes_list(return_flat, errp);
  2441. }
  2442. XDbgBlockGraph *qmp_x_debug_query_block_graph(Error **errp)
  2443. {
  2444. return bdrv_get_xdbg_block_graph(errp);
  2445. }
  2446. void qmp_blockdev_backup(BlockdevBackup *backup, Error **errp)
  2447. {
  2448. TransactionAction action = {
  2449. .type = TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP,
  2450. .u.blockdev_backup.data = backup,
  2451. };
  2452. blockdev_do_action(&action, errp);
  2453. }
  2454. /* Parameter check and block job starting for drive mirroring.
  2455. * Caller should hold @device and @target's aio context (must be the same).
  2456. **/
  2457. static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs,
  2458. BlockDriverState *target,
  2459. bool has_replaces, const char *replaces,
  2460. enum MirrorSyncMode sync,
  2461. BlockMirrorBackingMode backing_mode,
  2462. bool zero_target,
  2463. bool has_speed, int64_t speed,
  2464. bool has_granularity, uint32_t granularity,
  2465. bool has_buf_size, int64_t buf_size,
  2466. bool has_on_source_error,
  2467. BlockdevOnError on_source_error,
  2468. bool has_on_target_error,
  2469. BlockdevOnError on_target_error,
  2470. bool has_unmap, bool unmap,
  2471. bool has_filter_node_name,
  2472. const char *filter_node_name,
  2473. bool has_copy_mode, MirrorCopyMode copy_mode,
  2474. bool has_auto_finalize, bool auto_finalize,
  2475. bool has_auto_dismiss, bool auto_dismiss,
  2476. Error **errp)
  2477. {
  2478. int job_flags = JOB_DEFAULT;
  2479. if (!has_speed) {
  2480. speed = 0;
  2481. }
  2482. if (!has_on_source_error) {
  2483. on_source_error = BLOCKDEV_ON_ERROR_REPORT;
  2484. }
  2485. if (!has_on_target_error) {
  2486. on_target_error = BLOCKDEV_ON_ERROR_REPORT;
  2487. }
  2488. if (!has_granularity) {
  2489. granularity = 0;
  2490. }
  2491. if (!has_buf_size) {
  2492. buf_size = 0;
  2493. }
  2494. if (!has_unmap) {
  2495. unmap = true;
  2496. }
  2497. if (!has_filter_node_name) {
  2498. filter_node_name = NULL;
  2499. }
  2500. if (!has_copy_mode) {
  2501. copy_mode = MIRROR_COPY_MODE_BACKGROUND;
  2502. }
  2503. if (has_auto_finalize && !auto_finalize) {
  2504. job_flags |= JOB_MANUAL_FINALIZE;
  2505. }
  2506. if (has_auto_dismiss && !auto_dismiss) {
  2507. job_flags |= JOB_MANUAL_DISMISS;
  2508. }
  2509. if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
  2510. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
  2511. "a value in range [512B, 64MB]");
  2512. return;
  2513. }
  2514. if (granularity & (granularity - 1)) {
  2515. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
  2516. "power of 2");
  2517. return;
  2518. }
  2519. if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
  2520. return;
  2521. }
  2522. if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) {
  2523. return;
  2524. }
  2525. if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) {
  2526. sync = MIRROR_SYNC_MODE_FULL;
  2527. }
  2528. if (has_replaces) {
  2529. BlockDriverState *to_replace_bs;
  2530. AioContext *replace_aio_context;
  2531. int64_t bs_size, replace_size;
  2532. bs_size = bdrv_getlength(bs);
  2533. if (bs_size < 0) {
  2534. error_setg_errno(errp, -bs_size, "Failed to query device's size");
  2535. return;
  2536. }
  2537. to_replace_bs = check_to_replace_node(bs, replaces, errp);
  2538. if (!to_replace_bs) {
  2539. return;
  2540. }
  2541. replace_aio_context = bdrv_get_aio_context(to_replace_bs);
  2542. aio_context_acquire(replace_aio_context);
  2543. replace_size = bdrv_getlength(to_replace_bs);
  2544. aio_context_release(replace_aio_context);
  2545. if (replace_size < 0) {
  2546. error_setg_errno(errp, -replace_size,
  2547. "Failed to query the replacement node's size");
  2548. return;
  2549. }
  2550. if (bs_size != replace_size) {
  2551. error_setg(errp, "cannot replace image with a mirror image of "
  2552. "different size");
  2553. return;
  2554. }
  2555. }
  2556. /* pass the node name to replace to mirror start since it's loose coupling
  2557. * and will allow to check whether the node still exist at mirror completion
  2558. */
  2559. mirror_start(job_id, bs, target,
  2560. has_replaces ? replaces : NULL, job_flags,
  2561. speed, granularity, buf_size, sync, backing_mode, zero_target,
  2562. on_source_error, on_target_error, unmap, filter_node_name,
  2563. copy_mode, errp);
  2564. }
  2565. void qmp_drive_mirror(DriveMirror *arg, Error **errp)
  2566. {
  2567. BlockDriverState *bs;
  2568. BlockDriverState *source, *target_bs;
  2569. AioContext *aio_context;
  2570. AioContext *old_context;
  2571. BlockMirrorBackingMode backing_mode;
  2572. Error *local_err = NULL;
  2573. QDict *options = NULL;
  2574. int flags;
  2575. int64_t size;
  2576. const char *format = arg->format;
  2577. bool zero_target;
  2578. int ret;
  2579. bs = qmp_get_root_bs(arg->device, errp);
  2580. if (!bs) {
  2581. return;
  2582. }
  2583. /* Early check to avoid creating target */
  2584. if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) {
  2585. return;
  2586. }
  2587. aio_context = bdrv_get_aio_context(bs);
  2588. aio_context_acquire(aio_context);
  2589. if (!arg->has_mode) {
  2590. arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
  2591. }
  2592. if (!arg->has_format) {
  2593. format = (arg->mode == NEW_IMAGE_MODE_EXISTING
  2594. ? NULL : bs->drv->format_name);
  2595. }
  2596. flags = bs->open_flags | BDRV_O_RDWR;
  2597. source = backing_bs(bs);
  2598. if (!source && arg->sync == MIRROR_SYNC_MODE_TOP) {
  2599. arg->sync = MIRROR_SYNC_MODE_FULL;
  2600. }
  2601. if (arg->sync == MIRROR_SYNC_MODE_NONE) {
  2602. source = bs;
  2603. }
  2604. size = bdrv_getlength(bs);
  2605. if (size < 0) {
  2606. error_setg_errno(errp, -size, "bdrv_getlength failed");
  2607. goto out;
  2608. }
  2609. if (arg->has_replaces) {
  2610. if (!arg->has_node_name) {
  2611. error_setg(errp, "a node-name must be provided when replacing a"
  2612. " named node of the graph");
  2613. goto out;
  2614. }
  2615. }
  2616. if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) {
  2617. backing_mode = MIRROR_SOURCE_BACKING_CHAIN;
  2618. } else {
  2619. backing_mode = MIRROR_OPEN_BACKING_CHAIN;
  2620. }
  2621. /* Don't open backing image in create() */
  2622. flags |= BDRV_O_NO_BACKING;
  2623. if ((arg->sync == MIRROR_SYNC_MODE_FULL || !source)
  2624. && arg->mode != NEW_IMAGE_MODE_EXISTING)
  2625. {
  2626. /* create new image w/o backing file */
  2627. assert(format);
  2628. bdrv_img_create(arg->target, format,
  2629. NULL, NULL, NULL, size, flags, false, &local_err);
  2630. } else {
  2631. switch (arg->mode) {
  2632. case NEW_IMAGE_MODE_EXISTING:
  2633. break;
  2634. case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
  2635. /* create new image with backing file */
  2636. bdrv_refresh_filename(source);
  2637. bdrv_img_create(arg->target, format,
  2638. source->filename,
  2639. source->drv->format_name,
  2640. NULL, size, flags, false, &local_err);
  2641. break;
  2642. default:
  2643. abort();
  2644. }
  2645. }
  2646. if (local_err) {
  2647. error_propagate(errp, local_err);
  2648. goto out;
  2649. }
  2650. options = qdict_new();
  2651. if (arg->has_node_name) {
  2652. qdict_put_str(options, "node-name", arg->node_name);
  2653. }
  2654. if (format) {
  2655. qdict_put_str(options, "driver", format);
  2656. }
  2657. /* Mirroring takes care of copy-on-write using the source's backing
  2658. * file.
  2659. */
  2660. target_bs = bdrv_open(arg->target, NULL, options, flags, errp);
  2661. if (!target_bs) {
  2662. goto out;
  2663. }
  2664. zero_target = (arg->sync == MIRROR_SYNC_MODE_FULL &&
  2665. (arg->mode == NEW_IMAGE_MODE_EXISTING ||
  2666. !bdrv_has_zero_init(target_bs)));
  2667. /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
  2668. old_context = bdrv_get_aio_context(target_bs);
  2669. aio_context_release(aio_context);
  2670. aio_context_acquire(old_context);
  2671. ret = bdrv_try_set_aio_context(target_bs, aio_context, errp);
  2672. if (ret < 0) {
  2673. bdrv_unref(target_bs);
  2674. aio_context_release(old_context);
  2675. return;
  2676. }
  2677. aio_context_release(old_context);
  2678. aio_context_acquire(aio_context);
  2679. blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs,
  2680. arg->has_replaces, arg->replaces, arg->sync,
  2681. backing_mode, zero_target,
  2682. arg->has_speed, arg->speed,
  2683. arg->has_granularity, arg->granularity,
  2684. arg->has_buf_size, arg->buf_size,
  2685. arg->has_on_source_error, arg->on_source_error,
  2686. arg->has_on_target_error, arg->on_target_error,
  2687. arg->has_unmap, arg->unmap,
  2688. false, NULL,
  2689. arg->has_copy_mode, arg->copy_mode,
  2690. arg->has_auto_finalize, arg->auto_finalize,
  2691. arg->has_auto_dismiss, arg->auto_dismiss,
  2692. errp);
  2693. bdrv_unref(target_bs);
  2694. out:
  2695. aio_context_release(aio_context);
  2696. }
  2697. void qmp_blockdev_mirror(bool has_job_id, const char *job_id,
  2698. const char *device, const char *target,
  2699. bool has_replaces, const char *replaces,
  2700. MirrorSyncMode sync,
  2701. bool has_speed, int64_t speed,
  2702. bool has_granularity, uint32_t granularity,
  2703. bool has_buf_size, int64_t buf_size,
  2704. bool has_on_source_error,
  2705. BlockdevOnError on_source_error,
  2706. bool has_on_target_error,
  2707. BlockdevOnError on_target_error,
  2708. bool has_filter_node_name,
  2709. const char *filter_node_name,
  2710. bool has_copy_mode, MirrorCopyMode copy_mode,
  2711. bool has_auto_finalize, bool auto_finalize,
  2712. bool has_auto_dismiss, bool auto_dismiss,
  2713. Error **errp)
  2714. {
  2715. BlockDriverState *bs;
  2716. BlockDriverState *target_bs;
  2717. AioContext *aio_context;
  2718. AioContext *old_context;
  2719. BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN;
  2720. bool zero_target;
  2721. int ret;
  2722. bs = qmp_get_root_bs(device, errp);
  2723. if (!bs) {
  2724. return;
  2725. }
  2726. target_bs = bdrv_lookup_bs(target, target, errp);
  2727. if (!target_bs) {
  2728. return;
  2729. }
  2730. zero_target = (sync == MIRROR_SYNC_MODE_FULL);
  2731. /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
  2732. old_context = bdrv_get_aio_context(target_bs);
  2733. aio_context = bdrv_get_aio_context(bs);
  2734. aio_context_acquire(old_context);
  2735. ret = bdrv_try_set_aio_context(target_bs, aio_context, errp);
  2736. aio_context_release(old_context);
  2737. aio_context_acquire(aio_context);
  2738. if (ret < 0) {
  2739. goto out;
  2740. }
  2741. blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs,
  2742. has_replaces, replaces, sync, backing_mode,
  2743. zero_target, has_speed, speed,
  2744. has_granularity, granularity,
  2745. has_buf_size, buf_size,
  2746. has_on_source_error, on_source_error,
  2747. has_on_target_error, on_target_error,
  2748. true, true,
  2749. has_filter_node_name, filter_node_name,
  2750. has_copy_mode, copy_mode,
  2751. has_auto_finalize, auto_finalize,
  2752. has_auto_dismiss, auto_dismiss,
  2753. errp);
  2754. out:
  2755. aio_context_release(aio_context);
  2756. }
  2757. /* Get a block job using its ID and acquire its AioContext */
  2758. static BlockJob *find_block_job(const char *id, AioContext **aio_context,
  2759. Error **errp)
  2760. {
  2761. BlockJob *job;
  2762. assert(id != NULL);
  2763. *aio_context = NULL;
  2764. job = block_job_get(id);
  2765. if (!job) {
  2766. error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
  2767. "Block job '%s' not found", id);
  2768. return NULL;
  2769. }
  2770. *aio_context = blk_get_aio_context(job->blk);
  2771. aio_context_acquire(*aio_context);
  2772. return job;
  2773. }
  2774. void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
  2775. {
  2776. AioContext *aio_context;
  2777. BlockJob *job = find_block_job(device, &aio_context, errp);
  2778. if (!job) {
  2779. return;
  2780. }
  2781. block_job_set_speed(job, speed, errp);
  2782. aio_context_release(aio_context);
  2783. }
  2784. void qmp_block_job_cancel(const char *device,
  2785. bool has_force, bool force, Error **errp)
  2786. {
  2787. AioContext *aio_context;
  2788. BlockJob *job = find_block_job(device, &aio_context, errp);
  2789. if (!job) {
  2790. return;
  2791. }
  2792. if (!has_force) {
  2793. force = false;
  2794. }
  2795. if (job_user_paused(&job->job) && !force) {
  2796. error_setg(errp, "The block job for device '%s' is currently paused",
  2797. device);
  2798. goto out;
  2799. }
  2800. trace_qmp_block_job_cancel(job);
  2801. job_user_cancel(&job->job, force, errp);
  2802. out:
  2803. aio_context_release(aio_context);
  2804. }
  2805. void qmp_block_job_pause(const char *device, Error **errp)
  2806. {
  2807. AioContext *aio_context;
  2808. BlockJob *job = find_block_job(device, &aio_context, errp);
  2809. if (!job) {
  2810. return;
  2811. }
  2812. trace_qmp_block_job_pause(job);
  2813. job_user_pause(&job->job, errp);
  2814. aio_context_release(aio_context);
  2815. }
  2816. void qmp_block_job_resume(const char *device, Error **errp)
  2817. {
  2818. AioContext *aio_context;
  2819. BlockJob *job = find_block_job(device, &aio_context, errp);
  2820. if (!job) {
  2821. return;
  2822. }
  2823. trace_qmp_block_job_resume(job);
  2824. job_user_resume(&job->job, errp);
  2825. aio_context_release(aio_context);
  2826. }
  2827. void qmp_block_job_complete(const char *device, Error **errp)
  2828. {
  2829. AioContext *aio_context;
  2830. BlockJob *job = find_block_job(device, &aio_context, errp);
  2831. if (!job) {
  2832. return;
  2833. }
  2834. trace_qmp_block_job_complete(job);
  2835. job_complete(&job->job, errp);
  2836. aio_context_release(aio_context);
  2837. }
  2838. void qmp_block_job_finalize(const char *id, Error **errp)
  2839. {
  2840. AioContext *aio_context;
  2841. BlockJob *job = find_block_job(id, &aio_context, errp);
  2842. if (!job) {
  2843. return;
  2844. }
  2845. trace_qmp_block_job_finalize(job);
  2846. job_ref(&job->job);
  2847. job_finalize(&job->job, errp);
  2848. /*
  2849. * Job's context might have changed via job_finalize (and job_txn_apply
  2850. * automatically acquires the new one), so make sure we release the correct
  2851. * one.
  2852. */
  2853. aio_context = blk_get_aio_context(job->blk);
  2854. job_unref(&job->job);
  2855. aio_context_release(aio_context);
  2856. }
  2857. void qmp_block_job_dismiss(const char *id, Error **errp)
  2858. {
  2859. AioContext *aio_context;
  2860. BlockJob *bjob = find_block_job(id, &aio_context, errp);
  2861. Job *job;
  2862. if (!bjob) {
  2863. return;
  2864. }
  2865. trace_qmp_block_job_dismiss(bjob);
  2866. job = &bjob->job;
  2867. job_dismiss(&job, errp);
  2868. aio_context_release(aio_context);
  2869. }
  2870. void qmp_change_backing_file(const char *device,
  2871. const char *image_node_name,
  2872. const char *backing_file,
  2873. Error **errp)
  2874. {
  2875. BlockDriverState *bs = NULL;
  2876. AioContext *aio_context;
  2877. BlockDriverState *image_bs = NULL;
  2878. Error *local_err = NULL;
  2879. bool ro;
  2880. int ret;
  2881. bs = qmp_get_root_bs(device, errp);
  2882. if (!bs) {
  2883. return;
  2884. }
  2885. aio_context = bdrv_get_aio_context(bs);
  2886. aio_context_acquire(aio_context);
  2887. image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
  2888. if (local_err) {
  2889. error_propagate(errp, local_err);
  2890. goto out;
  2891. }
  2892. if (!image_bs) {
  2893. error_setg(errp, "image file not found");
  2894. goto out;
  2895. }
  2896. if (bdrv_find_base(image_bs) == image_bs) {
  2897. error_setg(errp, "not allowing backing file change on an image "
  2898. "without a backing file");
  2899. goto out;
  2900. }
  2901. /* even though we are not necessarily operating on bs, we need it to
  2902. * determine if block ops are currently prohibited on the chain */
  2903. if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
  2904. goto out;
  2905. }
  2906. /* final sanity check */
  2907. if (!bdrv_chain_contains(bs, image_bs)) {
  2908. error_setg(errp, "'%s' and image file are not in the same chain",
  2909. device);
  2910. goto out;
  2911. }
  2912. /* if not r/w, reopen to make r/w */
  2913. ro = bdrv_is_read_only(image_bs);
  2914. if (ro) {
  2915. if (bdrv_reopen_set_read_only(image_bs, false, errp) != 0) {
  2916. goto out;
  2917. }
  2918. }
  2919. ret = bdrv_change_backing_file(image_bs, backing_file,
  2920. image_bs->drv ? image_bs->drv->format_name : "",
  2921. false);
  2922. if (ret < 0) {
  2923. error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
  2924. backing_file);
  2925. /* don't exit here, so we can try to restore open flags if
  2926. * appropriate */
  2927. }
  2928. if (ro) {
  2929. bdrv_reopen_set_read_only(image_bs, true, errp);
  2930. }
  2931. out:
  2932. aio_context_release(aio_context);
  2933. }
  2934. void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
  2935. {
  2936. BlockDriverState *bs;
  2937. QObject *obj;
  2938. Visitor *v = qobject_output_visitor_new(&obj);
  2939. QDict *qdict;
  2940. visit_type_BlockdevOptions(v, NULL, &options, &error_abort);
  2941. visit_complete(v, &obj);
  2942. qdict = qobject_to(QDict, obj);
  2943. qdict_flatten(qdict);
  2944. if (!qdict_get_try_str(qdict, "node-name")) {
  2945. error_setg(errp, "'node-name' must be specified for the root node");
  2946. goto fail;
  2947. }
  2948. bs = bds_tree_init(qdict, errp);
  2949. if (!bs) {
  2950. goto fail;
  2951. }
  2952. bdrv_set_monitor_owned(bs);
  2953. fail:
  2954. visit_free(v);
  2955. }
  2956. void qmp_x_blockdev_reopen(BlockdevOptions *options, Error **errp)
  2957. {
  2958. BlockDriverState *bs;
  2959. AioContext *ctx;
  2960. QObject *obj;
  2961. Visitor *v = qobject_output_visitor_new(&obj);
  2962. BlockReopenQueue *queue;
  2963. QDict *qdict;
  2964. /* Check for the selected node name */
  2965. if (!options->has_node_name) {
  2966. error_setg(errp, "Node name not specified");
  2967. goto fail;
  2968. }
  2969. bs = bdrv_find_node(options->node_name);
  2970. if (!bs) {
  2971. error_setg(errp, "Cannot find node named '%s'", options->node_name);
  2972. goto fail;
  2973. }
  2974. /* Put all options in a QDict and flatten it */
  2975. visit_type_BlockdevOptions(v, NULL, &options, &error_abort);
  2976. visit_complete(v, &obj);
  2977. qdict = qobject_to(QDict, obj);
  2978. qdict_flatten(qdict);
  2979. /* Perform the reopen operation */
  2980. ctx = bdrv_get_aio_context(bs);
  2981. aio_context_acquire(ctx);
  2982. bdrv_subtree_drained_begin(bs);
  2983. queue = bdrv_reopen_queue(NULL, bs, qdict, false);
  2984. bdrv_reopen_multiple(queue, errp);
  2985. bdrv_subtree_drained_end(bs);
  2986. aio_context_release(ctx);
  2987. fail:
  2988. visit_free(v);
  2989. }
  2990. void qmp_blockdev_del(const char *node_name, Error **errp)
  2991. {
  2992. AioContext *aio_context;
  2993. BlockDriverState *bs;
  2994. bs = bdrv_find_node(node_name);
  2995. if (!bs) {
  2996. error_setg(errp, "Cannot find node %s", node_name);
  2997. return;
  2998. }
  2999. if (bdrv_has_blk(bs)) {
  3000. error_setg(errp, "Node %s is in use", node_name);
  3001. return;
  3002. }
  3003. aio_context = bdrv_get_aio_context(bs);
  3004. aio_context_acquire(aio_context);
  3005. if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) {
  3006. goto out;
  3007. }
  3008. if (!QTAILQ_IN_USE(bs, monitor_list)) {
  3009. error_setg(errp, "Node %s is not owned by the monitor",
  3010. bs->node_name);
  3011. goto out;
  3012. }
  3013. if (bs->refcnt > 1) {
  3014. error_setg(errp, "Block device %s is in use",
  3015. bdrv_get_device_or_node_name(bs));
  3016. goto out;
  3017. }
  3018. QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list);
  3019. bdrv_unref(bs);
  3020. out:
  3021. aio_context_release(aio_context);
  3022. }
  3023. static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs,
  3024. const char *child_name)
  3025. {
  3026. BdrvChild *child;
  3027. QLIST_FOREACH(child, &parent_bs->children, next) {
  3028. if (strcmp(child->name, child_name) == 0) {
  3029. return child;
  3030. }
  3031. }
  3032. return NULL;
  3033. }
  3034. void qmp_x_blockdev_change(const char *parent, bool has_child,
  3035. const char *child, bool has_node,
  3036. const char *node, Error **errp)
  3037. {
  3038. BlockDriverState *parent_bs, *new_bs = NULL;
  3039. BdrvChild *p_child;
  3040. parent_bs = bdrv_lookup_bs(parent, parent, errp);
  3041. if (!parent_bs) {
  3042. return;
  3043. }
  3044. if (has_child == has_node) {
  3045. if (has_child) {
  3046. error_setg(errp, "The parameters child and node are in conflict");
  3047. } else {
  3048. error_setg(errp, "Either child or node must be specified");
  3049. }
  3050. return;
  3051. }
  3052. if (has_child) {
  3053. p_child = bdrv_find_child(parent_bs, child);
  3054. if (!p_child) {
  3055. error_setg(errp, "Node '%s' does not have child '%s'",
  3056. parent, child);
  3057. return;
  3058. }
  3059. bdrv_del_child(parent_bs, p_child, errp);
  3060. }
  3061. if (has_node) {
  3062. new_bs = bdrv_find_node(node);
  3063. if (!new_bs) {
  3064. error_setg(errp, "Node '%s' not found", node);
  3065. return;
  3066. }
  3067. bdrv_add_child(parent_bs, new_bs, errp);
  3068. }
  3069. }
  3070. BlockJobInfoList *qmp_query_block_jobs(Error **errp)
  3071. {
  3072. BlockJobInfoList *head = NULL, **p_next = &head;
  3073. BlockJob *job;
  3074. for (job = block_job_next(NULL); job; job = block_job_next(job)) {
  3075. BlockJobInfoList *elem;
  3076. AioContext *aio_context;
  3077. if (block_job_is_internal(job)) {
  3078. continue;
  3079. }
  3080. elem = g_new0(BlockJobInfoList, 1);
  3081. aio_context = blk_get_aio_context(job->blk);
  3082. aio_context_acquire(aio_context);
  3083. elem->value = block_job_query(job, errp);
  3084. aio_context_release(aio_context);
  3085. if (!elem->value) {
  3086. g_free(elem);
  3087. qapi_free_BlockJobInfoList(head);
  3088. return NULL;
  3089. }
  3090. *p_next = elem;
  3091. p_next = &elem->next;
  3092. }
  3093. return head;
  3094. }
  3095. void qmp_x_blockdev_set_iothread(const char *node_name, StrOrNull *iothread,
  3096. bool has_force, bool force, Error **errp)
  3097. {
  3098. AioContext *old_context;
  3099. AioContext *new_context;
  3100. BlockDriverState *bs;
  3101. bs = bdrv_find_node(node_name);
  3102. if (!bs) {
  3103. error_setg(errp, "Cannot find node %s", node_name);
  3104. return;
  3105. }
  3106. /* Protects against accidents. */
  3107. if (!(has_force && force) && bdrv_has_blk(bs)) {
  3108. error_setg(errp, "Node %s is associated with a BlockBackend and could "
  3109. "be in use (use force=true to override this check)",
  3110. node_name);
  3111. return;
  3112. }
  3113. if (iothread->type == QTYPE_QSTRING) {
  3114. IOThread *obj = iothread_by_id(iothread->u.s);
  3115. if (!obj) {
  3116. error_setg(errp, "Cannot find iothread %s", iothread->u.s);
  3117. return;
  3118. }
  3119. new_context = iothread_get_aio_context(obj);
  3120. } else {
  3121. new_context = qemu_get_aio_context();
  3122. }
  3123. old_context = bdrv_get_aio_context(bs);
  3124. aio_context_acquire(old_context);
  3125. bdrv_try_set_aio_context(bs, new_context, errp);
  3126. aio_context_release(old_context);
  3127. }
  3128. QemuOptsList qemu_common_drive_opts = {
  3129. .name = "drive",
  3130. .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
  3131. .desc = {
  3132. {
  3133. .name = "snapshot",
  3134. .type = QEMU_OPT_BOOL,
  3135. .help = "enable/disable snapshot mode",
  3136. },{
  3137. .name = "aio",
  3138. .type = QEMU_OPT_STRING,
  3139. .help = "host AIO implementation (threads, native, io_uring)",
  3140. },{
  3141. .name = BDRV_OPT_CACHE_WB,
  3142. .type = QEMU_OPT_BOOL,
  3143. .help = "Enable writeback mode",
  3144. },{
  3145. .name = "format",
  3146. .type = QEMU_OPT_STRING,
  3147. .help = "disk format (raw, qcow2, ...)",
  3148. },{
  3149. .name = "rerror",
  3150. .type = QEMU_OPT_STRING,
  3151. .help = "read error action",
  3152. },{
  3153. .name = "werror",
  3154. .type = QEMU_OPT_STRING,
  3155. .help = "write error action",
  3156. },{
  3157. .name = BDRV_OPT_READ_ONLY,
  3158. .type = QEMU_OPT_BOOL,
  3159. .help = "open drive file as read-only",
  3160. },
  3161. THROTTLE_OPTS,
  3162. {
  3163. .name = "throttling.group",
  3164. .type = QEMU_OPT_STRING,
  3165. .help = "name of the block throttling group",
  3166. },{
  3167. .name = "copy-on-read",
  3168. .type = QEMU_OPT_BOOL,
  3169. .help = "copy read data from backing file into image file",
  3170. },{
  3171. .name = "detect-zeroes",
  3172. .type = QEMU_OPT_STRING,
  3173. .help = "try to optimize zero writes (off, on, unmap)",
  3174. },{
  3175. .name = "stats-account-invalid",
  3176. .type = QEMU_OPT_BOOL,
  3177. .help = "whether to account for invalid I/O operations "
  3178. "in the statistics",
  3179. },{
  3180. .name = "stats-account-failed",
  3181. .type = QEMU_OPT_BOOL,
  3182. .help = "whether to account for failed I/O operations "
  3183. "in the statistics",
  3184. },
  3185. { /* end of list */ }
  3186. },
  3187. };
  3188. QemuOptsList qemu_drive_opts = {
  3189. .name = "drive",
  3190. .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
  3191. .desc = {
  3192. /*
  3193. * no elements => accept any params
  3194. * validation will happen later
  3195. */
  3196. { /* end of list */ }
  3197. },
  3198. };