blockdev.c 71 KB

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