blockdev.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823
  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 "sysemu/block-backend.h"
  33. #include "sysemu/blockdev.h"
  34. #include "hw/block/block.h"
  35. #include "block/blockjob.h"
  36. #include "monitor/monitor.h"
  37. #include "qemu/option.h"
  38. #include "qemu/config-file.h"
  39. #include "qapi/qmp/types.h"
  40. #include "qapi-visit.h"
  41. #include "qapi/qmp-output-visitor.h"
  42. #include "qapi/util.h"
  43. #include "sysemu/sysemu.h"
  44. #include "block/block_int.h"
  45. #include "qmp-commands.h"
  46. #include "trace.h"
  47. #include "sysemu/arch_init.h"
  48. static const char *const if_name[IF_COUNT] = {
  49. [IF_NONE] = "none",
  50. [IF_IDE] = "ide",
  51. [IF_SCSI] = "scsi",
  52. [IF_FLOPPY] = "floppy",
  53. [IF_PFLASH] = "pflash",
  54. [IF_MTD] = "mtd",
  55. [IF_SD] = "sd",
  56. [IF_VIRTIO] = "virtio",
  57. [IF_XEN] = "xen",
  58. };
  59. static int if_max_devs[IF_COUNT] = {
  60. /*
  61. * Do not change these numbers! They govern how drive option
  62. * index maps to unit and bus. That mapping is ABI.
  63. *
  64. * All controllers used to imlement if=T drives need to support
  65. * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
  66. * Otherwise, some index values map to "impossible" bus, unit
  67. * values.
  68. *
  69. * For instance, if you change [IF_SCSI] to 255, -drive
  70. * if=scsi,index=12 no longer means bus=1,unit=5, but
  71. * bus=0,unit=12. With an lsi53c895a controller (7 units max),
  72. * the drive can't be set up. Regression.
  73. */
  74. [IF_IDE] = 2,
  75. [IF_SCSI] = 7,
  76. };
  77. /**
  78. * Boards may call this to offer board-by-board overrides
  79. * of the default, global values.
  80. */
  81. void override_max_devs(BlockInterfaceType type, int max_devs)
  82. {
  83. BlockBackend *blk;
  84. DriveInfo *dinfo;
  85. if (max_devs <= 0) {
  86. return;
  87. }
  88. for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
  89. dinfo = blk_legacy_dinfo(blk);
  90. if (dinfo->type == type) {
  91. fprintf(stderr, "Cannot override units-per-bus property of"
  92. " the %s interface, because a drive of that type has"
  93. " already been added.\n", if_name[type]);
  94. g_assert_not_reached();
  95. }
  96. }
  97. if_max_devs[type] = max_devs;
  98. }
  99. /*
  100. * We automatically delete the drive when a device using it gets
  101. * unplugged. Questionable feature, but we can't just drop it.
  102. * Device models call blockdev_mark_auto_del() to schedule the
  103. * automatic deletion, and generic qdev code calls blockdev_auto_del()
  104. * when deletion is actually safe.
  105. */
  106. void blockdev_mark_auto_del(BlockBackend *blk)
  107. {
  108. DriveInfo *dinfo = blk_legacy_dinfo(blk);
  109. BlockDriverState *bs = blk_bs(blk);
  110. AioContext *aio_context;
  111. if (!dinfo) {
  112. return;
  113. }
  114. aio_context = bdrv_get_aio_context(bs);
  115. aio_context_acquire(aio_context);
  116. if (bs->job) {
  117. block_job_cancel(bs->job);
  118. }
  119. aio_context_release(aio_context);
  120. dinfo->auto_del = 1;
  121. }
  122. void blockdev_auto_del(BlockBackend *blk)
  123. {
  124. DriveInfo *dinfo = blk_legacy_dinfo(blk);
  125. if (dinfo && dinfo->auto_del) {
  126. blk_unref(blk);
  127. }
  128. }
  129. /**
  130. * Returns the current mapping of how many units per bus
  131. * a particular interface can support.
  132. *
  133. * A positive integer indicates n units per bus.
  134. * 0 implies the mapping has not been established.
  135. * -1 indicates an invalid BlockInterfaceType was given.
  136. */
  137. int drive_get_max_devs(BlockInterfaceType type)
  138. {
  139. if (type >= IF_IDE && type < IF_COUNT) {
  140. return if_max_devs[type];
  141. }
  142. return -1;
  143. }
  144. static int drive_index_to_bus_id(BlockInterfaceType type, int index)
  145. {
  146. int max_devs = if_max_devs[type];
  147. return max_devs ? index / max_devs : 0;
  148. }
  149. static int drive_index_to_unit_id(BlockInterfaceType type, int index)
  150. {
  151. int max_devs = if_max_devs[type];
  152. return max_devs ? index % max_devs : index;
  153. }
  154. QemuOpts *drive_def(const char *optstr)
  155. {
  156. return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
  157. }
  158. QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
  159. const char *optstr)
  160. {
  161. QemuOpts *opts;
  162. char buf[32];
  163. opts = drive_def(optstr);
  164. if (!opts) {
  165. return NULL;
  166. }
  167. if (type != IF_DEFAULT) {
  168. qemu_opt_set(opts, "if", if_name[type]);
  169. }
  170. if (index >= 0) {
  171. snprintf(buf, sizeof(buf), "%d", index);
  172. qemu_opt_set(opts, "index", buf);
  173. }
  174. if (file)
  175. qemu_opt_set(opts, "file", file);
  176. return opts;
  177. }
  178. DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
  179. {
  180. BlockBackend *blk;
  181. DriveInfo *dinfo;
  182. for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
  183. dinfo = blk_legacy_dinfo(blk);
  184. if (dinfo && dinfo->type == type
  185. && dinfo->bus == bus && dinfo->unit == unit) {
  186. return dinfo;
  187. }
  188. }
  189. return NULL;
  190. }
  191. bool drive_check_orphaned(void)
  192. {
  193. BlockBackend *blk;
  194. DriveInfo *dinfo;
  195. bool rs = false;
  196. for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
  197. dinfo = blk_legacy_dinfo(blk);
  198. /* If dinfo->bdrv->dev is NULL, it has no device attached. */
  199. /* Unless this is a default drive, this may be an oversight. */
  200. if (!blk_get_attached_dev(blk) && !dinfo->is_default &&
  201. dinfo->type != IF_NONE) {
  202. fprintf(stderr, "Warning: Orphaned drive without device: "
  203. "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
  204. blk_name(blk), blk_bs(blk)->filename, if_name[dinfo->type],
  205. dinfo->bus, dinfo->unit);
  206. rs = true;
  207. }
  208. }
  209. return rs;
  210. }
  211. DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
  212. {
  213. return drive_get(type,
  214. drive_index_to_bus_id(type, index),
  215. drive_index_to_unit_id(type, index));
  216. }
  217. int drive_get_max_bus(BlockInterfaceType type)
  218. {
  219. int max_bus;
  220. BlockBackend *blk;
  221. DriveInfo *dinfo;
  222. max_bus = -1;
  223. for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
  224. dinfo = blk_legacy_dinfo(blk);
  225. if (dinfo && dinfo->type == type && dinfo->bus > max_bus) {
  226. max_bus = dinfo->bus;
  227. }
  228. }
  229. return max_bus;
  230. }
  231. /* Get a block device. This should only be used for single-drive devices
  232. (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
  233. appropriate bus. */
  234. DriveInfo *drive_get_next(BlockInterfaceType type)
  235. {
  236. static int next_block_unit[IF_COUNT];
  237. return drive_get(type, 0, next_block_unit[type]++);
  238. }
  239. static void bdrv_format_print(void *opaque, const char *name)
  240. {
  241. error_printf(" %s", name);
  242. }
  243. typedef struct {
  244. QEMUBH *bh;
  245. BlockDriverState *bs;
  246. } BDRVPutRefBH;
  247. static void bdrv_put_ref_bh(void *opaque)
  248. {
  249. BDRVPutRefBH *s = opaque;
  250. bdrv_unref(s->bs);
  251. qemu_bh_delete(s->bh);
  252. g_free(s);
  253. }
  254. /*
  255. * Release a BDS reference in a BH
  256. *
  257. * It is not safe to use bdrv_unref() from a callback function when the callers
  258. * still need the BlockDriverState. In such cases we schedule a BH to release
  259. * the reference.
  260. */
  261. static void bdrv_put_ref_bh_schedule(BlockDriverState *bs)
  262. {
  263. BDRVPutRefBH *s;
  264. s = g_new(BDRVPutRefBH, 1);
  265. s->bh = qemu_bh_new(bdrv_put_ref_bh, s);
  266. s->bs = bs;
  267. qemu_bh_schedule(s->bh);
  268. }
  269. static int parse_block_error_action(const char *buf, bool is_read, Error **errp)
  270. {
  271. if (!strcmp(buf, "ignore")) {
  272. return BLOCKDEV_ON_ERROR_IGNORE;
  273. } else if (!is_read && !strcmp(buf, "enospc")) {
  274. return BLOCKDEV_ON_ERROR_ENOSPC;
  275. } else if (!strcmp(buf, "stop")) {
  276. return BLOCKDEV_ON_ERROR_STOP;
  277. } else if (!strcmp(buf, "report")) {
  278. return BLOCKDEV_ON_ERROR_REPORT;
  279. } else {
  280. error_setg(errp, "'%s' invalid %s error action",
  281. buf, is_read ? "read" : "write");
  282. return -1;
  283. }
  284. }
  285. static bool check_throttle_config(ThrottleConfig *cfg, Error **errp)
  286. {
  287. if (throttle_conflicting(cfg)) {
  288. error_setg(errp, "bps/iops/max total values and read/write values"
  289. " cannot be used at the same time");
  290. return false;
  291. }
  292. if (!throttle_is_valid(cfg)) {
  293. error_setg(errp, "bps/iops/maxs values must be 0 or greater");
  294. return false;
  295. }
  296. return true;
  297. }
  298. typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType;
  299. /* Takes the ownership of bs_opts */
  300. static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,
  301. Error **errp)
  302. {
  303. const char *buf;
  304. int ro = 0;
  305. int bdrv_flags = 0;
  306. int on_read_error, on_write_error;
  307. BlockBackend *blk;
  308. BlockDriverState *bs;
  309. ThrottleConfig cfg;
  310. int snapshot = 0;
  311. bool copy_on_read;
  312. int ret;
  313. Error *error = NULL;
  314. QemuOpts *opts;
  315. const char *id;
  316. bool has_driver_specific_opts;
  317. BlockdevDetectZeroesOptions detect_zeroes;
  318. BlockDriver *drv = NULL;
  319. /* Check common options by copying from bs_opts to opts, all other options
  320. * stay in bs_opts for processing by bdrv_open(). */
  321. id = qdict_get_try_str(bs_opts, "id");
  322. opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error);
  323. if (error) {
  324. error_propagate(errp, error);
  325. goto err_no_opts;
  326. }
  327. qemu_opts_absorb_qdict(opts, bs_opts, &error);
  328. if (error) {
  329. error_propagate(errp, error);
  330. goto early_err;
  331. }
  332. if (id) {
  333. qdict_del(bs_opts, "id");
  334. }
  335. has_driver_specific_opts = !!qdict_size(bs_opts);
  336. /* extract parameters */
  337. snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
  338. ro = qemu_opt_get_bool(opts, "read-only", 0);
  339. copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);
  340. if ((buf = qemu_opt_get(opts, "discard")) != NULL) {
  341. if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) {
  342. error_setg(errp, "invalid discard option");
  343. goto early_err;
  344. }
  345. }
  346. if (qemu_opt_get_bool(opts, "cache.writeback", true)) {
  347. bdrv_flags |= BDRV_O_CACHE_WB;
  348. }
  349. if (qemu_opt_get_bool(opts, "cache.direct", false)) {
  350. bdrv_flags |= BDRV_O_NOCACHE;
  351. }
  352. if (qemu_opt_get_bool(opts, "cache.no-flush", false)) {
  353. bdrv_flags |= BDRV_O_NO_FLUSH;
  354. }
  355. #ifdef CONFIG_LINUX_AIO
  356. if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
  357. if (!strcmp(buf, "native")) {
  358. bdrv_flags |= BDRV_O_NATIVE_AIO;
  359. } else if (!strcmp(buf, "threads")) {
  360. /* this is the default */
  361. } else {
  362. error_setg(errp, "invalid aio option");
  363. goto early_err;
  364. }
  365. }
  366. #endif
  367. if ((buf = qemu_opt_get(opts, "format")) != NULL) {
  368. if (is_help_option(buf)) {
  369. error_printf("Supported formats:");
  370. bdrv_iterate_format(bdrv_format_print, NULL);
  371. error_printf("\n");
  372. goto early_err;
  373. }
  374. drv = bdrv_find_format(buf);
  375. if (!drv) {
  376. error_setg(errp, "'%s' invalid format", buf);
  377. goto early_err;
  378. }
  379. }
  380. /* disk I/O throttling */
  381. memset(&cfg, 0, sizeof(cfg));
  382. cfg.buckets[THROTTLE_BPS_TOTAL].avg =
  383. qemu_opt_get_number(opts, "throttling.bps-total", 0);
  384. cfg.buckets[THROTTLE_BPS_READ].avg =
  385. qemu_opt_get_number(opts, "throttling.bps-read", 0);
  386. cfg.buckets[THROTTLE_BPS_WRITE].avg =
  387. qemu_opt_get_number(opts, "throttling.bps-write", 0);
  388. cfg.buckets[THROTTLE_OPS_TOTAL].avg =
  389. qemu_opt_get_number(opts, "throttling.iops-total", 0);
  390. cfg.buckets[THROTTLE_OPS_READ].avg =
  391. qemu_opt_get_number(opts, "throttling.iops-read", 0);
  392. cfg.buckets[THROTTLE_OPS_WRITE].avg =
  393. qemu_opt_get_number(opts, "throttling.iops-write", 0);
  394. cfg.buckets[THROTTLE_BPS_TOTAL].max =
  395. qemu_opt_get_number(opts, "throttling.bps-total-max", 0);
  396. cfg.buckets[THROTTLE_BPS_READ].max =
  397. qemu_opt_get_number(opts, "throttling.bps-read-max", 0);
  398. cfg.buckets[THROTTLE_BPS_WRITE].max =
  399. qemu_opt_get_number(opts, "throttling.bps-write-max", 0);
  400. cfg.buckets[THROTTLE_OPS_TOTAL].max =
  401. qemu_opt_get_number(opts, "throttling.iops-total-max", 0);
  402. cfg.buckets[THROTTLE_OPS_READ].max =
  403. qemu_opt_get_number(opts, "throttling.iops-read-max", 0);
  404. cfg.buckets[THROTTLE_OPS_WRITE].max =
  405. qemu_opt_get_number(opts, "throttling.iops-write-max", 0);
  406. cfg.op_size = qemu_opt_get_number(opts, "throttling.iops-size", 0);
  407. if (!check_throttle_config(&cfg, &error)) {
  408. error_propagate(errp, error);
  409. goto early_err;
  410. }
  411. on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
  412. if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
  413. on_write_error = parse_block_error_action(buf, 0, &error);
  414. if (error) {
  415. error_propagate(errp, error);
  416. goto early_err;
  417. }
  418. }
  419. on_read_error = BLOCKDEV_ON_ERROR_REPORT;
  420. if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
  421. on_read_error = parse_block_error_action(buf, 1, &error);
  422. if (error) {
  423. error_propagate(errp, error);
  424. goto early_err;
  425. }
  426. }
  427. detect_zeroes =
  428. qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
  429. qemu_opt_get(opts, "detect-zeroes"),
  430. BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX,
  431. BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
  432. &error);
  433. if (error) {
  434. error_propagate(errp, error);
  435. goto early_err;
  436. }
  437. if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
  438. !(bdrv_flags & BDRV_O_UNMAP)) {
  439. error_setg(errp, "setting detect-zeroes to unmap is not allowed "
  440. "without setting discard operation to unmap");
  441. goto early_err;
  442. }
  443. /* init */
  444. blk = blk_new_with_bs(qemu_opts_id(opts), errp);
  445. if (!blk) {
  446. goto early_err;
  447. }
  448. bs = blk_bs(blk);
  449. bs->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0;
  450. bs->read_only = ro;
  451. bs->detect_zeroes = detect_zeroes;
  452. bdrv_set_on_error(bs, on_read_error, on_write_error);
  453. /* disk I/O throttling */
  454. if (throttle_enabled(&cfg)) {
  455. bdrv_io_limits_enable(bs);
  456. bdrv_set_io_limits(bs, &cfg);
  457. }
  458. if (!file || !*file) {
  459. if (has_driver_specific_opts) {
  460. file = NULL;
  461. } else {
  462. QDECREF(bs_opts);
  463. qemu_opts_del(opts);
  464. return blk;
  465. }
  466. }
  467. if (snapshot) {
  468. /* always use cache=unsafe with snapshot */
  469. bdrv_flags &= ~BDRV_O_CACHE_MASK;
  470. bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
  471. }
  472. if (copy_on_read) {
  473. bdrv_flags |= BDRV_O_COPY_ON_READ;
  474. }
  475. if (runstate_check(RUN_STATE_INMIGRATE)) {
  476. bdrv_flags |= BDRV_O_INCOMING;
  477. }
  478. bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
  479. QINCREF(bs_opts);
  480. ret = bdrv_open(&bs, file, NULL, bs_opts, bdrv_flags, drv, &error);
  481. assert(bs == blk_bs(blk));
  482. if (ret < 0) {
  483. error_setg(errp, "could not open disk image %s: %s",
  484. file ?: blk_name(blk), error_get_pretty(error));
  485. error_free(error);
  486. goto err;
  487. }
  488. if (bdrv_key_required(bs)) {
  489. autostart = 0;
  490. }
  491. QDECREF(bs_opts);
  492. qemu_opts_del(opts);
  493. return blk;
  494. err:
  495. blk_unref(blk);
  496. early_err:
  497. qemu_opts_del(opts);
  498. err_no_opts:
  499. QDECREF(bs_opts);
  500. return NULL;
  501. }
  502. static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to,
  503. Error **errp)
  504. {
  505. const char *value;
  506. value = qemu_opt_get(opts, from);
  507. if (value) {
  508. if (qemu_opt_find(opts, to)) {
  509. error_setg(errp, "'%s' and its alias '%s' can't be used at the "
  510. "same time", to, from);
  511. return;
  512. }
  513. }
  514. /* rename all items in opts */
  515. while ((value = qemu_opt_get(opts, from))) {
  516. qemu_opt_set(opts, to, value);
  517. qemu_opt_unset(opts, from);
  518. }
  519. }
  520. QemuOptsList qemu_legacy_drive_opts = {
  521. .name = "drive",
  522. .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head),
  523. .desc = {
  524. {
  525. .name = "bus",
  526. .type = QEMU_OPT_NUMBER,
  527. .help = "bus number",
  528. },{
  529. .name = "unit",
  530. .type = QEMU_OPT_NUMBER,
  531. .help = "unit number (i.e. lun for scsi)",
  532. },{
  533. .name = "index",
  534. .type = QEMU_OPT_NUMBER,
  535. .help = "index number",
  536. },{
  537. .name = "media",
  538. .type = QEMU_OPT_STRING,
  539. .help = "media type (disk, cdrom)",
  540. },{
  541. .name = "if",
  542. .type = QEMU_OPT_STRING,
  543. .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
  544. },{
  545. .name = "cyls",
  546. .type = QEMU_OPT_NUMBER,
  547. .help = "number of cylinders (ide disk geometry)",
  548. },{
  549. .name = "heads",
  550. .type = QEMU_OPT_NUMBER,
  551. .help = "number of heads (ide disk geometry)",
  552. },{
  553. .name = "secs",
  554. .type = QEMU_OPT_NUMBER,
  555. .help = "number of sectors (ide disk geometry)",
  556. },{
  557. .name = "trans",
  558. .type = QEMU_OPT_STRING,
  559. .help = "chs translation (auto, lba, none)",
  560. },{
  561. .name = "boot",
  562. .type = QEMU_OPT_BOOL,
  563. .help = "(deprecated, ignored)",
  564. },{
  565. .name = "addr",
  566. .type = QEMU_OPT_STRING,
  567. .help = "pci address (virtio only)",
  568. },{
  569. .name = "serial",
  570. .type = QEMU_OPT_STRING,
  571. .help = "disk serial number",
  572. },{
  573. .name = "file",
  574. .type = QEMU_OPT_STRING,
  575. .help = "file name",
  576. },
  577. /* Options that are passed on, but have special semantics with -drive */
  578. {
  579. .name = "read-only",
  580. .type = QEMU_OPT_BOOL,
  581. .help = "open drive file as read-only",
  582. },{
  583. .name = "rerror",
  584. .type = QEMU_OPT_STRING,
  585. .help = "read error action",
  586. },{
  587. .name = "werror",
  588. .type = QEMU_OPT_STRING,
  589. .help = "write error action",
  590. },{
  591. .name = "copy-on-read",
  592. .type = QEMU_OPT_BOOL,
  593. .help = "copy read data from backing file into image file",
  594. },
  595. { /* end of list */ }
  596. },
  597. };
  598. DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type)
  599. {
  600. const char *value;
  601. BlockBackend *blk;
  602. DriveInfo *dinfo = NULL;
  603. QDict *bs_opts;
  604. QemuOpts *legacy_opts;
  605. DriveMediaType media = MEDIA_DISK;
  606. BlockInterfaceType type;
  607. int cyls, heads, secs, translation;
  608. int max_devs, bus_id, unit_id, index;
  609. const char *devaddr;
  610. const char *werror, *rerror;
  611. bool read_only = false;
  612. bool copy_on_read;
  613. const char *serial;
  614. const char *filename;
  615. Error *local_err = NULL;
  616. int i;
  617. /* Change legacy command line options into QMP ones */
  618. static const struct {
  619. const char *from;
  620. const char *to;
  621. } opt_renames[] = {
  622. { "iops", "throttling.iops-total" },
  623. { "iops_rd", "throttling.iops-read" },
  624. { "iops_wr", "throttling.iops-write" },
  625. { "bps", "throttling.bps-total" },
  626. { "bps_rd", "throttling.bps-read" },
  627. { "bps_wr", "throttling.bps-write" },
  628. { "iops_max", "throttling.iops-total-max" },
  629. { "iops_rd_max", "throttling.iops-read-max" },
  630. { "iops_wr_max", "throttling.iops-write-max" },
  631. { "bps_max", "throttling.bps-total-max" },
  632. { "bps_rd_max", "throttling.bps-read-max" },
  633. { "bps_wr_max", "throttling.bps-write-max" },
  634. { "iops_size", "throttling.iops-size" },
  635. { "readonly", "read-only" },
  636. };
  637. for (i = 0; i < ARRAY_SIZE(opt_renames); i++) {
  638. qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to,
  639. &local_err);
  640. if (local_err) {
  641. error_report("%s", error_get_pretty(local_err));
  642. error_free(local_err);
  643. return NULL;
  644. }
  645. }
  646. value = qemu_opt_get(all_opts, "cache");
  647. if (value) {
  648. int flags = 0;
  649. if (bdrv_parse_cache_flags(value, &flags) != 0) {
  650. error_report("invalid cache option");
  651. return NULL;
  652. }
  653. /* Specific options take precedence */
  654. if (!qemu_opt_get(all_opts, "cache.writeback")) {
  655. qemu_opt_set_bool(all_opts, "cache.writeback",
  656. !!(flags & BDRV_O_CACHE_WB));
  657. }
  658. if (!qemu_opt_get(all_opts, "cache.direct")) {
  659. qemu_opt_set_bool(all_opts, "cache.direct",
  660. !!(flags & BDRV_O_NOCACHE));
  661. }
  662. if (!qemu_opt_get(all_opts, "cache.no-flush")) {
  663. qemu_opt_set_bool(all_opts, "cache.no-flush",
  664. !!(flags & BDRV_O_NO_FLUSH));
  665. }
  666. qemu_opt_unset(all_opts, "cache");
  667. }
  668. /* Get a QDict for processing the options */
  669. bs_opts = qdict_new();
  670. qemu_opts_to_qdict(all_opts, bs_opts);
  671. legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0,
  672. &error_abort);
  673. qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err);
  674. if (local_err) {
  675. error_report("%s", error_get_pretty(local_err));
  676. error_free(local_err);
  677. goto fail;
  678. }
  679. /* Deprecated option boot=[on|off] */
  680. if (qemu_opt_get(legacy_opts, "boot") != NULL) {
  681. fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
  682. "ignored. Future versions will reject this parameter. Please "
  683. "update your scripts.\n");
  684. }
  685. /* Media type */
  686. value = qemu_opt_get(legacy_opts, "media");
  687. if (value) {
  688. if (!strcmp(value, "disk")) {
  689. media = MEDIA_DISK;
  690. } else if (!strcmp(value, "cdrom")) {
  691. media = MEDIA_CDROM;
  692. read_only = true;
  693. } else {
  694. error_report("'%s' invalid media", value);
  695. goto fail;
  696. }
  697. }
  698. /* copy-on-read is disabled with a warning for read-only devices */
  699. read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false);
  700. copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
  701. if (read_only && copy_on_read) {
  702. error_report("warning: disabling copy-on-read on read-only drive");
  703. copy_on_read = false;
  704. }
  705. qdict_put(bs_opts, "read-only",
  706. qstring_from_str(read_only ? "on" : "off"));
  707. qdict_put(bs_opts, "copy-on-read",
  708. qstring_from_str(copy_on_read ? "on" :"off"));
  709. /* Controller type */
  710. value = qemu_opt_get(legacy_opts, "if");
  711. if (value) {
  712. for (type = 0;
  713. type < IF_COUNT && strcmp(value, if_name[type]);
  714. type++) {
  715. }
  716. if (type == IF_COUNT) {
  717. error_report("unsupported bus type '%s'", value);
  718. goto fail;
  719. }
  720. } else {
  721. type = block_default_type;
  722. }
  723. /* Geometry */
  724. cyls = qemu_opt_get_number(legacy_opts, "cyls", 0);
  725. heads = qemu_opt_get_number(legacy_opts, "heads", 0);
  726. secs = qemu_opt_get_number(legacy_opts, "secs", 0);
  727. if (cyls || heads || secs) {
  728. if (cyls < 1) {
  729. error_report("invalid physical cyls number");
  730. goto fail;
  731. }
  732. if (heads < 1) {
  733. error_report("invalid physical heads number");
  734. goto fail;
  735. }
  736. if (secs < 1) {
  737. error_report("invalid physical secs number");
  738. goto fail;
  739. }
  740. }
  741. translation = BIOS_ATA_TRANSLATION_AUTO;
  742. value = qemu_opt_get(legacy_opts, "trans");
  743. if (value != NULL) {
  744. if (!cyls) {
  745. error_report("'%s' trans must be used with cyls, heads and secs",
  746. value);
  747. goto fail;
  748. }
  749. if (!strcmp(value, "none")) {
  750. translation = BIOS_ATA_TRANSLATION_NONE;
  751. } else if (!strcmp(value, "lba")) {
  752. translation = BIOS_ATA_TRANSLATION_LBA;
  753. } else if (!strcmp(value, "large")) {
  754. translation = BIOS_ATA_TRANSLATION_LARGE;
  755. } else if (!strcmp(value, "rechs")) {
  756. translation = BIOS_ATA_TRANSLATION_RECHS;
  757. } else if (!strcmp(value, "auto")) {
  758. translation = BIOS_ATA_TRANSLATION_AUTO;
  759. } else {
  760. error_report("'%s' invalid translation type", value);
  761. goto fail;
  762. }
  763. }
  764. if (media == MEDIA_CDROM) {
  765. if (cyls || secs || heads) {
  766. error_report("CHS can't be set with media=cdrom");
  767. goto fail;
  768. }
  769. }
  770. /* Device address specified by bus/unit or index.
  771. * If none was specified, try to find the first free one. */
  772. bus_id = qemu_opt_get_number(legacy_opts, "bus", 0);
  773. unit_id = qemu_opt_get_number(legacy_opts, "unit", -1);
  774. index = qemu_opt_get_number(legacy_opts, "index", -1);
  775. max_devs = if_max_devs[type];
  776. if (index != -1) {
  777. if (bus_id != 0 || unit_id != -1) {
  778. error_report("index cannot be used with bus and unit");
  779. goto fail;
  780. }
  781. bus_id = drive_index_to_bus_id(type, index);
  782. unit_id = drive_index_to_unit_id(type, index);
  783. }
  784. if (unit_id == -1) {
  785. unit_id = 0;
  786. while (drive_get(type, bus_id, unit_id) != NULL) {
  787. unit_id++;
  788. if (max_devs && unit_id >= max_devs) {
  789. unit_id -= max_devs;
  790. bus_id++;
  791. }
  792. }
  793. }
  794. if (max_devs && unit_id >= max_devs) {
  795. error_report("unit %d too big (max is %d)", unit_id, max_devs - 1);
  796. goto fail;
  797. }
  798. if (drive_get(type, bus_id, unit_id) != NULL) {
  799. error_report("drive with bus=%d, unit=%d (index=%d) exists",
  800. bus_id, unit_id, index);
  801. goto fail;
  802. }
  803. /* Serial number */
  804. serial = qemu_opt_get(legacy_opts, "serial");
  805. /* no id supplied -> create one */
  806. if (qemu_opts_id(all_opts) == NULL) {
  807. char *new_id;
  808. const char *mediastr = "";
  809. if (type == IF_IDE || type == IF_SCSI) {
  810. mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
  811. }
  812. if (max_devs) {
  813. new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id,
  814. mediastr, unit_id);
  815. } else {
  816. new_id = g_strdup_printf("%s%s%i", if_name[type],
  817. mediastr, unit_id);
  818. }
  819. qdict_put(bs_opts, "id", qstring_from_str(new_id));
  820. g_free(new_id);
  821. }
  822. /* Add virtio block device */
  823. devaddr = qemu_opt_get(legacy_opts, "addr");
  824. if (devaddr && type != IF_VIRTIO) {
  825. error_report("addr is not supported by this bus type");
  826. goto fail;
  827. }
  828. if (type == IF_VIRTIO) {
  829. QemuOpts *devopts;
  830. devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
  831. &error_abort);
  832. if (arch_type == QEMU_ARCH_S390X) {
  833. qemu_opt_set(devopts, "driver", "virtio-blk-s390");
  834. } else {
  835. qemu_opt_set(devopts, "driver", "virtio-blk-pci");
  836. }
  837. qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"));
  838. if (devaddr) {
  839. qemu_opt_set(devopts, "addr", devaddr);
  840. }
  841. }
  842. filename = qemu_opt_get(legacy_opts, "file");
  843. /* Check werror/rerror compatibility with if=... */
  844. werror = qemu_opt_get(legacy_opts, "werror");
  845. if (werror != NULL) {
  846. if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO &&
  847. type != IF_NONE) {
  848. error_report("werror is not supported by this bus type");
  849. goto fail;
  850. }
  851. qdict_put(bs_opts, "werror", qstring_from_str(werror));
  852. }
  853. rerror = qemu_opt_get(legacy_opts, "rerror");
  854. if (rerror != NULL) {
  855. if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI &&
  856. type != IF_NONE) {
  857. error_report("rerror is not supported by this bus type");
  858. goto fail;
  859. }
  860. qdict_put(bs_opts, "rerror", qstring_from_str(rerror));
  861. }
  862. /* Actual block device init: Functionality shared with blockdev-add */
  863. blk = blockdev_init(filename, bs_opts, &local_err);
  864. bs_opts = NULL;
  865. if (!blk) {
  866. if (local_err) {
  867. error_report("%s", error_get_pretty(local_err));
  868. error_free(local_err);
  869. }
  870. goto fail;
  871. } else {
  872. assert(!local_err);
  873. }
  874. /* Create legacy DriveInfo */
  875. dinfo = g_malloc0(sizeof(*dinfo));
  876. dinfo->opts = all_opts;
  877. dinfo->cyls = cyls;
  878. dinfo->heads = heads;
  879. dinfo->secs = secs;
  880. dinfo->trans = translation;
  881. dinfo->type = type;
  882. dinfo->bus = bus_id;
  883. dinfo->unit = unit_id;
  884. dinfo->devaddr = devaddr;
  885. dinfo->serial = g_strdup(serial);
  886. blk_set_legacy_dinfo(blk, dinfo);
  887. switch(type) {
  888. case IF_IDE:
  889. case IF_SCSI:
  890. case IF_XEN:
  891. case IF_NONE:
  892. dinfo->media_cd = media == MEDIA_CDROM;
  893. break;
  894. default:
  895. break;
  896. }
  897. fail:
  898. qemu_opts_del(legacy_opts);
  899. QDECREF(bs_opts);
  900. return dinfo;
  901. }
  902. void do_commit(Monitor *mon, const QDict *qdict)
  903. {
  904. const char *device = qdict_get_str(qdict, "device");
  905. BlockDriverState *bs;
  906. int ret;
  907. if (!strcmp(device, "all")) {
  908. ret = bdrv_commit_all();
  909. } else {
  910. bs = bdrv_find(device);
  911. if (!bs) {
  912. monitor_printf(mon, "Device '%s' not found\n", device);
  913. return;
  914. }
  915. ret = bdrv_commit(bs);
  916. }
  917. if (ret < 0) {
  918. monitor_printf(mon, "'commit' error for '%s': %s\n", device,
  919. strerror(-ret));
  920. }
  921. }
  922. static void blockdev_do_action(int kind, void *data, Error **errp)
  923. {
  924. TransactionAction action;
  925. TransactionActionList list;
  926. action.kind = kind;
  927. action.data = data;
  928. list.value = &action;
  929. list.next = NULL;
  930. qmp_transaction(&list, errp);
  931. }
  932. void qmp_blockdev_snapshot_sync(bool has_device, const char *device,
  933. bool has_node_name, const char *node_name,
  934. const char *snapshot_file,
  935. bool has_snapshot_node_name,
  936. const char *snapshot_node_name,
  937. bool has_format, const char *format,
  938. bool has_mode, NewImageMode mode, Error **errp)
  939. {
  940. BlockdevSnapshot snapshot = {
  941. .has_device = has_device,
  942. .device = (char *) device,
  943. .has_node_name = has_node_name,
  944. .node_name = (char *) node_name,
  945. .snapshot_file = (char *) snapshot_file,
  946. .has_snapshot_node_name = has_snapshot_node_name,
  947. .snapshot_node_name = (char *) snapshot_node_name,
  948. .has_format = has_format,
  949. .format = (char *) format,
  950. .has_mode = has_mode,
  951. .mode = mode,
  952. };
  953. blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
  954. &snapshot, errp);
  955. }
  956. void qmp_blockdev_snapshot_internal_sync(const char *device,
  957. const char *name,
  958. Error **errp)
  959. {
  960. BlockdevSnapshotInternal snapshot = {
  961. .device = (char *) device,
  962. .name = (char *) name
  963. };
  964. blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC,
  965. &snapshot, errp);
  966. }
  967. SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,
  968. bool has_id,
  969. const char *id,
  970. bool has_name,
  971. const char *name,
  972. Error **errp)
  973. {
  974. BlockDriverState *bs = bdrv_find(device);
  975. QEMUSnapshotInfo sn;
  976. Error *local_err = NULL;
  977. SnapshotInfo *info = NULL;
  978. int ret;
  979. if (!bs) {
  980. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  981. return NULL;
  982. }
  983. if (!has_id) {
  984. id = NULL;
  985. }
  986. if (!has_name) {
  987. name = NULL;
  988. }
  989. if (!id && !name) {
  990. error_setg(errp, "Name or id must be provided");
  991. return NULL;
  992. }
  993. ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err);
  994. if (local_err) {
  995. error_propagate(errp, local_err);
  996. return NULL;
  997. }
  998. if (!ret) {
  999. error_setg(errp,
  1000. "Snapshot with id '%s' and name '%s' does not exist on "
  1001. "device '%s'",
  1002. STR_OR_NULL(id), STR_OR_NULL(name), device);
  1003. return NULL;
  1004. }
  1005. bdrv_snapshot_delete(bs, id, name, &local_err);
  1006. if (local_err) {
  1007. error_propagate(errp, local_err);
  1008. return NULL;
  1009. }
  1010. info = g_new0(SnapshotInfo, 1);
  1011. info->id = g_strdup(sn.id_str);
  1012. info->name = g_strdup(sn.name);
  1013. info->date_nsec = sn.date_nsec;
  1014. info->date_sec = sn.date_sec;
  1015. info->vm_state_size = sn.vm_state_size;
  1016. info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000;
  1017. info->vm_clock_sec = sn.vm_clock_nsec / 1000000000;
  1018. return info;
  1019. }
  1020. /* New and old BlockDriverState structs for group snapshots */
  1021. typedef struct BlkTransactionState BlkTransactionState;
  1022. /* Only prepare() may fail. In a single transaction, only one of commit() or
  1023. abort() will be called, clean() will always be called if it present. */
  1024. typedef struct BdrvActionOps {
  1025. /* Size of state struct, in bytes. */
  1026. size_t instance_size;
  1027. /* Prepare the work, must NOT be NULL. */
  1028. void (*prepare)(BlkTransactionState *common, Error **errp);
  1029. /* Commit the changes, can be NULL. */
  1030. void (*commit)(BlkTransactionState *common);
  1031. /* Abort the changes on fail, can be NULL. */
  1032. void (*abort)(BlkTransactionState *common);
  1033. /* Clean up resource in the end, can be NULL. */
  1034. void (*clean)(BlkTransactionState *common);
  1035. } BdrvActionOps;
  1036. /*
  1037. * This structure must be arranged as first member in child type, assuming
  1038. * that compiler will also arrange it to the same address with parent instance.
  1039. * Later it will be used in free().
  1040. */
  1041. struct BlkTransactionState {
  1042. TransactionAction *action;
  1043. const BdrvActionOps *ops;
  1044. QSIMPLEQ_ENTRY(BlkTransactionState) entry;
  1045. };
  1046. /* internal snapshot private data */
  1047. typedef struct InternalSnapshotState {
  1048. BlkTransactionState common;
  1049. BlockDriverState *bs;
  1050. QEMUSnapshotInfo sn;
  1051. } InternalSnapshotState;
  1052. static void internal_snapshot_prepare(BlkTransactionState *common,
  1053. Error **errp)
  1054. {
  1055. Error *local_err = NULL;
  1056. const char *device;
  1057. const char *name;
  1058. BlockDriverState *bs;
  1059. QEMUSnapshotInfo old_sn, *sn;
  1060. bool ret;
  1061. qemu_timeval tv;
  1062. BlockdevSnapshotInternal *internal;
  1063. InternalSnapshotState *state;
  1064. int ret1;
  1065. g_assert(common->action->kind ==
  1066. TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
  1067. internal = common->action->blockdev_snapshot_internal_sync;
  1068. state = DO_UPCAST(InternalSnapshotState, common, common);
  1069. /* 1. parse input */
  1070. device = internal->device;
  1071. name = internal->name;
  1072. /* 2. check for validation */
  1073. bs = bdrv_find(device);
  1074. if (!bs) {
  1075. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  1076. return;
  1077. }
  1078. if (!bdrv_is_inserted(bs)) {
  1079. error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
  1080. return;
  1081. }
  1082. if (bdrv_is_read_only(bs)) {
  1083. error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
  1084. return;
  1085. }
  1086. if (!bdrv_can_snapshot(bs)) {
  1087. error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
  1088. bs->drv->format_name, device, "internal snapshot");
  1089. return;
  1090. }
  1091. if (!strlen(name)) {
  1092. error_setg(errp, "Name is empty");
  1093. return;
  1094. }
  1095. /* check whether a snapshot with name exist */
  1096. ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn,
  1097. &local_err);
  1098. if (local_err) {
  1099. error_propagate(errp, local_err);
  1100. return;
  1101. } else if (ret) {
  1102. error_setg(errp,
  1103. "Snapshot with name '%s' already exists on device '%s'",
  1104. name, device);
  1105. return;
  1106. }
  1107. /* 3. take the snapshot */
  1108. sn = &state->sn;
  1109. pstrcpy(sn->name, sizeof(sn->name), name);
  1110. qemu_gettimeofday(&tv);
  1111. sn->date_sec = tv.tv_sec;
  1112. sn->date_nsec = tv.tv_usec * 1000;
  1113. sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  1114. ret1 = bdrv_snapshot_create(bs, sn);
  1115. if (ret1 < 0) {
  1116. error_setg_errno(errp, -ret1,
  1117. "Failed to create snapshot '%s' on device '%s'",
  1118. name, device);
  1119. return;
  1120. }
  1121. /* 4. succeed, mark a snapshot is created */
  1122. state->bs = bs;
  1123. }
  1124. static void internal_snapshot_abort(BlkTransactionState *common)
  1125. {
  1126. InternalSnapshotState *state =
  1127. DO_UPCAST(InternalSnapshotState, common, common);
  1128. BlockDriverState *bs = state->bs;
  1129. QEMUSnapshotInfo *sn = &state->sn;
  1130. Error *local_error = NULL;
  1131. if (!bs) {
  1132. return;
  1133. }
  1134. if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) {
  1135. error_report("Failed to delete snapshot with id '%s' and name '%s' on "
  1136. "device '%s' in abort: %s",
  1137. sn->id_str,
  1138. sn->name,
  1139. bdrv_get_device_name(bs),
  1140. error_get_pretty(local_error));
  1141. error_free(local_error);
  1142. }
  1143. }
  1144. /* external snapshot private data */
  1145. typedef struct ExternalSnapshotState {
  1146. BlkTransactionState common;
  1147. BlockDriverState *old_bs;
  1148. BlockDriverState *new_bs;
  1149. } ExternalSnapshotState;
  1150. static void external_snapshot_prepare(BlkTransactionState *common,
  1151. Error **errp)
  1152. {
  1153. BlockDriver *drv;
  1154. int flags, ret;
  1155. QDict *options = NULL;
  1156. Error *local_err = NULL;
  1157. bool has_device = false;
  1158. const char *device;
  1159. bool has_node_name = false;
  1160. const char *node_name;
  1161. bool has_snapshot_node_name = false;
  1162. const char *snapshot_node_name;
  1163. const char *new_image_file;
  1164. const char *format = "qcow2";
  1165. enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
  1166. ExternalSnapshotState *state =
  1167. DO_UPCAST(ExternalSnapshotState, common, common);
  1168. TransactionAction *action = common->action;
  1169. /* get parameters */
  1170. g_assert(action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC);
  1171. has_device = action->blockdev_snapshot_sync->has_device;
  1172. device = action->blockdev_snapshot_sync->device;
  1173. has_node_name = action->blockdev_snapshot_sync->has_node_name;
  1174. node_name = action->blockdev_snapshot_sync->node_name;
  1175. has_snapshot_node_name =
  1176. action->blockdev_snapshot_sync->has_snapshot_node_name;
  1177. snapshot_node_name = action->blockdev_snapshot_sync->snapshot_node_name;
  1178. new_image_file = action->blockdev_snapshot_sync->snapshot_file;
  1179. if (action->blockdev_snapshot_sync->has_format) {
  1180. format = action->blockdev_snapshot_sync->format;
  1181. }
  1182. if (action->blockdev_snapshot_sync->has_mode) {
  1183. mode = action->blockdev_snapshot_sync->mode;
  1184. }
  1185. /* start processing */
  1186. drv = bdrv_find_format(format);
  1187. if (!drv) {
  1188. error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
  1189. return;
  1190. }
  1191. state->old_bs = bdrv_lookup_bs(has_device ? device : NULL,
  1192. has_node_name ? node_name : NULL,
  1193. &local_err);
  1194. if (local_err) {
  1195. error_propagate(errp, local_err);
  1196. return;
  1197. }
  1198. if (has_node_name && !has_snapshot_node_name) {
  1199. error_setg(errp, "New snapshot node name missing");
  1200. return;
  1201. }
  1202. if (has_snapshot_node_name && bdrv_find_node(snapshot_node_name)) {
  1203. error_setg(errp, "New snapshot node name already existing");
  1204. return;
  1205. }
  1206. if (!bdrv_is_inserted(state->old_bs)) {
  1207. error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
  1208. return;
  1209. }
  1210. if (bdrv_op_is_blocked(state->old_bs,
  1211. BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) {
  1212. return;
  1213. }
  1214. if (!bdrv_is_read_only(state->old_bs)) {
  1215. if (bdrv_flush(state->old_bs)) {
  1216. error_set(errp, QERR_IO_ERROR);
  1217. return;
  1218. }
  1219. }
  1220. if (!bdrv_is_first_non_filter(state->old_bs)) {
  1221. error_set(errp, QERR_FEATURE_DISABLED, "snapshot");
  1222. return;
  1223. }
  1224. flags = state->old_bs->open_flags;
  1225. /* create new image w/backing file */
  1226. if (mode != NEW_IMAGE_MODE_EXISTING) {
  1227. bdrv_img_create(new_image_file, format,
  1228. state->old_bs->filename,
  1229. state->old_bs->drv->format_name,
  1230. NULL, -1, flags, &local_err, false);
  1231. if (local_err) {
  1232. error_propagate(errp, local_err);
  1233. return;
  1234. }
  1235. }
  1236. if (has_snapshot_node_name) {
  1237. options = qdict_new();
  1238. qdict_put(options, "node-name",
  1239. qstring_from_str(snapshot_node_name));
  1240. }
  1241. /* TODO Inherit bs->options or only take explicit options with an
  1242. * extended QMP command? */
  1243. assert(state->new_bs == NULL);
  1244. ret = bdrv_open(&state->new_bs, new_image_file, NULL, options,
  1245. flags | BDRV_O_NO_BACKING, drv, &local_err);
  1246. /* We will manually add the backing_hd field to the bs later */
  1247. if (ret != 0) {
  1248. error_propagate(errp, local_err);
  1249. }
  1250. }
  1251. static void external_snapshot_commit(BlkTransactionState *common)
  1252. {
  1253. ExternalSnapshotState *state =
  1254. DO_UPCAST(ExternalSnapshotState, common, common);
  1255. /* This removes our old bs and adds the new bs */
  1256. bdrv_append(state->new_bs, state->old_bs);
  1257. /* We don't need (or want) to use the transactional
  1258. * bdrv_reopen_multiple() across all the entries at once, because we
  1259. * don't want to abort all of them if one of them fails the reopen */
  1260. bdrv_reopen(state->new_bs, state->new_bs->open_flags & ~BDRV_O_RDWR,
  1261. NULL);
  1262. }
  1263. static void external_snapshot_abort(BlkTransactionState *common)
  1264. {
  1265. ExternalSnapshotState *state =
  1266. DO_UPCAST(ExternalSnapshotState, common, common);
  1267. if (state->new_bs) {
  1268. bdrv_unref(state->new_bs);
  1269. }
  1270. }
  1271. typedef struct DriveBackupState {
  1272. BlkTransactionState common;
  1273. BlockDriverState *bs;
  1274. BlockJob *job;
  1275. } DriveBackupState;
  1276. static void drive_backup_prepare(BlkTransactionState *common, Error **errp)
  1277. {
  1278. DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
  1279. DriveBackup *backup;
  1280. Error *local_err = NULL;
  1281. assert(common->action->kind == TRANSACTION_ACTION_KIND_DRIVE_BACKUP);
  1282. backup = common->action->drive_backup;
  1283. qmp_drive_backup(backup->device, backup->target,
  1284. backup->has_format, backup->format,
  1285. backup->sync,
  1286. backup->has_mode, backup->mode,
  1287. backup->has_speed, backup->speed,
  1288. backup->has_on_source_error, backup->on_source_error,
  1289. backup->has_on_target_error, backup->on_target_error,
  1290. &local_err);
  1291. if (local_err) {
  1292. error_propagate(errp, local_err);
  1293. state->bs = NULL;
  1294. state->job = NULL;
  1295. return;
  1296. }
  1297. state->bs = bdrv_find(backup->device);
  1298. state->job = state->bs->job;
  1299. }
  1300. static void drive_backup_abort(BlkTransactionState *common)
  1301. {
  1302. DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common);
  1303. BlockDriverState *bs = state->bs;
  1304. /* Only cancel if it's the job we started */
  1305. if (bs && bs->job && bs->job == state->job) {
  1306. block_job_cancel_sync(bs->job);
  1307. }
  1308. }
  1309. static void abort_prepare(BlkTransactionState *common, Error **errp)
  1310. {
  1311. error_setg(errp, "Transaction aborted using Abort action");
  1312. }
  1313. static void abort_commit(BlkTransactionState *common)
  1314. {
  1315. g_assert_not_reached(); /* this action never succeeds */
  1316. }
  1317. static const BdrvActionOps actions[] = {
  1318. [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = {
  1319. .instance_size = sizeof(ExternalSnapshotState),
  1320. .prepare = external_snapshot_prepare,
  1321. .commit = external_snapshot_commit,
  1322. .abort = external_snapshot_abort,
  1323. },
  1324. [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = {
  1325. .instance_size = sizeof(DriveBackupState),
  1326. .prepare = drive_backup_prepare,
  1327. .abort = drive_backup_abort,
  1328. },
  1329. [TRANSACTION_ACTION_KIND_ABORT] = {
  1330. .instance_size = sizeof(BlkTransactionState),
  1331. .prepare = abort_prepare,
  1332. .commit = abort_commit,
  1333. },
  1334. [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = {
  1335. .instance_size = sizeof(InternalSnapshotState),
  1336. .prepare = internal_snapshot_prepare,
  1337. .abort = internal_snapshot_abort,
  1338. },
  1339. };
  1340. /*
  1341. * 'Atomic' group snapshots. The snapshots are taken as a set, and if any fail
  1342. * then we do not pivot any of the devices in the group, and abandon the
  1343. * snapshots
  1344. */
  1345. void qmp_transaction(TransactionActionList *dev_list, Error **errp)
  1346. {
  1347. TransactionActionList *dev_entry = dev_list;
  1348. BlkTransactionState *state, *next;
  1349. Error *local_err = NULL;
  1350. QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionState) snap_bdrv_states;
  1351. QSIMPLEQ_INIT(&snap_bdrv_states);
  1352. /* drain all i/o before any snapshots */
  1353. bdrv_drain_all();
  1354. /* We don't do anything in this loop that commits us to the snapshot */
  1355. while (NULL != dev_entry) {
  1356. TransactionAction *dev_info = NULL;
  1357. const BdrvActionOps *ops;
  1358. dev_info = dev_entry->value;
  1359. dev_entry = dev_entry->next;
  1360. assert(dev_info->kind < ARRAY_SIZE(actions));
  1361. ops = &actions[dev_info->kind];
  1362. assert(ops->instance_size > 0);
  1363. state = g_malloc0(ops->instance_size);
  1364. state->ops = ops;
  1365. state->action = dev_info;
  1366. QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry);
  1367. state->ops->prepare(state, &local_err);
  1368. if (local_err) {
  1369. error_propagate(errp, local_err);
  1370. goto delete_and_fail;
  1371. }
  1372. }
  1373. QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
  1374. if (state->ops->commit) {
  1375. state->ops->commit(state);
  1376. }
  1377. }
  1378. /* success */
  1379. goto exit;
  1380. delete_and_fail:
  1381. /*
  1382. * failure, and it is all-or-none; abandon each new bs, and keep using
  1383. * the original bs for all images
  1384. */
  1385. QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) {
  1386. if (state->ops->abort) {
  1387. state->ops->abort(state);
  1388. }
  1389. }
  1390. exit:
  1391. QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) {
  1392. if (state->ops->clean) {
  1393. state->ops->clean(state);
  1394. }
  1395. g_free(state);
  1396. }
  1397. }
  1398. static void eject_device(BlockBackend *blk, int force, Error **errp)
  1399. {
  1400. BlockDriverState *bs = blk_bs(blk);
  1401. if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
  1402. return;
  1403. }
  1404. if (!blk_dev_has_removable_media(blk)) {
  1405. error_setg(errp, "Device '%s' is not removable",
  1406. bdrv_get_device_name(bs));
  1407. return;
  1408. }
  1409. if (blk_dev_is_medium_locked(blk) && !blk_dev_is_tray_open(blk)) {
  1410. blk_dev_eject_request(blk, force);
  1411. if (!force) {
  1412. error_setg(errp, "Device '%s' is locked",
  1413. bdrv_get_device_name(bs));
  1414. return;
  1415. }
  1416. }
  1417. bdrv_close(bs);
  1418. }
  1419. void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
  1420. {
  1421. BlockBackend *blk;
  1422. blk = blk_by_name(device);
  1423. if (!blk) {
  1424. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  1425. return;
  1426. }
  1427. eject_device(blk, force, errp);
  1428. }
  1429. void qmp_block_passwd(bool has_device, const char *device,
  1430. bool has_node_name, const char *node_name,
  1431. const char *password, Error **errp)
  1432. {
  1433. Error *local_err = NULL;
  1434. BlockDriverState *bs;
  1435. int err;
  1436. bs = bdrv_lookup_bs(has_device ? device : NULL,
  1437. has_node_name ? node_name : NULL,
  1438. &local_err);
  1439. if (local_err) {
  1440. error_propagate(errp, local_err);
  1441. return;
  1442. }
  1443. err = bdrv_set_key(bs, password);
  1444. if (err == -EINVAL) {
  1445. error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
  1446. return;
  1447. } else if (err < 0) {
  1448. error_set(errp, QERR_INVALID_PASSWORD);
  1449. return;
  1450. }
  1451. }
  1452. static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename,
  1453. int bdrv_flags, BlockDriver *drv,
  1454. const char *password, Error **errp)
  1455. {
  1456. Error *local_err = NULL;
  1457. int ret;
  1458. ret = bdrv_open(&bs, filename, NULL, NULL, bdrv_flags, drv, &local_err);
  1459. if (ret < 0) {
  1460. error_propagate(errp, local_err);
  1461. return;
  1462. }
  1463. if (bdrv_key_required(bs)) {
  1464. if (password) {
  1465. if (bdrv_set_key(bs, password) < 0) {
  1466. error_set(errp, QERR_INVALID_PASSWORD);
  1467. }
  1468. } else {
  1469. error_set(errp, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
  1470. bdrv_get_encrypted_filename(bs));
  1471. }
  1472. } else if (password) {
  1473. error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
  1474. }
  1475. }
  1476. void qmp_change_blockdev(const char *device, const char *filename,
  1477. const char *format, Error **errp)
  1478. {
  1479. BlockBackend *blk;
  1480. BlockDriverState *bs;
  1481. BlockDriver *drv = NULL;
  1482. int bdrv_flags;
  1483. Error *err = NULL;
  1484. blk = blk_by_name(device);
  1485. if (!blk) {
  1486. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  1487. return;
  1488. }
  1489. bs = blk_bs(blk);
  1490. if (format) {
  1491. drv = bdrv_find_whitelisted_format(format, bs->read_only);
  1492. if (!drv) {
  1493. error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
  1494. return;
  1495. }
  1496. }
  1497. eject_device(blk, 0, &err);
  1498. if (err) {
  1499. error_propagate(errp, err);
  1500. return;
  1501. }
  1502. bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
  1503. bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
  1504. qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp);
  1505. }
  1506. /* throttling disk I/O limits */
  1507. void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
  1508. int64_t bps_wr,
  1509. int64_t iops,
  1510. int64_t iops_rd,
  1511. int64_t iops_wr,
  1512. bool has_bps_max,
  1513. int64_t bps_max,
  1514. bool has_bps_rd_max,
  1515. int64_t bps_rd_max,
  1516. bool has_bps_wr_max,
  1517. int64_t bps_wr_max,
  1518. bool has_iops_max,
  1519. int64_t iops_max,
  1520. bool has_iops_rd_max,
  1521. int64_t iops_rd_max,
  1522. bool has_iops_wr_max,
  1523. int64_t iops_wr_max,
  1524. bool has_iops_size,
  1525. int64_t iops_size, Error **errp)
  1526. {
  1527. ThrottleConfig cfg;
  1528. BlockDriverState *bs;
  1529. AioContext *aio_context;
  1530. bs = bdrv_find(device);
  1531. if (!bs) {
  1532. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  1533. return;
  1534. }
  1535. memset(&cfg, 0, sizeof(cfg));
  1536. cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps;
  1537. cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd;
  1538. cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr;
  1539. cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops;
  1540. cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd;
  1541. cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr;
  1542. if (has_bps_max) {
  1543. cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max;
  1544. }
  1545. if (has_bps_rd_max) {
  1546. cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max;
  1547. }
  1548. if (has_bps_wr_max) {
  1549. cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max;
  1550. }
  1551. if (has_iops_max) {
  1552. cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max;
  1553. }
  1554. if (has_iops_rd_max) {
  1555. cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max;
  1556. }
  1557. if (has_iops_wr_max) {
  1558. cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max;
  1559. }
  1560. if (has_iops_size) {
  1561. cfg.op_size = iops_size;
  1562. }
  1563. if (!check_throttle_config(&cfg, errp)) {
  1564. return;
  1565. }
  1566. aio_context = bdrv_get_aio_context(bs);
  1567. aio_context_acquire(aio_context);
  1568. if (!bs->io_limits_enabled && throttle_enabled(&cfg)) {
  1569. bdrv_io_limits_enable(bs);
  1570. } else if (bs->io_limits_enabled && !throttle_enabled(&cfg)) {
  1571. bdrv_io_limits_disable(bs);
  1572. }
  1573. if (bs->io_limits_enabled) {
  1574. bdrv_set_io_limits(bs, &cfg);
  1575. }
  1576. aio_context_release(aio_context);
  1577. }
  1578. int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
  1579. {
  1580. const char *id = qdict_get_str(qdict, "id");
  1581. BlockBackend *blk;
  1582. BlockDriverState *bs;
  1583. AioContext *aio_context;
  1584. Error *local_err = NULL;
  1585. blk = blk_by_name(id);
  1586. if (!blk) {
  1587. error_report("Device '%s' not found", id);
  1588. return -1;
  1589. }
  1590. bs = blk_bs(blk);
  1591. if (!blk_legacy_dinfo(blk)) {
  1592. error_report("Deleting device added with blockdev-add"
  1593. " is not supported");
  1594. return -1;
  1595. }
  1596. aio_context = bdrv_get_aio_context(bs);
  1597. aio_context_acquire(aio_context);
  1598. if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
  1599. error_report("%s", error_get_pretty(local_err));
  1600. error_free(local_err);
  1601. aio_context_release(aio_context);
  1602. return -1;
  1603. }
  1604. /* quiesce block driver; prevent further io */
  1605. bdrv_drain_all();
  1606. bdrv_flush(bs);
  1607. bdrv_close(bs);
  1608. /* if we have a device attached to this BlockDriverState
  1609. * then we need to make the drive anonymous until the device
  1610. * can be removed. If this is a drive with no device backing
  1611. * then we can just get rid of the block driver state right here.
  1612. */
  1613. if (blk_get_attached_dev(blk)) {
  1614. blk_hide_on_behalf_of_do_drive_del(blk);
  1615. /* Further I/O must not pause the guest */
  1616. bdrv_set_on_error(bs, BLOCKDEV_ON_ERROR_REPORT,
  1617. BLOCKDEV_ON_ERROR_REPORT);
  1618. } else {
  1619. blk_unref(blk);
  1620. }
  1621. aio_context_release(aio_context);
  1622. return 0;
  1623. }
  1624. void qmp_block_resize(bool has_device, const char *device,
  1625. bool has_node_name, const char *node_name,
  1626. int64_t size, Error **errp)
  1627. {
  1628. Error *local_err = NULL;
  1629. BlockDriverState *bs;
  1630. AioContext *aio_context;
  1631. int ret;
  1632. bs = bdrv_lookup_bs(has_device ? device : NULL,
  1633. has_node_name ? node_name : NULL,
  1634. &local_err);
  1635. if (local_err) {
  1636. error_propagate(errp, local_err);
  1637. return;
  1638. }
  1639. aio_context = bdrv_get_aio_context(bs);
  1640. aio_context_acquire(aio_context);
  1641. if (!bdrv_is_first_non_filter(bs)) {
  1642. error_set(errp, QERR_FEATURE_DISABLED, "resize");
  1643. goto out;
  1644. }
  1645. if (size < 0) {
  1646. error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
  1647. goto out;
  1648. }
  1649. if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) {
  1650. error_set(errp, QERR_DEVICE_IN_USE, device);
  1651. goto out;
  1652. }
  1653. /* complete all in-flight operations before resizing the device */
  1654. bdrv_drain_all();
  1655. ret = bdrv_truncate(bs, size);
  1656. switch (ret) {
  1657. case 0:
  1658. break;
  1659. case -ENOMEDIUM:
  1660. error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
  1661. break;
  1662. case -ENOTSUP:
  1663. error_set(errp, QERR_UNSUPPORTED);
  1664. break;
  1665. case -EACCES:
  1666. error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
  1667. break;
  1668. case -EBUSY:
  1669. error_set(errp, QERR_DEVICE_IN_USE, device);
  1670. break;
  1671. default:
  1672. error_setg_errno(errp, -ret, "Could not resize");
  1673. break;
  1674. }
  1675. out:
  1676. aio_context_release(aio_context);
  1677. }
  1678. static void block_job_cb(void *opaque, int ret)
  1679. {
  1680. /* Note that this function may be executed from another AioContext besides
  1681. * the QEMU main loop. If you need to access anything that assumes the
  1682. * QEMU global mutex, use a BH or introduce a mutex.
  1683. */
  1684. BlockDriverState *bs = opaque;
  1685. const char *msg = NULL;
  1686. trace_block_job_cb(bs, bs->job, ret);
  1687. assert(bs->job);
  1688. if (ret < 0) {
  1689. msg = strerror(-ret);
  1690. }
  1691. if (block_job_is_cancelled(bs->job)) {
  1692. block_job_event_cancelled(bs->job);
  1693. } else {
  1694. block_job_event_completed(bs->job, msg);
  1695. }
  1696. bdrv_put_ref_bh_schedule(bs);
  1697. }
  1698. void qmp_block_stream(const char *device,
  1699. bool has_base, const char *base,
  1700. bool has_backing_file, const char *backing_file,
  1701. bool has_speed, int64_t speed,
  1702. bool has_on_error, BlockdevOnError on_error,
  1703. Error **errp)
  1704. {
  1705. BlockDriverState *bs;
  1706. BlockDriverState *base_bs = NULL;
  1707. AioContext *aio_context;
  1708. Error *local_err = NULL;
  1709. const char *base_name = NULL;
  1710. if (!has_on_error) {
  1711. on_error = BLOCKDEV_ON_ERROR_REPORT;
  1712. }
  1713. bs = bdrv_find(device);
  1714. if (!bs) {
  1715. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  1716. return;
  1717. }
  1718. aio_context = bdrv_get_aio_context(bs);
  1719. aio_context_acquire(aio_context);
  1720. if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) {
  1721. goto out;
  1722. }
  1723. if (has_base) {
  1724. base_bs = bdrv_find_backing_image(bs, base);
  1725. if (base_bs == NULL) {
  1726. error_set(errp, QERR_BASE_NOT_FOUND, base);
  1727. goto out;
  1728. }
  1729. assert(bdrv_get_aio_context(base_bs) == aio_context);
  1730. base_name = base;
  1731. }
  1732. /* if we are streaming the entire chain, the result will have no backing
  1733. * file, and specifying one is therefore an error */
  1734. if (base_bs == NULL && has_backing_file) {
  1735. error_setg(errp, "backing file specified, but streaming the "
  1736. "entire chain");
  1737. goto out;
  1738. }
  1739. /* backing_file string overrides base bs filename */
  1740. base_name = has_backing_file ? backing_file : base_name;
  1741. stream_start(bs, base_bs, base_name, has_speed ? speed : 0,
  1742. on_error, block_job_cb, bs, &local_err);
  1743. if (local_err) {
  1744. error_propagate(errp, local_err);
  1745. goto out;
  1746. }
  1747. trace_qmp_block_stream(bs, bs->job);
  1748. out:
  1749. aio_context_release(aio_context);
  1750. }
  1751. void qmp_block_commit(const char *device,
  1752. bool has_base, const char *base,
  1753. bool has_top, const char *top,
  1754. bool has_backing_file, const char *backing_file,
  1755. bool has_speed, int64_t speed,
  1756. Error **errp)
  1757. {
  1758. BlockDriverState *bs;
  1759. BlockDriverState *base_bs, *top_bs;
  1760. AioContext *aio_context;
  1761. Error *local_err = NULL;
  1762. /* This will be part of the QMP command, if/when the
  1763. * BlockdevOnError change for blkmirror makes it in
  1764. */
  1765. BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
  1766. if (!has_speed) {
  1767. speed = 0;
  1768. }
  1769. /* Important Note:
  1770. * libvirt relies on the DeviceNotFound error class in order to probe for
  1771. * live commit feature versions; for this to work, we must make sure to
  1772. * perform the device lookup before any generic errors that may occur in a
  1773. * scenario in which all optional arguments are omitted. */
  1774. bs = bdrv_find(device);
  1775. if (!bs) {
  1776. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  1777. return;
  1778. }
  1779. aio_context = bdrv_get_aio_context(bs);
  1780. aio_context_acquire(aio_context);
  1781. /* drain all i/o before commits */
  1782. bdrv_drain_all();
  1783. if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT, errp)) {
  1784. goto out;
  1785. }
  1786. /* default top_bs is the active layer */
  1787. top_bs = bs;
  1788. if (has_top && top) {
  1789. if (strcmp(bs->filename, top) != 0) {
  1790. top_bs = bdrv_find_backing_image(bs, top);
  1791. }
  1792. }
  1793. if (top_bs == NULL) {
  1794. error_setg(errp, "Top image file %s not found", top ? top : "NULL");
  1795. goto out;
  1796. }
  1797. assert(bdrv_get_aio_context(top_bs) == aio_context);
  1798. if (has_base && base) {
  1799. base_bs = bdrv_find_backing_image(top_bs, base);
  1800. } else {
  1801. base_bs = bdrv_find_base(top_bs);
  1802. }
  1803. if (base_bs == NULL) {
  1804. error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
  1805. goto out;
  1806. }
  1807. assert(bdrv_get_aio_context(base_bs) == aio_context);
  1808. /* Do not allow attempts to commit an image into itself */
  1809. if (top_bs == base_bs) {
  1810. error_setg(errp, "cannot commit an image into itself");
  1811. goto out;
  1812. }
  1813. if (top_bs == bs) {
  1814. if (has_backing_file) {
  1815. error_setg(errp, "'backing-file' specified,"
  1816. " but 'top' is the active layer");
  1817. goto out;
  1818. }
  1819. commit_active_start(bs, base_bs, speed, on_error, block_job_cb,
  1820. bs, &local_err);
  1821. } else {
  1822. commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
  1823. has_backing_file ? backing_file : NULL, &local_err);
  1824. }
  1825. if (local_err != NULL) {
  1826. error_propagate(errp, local_err);
  1827. goto out;
  1828. }
  1829. out:
  1830. aio_context_release(aio_context);
  1831. }
  1832. void qmp_drive_backup(const char *device, const char *target,
  1833. bool has_format, const char *format,
  1834. enum MirrorSyncMode sync,
  1835. bool has_mode, enum NewImageMode mode,
  1836. bool has_speed, int64_t speed,
  1837. bool has_on_source_error, BlockdevOnError on_source_error,
  1838. bool has_on_target_error, BlockdevOnError on_target_error,
  1839. Error **errp)
  1840. {
  1841. BlockDriverState *bs;
  1842. BlockDriverState *target_bs;
  1843. BlockDriverState *source = NULL;
  1844. AioContext *aio_context;
  1845. BlockDriver *drv = NULL;
  1846. Error *local_err = NULL;
  1847. int flags;
  1848. int64_t size;
  1849. int ret;
  1850. if (!has_speed) {
  1851. speed = 0;
  1852. }
  1853. if (!has_on_source_error) {
  1854. on_source_error = BLOCKDEV_ON_ERROR_REPORT;
  1855. }
  1856. if (!has_on_target_error) {
  1857. on_target_error = BLOCKDEV_ON_ERROR_REPORT;
  1858. }
  1859. if (!has_mode) {
  1860. mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
  1861. }
  1862. bs = bdrv_find(device);
  1863. if (!bs) {
  1864. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  1865. return;
  1866. }
  1867. aio_context = bdrv_get_aio_context(bs);
  1868. aio_context_acquire(aio_context);
  1869. if (!bdrv_is_inserted(bs)) {
  1870. error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
  1871. goto out;
  1872. }
  1873. if (!has_format) {
  1874. format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
  1875. }
  1876. if (format) {
  1877. drv = bdrv_find_format(format);
  1878. if (!drv) {
  1879. error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
  1880. goto out;
  1881. }
  1882. }
  1883. if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
  1884. goto out;
  1885. }
  1886. flags = bs->open_flags | BDRV_O_RDWR;
  1887. /* See if we have a backing HD we can use to create our new image
  1888. * on top of. */
  1889. if (sync == MIRROR_SYNC_MODE_TOP) {
  1890. source = bs->backing_hd;
  1891. if (!source) {
  1892. sync = MIRROR_SYNC_MODE_FULL;
  1893. }
  1894. }
  1895. if (sync == MIRROR_SYNC_MODE_NONE) {
  1896. source = bs;
  1897. }
  1898. size = bdrv_getlength(bs);
  1899. if (size < 0) {
  1900. error_setg_errno(errp, -size, "bdrv_getlength failed");
  1901. goto out;
  1902. }
  1903. if (mode != NEW_IMAGE_MODE_EXISTING) {
  1904. assert(format && drv);
  1905. if (source) {
  1906. bdrv_img_create(target, format, source->filename,
  1907. source->drv->format_name, NULL,
  1908. size, flags, &local_err, false);
  1909. } else {
  1910. bdrv_img_create(target, format, NULL, NULL, NULL,
  1911. size, flags, &local_err, false);
  1912. }
  1913. }
  1914. if (local_err) {
  1915. error_propagate(errp, local_err);
  1916. goto out;
  1917. }
  1918. target_bs = NULL;
  1919. ret = bdrv_open(&target_bs, target, NULL, NULL, flags, drv, &local_err);
  1920. if (ret < 0) {
  1921. error_propagate(errp, local_err);
  1922. goto out;
  1923. }
  1924. bdrv_set_aio_context(target_bs, aio_context);
  1925. backup_start(bs, target_bs, speed, sync, on_source_error, on_target_error,
  1926. block_job_cb, bs, &local_err);
  1927. if (local_err != NULL) {
  1928. bdrv_unref(target_bs);
  1929. error_propagate(errp, local_err);
  1930. goto out;
  1931. }
  1932. out:
  1933. aio_context_release(aio_context);
  1934. }
  1935. BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp)
  1936. {
  1937. return bdrv_named_nodes_list();
  1938. }
  1939. #define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
  1940. void qmp_drive_mirror(const char *device, const char *target,
  1941. bool has_format, const char *format,
  1942. bool has_node_name, const char *node_name,
  1943. bool has_replaces, const char *replaces,
  1944. enum MirrorSyncMode sync,
  1945. bool has_mode, enum NewImageMode mode,
  1946. bool has_speed, int64_t speed,
  1947. bool has_granularity, uint32_t granularity,
  1948. bool has_buf_size, int64_t buf_size,
  1949. bool has_on_source_error, BlockdevOnError on_source_error,
  1950. bool has_on_target_error, BlockdevOnError on_target_error,
  1951. Error **errp)
  1952. {
  1953. BlockDriverState *bs;
  1954. BlockDriverState *source, *target_bs;
  1955. AioContext *aio_context;
  1956. BlockDriver *drv = NULL;
  1957. Error *local_err = NULL;
  1958. QDict *options = NULL;
  1959. int flags;
  1960. int64_t size;
  1961. int ret;
  1962. if (!has_speed) {
  1963. speed = 0;
  1964. }
  1965. if (!has_on_source_error) {
  1966. on_source_error = BLOCKDEV_ON_ERROR_REPORT;
  1967. }
  1968. if (!has_on_target_error) {
  1969. on_target_error = BLOCKDEV_ON_ERROR_REPORT;
  1970. }
  1971. if (!has_mode) {
  1972. mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
  1973. }
  1974. if (!has_granularity) {
  1975. granularity = 0;
  1976. }
  1977. if (!has_buf_size) {
  1978. buf_size = DEFAULT_MIRROR_BUF_SIZE;
  1979. }
  1980. if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
  1981. error_set(errp, QERR_INVALID_PARAMETER_VALUE, "granularity",
  1982. "a value in range [512B, 64MB]");
  1983. return;
  1984. }
  1985. if (granularity & (granularity - 1)) {
  1986. error_set(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", "power of 2");
  1987. return;
  1988. }
  1989. bs = bdrv_find(device);
  1990. if (!bs) {
  1991. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  1992. return;
  1993. }
  1994. aio_context = bdrv_get_aio_context(bs);
  1995. aio_context_acquire(aio_context);
  1996. if (!bdrv_is_inserted(bs)) {
  1997. error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
  1998. goto out;
  1999. }
  2000. if (!has_format) {
  2001. format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
  2002. }
  2003. if (format) {
  2004. drv = bdrv_find_format(format);
  2005. if (!drv) {
  2006. error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
  2007. goto out;
  2008. }
  2009. }
  2010. if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR, errp)) {
  2011. goto out;
  2012. }
  2013. flags = bs->open_flags | BDRV_O_RDWR;
  2014. source = bs->backing_hd;
  2015. if (!source && sync == MIRROR_SYNC_MODE_TOP) {
  2016. sync = MIRROR_SYNC_MODE_FULL;
  2017. }
  2018. if (sync == MIRROR_SYNC_MODE_NONE) {
  2019. source = bs;
  2020. }
  2021. size = bdrv_getlength(bs);
  2022. if (size < 0) {
  2023. error_setg_errno(errp, -size, "bdrv_getlength failed");
  2024. goto out;
  2025. }
  2026. if (has_replaces) {
  2027. BlockDriverState *to_replace_bs;
  2028. AioContext *replace_aio_context;
  2029. int64_t replace_size;
  2030. if (!has_node_name) {
  2031. error_setg(errp, "a node-name must be provided when replacing a"
  2032. " named node of the graph");
  2033. goto out;
  2034. }
  2035. to_replace_bs = check_to_replace_node(replaces, &local_err);
  2036. if (!to_replace_bs) {
  2037. error_propagate(errp, local_err);
  2038. goto out;
  2039. }
  2040. replace_aio_context = bdrv_get_aio_context(to_replace_bs);
  2041. aio_context_acquire(replace_aio_context);
  2042. replace_size = bdrv_getlength(to_replace_bs);
  2043. aio_context_release(replace_aio_context);
  2044. if (size != replace_size) {
  2045. error_setg(errp, "cannot replace image with a mirror image of "
  2046. "different size");
  2047. goto out;
  2048. }
  2049. }
  2050. if ((sync == MIRROR_SYNC_MODE_FULL || !source)
  2051. && mode != NEW_IMAGE_MODE_EXISTING)
  2052. {
  2053. /* create new image w/o backing file */
  2054. assert(format && drv);
  2055. bdrv_img_create(target, format,
  2056. NULL, NULL, NULL, size, flags, &local_err, false);
  2057. } else {
  2058. switch (mode) {
  2059. case NEW_IMAGE_MODE_EXISTING:
  2060. break;
  2061. case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
  2062. /* create new image with backing file */
  2063. bdrv_img_create(target, format,
  2064. source->filename,
  2065. source->drv->format_name,
  2066. NULL, size, flags, &local_err, false);
  2067. break;
  2068. default:
  2069. abort();
  2070. }
  2071. }
  2072. if (local_err) {
  2073. error_propagate(errp, local_err);
  2074. goto out;
  2075. }
  2076. if (has_node_name) {
  2077. options = qdict_new();
  2078. qdict_put(options, "node-name", qstring_from_str(node_name));
  2079. }
  2080. /* Mirroring takes care of copy-on-write using the source's backing
  2081. * file.
  2082. */
  2083. target_bs = NULL;
  2084. ret = bdrv_open(&target_bs, target, NULL, options,
  2085. flags | BDRV_O_NO_BACKING, drv, &local_err);
  2086. if (ret < 0) {
  2087. error_propagate(errp, local_err);
  2088. goto out;
  2089. }
  2090. bdrv_set_aio_context(target_bs, aio_context);
  2091. /* pass the node name to replace to mirror start since it's loose coupling
  2092. * and will allow to check whether the node still exist at mirror completion
  2093. */
  2094. mirror_start(bs, target_bs,
  2095. has_replaces ? replaces : NULL,
  2096. speed, granularity, buf_size, sync,
  2097. on_source_error, on_target_error,
  2098. block_job_cb, bs, &local_err);
  2099. if (local_err != NULL) {
  2100. bdrv_unref(target_bs);
  2101. error_propagate(errp, local_err);
  2102. goto out;
  2103. }
  2104. out:
  2105. aio_context_release(aio_context);
  2106. }
  2107. /* Get the block job for a given device name and acquire its AioContext */
  2108. static BlockJob *find_block_job(const char *device, AioContext **aio_context)
  2109. {
  2110. BlockDriverState *bs;
  2111. bs = bdrv_find(device);
  2112. if (!bs) {
  2113. goto notfound;
  2114. }
  2115. *aio_context = bdrv_get_aio_context(bs);
  2116. aio_context_acquire(*aio_context);
  2117. if (!bs->job) {
  2118. aio_context_release(*aio_context);
  2119. goto notfound;
  2120. }
  2121. return bs->job;
  2122. notfound:
  2123. *aio_context = NULL;
  2124. return NULL;
  2125. }
  2126. void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
  2127. {
  2128. AioContext *aio_context;
  2129. BlockJob *job = find_block_job(device, &aio_context);
  2130. if (!job) {
  2131. error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
  2132. return;
  2133. }
  2134. block_job_set_speed(job, speed, errp);
  2135. aio_context_release(aio_context);
  2136. }
  2137. void qmp_block_job_cancel(const char *device,
  2138. bool has_force, bool force, Error **errp)
  2139. {
  2140. AioContext *aio_context;
  2141. BlockJob *job = find_block_job(device, &aio_context);
  2142. if (!job) {
  2143. error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
  2144. return;
  2145. }
  2146. if (!has_force) {
  2147. force = false;
  2148. }
  2149. if (job->paused && !force) {
  2150. error_setg(errp, "The block job for device '%s' is currently paused",
  2151. device);
  2152. goto out;
  2153. }
  2154. trace_qmp_block_job_cancel(job);
  2155. block_job_cancel(job);
  2156. out:
  2157. aio_context_release(aio_context);
  2158. }
  2159. void qmp_block_job_pause(const char *device, Error **errp)
  2160. {
  2161. AioContext *aio_context;
  2162. BlockJob *job = find_block_job(device, &aio_context);
  2163. if (!job) {
  2164. error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
  2165. return;
  2166. }
  2167. trace_qmp_block_job_pause(job);
  2168. block_job_pause(job);
  2169. aio_context_release(aio_context);
  2170. }
  2171. void qmp_block_job_resume(const char *device, Error **errp)
  2172. {
  2173. AioContext *aio_context;
  2174. BlockJob *job = find_block_job(device, &aio_context);
  2175. if (!job) {
  2176. error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
  2177. return;
  2178. }
  2179. trace_qmp_block_job_resume(job);
  2180. block_job_resume(job);
  2181. aio_context_release(aio_context);
  2182. }
  2183. void qmp_block_job_complete(const char *device, Error **errp)
  2184. {
  2185. AioContext *aio_context;
  2186. BlockJob *job = find_block_job(device, &aio_context);
  2187. if (!job) {
  2188. error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
  2189. return;
  2190. }
  2191. trace_qmp_block_job_complete(job);
  2192. block_job_complete(job, errp);
  2193. aio_context_release(aio_context);
  2194. }
  2195. void qmp_change_backing_file(const char *device,
  2196. const char *image_node_name,
  2197. const char *backing_file,
  2198. Error **errp)
  2199. {
  2200. BlockDriverState *bs = NULL;
  2201. BlockDriverState *image_bs = NULL;
  2202. Error *local_err = NULL;
  2203. bool ro;
  2204. int open_flags;
  2205. int ret;
  2206. /* find the top layer BDS of the chain */
  2207. bs = bdrv_find(device);
  2208. if (!bs) {
  2209. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  2210. return;
  2211. }
  2212. image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err);
  2213. if (local_err) {
  2214. error_propagate(errp, local_err);
  2215. return;
  2216. }
  2217. if (!image_bs) {
  2218. error_setg(errp, "image file not found");
  2219. return;
  2220. }
  2221. if (bdrv_find_base(image_bs) == image_bs) {
  2222. error_setg(errp, "not allowing backing file change on an image "
  2223. "without a backing file");
  2224. return;
  2225. }
  2226. /* even though we are not necessarily operating on bs, we need it to
  2227. * determine if block ops are currently prohibited on the chain */
  2228. if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) {
  2229. return;
  2230. }
  2231. /* final sanity check */
  2232. if (!bdrv_chain_contains(bs, image_bs)) {
  2233. error_setg(errp, "'%s' and image file are not in the same chain",
  2234. device);
  2235. return;
  2236. }
  2237. /* if not r/w, reopen to make r/w */
  2238. open_flags = image_bs->open_flags;
  2239. ro = bdrv_is_read_only(image_bs);
  2240. if (ro) {
  2241. bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err);
  2242. if (local_err) {
  2243. error_propagate(errp, local_err);
  2244. return;
  2245. }
  2246. }
  2247. ret = bdrv_change_backing_file(image_bs, backing_file,
  2248. image_bs->drv ? image_bs->drv->format_name : "");
  2249. if (ret < 0) {
  2250. error_setg_errno(errp, -ret, "Could not change backing file to '%s'",
  2251. backing_file);
  2252. /* don't exit here, so we can try to restore open flags if
  2253. * appropriate */
  2254. }
  2255. if (ro) {
  2256. bdrv_reopen(image_bs, open_flags, &local_err);
  2257. if (local_err) {
  2258. error_propagate(errp, local_err); /* will preserve prior errp */
  2259. }
  2260. }
  2261. }
  2262. void qmp_blockdev_add(BlockdevOptions *options, Error **errp)
  2263. {
  2264. QmpOutputVisitor *ov = qmp_output_visitor_new();
  2265. BlockBackend *blk;
  2266. QObject *obj;
  2267. QDict *qdict;
  2268. Error *local_err = NULL;
  2269. /* Require an ID in the top level */
  2270. if (!options->has_id) {
  2271. error_setg(errp, "Block device needs an ID");
  2272. goto fail;
  2273. }
  2274. /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
  2275. * cache.direct=false instead of silently switching to aio=threads, except
  2276. * when called from drive_new().
  2277. *
  2278. * For now, simply forbidding the combination for all drivers will do. */
  2279. if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) {
  2280. bool direct = options->has_cache &&
  2281. options->cache->has_direct &&
  2282. options->cache->direct;
  2283. if (!direct) {
  2284. error_setg(errp, "aio=native requires cache.direct=true");
  2285. goto fail;
  2286. }
  2287. }
  2288. visit_type_BlockdevOptions(qmp_output_get_visitor(ov),
  2289. &options, NULL, &local_err);
  2290. if (local_err) {
  2291. error_propagate(errp, local_err);
  2292. goto fail;
  2293. }
  2294. obj = qmp_output_get_qobject(ov);
  2295. qdict = qobject_to_qdict(obj);
  2296. qdict_flatten(qdict);
  2297. blk = blockdev_init(NULL, qdict, &local_err);
  2298. if (local_err) {
  2299. error_propagate(errp, local_err);
  2300. goto fail;
  2301. }
  2302. if (bdrv_key_required(blk_bs(blk))) {
  2303. blk_unref(blk);
  2304. error_setg(errp, "blockdev-add doesn't support encrypted devices");
  2305. goto fail;
  2306. }
  2307. fail:
  2308. qmp_output_visitor_cleanup(ov);
  2309. }
  2310. BlockJobInfoList *qmp_query_block_jobs(Error **errp)
  2311. {
  2312. BlockJobInfoList *head = NULL, **p_next = &head;
  2313. BlockDriverState *bs;
  2314. for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
  2315. AioContext *aio_context = bdrv_get_aio_context(bs);
  2316. aio_context_acquire(aio_context);
  2317. if (bs->job) {
  2318. BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
  2319. elem->value = block_job_query(bs->job);
  2320. *p_next = elem;
  2321. p_next = &elem->next;
  2322. }
  2323. aio_context_release(aio_context);
  2324. }
  2325. return head;
  2326. }
  2327. QemuOptsList qemu_common_drive_opts = {
  2328. .name = "drive",
  2329. .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head),
  2330. .desc = {
  2331. {
  2332. .name = "snapshot",
  2333. .type = QEMU_OPT_BOOL,
  2334. .help = "enable/disable snapshot mode",
  2335. },{
  2336. .name = "discard",
  2337. .type = QEMU_OPT_STRING,
  2338. .help = "discard operation (ignore/off, unmap/on)",
  2339. },{
  2340. .name = "cache.writeback",
  2341. .type = QEMU_OPT_BOOL,
  2342. .help = "enables writeback mode for any caches",
  2343. },{
  2344. .name = "cache.direct",
  2345. .type = QEMU_OPT_BOOL,
  2346. .help = "enables use of O_DIRECT (bypass the host page cache)",
  2347. },{
  2348. .name = "cache.no-flush",
  2349. .type = QEMU_OPT_BOOL,
  2350. .help = "ignore any flush requests for the device",
  2351. },{
  2352. .name = "aio",
  2353. .type = QEMU_OPT_STRING,
  2354. .help = "host AIO implementation (threads, native)",
  2355. },{
  2356. .name = "format",
  2357. .type = QEMU_OPT_STRING,
  2358. .help = "disk format (raw, qcow2, ...)",
  2359. },{
  2360. .name = "rerror",
  2361. .type = QEMU_OPT_STRING,
  2362. .help = "read error action",
  2363. },{
  2364. .name = "werror",
  2365. .type = QEMU_OPT_STRING,
  2366. .help = "write error action",
  2367. },{
  2368. .name = "read-only",
  2369. .type = QEMU_OPT_BOOL,
  2370. .help = "open drive file as read-only",
  2371. },{
  2372. .name = "throttling.iops-total",
  2373. .type = QEMU_OPT_NUMBER,
  2374. .help = "limit total I/O operations per second",
  2375. },{
  2376. .name = "throttling.iops-read",
  2377. .type = QEMU_OPT_NUMBER,
  2378. .help = "limit read operations per second",
  2379. },{
  2380. .name = "throttling.iops-write",
  2381. .type = QEMU_OPT_NUMBER,
  2382. .help = "limit write operations per second",
  2383. },{
  2384. .name = "throttling.bps-total",
  2385. .type = QEMU_OPT_NUMBER,
  2386. .help = "limit total bytes per second",
  2387. },{
  2388. .name = "throttling.bps-read",
  2389. .type = QEMU_OPT_NUMBER,
  2390. .help = "limit read bytes per second",
  2391. },{
  2392. .name = "throttling.bps-write",
  2393. .type = QEMU_OPT_NUMBER,
  2394. .help = "limit write bytes per second",
  2395. },{
  2396. .name = "throttling.iops-total-max",
  2397. .type = QEMU_OPT_NUMBER,
  2398. .help = "I/O operations burst",
  2399. },{
  2400. .name = "throttling.iops-read-max",
  2401. .type = QEMU_OPT_NUMBER,
  2402. .help = "I/O operations read burst",
  2403. },{
  2404. .name = "throttling.iops-write-max",
  2405. .type = QEMU_OPT_NUMBER,
  2406. .help = "I/O operations write burst",
  2407. },{
  2408. .name = "throttling.bps-total-max",
  2409. .type = QEMU_OPT_NUMBER,
  2410. .help = "total bytes burst",
  2411. },{
  2412. .name = "throttling.bps-read-max",
  2413. .type = QEMU_OPT_NUMBER,
  2414. .help = "total bytes read burst",
  2415. },{
  2416. .name = "throttling.bps-write-max",
  2417. .type = QEMU_OPT_NUMBER,
  2418. .help = "total bytes write burst",
  2419. },{
  2420. .name = "throttling.iops-size",
  2421. .type = QEMU_OPT_NUMBER,
  2422. .help = "when limiting by iops max size of an I/O in bytes",
  2423. },{
  2424. .name = "copy-on-read",
  2425. .type = QEMU_OPT_BOOL,
  2426. .help = "copy read data from backing file into image file",
  2427. },{
  2428. .name = "detect-zeroes",
  2429. .type = QEMU_OPT_STRING,
  2430. .help = "try to optimize zero writes (off, on, unmap)",
  2431. },
  2432. { /* end of list */ }
  2433. },
  2434. };
  2435. QemuOptsList qemu_drive_opts = {
  2436. .name = "drive",
  2437. .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
  2438. .desc = {
  2439. /*
  2440. * no elements => accept any params
  2441. * validation will happen later
  2442. */
  2443. { /* end of list */ }
  2444. },
  2445. };