You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

blockdev.c 139KB

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