blockdev.c 116 KB

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