blockdev.c 115 KB

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