blockdev.c 77 KB

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