blockdev.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  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 "blockdev.h"
  10. #include "hw/block-common.h"
  11. #include "monitor.h"
  12. #include "qerror.h"
  13. #include "qemu-option.h"
  14. #include "qemu-config.h"
  15. #include "qemu-objects.h"
  16. #include "sysemu.h"
  17. #include "block_int.h"
  18. #include "qmp-commands.h"
  19. #include "trace.h"
  20. #include "arch_init.h"
  21. static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
  22. static const char *const if_name[IF_COUNT] = {
  23. [IF_NONE] = "none",
  24. [IF_IDE] = "ide",
  25. [IF_SCSI] = "scsi",
  26. [IF_FLOPPY] = "floppy",
  27. [IF_PFLASH] = "pflash",
  28. [IF_MTD] = "mtd",
  29. [IF_SD] = "sd",
  30. [IF_VIRTIO] = "virtio",
  31. [IF_XEN] = "xen",
  32. };
  33. static const int if_max_devs[IF_COUNT] = {
  34. /*
  35. * Do not change these numbers! They govern how drive option
  36. * index maps to unit and bus. That mapping is ABI.
  37. *
  38. * All controllers used to imlement if=T drives need to support
  39. * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
  40. * Otherwise, some index values map to "impossible" bus, unit
  41. * values.
  42. *
  43. * For instance, if you change [IF_SCSI] to 255, -drive
  44. * if=scsi,index=12 no longer means bus=1,unit=5, but
  45. * bus=0,unit=12. With an lsi53c895a controller (7 units max),
  46. * the drive can't be set up. Regression.
  47. */
  48. [IF_IDE] = 2,
  49. [IF_SCSI] = 7,
  50. };
  51. /*
  52. * We automatically delete the drive when a device using it gets
  53. * unplugged. Questionable feature, but we can't just drop it.
  54. * Device models call blockdev_mark_auto_del() to schedule the
  55. * automatic deletion, and generic qdev code calls blockdev_auto_del()
  56. * when deletion is actually safe.
  57. */
  58. void blockdev_mark_auto_del(BlockDriverState *bs)
  59. {
  60. DriveInfo *dinfo = drive_get_by_blockdev(bs);
  61. if (bs->job) {
  62. block_job_cancel(bs->job);
  63. }
  64. if (dinfo) {
  65. dinfo->auto_del = 1;
  66. }
  67. }
  68. void blockdev_auto_del(BlockDriverState *bs)
  69. {
  70. DriveInfo *dinfo = drive_get_by_blockdev(bs);
  71. if (dinfo && dinfo->auto_del) {
  72. drive_put_ref(dinfo);
  73. }
  74. }
  75. static int drive_index_to_bus_id(BlockInterfaceType type, int index)
  76. {
  77. int max_devs = if_max_devs[type];
  78. return max_devs ? index / max_devs : 0;
  79. }
  80. static int drive_index_to_unit_id(BlockInterfaceType type, int index)
  81. {
  82. int max_devs = if_max_devs[type];
  83. return max_devs ? index % max_devs : index;
  84. }
  85. QemuOpts *drive_def(const char *optstr)
  86. {
  87. return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
  88. }
  89. QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
  90. const char *optstr)
  91. {
  92. QemuOpts *opts;
  93. char buf[32];
  94. opts = drive_def(optstr);
  95. if (!opts) {
  96. return NULL;
  97. }
  98. if (type != IF_DEFAULT) {
  99. qemu_opt_set(opts, "if", if_name[type]);
  100. }
  101. if (index >= 0) {
  102. snprintf(buf, sizeof(buf), "%d", index);
  103. qemu_opt_set(opts, "index", buf);
  104. }
  105. if (file)
  106. qemu_opt_set(opts, "file", file);
  107. return opts;
  108. }
  109. DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
  110. {
  111. DriveInfo *dinfo;
  112. /* seek interface, bus and unit */
  113. QTAILQ_FOREACH(dinfo, &drives, next) {
  114. if (dinfo->type == type &&
  115. dinfo->bus == bus &&
  116. dinfo->unit == unit)
  117. return dinfo;
  118. }
  119. return NULL;
  120. }
  121. DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
  122. {
  123. return drive_get(type,
  124. drive_index_to_bus_id(type, index),
  125. drive_index_to_unit_id(type, index));
  126. }
  127. int drive_get_max_bus(BlockInterfaceType type)
  128. {
  129. int max_bus;
  130. DriveInfo *dinfo;
  131. max_bus = -1;
  132. QTAILQ_FOREACH(dinfo, &drives, next) {
  133. if(dinfo->type == type &&
  134. dinfo->bus > max_bus)
  135. max_bus = dinfo->bus;
  136. }
  137. return max_bus;
  138. }
  139. /* Get a block device. This should only be used for single-drive devices
  140. (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
  141. appropriate bus. */
  142. DriveInfo *drive_get_next(BlockInterfaceType type)
  143. {
  144. static int next_block_unit[IF_COUNT];
  145. return drive_get(type, 0, next_block_unit[type]++);
  146. }
  147. DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
  148. {
  149. DriveInfo *dinfo;
  150. QTAILQ_FOREACH(dinfo, &drives, next) {
  151. if (dinfo->bdrv == bs) {
  152. return dinfo;
  153. }
  154. }
  155. return NULL;
  156. }
  157. static void bdrv_format_print(void *opaque, const char *name)
  158. {
  159. error_printf(" %s", name);
  160. }
  161. static void drive_uninit(DriveInfo *dinfo)
  162. {
  163. qemu_opts_del(dinfo->opts);
  164. bdrv_delete(dinfo->bdrv);
  165. g_free(dinfo->id);
  166. QTAILQ_REMOVE(&drives, dinfo, next);
  167. g_free(dinfo);
  168. }
  169. void drive_put_ref(DriveInfo *dinfo)
  170. {
  171. assert(dinfo->refcount);
  172. if (--dinfo->refcount == 0) {
  173. drive_uninit(dinfo);
  174. }
  175. }
  176. void drive_get_ref(DriveInfo *dinfo)
  177. {
  178. dinfo->refcount++;
  179. }
  180. typedef struct {
  181. QEMUBH *bh;
  182. DriveInfo *dinfo;
  183. } DrivePutRefBH;
  184. static void drive_put_ref_bh(void *opaque)
  185. {
  186. DrivePutRefBH *s = opaque;
  187. drive_put_ref(s->dinfo);
  188. qemu_bh_delete(s->bh);
  189. g_free(s);
  190. }
  191. /*
  192. * Release a drive reference in a BH
  193. *
  194. * It is not possible to use drive_put_ref() from a callback function when the
  195. * callers still need the drive. In such cases we schedule a BH to release the
  196. * reference.
  197. */
  198. static void drive_put_ref_bh_schedule(DriveInfo *dinfo)
  199. {
  200. DrivePutRefBH *s;
  201. s = g_new(DrivePutRefBH, 1);
  202. s->bh = qemu_bh_new(drive_put_ref_bh, s);
  203. s->dinfo = dinfo;
  204. qemu_bh_schedule(s->bh);
  205. }
  206. static int parse_block_error_action(const char *buf, int is_read)
  207. {
  208. if (!strcmp(buf, "ignore")) {
  209. return BLOCK_ERR_IGNORE;
  210. } else if (!is_read && !strcmp(buf, "enospc")) {
  211. return BLOCK_ERR_STOP_ENOSPC;
  212. } else if (!strcmp(buf, "stop")) {
  213. return BLOCK_ERR_STOP_ANY;
  214. } else if (!strcmp(buf, "report")) {
  215. return BLOCK_ERR_REPORT;
  216. } else {
  217. error_report("'%s' invalid %s error action",
  218. buf, is_read ? "read" : "write");
  219. return -1;
  220. }
  221. }
  222. static bool do_check_io_limits(BlockIOLimit *io_limits)
  223. {
  224. bool bps_flag;
  225. bool iops_flag;
  226. assert(io_limits);
  227. bps_flag = (io_limits->bps[BLOCK_IO_LIMIT_TOTAL] != 0)
  228. && ((io_limits->bps[BLOCK_IO_LIMIT_READ] != 0)
  229. || (io_limits->bps[BLOCK_IO_LIMIT_WRITE] != 0));
  230. iops_flag = (io_limits->iops[BLOCK_IO_LIMIT_TOTAL] != 0)
  231. && ((io_limits->iops[BLOCK_IO_LIMIT_READ] != 0)
  232. || (io_limits->iops[BLOCK_IO_LIMIT_WRITE] != 0));
  233. if (bps_flag || iops_flag) {
  234. return false;
  235. }
  236. return true;
  237. }
  238. DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)
  239. {
  240. const char *buf;
  241. const char *file = NULL;
  242. const char *serial;
  243. const char *mediastr = "";
  244. BlockInterfaceType type;
  245. enum { MEDIA_DISK, MEDIA_CDROM } media;
  246. int bus_id, unit_id;
  247. int cyls, heads, secs, translation;
  248. BlockDriver *drv = NULL;
  249. int max_devs;
  250. int index;
  251. int ro = 0;
  252. int bdrv_flags = 0;
  253. int on_read_error, on_write_error;
  254. const char *devaddr;
  255. DriveInfo *dinfo;
  256. BlockIOLimit io_limits;
  257. int snapshot = 0;
  258. bool copy_on_read;
  259. int ret;
  260. translation = BIOS_ATA_TRANSLATION_AUTO;
  261. media = MEDIA_DISK;
  262. /* extract parameters */
  263. bus_id = qemu_opt_get_number(opts, "bus", 0);
  264. unit_id = qemu_opt_get_number(opts, "unit", -1);
  265. index = qemu_opt_get_number(opts, "index", -1);
  266. cyls = qemu_opt_get_number(opts, "cyls", 0);
  267. heads = qemu_opt_get_number(opts, "heads", 0);
  268. secs = qemu_opt_get_number(opts, "secs", 0);
  269. snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
  270. ro = qemu_opt_get_bool(opts, "readonly", 0);
  271. copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false);
  272. file = qemu_opt_get(opts, "file");
  273. serial = qemu_opt_get(opts, "serial");
  274. if ((buf = qemu_opt_get(opts, "if")) != NULL) {
  275. for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
  276. ;
  277. if (type == IF_COUNT) {
  278. error_report("unsupported bus type '%s'", buf);
  279. return NULL;
  280. }
  281. } else {
  282. type = default_to_scsi ? IF_SCSI : IF_IDE;
  283. }
  284. max_devs = if_max_devs[type];
  285. if (cyls || heads || secs) {
  286. if (cyls < 1) {
  287. error_report("invalid physical cyls number");
  288. return NULL;
  289. }
  290. if (heads < 1) {
  291. error_report("invalid physical heads number");
  292. return NULL;
  293. }
  294. if (secs < 1) {
  295. error_report("invalid physical secs number");
  296. return NULL;
  297. }
  298. }
  299. if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
  300. if (!cyls) {
  301. error_report("'%s' trans must be used with cyls, heads and secs",
  302. buf);
  303. return NULL;
  304. }
  305. if (!strcmp(buf, "none"))
  306. translation = BIOS_ATA_TRANSLATION_NONE;
  307. else if (!strcmp(buf, "lba"))
  308. translation = BIOS_ATA_TRANSLATION_LBA;
  309. else if (!strcmp(buf, "auto"))
  310. translation = BIOS_ATA_TRANSLATION_AUTO;
  311. else {
  312. error_report("'%s' invalid translation type", buf);
  313. return NULL;
  314. }
  315. }
  316. if ((buf = qemu_opt_get(opts, "media")) != NULL) {
  317. if (!strcmp(buf, "disk")) {
  318. media = MEDIA_DISK;
  319. } else if (!strcmp(buf, "cdrom")) {
  320. if (cyls || secs || heads) {
  321. error_report("CHS can't be set with media=%s", buf);
  322. return NULL;
  323. }
  324. media = MEDIA_CDROM;
  325. } else {
  326. error_report("'%s' invalid media", buf);
  327. return NULL;
  328. }
  329. }
  330. bdrv_flags |= BDRV_O_CACHE_WB;
  331. if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
  332. if (bdrv_parse_cache_flags(buf, &bdrv_flags) != 0) {
  333. error_report("invalid cache option");
  334. return NULL;
  335. }
  336. }
  337. #ifdef CONFIG_LINUX_AIO
  338. if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
  339. if (!strcmp(buf, "native")) {
  340. bdrv_flags |= BDRV_O_NATIVE_AIO;
  341. } else if (!strcmp(buf, "threads")) {
  342. /* this is the default */
  343. } else {
  344. error_report("invalid aio option");
  345. return NULL;
  346. }
  347. }
  348. #endif
  349. if ((buf = qemu_opt_get(opts, "format")) != NULL) {
  350. if (is_help_option(buf)) {
  351. error_printf("Supported formats:");
  352. bdrv_iterate_format(bdrv_format_print, NULL);
  353. error_printf("\n");
  354. return NULL;
  355. }
  356. drv = bdrv_find_whitelisted_format(buf);
  357. if (!drv) {
  358. error_report("'%s' invalid format", buf);
  359. return NULL;
  360. }
  361. }
  362. /* disk I/O throttling */
  363. io_limits.bps[BLOCK_IO_LIMIT_TOTAL] =
  364. qemu_opt_get_number(opts, "bps", 0);
  365. io_limits.bps[BLOCK_IO_LIMIT_READ] =
  366. qemu_opt_get_number(opts, "bps_rd", 0);
  367. io_limits.bps[BLOCK_IO_LIMIT_WRITE] =
  368. qemu_opt_get_number(opts, "bps_wr", 0);
  369. io_limits.iops[BLOCK_IO_LIMIT_TOTAL] =
  370. qemu_opt_get_number(opts, "iops", 0);
  371. io_limits.iops[BLOCK_IO_LIMIT_READ] =
  372. qemu_opt_get_number(opts, "iops_rd", 0);
  373. io_limits.iops[BLOCK_IO_LIMIT_WRITE] =
  374. qemu_opt_get_number(opts, "iops_wr", 0);
  375. if (!do_check_io_limits(&io_limits)) {
  376. error_report("bps(iops) and bps_rd/bps_wr(iops_rd/iops_wr) "
  377. "cannot be used at the same time");
  378. return NULL;
  379. }
  380. on_write_error = BLOCK_ERR_STOP_ENOSPC;
  381. if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
  382. if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
  383. error_report("werror is not supported by this bus type");
  384. return NULL;
  385. }
  386. on_write_error = parse_block_error_action(buf, 0);
  387. if (on_write_error < 0) {
  388. return NULL;
  389. }
  390. }
  391. on_read_error = BLOCK_ERR_REPORT;
  392. if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
  393. if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
  394. error_report("rerror is not supported by this bus type");
  395. return NULL;
  396. }
  397. on_read_error = parse_block_error_action(buf, 1);
  398. if (on_read_error < 0) {
  399. return NULL;
  400. }
  401. }
  402. if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
  403. if (type != IF_VIRTIO) {
  404. error_report("addr is not supported by this bus type");
  405. return NULL;
  406. }
  407. }
  408. /* compute bus and unit according index */
  409. if (index != -1) {
  410. if (bus_id != 0 || unit_id != -1) {
  411. error_report("index cannot be used with bus and unit");
  412. return NULL;
  413. }
  414. bus_id = drive_index_to_bus_id(type, index);
  415. unit_id = drive_index_to_unit_id(type, index);
  416. }
  417. /* if user doesn't specify a unit_id,
  418. * try to find the first free
  419. */
  420. if (unit_id == -1) {
  421. unit_id = 0;
  422. while (drive_get(type, bus_id, unit_id) != NULL) {
  423. unit_id++;
  424. if (max_devs && unit_id >= max_devs) {
  425. unit_id -= max_devs;
  426. bus_id++;
  427. }
  428. }
  429. }
  430. /* check unit id */
  431. if (max_devs && unit_id >= max_devs) {
  432. error_report("unit %d too big (max is %d)",
  433. unit_id, max_devs - 1);
  434. return NULL;
  435. }
  436. /*
  437. * catch multiple definitions
  438. */
  439. if (drive_get(type, bus_id, unit_id) != NULL) {
  440. error_report("drive with bus=%d, unit=%d (index=%d) exists",
  441. bus_id, unit_id, index);
  442. return NULL;
  443. }
  444. /* init */
  445. dinfo = g_malloc0(sizeof(*dinfo));
  446. if ((buf = qemu_opts_id(opts)) != NULL) {
  447. dinfo->id = g_strdup(buf);
  448. } else {
  449. /* no id supplied -> create one */
  450. dinfo->id = g_malloc0(32);
  451. if (type == IF_IDE || type == IF_SCSI)
  452. mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
  453. if (max_devs)
  454. snprintf(dinfo->id, 32, "%s%i%s%i",
  455. if_name[type], bus_id, mediastr, unit_id);
  456. else
  457. snprintf(dinfo->id, 32, "%s%s%i",
  458. if_name[type], mediastr, unit_id);
  459. }
  460. dinfo->bdrv = bdrv_new(dinfo->id);
  461. dinfo->bdrv->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0;
  462. dinfo->bdrv->read_only = ro;
  463. dinfo->devaddr = devaddr;
  464. dinfo->type = type;
  465. dinfo->bus = bus_id;
  466. dinfo->unit = unit_id;
  467. dinfo->cyls = cyls;
  468. dinfo->heads = heads;
  469. dinfo->secs = secs;
  470. dinfo->trans = translation;
  471. dinfo->opts = opts;
  472. dinfo->refcount = 1;
  473. dinfo->serial = serial;
  474. QTAILQ_INSERT_TAIL(&drives, dinfo, next);
  475. bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
  476. /* disk I/O throttling */
  477. bdrv_set_io_limits(dinfo->bdrv, &io_limits);
  478. switch(type) {
  479. case IF_IDE:
  480. case IF_SCSI:
  481. case IF_XEN:
  482. case IF_NONE:
  483. dinfo->media_cd = media == MEDIA_CDROM;
  484. break;
  485. case IF_SD:
  486. case IF_FLOPPY:
  487. case IF_PFLASH:
  488. case IF_MTD:
  489. break;
  490. case IF_VIRTIO:
  491. /* add virtio block device */
  492. opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, NULL);
  493. if (arch_type == QEMU_ARCH_S390X) {
  494. qemu_opt_set(opts, "driver", "virtio-blk-s390");
  495. } else {
  496. qemu_opt_set(opts, "driver", "virtio-blk-pci");
  497. }
  498. qemu_opt_set(opts, "drive", dinfo->id);
  499. if (devaddr)
  500. qemu_opt_set(opts, "addr", devaddr);
  501. break;
  502. default:
  503. abort();
  504. }
  505. if (!file || !*file) {
  506. return dinfo;
  507. }
  508. if (snapshot) {
  509. /* always use cache=unsafe with snapshot */
  510. bdrv_flags &= ~BDRV_O_CACHE_MASK;
  511. bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
  512. }
  513. if (copy_on_read) {
  514. bdrv_flags |= BDRV_O_COPY_ON_READ;
  515. }
  516. if (runstate_check(RUN_STATE_INMIGRATE)) {
  517. bdrv_flags |= BDRV_O_INCOMING;
  518. }
  519. if (media == MEDIA_CDROM) {
  520. /* CDROM is fine for any interface, don't check. */
  521. ro = 1;
  522. } else if (ro == 1) {
  523. if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY &&
  524. type != IF_NONE && type != IF_PFLASH) {
  525. error_report("readonly not supported by this bus type");
  526. goto err;
  527. }
  528. }
  529. bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
  530. if (ro && copy_on_read) {
  531. error_report("warning: disabling copy_on_read on readonly drive");
  532. }
  533. ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
  534. if (ret < 0) {
  535. error_report("could not open disk image %s: %s",
  536. file, strerror(-ret));
  537. goto err;
  538. }
  539. if (bdrv_key_required(dinfo->bdrv))
  540. autostart = 0;
  541. return dinfo;
  542. err:
  543. bdrv_delete(dinfo->bdrv);
  544. g_free(dinfo->id);
  545. QTAILQ_REMOVE(&drives, dinfo, next);
  546. g_free(dinfo);
  547. return NULL;
  548. }
  549. void do_commit(Monitor *mon, const QDict *qdict)
  550. {
  551. const char *device = qdict_get_str(qdict, "device");
  552. BlockDriverState *bs;
  553. int ret;
  554. if (!strcmp(device, "all")) {
  555. ret = bdrv_commit_all();
  556. if (ret == -EBUSY) {
  557. qerror_report(QERR_DEVICE_IN_USE, device);
  558. return;
  559. }
  560. } else {
  561. bs = bdrv_find(device);
  562. if (!bs) {
  563. qerror_report(QERR_DEVICE_NOT_FOUND, device);
  564. return;
  565. }
  566. ret = bdrv_commit(bs);
  567. if (ret == -EBUSY) {
  568. qerror_report(QERR_DEVICE_IN_USE, device);
  569. return;
  570. }
  571. }
  572. }
  573. static void blockdev_do_action(int kind, void *data, Error **errp)
  574. {
  575. BlockdevAction action;
  576. BlockdevActionList list;
  577. action.kind = kind;
  578. action.data = data;
  579. list.value = &action;
  580. list.next = NULL;
  581. qmp_transaction(&list, errp);
  582. }
  583. void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file,
  584. bool has_format, const char *format,
  585. bool has_mode, enum NewImageMode mode,
  586. Error **errp)
  587. {
  588. BlockdevSnapshot snapshot = {
  589. .device = (char *) device,
  590. .snapshot_file = (char *) snapshot_file,
  591. .has_format = has_format,
  592. .format = (char *) format,
  593. .has_mode = has_mode,
  594. .mode = mode,
  595. };
  596. blockdev_do_action(BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, &snapshot,
  597. errp);
  598. }
  599. /* New and old BlockDriverState structs for group snapshots */
  600. typedef struct BlkTransactionStates {
  601. BlockDriverState *old_bs;
  602. BlockDriverState *new_bs;
  603. QSIMPLEQ_ENTRY(BlkTransactionStates) entry;
  604. } BlkTransactionStates;
  605. /*
  606. * 'Atomic' group snapshots. The snapshots are taken as a set, and if any fail
  607. * then we do not pivot any of the devices in the group, and abandon the
  608. * snapshots
  609. */
  610. void qmp_transaction(BlockdevActionList *dev_list, Error **errp)
  611. {
  612. int ret = 0;
  613. BlockdevActionList *dev_entry = dev_list;
  614. BlkTransactionStates *states, *next;
  615. QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionStates) snap_bdrv_states;
  616. QSIMPLEQ_INIT(&snap_bdrv_states);
  617. /* drain all i/o before any snapshots */
  618. bdrv_drain_all();
  619. /* We don't do anything in this loop that commits us to the snapshot */
  620. while (NULL != dev_entry) {
  621. BlockdevAction *dev_info = NULL;
  622. BlockDriver *proto_drv;
  623. BlockDriver *drv;
  624. int flags;
  625. enum NewImageMode mode;
  626. const char *new_image_file;
  627. const char *device;
  628. const char *format = "qcow2";
  629. dev_info = dev_entry->value;
  630. dev_entry = dev_entry->next;
  631. states = g_malloc0(sizeof(BlkTransactionStates));
  632. QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, states, entry);
  633. switch (dev_info->kind) {
  634. case BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
  635. device = dev_info->blockdev_snapshot_sync->device;
  636. if (!dev_info->blockdev_snapshot_sync->has_mode) {
  637. dev_info->blockdev_snapshot_sync->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
  638. }
  639. new_image_file = dev_info->blockdev_snapshot_sync->snapshot_file;
  640. if (dev_info->blockdev_snapshot_sync->has_format) {
  641. format = dev_info->blockdev_snapshot_sync->format;
  642. }
  643. mode = dev_info->blockdev_snapshot_sync->mode;
  644. break;
  645. default:
  646. abort();
  647. }
  648. drv = bdrv_find_format(format);
  649. if (!drv) {
  650. error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
  651. goto delete_and_fail;
  652. }
  653. states->old_bs = bdrv_find(device);
  654. if (!states->old_bs) {
  655. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  656. goto delete_and_fail;
  657. }
  658. if (!bdrv_is_inserted(states->old_bs)) {
  659. error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
  660. goto delete_and_fail;
  661. }
  662. if (bdrv_in_use(states->old_bs)) {
  663. error_set(errp, QERR_DEVICE_IN_USE, device);
  664. goto delete_and_fail;
  665. }
  666. if (!bdrv_is_read_only(states->old_bs)) {
  667. if (bdrv_flush(states->old_bs)) {
  668. error_set(errp, QERR_IO_ERROR);
  669. goto delete_and_fail;
  670. }
  671. }
  672. flags = states->old_bs->open_flags;
  673. proto_drv = bdrv_find_protocol(new_image_file);
  674. if (!proto_drv) {
  675. error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
  676. goto delete_and_fail;
  677. }
  678. /* create new image w/backing file */
  679. if (mode != NEW_IMAGE_MODE_EXISTING) {
  680. ret = bdrv_img_create(new_image_file, format,
  681. states->old_bs->filename,
  682. states->old_bs->drv->format_name,
  683. NULL, -1, flags);
  684. if (ret) {
  685. error_set(errp, QERR_OPEN_FILE_FAILED, new_image_file);
  686. goto delete_and_fail;
  687. }
  688. }
  689. /* We will manually add the backing_hd field to the bs later */
  690. states->new_bs = bdrv_new("");
  691. ret = bdrv_open(states->new_bs, new_image_file,
  692. flags | BDRV_O_NO_BACKING, drv);
  693. if (ret != 0) {
  694. error_set(errp, QERR_OPEN_FILE_FAILED, new_image_file);
  695. goto delete_and_fail;
  696. }
  697. }
  698. /* Now we are going to do the actual pivot. Everything up to this point
  699. * is reversible, but we are committed at this point */
  700. QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) {
  701. /* This removes our old bs from the bdrv_states, and adds the new bs */
  702. bdrv_append(states->new_bs, states->old_bs);
  703. }
  704. /* success */
  705. goto exit;
  706. delete_and_fail:
  707. /*
  708. * failure, and it is all-or-none; abandon each new bs, and keep using
  709. * the original bs for all images
  710. */
  711. QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) {
  712. if (states->new_bs) {
  713. bdrv_delete(states->new_bs);
  714. }
  715. }
  716. exit:
  717. QSIMPLEQ_FOREACH_SAFE(states, &snap_bdrv_states, entry, next) {
  718. g_free(states);
  719. }
  720. return;
  721. }
  722. static void eject_device(BlockDriverState *bs, int force, Error **errp)
  723. {
  724. if (bdrv_in_use(bs)) {
  725. error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
  726. return;
  727. }
  728. if (!bdrv_dev_has_removable_media(bs)) {
  729. error_set(errp, QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
  730. return;
  731. }
  732. if (bdrv_dev_is_medium_locked(bs) && !bdrv_dev_is_tray_open(bs)) {
  733. bdrv_dev_eject_request(bs, force);
  734. if (!force) {
  735. error_set(errp, QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
  736. return;
  737. }
  738. }
  739. bdrv_close(bs);
  740. }
  741. void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
  742. {
  743. BlockDriverState *bs;
  744. bs = bdrv_find(device);
  745. if (!bs) {
  746. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  747. return;
  748. }
  749. eject_device(bs, force, errp);
  750. }
  751. void qmp_block_passwd(const char *device, const char *password, Error **errp)
  752. {
  753. BlockDriverState *bs;
  754. int err;
  755. bs = bdrv_find(device);
  756. if (!bs) {
  757. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  758. return;
  759. }
  760. err = bdrv_set_key(bs, password);
  761. if (err == -EINVAL) {
  762. error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
  763. return;
  764. } else if (err < 0) {
  765. error_set(errp, QERR_INVALID_PASSWORD);
  766. return;
  767. }
  768. }
  769. static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename,
  770. int bdrv_flags, BlockDriver *drv,
  771. const char *password, Error **errp)
  772. {
  773. if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
  774. error_set(errp, QERR_OPEN_FILE_FAILED, filename);
  775. return;
  776. }
  777. if (bdrv_key_required(bs)) {
  778. if (password) {
  779. if (bdrv_set_key(bs, password) < 0) {
  780. error_set(errp, QERR_INVALID_PASSWORD);
  781. }
  782. } else {
  783. error_set(errp, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
  784. bdrv_get_encrypted_filename(bs));
  785. }
  786. } else if (password) {
  787. error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
  788. }
  789. }
  790. void qmp_change_blockdev(const char *device, const char *filename,
  791. bool has_format, const char *format, Error **errp)
  792. {
  793. BlockDriverState *bs;
  794. BlockDriver *drv = NULL;
  795. int bdrv_flags;
  796. Error *err = NULL;
  797. bs = bdrv_find(device);
  798. if (!bs) {
  799. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  800. return;
  801. }
  802. if (format) {
  803. drv = bdrv_find_whitelisted_format(format);
  804. if (!drv) {
  805. error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
  806. return;
  807. }
  808. }
  809. eject_device(bs, 0, &err);
  810. if (error_is_set(&err)) {
  811. error_propagate(errp, err);
  812. return;
  813. }
  814. bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
  815. bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
  816. qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp);
  817. }
  818. /* throttling disk I/O limits */
  819. void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
  820. int64_t bps_wr, int64_t iops, int64_t iops_rd,
  821. int64_t iops_wr, Error **errp)
  822. {
  823. BlockIOLimit io_limits;
  824. BlockDriverState *bs;
  825. bs = bdrv_find(device);
  826. if (!bs) {
  827. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  828. return;
  829. }
  830. io_limits.bps[BLOCK_IO_LIMIT_TOTAL] = bps;
  831. io_limits.bps[BLOCK_IO_LIMIT_READ] = bps_rd;
  832. io_limits.bps[BLOCK_IO_LIMIT_WRITE] = bps_wr;
  833. io_limits.iops[BLOCK_IO_LIMIT_TOTAL]= iops;
  834. io_limits.iops[BLOCK_IO_LIMIT_READ] = iops_rd;
  835. io_limits.iops[BLOCK_IO_LIMIT_WRITE]= iops_wr;
  836. if (!do_check_io_limits(&io_limits)) {
  837. error_set(errp, QERR_INVALID_PARAMETER_COMBINATION);
  838. return;
  839. }
  840. bs->io_limits = io_limits;
  841. bs->slice_time = BLOCK_IO_SLICE_TIME;
  842. if (!bs->io_limits_enabled && bdrv_io_limits_enabled(bs)) {
  843. bdrv_io_limits_enable(bs);
  844. } else if (bs->io_limits_enabled && !bdrv_io_limits_enabled(bs)) {
  845. bdrv_io_limits_disable(bs);
  846. } else {
  847. if (bs->block_timer) {
  848. qemu_mod_timer(bs->block_timer, qemu_get_clock_ns(vm_clock));
  849. }
  850. }
  851. }
  852. int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
  853. {
  854. const char *id = qdict_get_str(qdict, "id");
  855. BlockDriverState *bs;
  856. bs = bdrv_find(id);
  857. if (!bs) {
  858. qerror_report(QERR_DEVICE_NOT_FOUND, id);
  859. return -1;
  860. }
  861. if (bdrv_in_use(bs)) {
  862. qerror_report(QERR_DEVICE_IN_USE, id);
  863. return -1;
  864. }
  865. /* quiesce block driver; prevent further io */
  866. bdrv_drain_all();
  867. bdrv_flush(bs);
  868. bdrv_close(bs);
  869. /* if we have a device attached to this BlockDriverState
  870. * then we need to make the drive anonymous until the device
  871. * can be removed. If this is a drive with no device backing
  872. * then we can just get rid of the block driver state right here.
  873. */
  874. if (bdrv_get_attached_dev(bs)) {
  875. bdrv_make_anon(bs);
  876. } else {
  877. drive_uninit(drive_get_by_blockdev(bs));
  878. }
  879. return 0;
  880. }
  881. void qmp_block_resize(const char *device, int64_t size, Error **errp)
  882. {
  883. BlockDriverState *bs;
  884. bs = bdrv_find(device);
  885. if (!bs) {
  886. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  887. return;
  888. }
  889. if (size < 0) {
  890. error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
  891. return;
  892. }
  893. switch (bdrv_truncate(bs, size)) {
  894. case 0:
  895. break;
  896. case -ENOMEDIUM:
  897. error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
  898. break;
  899. case -ENOTSUP:
  900. error_set(errp, QERR_UNSUPPORTED);
  901. break;
  902. case -EACCES:
  903. error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
  904. break;
  905. case -EBUSY:
  906. error_set(errp, QERR_DEVICE_IN_USE, device);
  907. break;
  908. default:
  909. error_set(errp, QERR_UNDEFINED_ERROR);
  910. break;
  911. }
  912. }
  913. static QObject *qobject_from_block_job(BlockJob *job)
  914. {
  915. return qobject_from_jsonf("{ 'type': %s,"
  916. "'device': %s,"
  917. "'len': %" PRId64 ","
  918. "'offset': %" PRId64 ","
  919. "'speed': %" PRId64 " }",
  920. job->job_type->job_type,
  921. bdrv_get_device_name(job->bs),
  922. job->len,
  923. job->offset,
  924. job->speed);
  925. }
  926. static void block_stream_cb(void *opaque, int ret)
  927. {
  928. BlockDriverState *bs = opaque;
  929. QObject *obj;
  930. trace_block_stream_cb(bs, bs->job, ret);
  931. assert(bs->job);
  932. obj = qobject_from_block_job(bs->job);
  933. if (ret < 0) {
  934. QDict *dict = qobject_to_qdict(obj);
  935. qdict_put(dict, "error", qstring_from_str(strerror(-ret)));
  936. }
  937. if (block_job_is_cancelled(bs->job)) {
  938. monitor_protocol_event(QEVENT_BLOCK_JOB_CANCELLED, obj);
  939. } else {
  940. monitor_protocol_event(QEVENT_BLOCK_JOB_COMPLETED, obj);
  941. }
  942. qobject_decref(obj);
  943. drive_put_ref_bh_schedule(drive_get_by_blockdev(bs));
  944. }
  945. void qmp_block_stream(const char *device, bool has_base,
  946. const char *base, bool has_speed,
  947. int64_t speed, Error **errp)
  948. {
  949. BlockDriverState *bs;
  950. BlockDriverState *base_bs = NULL;
  951. Error *local_err = NULL;
  952. bs = bdrv_find(device);
  953. if (!bs) {
  954. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  955. return;
  956. }
  957. if (base) {
  958. base_bs = bdrv_find_backing_image(bs, base);
  959. if (base_bs == NULL) {
  960. error_set(errp, QERR_BASE_NOT_FOUND, base);
  961. return;
  962. }
  963. }
  964. stream_start(bs, base_bs, base, has_speed ? speed : 0,
  965. block_stream_cb, bs, &local_err);
  966. if (error_is_set(&local_err)) {
  967. error_propagate(errp, local_err);
  968. return;
  969. }
  970. /* Grab a reference so hotplug does not delete the BlockDriverState from
  971. * underneath us.
  972. */
  973. drive_get_ref(drive_get_by_blockdev(bs));
  974. trace_qmp_block_stream(bs, bs->job);
  975. }
  976. static BlockJob *find_block_job(const char *device)
  977. {
  978. BlockDriverState *bs;
  979. bs = bdrv_find(device);
  980. if (!bs || !bs->job) {
  981. return NULL;
  982. }
  983. return bs->job;
  984. }
  985. void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
  986. {
  987. BlockJob *job = find_block_job(device);
  988. if (!job) {
  989. error_set(errp, QERR_DEVICE_NOT_ACTIVE, device);
  990. return;
  991. }
  992. block_job_set_speed(job, speed, errp);
  993. }
  994. void qmp_block_job_cancel(const char *device, Error **errp)
  995. {
  996. BlockJob *job = find_block_job(device);
  997. if (!job) {
  998. error_set(errp, QERR_DEVICE_NOT_ACTIVE, device);
  999. return;
  1000. }
  1001. trace_qmp_block_job_cancel(job);
  1002. block_job_cancel(job);
  1003. }
  1004. static void do_qmp_query_block_jobs_one(void *opaque, BlockDriverState *bs)
  1005. {
  1006. BlockJobInfoList **prev = opaque;
  1007. BlockJob *job = bs->job;
  1008. if (job) {
  1009. BlockJobInfoList *elem;
  1010. BlockJobInfo *info = g_new(BlockJobInfo, 1);
  1011. *info = (BlockJobInfo){
  1012. .type = g_strdup(job->job_type->job_type),
  1013. .device = g_strdup(bdrv_get_device_name(bs)),
  1014. .len = job->len,
  1015. .offset = job->offset,
  1016. .speed = job->speed,
  1017. };
  1018. elem = g_new0(BlockJobInfoList, 1);
  1019. elem->value = info;
  1020. (*prev)->next = elem;
  1021. *prev = elem;
  1022. }
  1023. }
  1024. BlockJobInfoList *qmp_query_block_jobs(Error **errp)
  1025. {
  1026. /* Dummy is a fake list element for holding the head pointer */
  1027. BlockJobInfoList dummy = {};
  1028. BlockJobInfoList *prev = &dummy;
  1029. bdrv_iterate(do_qmp_query_block_jobs_one, &prev);
  1030. return dummy.next;
  1031. }