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 135KB

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