blockdev.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562
  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. #include "sysemu/blockdev.h"
  10. #include "hw/block-common.h"
  11. #include "block/blockjob.h"
  12. #include "monitor/monitor.h"
  13. #include "qapi/qmp/qerror.h"
  14. #include "qemu/option.h"
  15. #include "qemu/config-file.h"
  16. #include "qapi/qmp/types.h"
  17. #include "sysemu/sysemu.h"
  18. #include "block/block_int.h"
  19. #include "qmp-commands.h"
  20. #include "trace.h"
  21. #include "sysemu/arch_init.h"
  22. static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
  23. static const char *const if_name[IF_COUNT] = {
  24. [IF_NONE] = "none",
  25. [IF_IDE] = "ide",
  26. [IF_SCSI] = "scsi",
  27. [IF_FLOPPY] = "floppy",
  28. [IF_PFLASH] = "pflash",
  29. [IF_MTD] = "mtd",
  30. [IF_SD] = "sd",
  31. [IF_VIRTIO] = "virtio",
  32. [IF_XEN] = "xen",
  33. };
  34. static const int if_max_devs[IF_COUNT] = {
  35. /*
  36. * Do not change these numbers! They govern how drive option
  37. * index maps to unit and bus. That mapping is ABI.
  38. *
  39. * All controllers used to imlement if=T drives need to support
  40. * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
  41. * Otherwise, some index values map to "impossible" bus, unit
  42. * values.
  43. *
  44. * For instance, if you change [IF_SCSI] to 255, -drive
  45. * if=scsi,index=12 no longer means bus=1,unit=5, but
  46. * bus=0,unit=12. With an lsi53c895a controller (7 units max),
  47. * the drive can't be set up. Regression.
  48. */
  49. [IF_IDE] = 2,
  50. [IF_SCSI] = 7,
  51. };
  52. /*
  53. * We automatically delete the drive when a device using it gets
  54. * unplugged. Questionable feature, but we can't just drop it.
  55. * Device models call blockdev_mark_auto_del() to schedule the
  56. * automatic deletion, and generic qdev code calls blockdev_auto_del()
  57. * when deletion is actually safe.
  58. */
  59. void blockdev_mark_auto_del(BlockDriverState *bs)
  60. {
  61. DriveInfo *dinfo = drive_get_by_blockdev(bs);
  62. if (bs->job) {
  63. block_job_cancel(bs->job);
  64. }
  65. if (dinfo) {
  66. dinfo->auto_del = 1;
  67. }
  68. }
  69. void blockdev_auto_del(BlockDriverState *bs)
  70. {
  71. DriveInfo *dinfo = drive_get_by_blockdev(bs);
  72. if (dinfo && dinfo->auto_del) {
  73. drive_put_ref(dinfo);
  74. }
  75. }
  76. static int drive_index_to_bus_id(BlockInterfaceType type, int index)
  77. {
  78. int max_devs = if_max_devs[type];
  79. return max_devs ? index / max_devs : 0;
  80. }
  81. static int drive_index_to_unit_id(BlockInterfaceType type, int index)
  82. {
  83. int max_devs = if_max_devs[type];
  84. return max_devs ? index % max_devs : index;
  85. }
  86. QemuOpts *drive_def(const char *optstr)
  87. {
  88. return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
  89. }
  90. QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
  91. const char *optstr)
  92. {
  93. QemuOpts *opts;
  94. char buf[32];
  95. opts = drive_def(optstr);
  96. if (!opts) {
  97. return NULL;
  98. }
  99. if (type != IF_DEFAULT) {
  100. qemu_opt_set(opts, "if", if_name[type]);
  101. }
  102. if (index >= 0) {
  103. snprintf(buf, sizeof(buf), "%d", index);
  104. qemu_opt_set(opts, "index", buf);
  105. }
  106. if (file)
  107. qemu_opt_set(opts, "file", file);
  108. return opts;
  109. }
  110. DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
  111. {
  112. DriveInfo *dinfo;
  113. /* seek interface, bus and unit */
  114. QTAILQ_FOREACH(dinfo, &drives, next) {
  115. if (dinfo->type == type &&
  116. dinfo->bus == bus &&
  117. dinfo->unit == unit)
  118. return dinfo;
  119. }
  120. return NULL;
  121. }
  122. DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
  123. {
  124. return drive_get(type,
  125. drive_index_to_bus_id(type, index),
  126. drive_index_to_unit_id(type, index));
  127. }
  128. int drive_get_max_bus(BlockInterfaceType type)
  129. {
  130. int max_bus;
  131. DriveInfo *dinfo;
  132. max_bus = -1;
  133. QTAILQ_FOREACH(dinfo, &drives, next) {
  134. if(dinfo->type == type &&
  135. dinfo->bus > max_bus)
  136. max_bus = dinfo->bus;
  137. }
  138. return max_bus;
  139. }
  140. /* Get a block device. This should only be used for single-drive devices
  141. (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
  142. appropriate bus. */
  143. DriveInfo *drive_get_next(BlockInterfaceType type)
  144. {
  145. static int next_block_unit[IF_COUNT];
  146. return drive_get(type, 0, next_block_unit[type]++);
  147. }
  148. DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
  149. {
  150. DriveInfo *dinfo;
  151. QTAILQ_FOREACH(dinfo, &drives, next) {
  152. if (dinfo->bdrv == bs) {
  153. return dinfo;
  154. }
  155. }
  156. return NULL;
  157. }
  158. static void bdrv_format_print(void *opaque, const char *name)
  159. {
  160. error_printf(" %s", name);
  161. }
  162. static void drive_uninit(DriveInfo *dinfo)
  163. {
  164. qemu_opts_del(dinfo->opts);
  165. bdrv_delete(dinfo->bdrv);
  166. g_free(dinfo->id);
  167. QTAILQ_REMOVE(&drives, dinfo, next);
  168. g_free(dinfo);
  169. }
  170. void drive_put_ref(DriveInfo *dinfo)
  171. {
  172. assert(dinfo->refcount);
  173. if (--dinfo->refcount == 0) {
  174. drive_uninit(dinfo);
  175. }
  176. }
  177. void drive_get_ref(DriveInfo *dinfo)
  178. {
  179. dinfo->refcount++;
  180. }
  181. typedef struct {
  182. QEMUBH *bh;
  183. DriveInfo *dinfo;
  184. } DrivePutRefBH;
  185. static void drive_put_ref_bh(void *opaque)
  186. {
  187. DrivePutRefBH *s = opaque;
  188. drive_put_ref(s->dinfo);
  189. qemu_bh_delete(s->bh);
  190. g_free(s);
  191. }
  192. /*
  193. * Release a drive reference in a BH
  194. *
  195. * It is not possible to use drive_put_ref() from a callback function when the
  196. * callers still need the drive. In such cases we schedule a BH to release the
  197. * reference.
  198. */
  199. static void drive_put_ref_bh_schedule(DriveInfo *dinfo)
  200. {
  201. DrivePutRefBH *s;
  202. s = g_new(DrivePutRefBH, 1);
  203. s->bh = qemu_bh_new(drive_put_ref_bh, s);
  204. s->dinfo = dinfo;
  205. qemu_bh_schedule(s->bh);
  206. }
  207. static int parse_block_error_action(const char *buf, bool is_read)
  208. {
  209. if (!strcmp(buf, "ignore")) {
  210. return BLOCKDEV_ON_ERROR_IGNORE;
  211. } else if (!is_read && !strcmp(buf, "enospc")) {
  212. return BLOCKDEV_ON_ERROR_ENOSPC;
  213. } else if (!strcmp(buf, "stop")) {
  214. return BLOCKDEV_ON_ERROR_STOP;
  215. } else if (!strcmp(buf, "report")) {
  216. return BLOCKDEV_ON_ERROR_REPORT;
  217. } else {
  218. error_report("'%s' invalid %s error action",
  219. buf, is_read ? "read" : "write");
  220. return -1;
  221. }
  222. }
  223. static bool do_check_io_limits(BlockIOLimit *io_limits)
  224. {
  225. bool bps_flag;
  226. bool iops_flag;
  227. assert(io_limits);
  228. bps_flag = (io_limits->bps[BLOCK_IO_LIMIT_TOTAL] != 0)
  229. && ((io_limits->bps[BLOCK_IO_LIMIT_READ] != 0)
  230. || (io_limits->bps[BLOCK_IO_LIMIT_WRITE] != 0));
  231. iops_flag = (io_limits->iops[BLOCK_IO_LIMIT_TOTAL] != 0)
  232. && ((io_limits->iops[BLOCK_IO_LIMIT_READ] != 0)
  233. || (io_limits->iops[BLOCK_IO_LIMIT_WRITE] != 0));
  234. if (bps_flag || iops_flag) {
  235. return false;
  236. }
  237. return true;
  238. }
  239. DriveInfo *drive_init(QemuOpts *opts, BlockInterfaceType block_default_type)
  240. {
  241. const char *buf;
  242. const char *file = NULL;
  243. const char *serial;
  244. const char *mediastr = "";
  245. BlockInterfaceType type;
  246. enum { MEDIA_DISK, MEDIA_CDROM } media;
  247. int bus_id, unit_id;
  248. int cyls, heads, secs, translation;
  249. BlockDriver *drv = NULL;
  250. int max_devs;
  251. int index;
  252. int ro = 0;
  253. int bdrv_flags = 0;
  254. int on_read_error, on_write_error;
  255. const char *devaddr;
  256. DriveInfo *dinfo;
  257. BlockIOLimit io_limits;
  258. int snapshot = 0;
  259. bool copy_on_read;
  260. int ret;
  261. translation = BIOS_ATA_TRANSLATION_AUTO;
  262. media = MEDIA_DISK;
  263. /* extract parameters */
  264. bus_id = qemu_opt_get_number(opts, "bus", 0);
  265. unit_id = qemu_opt_get_number(opts, "unit", -1);
  266. index = qemu_opt_get_number(opts, "index", -1);
  267. cyls = qemu_opt_get_number(opts, "cyls", 0);
  268. heads = qemu_opt_get_number(opts, "heads", 0);
  269. secs = qemu_opt_get_number(opts, "secs", 0);
  270. snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
  271. ro = qemu_opt_get_bool(opts, "readonly", 0);
  272. copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);
  273. file = qemu_opt_get(opts, "file");
  274. serial = qemu_opt_get(opts, "serial");
  275. if ((buf = qemu_opt_get(opts, "if")) != NULL) {
  276. for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
  277. ;
  278. if (type == IF_COUNT) {
  279. error_report("unsupported bus type '%s'", buf);
  280. return NULL;
  281. }
  282. } else {
  283. type = block_default_type;
  284. }
  285. max_devs = if_max_devs[type];
  286. if (cyls || heads || secs) {
  287. if (cyls < 1) {
  288. error_report("invalid physical cyls number");
  289. return NULL;
  290. }
  291. if (heads < 1) {
  292. error_report("invalid physical heads number");
  293. return NULL;
  294. }
  295. if (secs < 1) {
  296. error_report("invalid physical secs number");
  297. return NULL;
  298. }
  299. }
  300. if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
  301. if (!cyls) {
  302. error_report("'%s' trans must be used with cyls, heads and secs",
  303. buf);
  304. return NULL;
  305. }
  306. if (!strcmp(buf, "none"))
  307. translation = BIOS_ATA_TRANSLATION_NONE;
  308. else if (!strcmp(buf, "lba"))
  309. translation = BIOS_ATA_TRANSLATION_LBA;
  310. else if (!strcmp(buf, "auto"))
  311. translation = BIOS_ATA_TRANSLATION_AUTO;
  312. else {
  313. error_report("'%s' invalid translation type", buf);
  314. return NULL;
  315. }
  316. }
  317. if ((buf = qemu_opt_get(opts, "media")) != NULL) {
  318. if (!strcmp(buf, "disk")) {
  319. media = MEDIA_DISK;
  320. } else if (!strcmp(buf, "cdrom")) {
  321. if (cyls || secs || heads) {
  322. error_report("CHS can't be set with media=%s", buf);
  323. return NULL;
  324. }
  325. media = MEDIA_CDROM;
  326. } else {
  327. error_report("'%s' invalid media", buf);
  328. return NULL;
  329. }
  330. }
  331. bdrv_flags |= BDRV_O_CACHE_WB;
  332. if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
  333. if (bdrv_parse_cache_flags(buf, &bdrv_flags) != 0) {
  334. error_report("invalid cache option");
  335. return NULL;
  336. }
  337. }
  338. #ifdef CONFIG_LINUX_AIO
  339. if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
  340. if (!strcmp(buf, "native")) {
  341. bdrv_flags |= BDRV_O_NATIVE_AIO;
  342. } else if (!strcmp(buf, "threads")) {
  343. /* this is the default */
  344. } else {
  345. error_report("invalid aio option");
  346. return NULL;
  347. }
  348. }
  349. #endif
  350. if ((buf = qemu_opt_get(opts, "format")) != NULL) {
  351. if (is_help_option(buf)) {
  352. error_printf("Supported formats:");
  353. bdrv_iterate_format(bdrv_format_print, NULL);
  354. error_printf("\n");
  355. return NULL;
  356. }
  357. drv = bdrv_find_whitelisted_format(buf);
  358. if (!drv) {
  359. error_report("'%s' invalid format", buf);
  360. return NULL;
  361. }
  362. }
  363. /* disk I/O throttling */
  364. io_limits.bps[BLOCK_IO_LIMIT_TOTAL] =
  365. qemu_opt_get_number(opts, "bps", 0);
  366. io_limits.bps[BLOCK_IO_LIMIT_READ] =
  367. qemu_opt_get_number(opts, "bps_rd", 0);
  368. io_limits.bps[BLOCK_IO_LIMIT_WRITE] =
  369. qemu_opt_get_number(opts, "bps_wr", 0);
  370. io_limits.iops[BLOCK_IO_LIMIT_TOTAL] =
  371. qemu_opt_get_number(opts, "iops", 0);
  372. io_limits.iops[BLOCK_IO_LIMIT_READ] =
  373. qemu_opt_get_number(opts, "iops_rd", 0);
  374. io_limits.iops[BLOCK_IO_LIMIT_WRITE] =
  375. qemu_opt_get_number(opts, "iops_wr", 0);
  376. if (!do_check_io_limits(&io_limits)) {
  377. error_report("bps(iops) and bps_rd/bps_wr(iops_rd/iops_wr) "
  378. "cannot be used at the same time");
  379. return NULL;
  380. }
  381. if (qemu_opt_get(opts, "boot") != NULL) {
  382. fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be "
  383. "ignored. Future versions will reject this parameter. Please "
  384. "update your scripts.\n");
  385. }
  386. on_write_error = BLOCKDEV_ON_ERROR_ENOSPC;
  387. if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
  388. if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
  389. error_report("werror is not supported by this bus type");
  390. return NULL;
  391. }
  392. on_write_error = parse_block_error_action(buf, 0);
  393. if (on_write_error < 0) {
  394. return NULL;
  395. }
  396. }
  397. on_read_error = BLOCKDEV_ON_ERROR_REPORT;
  398. if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
  399. if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
  400. error_report("rerror is not supported by this bus type");
  401. return NULL;
  402. }
  403. on_read_error = parse_block_error_action(buf, 1);
  404. if (on_read_error < 0) {
  405. return NULL;
  406. }
  407. }
  408. if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
  409. if (type != IF_VIRTIO) {
  410. error_report("addr is not supported by this bus type");
  411. return NULL;
  412. }
  413. }
  414. /* compute bus and unit according index */
  415. if (index != -1) {
  416. if (bus_id != 0 || unit_id != -1) {
  417. error_report("index cannot be used with bus and unit");
  418. return NULL;
  419. }
  420. bus_id = drive_index_to_bus_id(type, index);
  421. unit_id = drive_index_to_unit_id(type, index);
  422. }
  423. /* if user doesn't specify a unit_id,
  424. * try to find the first free
  425. */
  426. if (unit_id == -1) {
  427. unit_id = 0;
  428. while (drive_get(type, bus_id, unit_id) != NULL) {
  429. unit_id++;
  430. if (max_devs && unit_id >= max_devs) {
  431. unit_id -= max_devs;
  432. bus_id++;
  433. }
  434. }
  435. }
  436. /* check unit id */
  437. if (max_devs && unit_id >= max_devs) {
  438. error_report("unit %d too big (max is %d)",
  439. unit_id, max_devs - 1);
  440. return NULL;
  441. }
  442. /*
  443. * catch multiple definitions
  444. */
  445. if (drive_get(type, bus_id, unit_id) != NULL) {
  446. error_report("drive with bus=%d, unit=%d (index=%d) exists",
  447. bus_id, unit_id, index);
  448. return NULL;
  449. }
  450. /* init */
  451. dinfo = g_malloc0(sizeof(*dinfo));
  452. if ((buf = qemu_opts_id(opts)) != NULL) {
  453. dinfo->id = g_strdup(buf);
  454. } else {
  455. /* no id supplied -> create one */
  456. dinfo->id = g_malloc0(32);
  457. if (type == IF_IDE || type == IF_SCSI)
  458. mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
  459. if (max_devs)
  460. snprintf(dinfo->id, 32, "%s%i%s%i",
  461. if_name[type], bus_id, mediastr, unit_id);
  462. else
  463. snprintf(dinfo->id, 32, "%s%s%i",
  464. if_name[type], mediastr, unit_id);
  465. }
  466. dinfo->bdrv = bdrv_new(dinfo->id);
  467. dinfo->bdrv->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0;
  468. dinfo->bdrv->read_only = ro;
  469. dinfo->devaddr = devaddr;
  470. dinfo->type = type;
  471. dinfo->bus = bus_id;
  472. dinfo->unit = unit_id;
  473. dinfo->cyls = cyls;
  474. dinfo->heads = heads;
  475. dinfo->secs = secs;
  476. dinfo->trans = translation;
  477. dinfo->opts = opts;
  478. dinfo->refcount = 1;
  479. dinfo->serial = serial;
  480. QTAILQ_INSERT_TAIL(&drives, dinfo, next);
  481. bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
  482. /* disk I/O throttling */
  483. bdrv_set_io_limits(dinfo->bdrv, &io_limits);
  484. switch(type) {
  485. case IF_IDE:
  486. case IF_SCSI:
  487. case IF_XEN:
  488. case IF_NONE:
  489. dinfo->media_cd = media == MEDIA_CDROM;
  490. break;
  491. case IF_SD:
  492. case IF_FLOPPY:
  493. case IF_PFLASH:
  494. case IF_MTD:
  495. break;
  496. case IF_VIRTIO:
  497. /* add virtio block device */
  498. opts = qemu_opts_create_nofail(qemu_find_opts("device"));
  499. if (arch_type == QEMU_ARCH_S390X) {
  500. qemu_opt_set(opts, "driver", "virtio-blk-s390");
  501. } else {
  502. qemu_opt_set(opts, "driver", "virtio-blk-pci");
  503. }
  504. qemu_opt_set(opts, "drive", dinfo->id);
  505. if (devaddr)
  506. qemu_opt_set(opts, "addr", devaddr);
  507. break;
  508. default:
  509. abort();
  510. }
  511. if (!file || !*file) {
  512. return dinfo;
  513. }
  514. if (snapshot) {
  515. /* always use cache=unsafe with snapshot */
  516. bdrv_flags &= ~BDRV_O_CACHE_MASK;
  517. bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
  518. }
  519. if (copy_on_read) {
  520. bdrv_flags |= BDRV_O_COPY_ON_READ;
  521. }
  522. if (runstate_check(RUN_STATE_INMIGRATE)) {
  523. bdrv_flags |= BDRV_O_INCOMING;
  524. }
  525. if (media == MEDIA_CDROM) {
  526. /* CDROM is fine for any interface, don't check. */
  527. ro = 1;
  528. } else if (ro == 1) {
  529. if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY &&
  530. type != IF_NONE && type != IF_PFLASH) {
  531. error_report("readonly not supported by this bus type");
  532. goto err;
  533. }
  534. }
  535. bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
  536. if (ro && copy_on_read) {
  537. error_report("warning: disabling copy_on_read on readonly drive");
  538. }
  539. ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
  540. if (ret < 0) {
  541. if (ret == -EMEDIUMTYPE) {
  542. error_report("could not open disk image %s: not in %s format",
  543. file, drv->format_name);
  544. } else {
  545. error_report("could not open disk image %s: %s",
  546. file, strerror(-ret));
  547. }
  548. goto err;
  549. }
  550. if (bdrv_key_required(dinfo->bdrv))
  551. autostart = 0;
  552. return dinfo;
  553. err:
  554. bdrv_delete(dinfo->bdrv);
  555. g_free(dinfo->id);
  556. QTAILQ_REMOVE(&drives, dinfo, next);
  557. g_free(dinfo);
  558. return NULL;
  559. }
  560. void do_commit(Monitor *mon, const QDict *qdict)
  561. {
  562. const char *device = qdict_get_str(qdict, "device");
  563. BlockDriverState *bs;
  564. int ret;
  565. if (!strcmp(device, "all")) {
  566. ret = bdrv_commit_all();
  567. } else {
  568. bs = bdrv_find(device);
  569. if (!bs) {
  570. monitor_printf(mon, "Device '%s' not found\n", device);
  571. return;
  572. }
  573. ret = bdrv_commit(bs);
  574. }
  575. if (ret < 0) {
  576. monitor_printf(mon, "'commit' error for '%s': %s\n", device,
  577. strerror(-ret));
  578. }
  579. }
  580. static void blockdev_do_action(int kind, void *data, Error **errp)
  581. {
  582. BlockdevAction action;
  583. BlockdevActionList list;
  584. action.kind = kind;
  585. action.data = data;
  586. list.value = &action;
  587. list.next = NULL;
  588. qmp_transaction(&list, errp);
  589. }
  590. void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file,
  591. bool has_format, const char *format,
  592. bool has_mode, enum NewImageMode mode,
  593. Error **errp)
  594. {
  595. BlockdevSnapshot snapshot = {
  596. .device = (char *) device,
  597. .snapshot_file = (char *) snapshot_file,
  598. .has_format = has_format,
  599. .format = (char *) format,
  600. .has_mode = has_mode,
  601. .mode = mode,
  602. };
  603. blockdev_do_action(BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, &snapshot,
  604. errp);
  605. }
  606. /* New and old BlockDriverState structs for group snapshots */
  607. typedef struct BlkTransactionStates {
  608. BlockDriverState *old_bs;
  609. BlockDriverState *new_bs;
  610. QSIMPLEQ_ENTRY(BlkTransactionStates) entry;
  611. } BlkTransactionStates;
  612. /*
  613. * 'Atomic' group snapshots. The snapshots are taken as a set, and if any fail
  614. * then we do not pivot any of the devices in the group, and abandon the
  615. * snapshots
  616. */
  617. void qmp_transaction(BlockdevActionList *dev_list, Error **errp)
  618. {
  619. int ret = 0;
  620. BlockdevActionList *dev_entry = dev_list;
  621. BlkTransactionStates *states, *next;
  622. Error *local_err = NULL;
  623. QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionStates) snap_bdrv_states;
  624. QSIMPLEQ_INIT(&snap_bdrv_states);
  625. /* drain all i/o before any snapshots */
  626. bdrv_drain_all();
  627. /* We don't do anything in this loop that commits us to the snapshot */
  628. while (NULL != dev_entry) {
  629. BlockdevAction *dev_info = NULL;
  630. BlockDriver *proto_drv;
  631. BlockDriver *drv;
  632. int flags;
  633. enum NewImageMode mode;
  634. const char *new_image_file;
  635. const char *device;
  636. const char *format = "qcow2";
  637. dev_info = dev_entry->value;
  638. dev_entry = dev_entry->next;
  639. states = g_malloc0(sizeof(BlkTransactionStates));
  640. QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, states, entry);
  641. switch (dev_info->kind) {
  642. case BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
  643. device = dev_info->blockdev_snapshot_sync->device;
  644. if (!dev_info->blockdev_snapshot_sync->has_mode) {
  645. dev_info->blockdev_snapshot_sync->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
  646. }
  647. new_image_file = dev_info->blockdev_snapshot_sync->snapshot_file;
  648. if (dev_info->blockdev_snapshot_sync->has_format) {
  649. format = dev_info->blockdev_snapshot_sync->format;
  650. }
  651. mode = dev_info->blockdev_snapshot_sync->mode;
  652. break;
  653. default:
  654. abort();
  655. }
  656. drv = bdrv_find_format(format);
  657. if (!drv) {
  658. error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
  659. goto delete_and_fail;
  660. }
  661. states->old_bs = bdrv_find(device);
  662. if (!states->old_bs) {
  663. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  664. goto delete_and_fail;
  665. }
  666. if (!bdrv_is_inserted(states->old_bs)) {
  667. error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
  668. goto delete_and_fail;
  669. }
  670. if (bdrv_in_use(states->old_bs)) {
  671. error_set(errp, QERR_DEVICE_IN_USE, device);
  672. goto delete_and_fail;
  673. }
  674. if (!bdrv_is_read_only(states->old_bs)) {
  675. if (bdrv_flush(states->old_bs)) {
  676. error_set(errp, QERR_IO_ERROR);
  677. goto delete_and_fail;
  678. }
  679. }
  680. flags = states->old_bs->open_flags;
  681. proto_drv = bdrv_find_protocol(new_image_file);
  682. if (!proto_drv) {
  683. error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
  684. goto delete_and_fail;
  685. }
  686. /* create new image w/backing file */
  687. if (mode != NEW_IMAGE_MODE_EXISTING) {
  688. bdrv_img_create(new_image_file, format,
  689. states->old_bs->filename,
  690. states->old_bs->drv->format_name,
  691. NULL, -1, flags, &local_err);
  692. if (error_is_set(&local_err)) {
  693. error_propagate(errp, local_err);
  694. goto delete_and_fail;
  695. }
  696. }
  697. /* We will manually add the backing_hd field to the bs later */
  698. states->new_bs = bdrv_new("");
  699. ret = bdrv_open(states->new_bs, new_image_file,
  700. flags | BDRV_O_NO_BACKING, drv);
  701. if (ret != 0) {
  702. error_set(errp, QERR_OPEN_FILE_FAILED, new_image_file);
  703. goto delete_and_fail;
  704. }
  705. }
  706. /* Now we are going to do the actual pivot. Everything up to this point
  707. * is reversible, but we are committed at this point */
  708. QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) {
  709. /* This removes our old bs from the bdrv_states, and adds the new bs */
  710. bdrv_append(states->new_bs, states->old_bs);
  711. /* We don't need (or want) to use the transactional
  712. * bdrv_reopen_multiple() across all the entries at once, because we
  713. * don't want to abort all of them if one of them fails the reopen */
  714. bdrv_reopen(states->new_bs, states->new_bs->open_flags & ~BDRV_O_RDWR,
  715. NULL);
  716. }
  717. /* success */
  718. goto exit;
  719. delete_and_fail:
  720. /*
  721. * failure, and it is all-or-none; abandon each new bs, and keep using
  722. * the original bs for all images
  723. */
  724. QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) {
  725. if (states->new_bs) {
  726. bdrv_delete(states->new_bs);
  727. }
  728. }
  729. exit:
  730. QSIMPLEQ_FOREACH_SAFE(states, &snap_bdrv_states, entry, next) {
  731. g_free(states);
  732. }
  733. }
  734. static void eject_device(BlockDriverState *bs, int force, Error **errp)
  735. {
  736. if (bdrv_in_use(bs)) {
  737. error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
  738. return;
  739. }
  740. if (!bdrv_dev_has_removable_media(bs)) {
  741. error_set(errp, QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
  742. return;
  743. }
  744. if (bdrv_dev_is_medium_locked(bs) && !bdrv_dev_is_tray_open(bs)) {
  745. bdrv_dev_eject_request(bs, force);
  746. if (!force) {
  747. error_set(errp, QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
  748. return;
  749. }
  750. }
  751. bdrv_close(bs);
  752. }
  753. void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
  754. {
  755. BlockDriverState *bs;
  756. bs = bdrv_find(device);
  757. if (!bs) {
  758. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  759. return;
  760. }
  761. eject_device(bs, force, errp);
  762. }
  763. void qmp_block_passwd(const char *device, const char *password, Error **errp)
  764. {
  765. BlockDriverState *bs;
  766. int err;
  767. bs = bdrv_find(device);
  768. if (!bs) {
  769. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  770. return;
  771. }
  772. err = bdrv_set_key(bs, password);
  773. if (err == -EINVAL) {
  774. error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
  775. return;
  776. } else if (err < 0) {
  777. error_set(errp, QERR_INVALID_PASSWORD);
  778. return;
  779. }
  780. }
  781. static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename,
  782. int bdrv_flags, BlockDriver *drv,
  783. const char *password, Error **errp)
  784. {
  785. if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
  786. error_set(errp, QERR_OPEN_FILE_FAILED, filename);
  787. return;
  788. }
  789. if (bdrv_key_required(bs)) {
  790. if (password) {
  791. if (bdrv_set_key(bs, password) < 0) {
  792. error_set(errp, QERR_INVALID_PASSWORD);
  793. }
  794. } else {
  795. error_set(errp, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
  796. bdrv_get_encrypted_filename(bs));
  797. }
  798. } else if (password) {
  799. error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
  800. }
  801. }
  802. void qmp_change_blockdev(const char *device, const char *filename,
  803. bool has_format, const char *format, Error **errp)
  804. {
  805. BlockDriverState *bs;
  806. BlockDriver *drv = NULL;
  807. int bdrv_flags;
  808. Error *err = NULL;
  809. bs = bdrv_find(device);
  810. if (!bs) {
  811. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  812. return;
  813. }
  814. if (format) {
  815. drv = bdrv_find_whitelisted_format(format);
  816. if (!drv) {
  817. error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
  818. return;
  819. }
  820. }
  821. eject_device(bs, 0, &err);
  822. if (error_is_set(&err)) {
  823. error_propagate(errp, err);
  824. return;
  825. }
  826. bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
  827. bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
  828. qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp);
  829. }
  830. /* throttling disk I/O limits */
  831. void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
  832. int64_t bps_wr, int64_t iops, int64_t iops_rd,
  833. int64_t iops_wr, Error **errp)
  834. {
  835. BlockIOLimit io_limits;
  836. BlockDriverState *bs;
  837. bs = bdrv_find(device);
  838. if (!bs) {
  839. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  840. return;
  841. }
  842. io_limits.bps[BLOCK_IO_LIMIT_TOTAL] = bps;
  843. io_limits.bps[BLOCK_IO_LIMIT_READ] = bps_rd;
  844. io_limits.bps[BLOCK_IO_LIMIT_WRITE] = bps_wr;
  845. io_limits.iops[BLOCK_IO_LIMIT_TOTAL]= iops;
  846. io_limits.iops[BLOCK_IO_LIMIT_READ] = iops_rd;
  847. io_limits.iops[BLOCK_IO_LIMIT_WRITE]= iops_wr;
  848. if (!do_check_io_limits(&io_limits)) {
  849. error_set(errp, QERR_INVALID_PARAMETER_COMBINATION);
  850. return;
  851. }
  852. bs->io_limits = io_limits;
  853. bs->slice_time = BLOCK_IO_SLICE_TIME;
  854. if (!bs->io_limits_enabled && bdrv_io_limits_enabled(bs)) {
  855. bdrv_io_limits_enable(bs);
  856. } else if (bs->io_limits_enabled && !bdrv_io_limits_enabled(bs)) {
  857. bdrv_io_limits_disable(bs);
  858. } else {
  859. if (bs->block_timer) {
  860. qemu_mod_timer(bs->block_timer, qemu_get_clock_ns(vm_clock));
  861. }
  862. }
  863. }
  864. int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
  865. {
  866. const char *id = qdict_get_str(qdict, "id");
  867. BlockDriverState *bs;
  868. bs = bdrv_find(id);
  869. if (!bs) {
  870. qerror_report(QERR_DEVICE_NOT_FOUND, id);
  871. return -1;
  872. }
  873. if (bdrv_in_use(bs)) {
  874. qerror_report(QERR_DEVICE_IN_USE, id);
  875. return -1;
  876. }
  877. /* quiesce block driver; prevent further io */
  878. bdrv_drain_all();
  879. bdrv_flush(bs);
  880. bdrv_close(bs);
  881. /* if we have a device attached to this BlockDriverState
  882. * then we need to make the drive anonymous until the device
  883. * can be removed. If this is a drive with no device backing
  884. * then we can just get rid of the block driver state right here.
  885. */
  886. if (bdrv_get_attached_dev(bs)) {
  887. bdrv_make_anon(bs);
  888. } else {
  889. drive_uninit(drive_get_by_blockdev(bs));
  890. }
  891. return 0;
  892. }
  893. void qmp_block_resize(const char *device, int64_t size, Error **errp)
  894. {
  895. BlockDriverState *bs;
  896. bs = bdrv_find(device);
  897. if (!bs) {
  898. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  899. return;
  900. }
  901. if (size < 0) {
  902. error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
  903. return;
  904. }
  905. /* complete all in-flight operations before resizing the device */
  906. bdrv_drain_all();
  907. switch (bdrv_truncate(bs, size)) {
  908. case 0:
  909. break;
  910. case -ENOMEDIUM:
  911. error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
  912. break;
  913. case -ENOTSUP:
  914. error_set(errp, QERR_UNSUPPORTED);
  915. break;
  916. case -EACCES:
  917. error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
  918. break;
  919. case -EBUSY:
  920. error_set(errp, QERR_DEVICE_IN_USE, device);
  921. break;
  922. default:
  923. error_set(errp, QERR_UNDEFINED_ERROR);
  924. break;
  925. }
  926. }
  927. static void block_job_cb(void *opaque, int ret)
  928. {
  929. BlockDriverState *bs = opaque;
  930. QObject *obj;
  931. trace_block_job_cb(bs, bs->job, ret);
  932. assert(bs->job);
  933. obj = qobject_from_block_job(bs->job);
  934. if (ret < 0) {
  935. QDict *dict = qobject_to_qdict(obj);
  936. qdict_put(dict, "error", qstring_from_str(strerror(-ret)));
  937. }
  938. if (block_job_is_cancelled(bs->job)) {
  939. monitor_protocol_event(QEVENT_BLOCK_JOB_CANCELLED, obj);
  940. } else {
  941. monitor_protocol_event(QEVENT_BLOCK_JOB_COMPLETED, obj);
  942. }
  943. qobject_decref(obj);
  944. drive_put_ref_bh_schedule(drive_get_by_blockdev(bs));
  945. }
  946. void qmp_block_stream(const char *device, bool has_base,
  947. const char *base, bool has_speed, int64_t speed,
  948. bool has_on_error, BlockdevOnError on_error,
  949. Error **errp)
  950. {
  951. BlockDriverState *bs;
  952. BlockDriverState *base_bs = NULL;
  953. Error *local_err = NULL;
  954. if (!has_on_error) {
  955. on_error = BLOCKDEV_ON_ERROR_REPORT;
  956. }
  957. bs = bdrv_find(device);
  958. if (!bs) {
  959. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  960. return;
  961. }
  962. if (base) {
  963. base_bs = bdrv_find_backing_image(bs, base);
  964. if (base_bs == NULL) {
  965. error_set(errp, QERR_BASE_NOT_FOUND, base);
  966. return;
  967. }
  968. }
  969. stream_start(bs, base_bs, base, has_speed ? speed : 0,
  970. on_error, block_job_cb, bs, &local_err);
  971. if (error_is_set(&local_err)) {
  972. error_propagate(errp, local_err);
  973. return;
  974. }
  975. /* Grab a reference so hotplug does not delete the BlockDriverState from
  976. * underneath us.
  977. */
  978. drive_get_ref(drive_get_by_blockdev(bs));
  979. trace_qmp_block_stream(bs, bs->job);
  980. }
  981. void qmp_block_commit(const char *device,
  982. bool has_base, const char *base, const char *top,
  983. bool has_speed, int64_t speed,
  984. Error **errp)
  985. {
  986. BlockDriverState *bs;
  987. BlockDriverState *base_bs, *top_bs;
  988. Error *local_err = NULL;
  989. /* This will be part of the QMP command, if/when the
  990. * BlockdevOnError change for blkmirror makes it in
  991. */
  992. BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
  993. /* drain all i/o before commits */
  994. bdrv_drain_all();
  995. bs = bdrv_find(device);
  996. if (!bs) {
  997. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  998. return;
  999. }
  1000. /* default top_bs is the active layer */
  1001. top_bs = bs;
  1002. if (top) {
  1003. if (strcmp(bs->filename, top) != 0) {
  1004. top_bs = bdrv_find_backing_image(bs, top);
  1005. }
  1006. }
  1007. if (top_bs == NULL) {
  1008. error_setg(errp, "Top image file %s not found", top ? top : "NULL");
  1009. return;
  1010. }
  1011. if (has_base && base) {
  1012. base_bs = bdrv_find_backing_image(top_bs, base);
  1013. } else {
  1014. base_bs = bdrv_find_base(top_bs);
  1015. }
  1016. if (base_bs == NULL) {
  1017. error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL");
  1018. return;
  1019. }
  1020. commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
  1021. &local_err);
  1022. if (local_err != NULL) {
  1023. error_propagate(errp, local_err);
  1024. return;
  1025. }
  1026. /* Grab a reference so hotplug does not delete the BlockDriverState from
  1027. * underneath us.
  1028. */
  1029. drive_get_ref(drive_get_by_blockdev(bs));
  1030. }
  1031. #define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
  1032. void qmp_drive_mirror(const char *device, const char *target,
  1033. bool has_format, const char *format,
  1034. enum MirrorSyncMode sync,
  1035. bool has_mode, enum NewImageMode mode,
  1036. bool has_speed, int64_t speed,
  1037. bool has_granularity, uint32_t granularity,
  1038. bool has_buf_size, int64_t buf_size,
  1039. bool has_on_source_error, BlockdevOnError on_source_error,
  1040. bool has_on_target_error, BlockdevOnError on_target_error,
  1041. Error **errp)
  1042. {
  1043. BlockDriverState *bs;
  1044. BlockDriverState *source, *target_bs;
  1045. BlockDriver *proto_drv;
  1046. BlockDriver *drv = NULL;
  1047. Error *local_err = NULL;
  1048. int flags;
  1049. uint64_t size;
  1050. int ret;
  1051. if (!has_speed) {
  1052. speed = 0;
  1053. }
  1054. if (!has_on_source_error) {
  1055. on_source_error = BLOCKDEV_ON_ERROR_REPORT;
  1056. }
  1057. if (!has_on_target_error) {
  1058. on_target_error = BLOCKDEV_ON_ERROR_REPORT;
  1059. }
  1060. if (!has_mode) {
  1061. mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
  1062. }
  1063. if (!has_granularity) {
  1064. granularity = 0;
  1065. }
  1066. if (!has_buf_size) {
  1067. buf_size = DEFAULT_MIRROR_BUF_SIZE;
  1068. }
  1069. if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) {
  1070. error_set(errp, QERR_INVALID_PARAMETER, device);
  1071. return;
  1072. }
  1073. if (granularity & (granularity - 1)) {
  1074. error_set(errp, QERR_INVALID_PARAMETER, device);
  1075. return;
  1076. }
  1077. bs = bdrv_find(device);
  1078. if (!bs) {
  1079. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  1080. return;
  1081. }
  1082. if (!bdrv_is_inserted(bs)) {
  1083. error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
  1084. return;
  1085. }
  1086. if (!has_format) {
  1087. format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
  1088. }
  1089. if (format) {
  1090. drv = bdrv_find_format(format);
  1091. if (!drv) {
  1092. error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
  1093. return;
  1094. }
  1095. }
  1096. if (bdrv_in_use(bs)) {
  1097. error_set(errp, QERR_DEVICE_IN_USE, device);
  1098. return;
  1099. }
  1100. flags = bs->open_flags | BDRV_O_RDWR;
  1101. source = bs->backing_hd;
  1102. if (!source && sync == MIRROR_SYNC_MODE_TOP) {
  1103. sync = MIRROR_SYNC_MODE_FULL;
  1104. }
  1105. proto_drv = bdrv_find_protocol(target);
  1106. if (!proto_drv) {
  1107. error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
  1108. return;
  1109. }
  1110. bdrv_get_geometry(bs, &size);
  1111. size *= 512;
  1112. if (sync == MIRROR_SYNC_MODE_FULL && mode != NEW_IMAGE_MODE_EXISTING) {
  1113. /* create new image w/o backing file */
  1114. assert(format && drv);
  1115. bdrv_img_create(target, format,
  1116. NULL, NULL, NULL, size, flags, &local_err);
  1117. } else {
  1118. switch (mode) {
  1119. case NEW_IMAGE_MODE_EXISTING:
  1120. ret = 0;
  1121. break;
  1122. case NEW_IMAGE_MODE_ABSOLUTE_PATHS:
  1123. /* create new image with backing file */
  1124. bdrv_img_create(target, format,
  1125. source->filename,
  1126. source->drv->format_name,
  1127. NULL, size, flags, &local_err);
  1128. break;
  1129. default:
  1130. abort();
  1131. }
  1132. }
  1133. if (error_is_set(&local_err)) {
  1134. error_propagate(errp, local_err);
  1135. return;
  1136. }
  1137. /* Mirroring takes care of copy-on-write using the source's backing
  1138. * file.
  1139. */
  1140. target_bs = bdrv_new("");
  1141. ret = bdrv_open(target_bs, target, flags | BDRV_O_NO_BACKING, drv);
  1142. if (ret < 0) {
  1143. bdrv_delete(target_bs);
  1144. error_set(errp, QERR_OPEN_FILE_FAILED, target);
  1145. return;
  1146. }
  1147. mirror_start(bs, target_bs, speed, granularity, buf_size, sync,
  1148. on_source_error, on_target_error,
  1149. block_job_cb, bs, &local_err);
  1150. if (local_err != NULL) {
  1151. bdrv_delete(target_bs);
  1152. error_propagate(errp, local_err);
  1153. return;
  1154. }
  1155. /* Grab a reference so hotplug does not delete the BlockDriverState from
  1156. * underneath us.
  1157. */
  1158. drive_get_ref(drive_get_by_blockdev(bs));
  1159. }
  1160. static BlockJob *find_block_job(const char *device)
  1161. {
  1162. BlockDriverState *bs;
  1163. bs = bdrv_find(device);
  1164. if (!bs || !bs->job) {
  1165. return NULL;
  1166. }
  1167. return bs->job;
  1168. }
  1169. void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
  1170. {
  1171. BlockJob *job = find_block_job(device);
  1172. if (!job) {
  1173. error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
  1174. return;
  1175. }
  1176. block_job_set_speed(job, speed, errp);
  1177. }
  1178. void qmp_block_job_cancel(const char *device,
  1179. bool has_force, bool force, Error **errp)
  1180. {
  1181. BlockJob *job = find_block_job(device);
  1182. if (!has_force) {
  1183. force = false;
  1184. }
  1185. if (!job) {
  1186. error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
  1187. return;
  1188. }
  1189. if (job->paused && !force) {
  1190. error_set(errp, QERR_BLOCK_JOB_PAUSED, device);
  1191. return;
  1192. }
  1193. trace_qmp_block_job_cancel(job);
  1194. block_job_cancel(job);
  1195. }
  1196. void qmp_block_job_pause(const char *device, Error **errp)
  1197. {
  1198. BlockJob *job = find_block_job(device);
  1199. if (!job) {
  1200. error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
  1201. return;
  1202. }
  1203. trace_qmp_block_job_pause(job);
  1204. block_job_pause(job);
  1205. }
  1206. void qmp_block_job_resume(const char *device, Error **errp)
  1207. {
  1208. BlockJob *job = find_block_job(device);
  1209. if (!job) {
  1210. error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
  1211. return;
  1212. }
  1213. trace_qmp_block_job_resume(job);
  1214. block_job_resume(job);
  1215. }
  1216. void qmp_block_job_complete(const char *device, Error **errp)
  1217. {
  1218. BlockJob *job = find_block_job(device);
  1219. if (!job) {
  1220. error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device);
  1221. return;
  1222. }
  1223. trace_qmp_block_job_complete(job);
  1224. block_job_complete(job, errp);
  1225. }
  1226. static void do_qmp_query_block_jobs_one(void *opaque, BlockDriverState *bs)
  1227. {
  1228. BlockJobInfoList **prev = opaque;
  1229. BlockJob *job = bs->job;
  1230. if (job) {
  1231. BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1);
  1232. elem->value = block_job_query(bs->job);
  1233. (*prev)->next = elem;
  1234. *prev = elem;
  1235. }
  1236. }
  1237. BlockJobInfoList *qmp_query_block_jobs(Error **errp)
  1238. {
  1239. /* Dummy is a fake list element for holding the head pointer */
  1240. BlockJobInfoList dummy = {};
  1241. BlockJobInfoList *prev = &dummy;
  1242. bdrv_iterate(do_qmp_query_block_jobs_one, &prev);
  1243. return dummy.next;
  1244. }
  1245. QemuOptsList qemu_drive_opts = {
  1246. .name = "drive",
  1247. .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
  1248. .desc = {
  1249. {
  1250. .name = "bus",
  1251. .type = QEMU_OPT_NUMBER,
  1252. .help = "bus number",
  1253. },{
  1254. .name = "unit",
  1255. .type = QEMU_OPT_NUMBER,
  1256. .help = "unit number (i.e. lun for scsi)",
  1257. },{
  1258. .name = "if",
  1259. .type = QEMU_OPT_STRING,
  1260. .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
  1261. },{
  1262. .name = "index",
  1263. .type = QEMU_OPT_NUMBER,
  1264. .help = "index number",
  1265. },{
  1266. .name = "cyls",
  1267. .type = QEMU_OPT_NUMBER,
  1268. .help = "number of cylinders (ide disk geometry)",
  1269. },{
  1270. .name = "heads",
  1271. .type = QEMU_OPT_NUMBER,
  1272. .help = "number of heads (ide disk geometry)",
  1273. },{
  1274. .name = "secs",
  1275. .type = QEMU_OPT_NUMBER,
  1276. .help = "number of sectors (ide disk geometry)",
  1277. },{
  1278. .name = "trans",
  1279. .type = QEMU_OPT_STRING,
  1280. .help = "chs translation (auto, lba. none)",
  1281. },{
  1282. .name = "media",
  1283. .type = QEMU_OPT_STRING,
  1284. .help = "media type (disk, cdrom)",
  1285. },{
  1286. .name = "snapshot",
  1287. .type = QEMU_OPT_BOOL,
  1288. .help = "enable/disable snapshot mode",
  1289. },{
  1290. .name = "file",
  1291. .type = QEMU_OPT_STRING,
  1292. .help = "disk image",
  1293. },{
  1294. .name = "cache",
  1295. .type = QEMU_OPT_STRING,
  1296. .help = "host cache usage (none, writeback, writethrough, "
  1297. "directsync, unsafe)",
  1298. },{
  1299. .name = "aio",
  1300. .type = QEMU_OPT_STRING,
  1301. .help = "host AIO implementation (threads, native)",
  1302. },{
  1303. .name = "format",
  1304. .type = QEMU_OPT_STRING,
  1305. .help = "disk format (raw, qcow2, ...)",
  1306. },{
  1307. .name = "serial",
  1308. .type = QEMU_OPT_STRING,
  1309. .help = "disk serial number",
  1310. },{
  1311. .name = "rerror",
  1312. .type = QEMU_OPT_STRING,
  1313. .help = "read error action",
  1314. },{
  1315. .name = "werror",
  1316. .type = QEMU_OPT_STRING,
  1317. .help = "write error action",
  1318. },{
  1319. .name = "addr",
  1320. .type = QEMU_OPT_STRING,
  1321. .help = "pci address (virtio only)",
  1322. },{
  1323. .name = "readonly",
  1324. .type = QEMU_OPT_BOOL,
  1325. .help = "open drive file as read-only",
  1326. },{
  1327. .name = "iops",
  1328. .type = QEMU_OPT_NUMBER,
  1329. .help = "limit total I/O operations per second",
  1330. },{
  1331. .name = "iops_rd",
  1332. .type = QEMU_OPT_NUMBER,
  1333. .help = "limit read operations per second",
  1334. },{
  1335. .name = "iops_wr",
  1336. .type = QEMU_OPT_NUMBER,
  1337. .help = "limit write operations per second",
  1338. },{
  1339. .name = "bps",
  1340. .type = QEMU_OPT_NUMBER,
  1341. .help = "limit total bytes per second",
  1342. },{
  1343. .name = "bps_rd",
  1344. .type = QEMU_OPT_NUMBER,
  1345. .help = "limit read bytes per second",
  1346. },{
  1347. .name = "bps_wr",
  1348. .type = QEMU_OPT_NUMBER,
  1349. .help = "limit write bytes per second",
  1350. },{
  1351. .name = "copy-on-read",
  1352. .type = QEMU_OPT_BOOL,
  1353. .help = "copy read data from backing file into image file",
  1354. },{
  1355. .name = "boot",
  1356. .type = QEMU_OPT_BOOL,
  1357. .help = "(deprecated, ignored)",
  1358. },
  1359. { /* end of list */ }
  1360. },
  1361. };