2
0

blockdev.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  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 "block.h"
  10. #include "blockdev.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. char devname[128];
  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. pstrcpy(devname, sizeof(devname), buf);
  277. for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
  278. ;
  279. if (type == IF_COUNT) {
  280. error_report("unsupported bus type '%s'", buf);
  281. return NULL;
  282. }
  283. } else {
  284. type = default_to_scsi ? IF_SCSI : IF_IDE;
  285. pstrcpy(devname, sizeof(devname), if_name[type]);
  286. }
  287. max_devs = if_max_devs[type];
  288. if (cyls || heads || secs) {
  289. if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
  290. error_report("invalid physical cyls number");
  291. return NULL;
  292. }
  293. if (heads < 1 || (type == IF_IDE && heads > 16)) {
  294. error_report("invalid physical heads number");
  295. return NULL;
  296. }
  297. if (secs < 1 || (type == IF_IDE && secs > 63)) {
  298. error_report("invalid physical secs number");
  299. return NULL;
  300. }
  301. }
  302. if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
  303. if (!cyls) {
  304. error_report("'%s' trans must be used with cyls, heads and secs",
  305. buf);
  306. return NULL;
  307. }
  308. if (!strcmp(buf, "none"))
  309. translation = BIOS_ATA_TRANSLATION_NONE;
  310. else if (!strcmp(buf, "lba"))
  311. translation = BIOS_ATA_TRANSLATION_LBA;
  312. else if (!strcmp(buf, "auto"))
  313. translation = BIOS_ATA_TRANSLATION_AUTO;
  314. else {
  315. error_report("'%s' invalid translation type", buf);
  316. return NULL;
  317. }
  318. }
  319. if ((buf = qemu_opt_get(opts, "media")) != NULL) {
  320. if (!strcmp(buf, "disk")) {
  321. media = MEDIA_DISK;
  322. } else if (!strcmp(buf, "cdrom")) {
  323. if (cyls || secs || heads) {
  324. error_report("CHS can't be set with media=%s", buf);
  325. return NULL;
  326. }
  327. media = MEDIA_CDROM;
  328. } else {
  329. error_report("'%s' invalid media", buf);
  330. return NULL;
  331. }
  332. }
  333. if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
  334. if (bdrv_parse_cache_flags(buf, &bdrv_flags) != 0) {
  335. error_report("invalid cache option");
  336. return NULL;
  337. }
  338. }
  339. #ifdef CONFIG_LINUX_AIO
  340. if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
  341. if (!strcmp(buf, "native")) {
  342. bdrv_flags |= BDRV_O_NATIVE_AIO;
  343. } else if (!strcmp(buf, "threads")) {
  344. /* this is the default */
  345. } else {
  346. error_report("invalid aio option");
  347. return NULL;
  348. }
  349. }
  350. #endif
  351. if ((buf = qemu_opt_get(opts, "format")) != NULL) {
  352. if (strcmp(buf, "?") == 0) {
  353. error_printf("Supported formats:");
  354. bdrv_iterate_format(bdrv_format_print, NULL);
  355. error_printf("\n");
  356. return NULL;
  357. }
  358. drv = bdrv_find_whitelisted_format(buf);
  359. if (!drv) {
  360. error_report("'%s' invalid format", buf);
  361. return NULL;
  362. }
  363. }
  364. /* disk I/O throttling */
  365. io_limits.bps[BLOCK_IO_LIMIT_TOTAL] =
  366. qemu_opt_get_number(opts, "bps", 0);
  367. io_limits.bps[BLOCK_IO_LIMIT_READ] =
  368. qemu_opt_get_number(opts, "bps_rd", 0);
  369. io_limits.bps[BLOCK_IO_LIMIT_WRITE] =
  370. qemu_opt_get_number(opts, "bps_wr", 0);
  371. io_limits.iops[BLOCK_IO_LIMIT_TOTAL] =
  372. qemu_opt_get_number(opts, "iops", 0);
  373. io_limits.iops[BLOCK_IO_LIMIT_READ] =
  374. qemu_opt_get_number(opts, "iops_rd", 0);
  375. io_limits.iops[BLOCK_IO_LIMIT_WRITE] =
  376. qemu_opt_get_number(opts, "iops_wr", 0);
  377. if (!do_check_io_limits(&io_limits)) {
  378. error_report("bps(iops) and bps_rd/bps_wr(iops_rd/iops_wr) "
  379. "cannot be used at the same time");
  380. return NULL;
  381. }
  382. on_write_error = BLOCK_ERR_STOP_ENOSPC;
  383. if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
  384. if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
  385. error_report("werror is not supported by this bus type");
  386. return NULL;
  387. }
  388. on_write_error = parse_block_error_action(buf, 0);
  389. if (on_write_error < 0) {
  390. return NULL;
  391. }
  392. }
  393. on_read_error = BLOCK_ERR_REPORT;
  394. if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
  395. if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
  396. error_report("rerror is not supported by this bus type");
  397. return NULL;
  398. }
  399. on_read_error = parse_block_error_action(buf, 1);
  400. if (on_read_error < 0) {
  401. return NULL;
  402. }
  403. }
  404. if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
  405. if (type != IF_VIRTIO) {
  406. error_report("addr is not supported by this bus type");
  407. return NULL;
  408. }
  409. }
  410. /* compute bus and unit according index */
  411. if (index != -1) {
  412. if (bus_id != 0 || unit_id != -1) {
  413. error_report("index cannot be used with bus and unit");
  414. return NULL;
  415. }
  416. bus_id = drive_index_to_bus_id(type, index);
  417. unit_id = drive_index_to_unit_id(type, index);
  418. }
  419. /* if user doesn't specify a unit_id,
  420. * try to find the first free
  421. */
  422. if (unit_id == -1) {
  423. unit_id = 0;
  424. while (drive_get(type, bus_id, unit_id) != NULL) {
  425. unit_id++;
  426. if (max_devs && unit_id >= max_devs) {
  427. unit_id -= max_devs;
  428. bus_id++;
  429. }
  430. }
  431. }
  432. /* check unit id */
  433. if (max_devs && unit_id >= max_devs) {
  434. error_report("unit %d too big (max is %d)",
  435. unit_id, max_devs - 1);
  436. return NULL;
  437. }
  438. /*
  439. * catch multiple definitions
  440. */
  441. if (drive_get(type, bus_id, unit_id) != NULL) {
  442. error_report("drive with bus=%d, unit=%d (index=%d) exists",
  443. bus_id, unit_id, index);
  444. return NULL;
  445. }
  446. /* init */
  447. dinfo = g_malloc0(sizeof(*dinfo));
  448. if ((buf = qemu_opts_id(opts)) != NULL) {
  449. dinfo->id = g_strdup(buf);
  450. } else {
  451. /* no id supplied -> create one */
  452. dinfo->id = g_malloc0(32);
  453. if (type == IF_IDE || type == IF_SCSI)
  454. mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
  455. if (max_devs)
  456. snprintf(dinfo->id, 32, "%s%i%s%i",
  457. devname, bus_id, mediastr, unit_id);
  458. else
  459. snprintf(dinfo->id, 32, "%s%s%i",
  460. devname, mediastr, unit_id);
  461. }
  462. dinfo->bdrv = bdrv_new(dinfo->id);
  463. dinfo->devaddr = devaddr;
  464. dinfo->type = type;
  465. dinfo->bus = bus_id;
  466. dinfo->unit = unit_id;
  467. dinfo->opts = opts;
  468. dinfo->refcount = 1;
  469. if (serial) {
  470. pstrcpy(dinfo->serial, sizeof(dinfo->serial), serial);
  471. }
  472. QTAILQ_INSERT_TAIL(&drives, dinfo, next);
  473. bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
  474. /* disk I/O throttling */
  475. bdrv_set_io_limits(dinfo->bdrv, &io_limits);
  476. switch(type) {
  477. case IF_IDE:
  478. case IF_SCSI:
  479. case IF_XEN:
  480. case IF_NONE:
  481. switch(media) {
  482. case MEDIA_DISK:
  483. if (cyls != 0) {
  484. bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
  485. bdrv_set_translation_hint(dinfo->bdrv, translation);
  486. }
  487. break;
  488. case MEDIA_CDROM:
  489. dinfo->media_cd = 1;
  490. break;
  491. }
  492. break;
  493. case IF_SD:
  494. case IF_FLOPPY:
  495. case IF_PFLASH:
  496. case IF_MTD:
  497. break;
  498. case IF_VIRTIO:
  499. /* add virtio block device */
  500. opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
  501. if (arch_type == QEMU_ARCH_S390X) {
  502. qemu_opt_set(opts, "driver", "virtio-blk-s390");
  503. } else {
  504. qemu_opt_set(opts, "driver", "virtio-blk-pci");
  505. }
  506. qemu_opt_set(opts, "drive", dinfo->id);
  507. if (devaddr)
  508. qemu_opt_set(opts, "addr", devaddr);
  509. break;
  510. default:
  511. abort();
  512. }
  513. if (!file || !*file) {
  514. return dinfo;
  515. }
  516. if (snapshot) {
  517. /* always use cache=unsafe with snapshot */
  518. bdrv_flags &= ~BDRV_O_CACHE_MASK;
  519. bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
  520. }
  521. if (copy_on_read) {
  522. bdrv_flags |= BDRV_O_COPY_ON_READ;
  523. }
  524. if (runstate_check(RUN_STATE_INMIGRATE)) {
  525. bdrv_flags |= BDRV_O_INCOMING;
  526. }
  527. if (media == MEDIA_CDROM) {
  528. /* CDROM is fine for any interface, don't check. */
  529. ro = 1;
  530. } else if (ro == 1) {
  531. if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY &&
  532. type != IF_NONE && type != IF_PFLASH) {
  533. error_report("readonly not supported by this bus type");
  534. goto err;
  535. }
  536. }
  537. bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
  538. ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
  539. if (ret < 0) {
  540. error_report("could not open disk image %s: %s",
  541. file, strerror(-ret));
  542. goto err;
  543. }
  544. if (bdrv_key_required(dinfo->bdrv))
  545. autostart = 0;
  546. return dinfo;
  547. err:
  548. bdrv_delete(dinfo->bdrv);
  549. g_free(dinfo->id);
  550. QTAILQ_REMOVE(&drives, dinfo, next);
  551. g_free(dinfo);
  552. return NULL;
  553. }
  554. void do_commit(Monitor *mon, const QDict *qdict)
  555. {
  556. const char *device = qdict_get_str(qdict, "device");
  557. BlockDriverState *bs;
  558. int ret;
  559. if (!strcmp(device, "all")) {
  560. ret = bdrv_commit_all();
  561. if (ret == -EBUSY) {
  562. qerror_report(QERR_DEVICE_IN_USE, device);
  563. return;
  564. }
  565. } else {
  566. bs = bdrv_find(device);
  567. if (!bs) {
  568. qerror_report(QERR_DEVICE_NOT_FOUND, device);
  569. return;
  570. }
  571. ret = bdrv_commit(bs);
  572. if (ret == -EBUSY) {
  573. qerror_report(QERR_DEVICE_IN_USE, device);
  574. return;
  575. }
  576. }
  577. }
  578. static void blockdev_do_action(int kind, void *data, Error **errp)
  579. {
  580. BlockdevAction action;
  581. BlockdevActionList list;
  582. action.kind = kind;
  583. action.data = data;
  584. list.value = &action;
  585. list.next = NULL;
  586. qmp_transaction(&list, errp);
  587. }
  588. void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file,
  589. bool has_format, const char *format,
  590. bool has_mode, enum NewImageMode mode,
  591. Error **errp)
  592. {
  593. BlockdevSnapshot snapshot = {
  594. .device = (char *) device,
  595. .snapshot_file = (char *) snapshot_file,
  596. .has_format = has_format,
  597. .format = (char *) format,
  598. .has_mode = has_mode,
  599. .mode = mode,
  600. };
  601. blockdev_do_action(BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, &snapshot,
  602. errp);
  603. }
  604. /* New and old BlockDriverState structs for group snapshots */
  605. typedef struct BlkTransactionStates {
  606. BlockDriverState *old_bs;
  607. BlockDriverState *new_bs;
  608. QSIMPLEQ_ENTRY(BlkTransactionStates) entry;
  609. } BlkTransactionStates;
  610. /*
  611. * 'Atomic' group snapshots. The snapshots are taken as a set, and if any fail
  612. * then we do not pivot any of the devices in the group, and abandon the
  613. * snapshots
  614. */
  615. void qmp_transaction(BlockdevActionList *dev_list, Error **errp)
  616. {
  617. int ret = 0;
  618. BlockdevActionList *dev_entry = dev_list;
  619. BlkTransactionStates *states, *next;
  620. QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionStates) snap_bdrv_states;
  621. QSIMPLEQ_INIT(&snap_bdrv_states);
  622. /* drain all i/o before any snapshots */
  623. bdrv_drain_all();
  624. /* We don't do anything in this loop that commits us to the snapshot */
  625. while (NULL != dev_entry) {
  626. BlockdevAction *dev_info = NULL;
  627. BlockDriver *proto_drv;
  628. BlockDriver *drv;
  629. int flags;
  630. enum NewImageMode mode;
  631. const char *new_image_file;
  632. const char *device;
  633. const char *format = "qcow2";
  634. dev_info = dev_entry->value;
  635. dev_entry = dev_entry->next;
  636. states = g_malloc0(sizeof(BlkTransactionStates));
  637. QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, states, entry);
  638. switch (dev_info->kind) {
  639. case BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
  640. device = dev_info->blockdev_snapshot_sync->device;
  641. if (!dev_info->blockdev_snapshot_sync->has_mode) {
  642. dev_info->blockdev_snapshot_sync->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
  643. }
  644. new_image_file = dev_info->blockdev_snapshot_sync->snapshot_file;
  645. if (dev_info->blockdev_snapshot_sync->has_format) {
  646. format = dev_info->blockdev_snapshot_sync->format;
  647. }
  648. mode = dev_info->blockdev_snapshot_sync->mode;
  649. break;
  650. default:
  651. abort();
  652. }
  653. drv = bdrv_find_format(format);
  654. if (!drv) {
  655. error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
  656. goto delete_and_fail;
  657. }
  658. states->old_bs = bdrv_find(device);
  659. if (!states->old_bs) {
  660. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  661. goto delete_and_fail;
  662. }
  663. if (!bdrv_is_inserted(states->old_bs)) {
  664. error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
  665. goto delete_and_fail;
  666. }
  667. if (bdrv_in_use(states->old_bs)) {
  668. error_set(errp, QERR_DEVICE_IN_USE, device);
  669. goto delete_and_fail;
  670. }
  671. if (!bdrv_is_read_only(states->old_bs)) {
  672. if (bdrv_flush(states->old_bs)) {
  673. error_set(errp, QERR_IO_ERROR);
  674. goto delete_and_fail;
  675. }
  676. }
  677. flags = states->old_bs->open_flags;
  678. proto_drv = bdrv_find_protocol(new_image_file);
  679. if (!proto_drv) {
  680. error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
  681. goto delete_and_fail;
  682. }
  683. /* create new image w/backing file */
  684. if (mode != NEW_IMAGE_MODE_EXISTING) {
  685. ret = bdrv_img_create(new_image_file, format,
  686. states->old_bs->filename,
  687. states->old_bs->drv->format_name,
  688. NULL, -1, flags);
  689. if (ret) {
  690. error_set(errp, QERR_OPEN_FILE_FAILED, new_image_file);
  691. goto delete_and_fail;
  692. }
  693. }
  694. /* We will manually add the backing_hd field to the bs later */
  695. states->new_bs = bdrv_new("");
  696. ret = bdrv_open(states->new_bs, new_image_file,
  697. flags | BDRV_O_NO_BACKING, drv);
  698. if (ret != 0) {
  699. error_set(errp, QERR_OPEN_FILE_FAILED, new_image_file);
  700. goto delete_and_fail;
  701. }
  702. }
  703. /* Now we are going to do the actual pivot. Everything up to this point
  704. * is reversible, but we are committed at this point */
  705. QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) {
  706. /* This removes our old bs from the bdrv_states, and adds the new bs */
  707. bdrv_append(states->new_bs, states->old_bs);
  708. }
  709. /* success */
  710. goto exit;
  711. delete_and_fail:
  712. /*
  713. * failure, and it is all-or-none; abandon each new bs, and keep using
  714. * the original bs for all images
  715. */
  716. QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) {
  717. if (states->new_bs) {
  718. bdrv_delete(states->new_bs);
  719. }
  720. }
  721. exit:
  722. QSIMPLEQ_FOREACH_SAFE(states, &snap_bdrv_states, entry, next) {
  723. g_free(states);
  724. }
  725. return;
  726. }
  727. static void eject_device(BlockDriverState *bs, int force, Error **errp)
  728. {
  729. if (bdrv_in_use(bs)) {
  730. error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs));
  731. return;
  732. }
  733. if (!bdrv_dev_has_removable_media(bs)) {
  734. error_set(errp, QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
  735. return;
  736. }
  737. if (bdrv_dev_is_medium_locked(bs) && !bdrv_dev_is_tray_open(bs)) {
  738. bdrv_dev_eject_request(bs, force);
  739. if (!force) {
  740. error_set(errp, QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
  741. return;
  742. }
  743. }
  744. bdrv_close(bs);
  745. }
  746. void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
  747. {
  748. BlockDriverState *bs;
  749. bs = bdrv_find(device);
  750. if (!bs) {
  751. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  752. return;
  753. }
  754. eject_device(bs, force, errp);
  755. }
  756. void qmp_block_passwd(const char *device, const char *password, Error **errp)
  757. {
  758. BlockDriverState *bs;
  759. int err;
  760. bs = bdrv_find(device);
  761. if (!bs) {
  762. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  763. return;
  764. }
  765. err = bdrv_set_key(bs, password);
  766. if (err == -EINVAL) {
  767. error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
  768. return;
  769. } else if (err < 0) {
  770. error_set(errp, QERR_INVALID_PASSWORD);
  771. return;
  772. }
  773. }
  774. static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename,
  775. int bdrv_flags, BlockDriver *drv,
  776. const char *password, Error **errp)
  777. {
  778. if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
  779. error_set(errp, QERR_OPEN_FILE_FAILED, filename);
  780. return;
  781. }
  782. if (bdrv_key_required(bs)) {
  783. if (password) {
  784. if (bdrv_set_key(bs, password) < 0) {
  785. error_set(errp, QERR_INVALID_PASSWORD);
  786. }
  787. } else {
  788. error_set(errp, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
  789. bdrv_get_encrypted_filename(bs));
  790. }
  791. } else if (password) {
  792. error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
  793. }
  794. }
  795. void qmp_change_blockdev(const char *device, const char *filename,
  796. bool has_format, const char *format, Error **errp)
  797. {
  798. BlockDriverState *bs;
  799. BlockDriver *drv = NULL;
  800. int bdrv_flags;
  801. Error *err = NULL;
  802. bs = bdrv_find(device);
  803. if (!bs) {
  804. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  805. return;
  806. }
  807. if (format) {
  808. drv = bdrv_find_whitelisted_format(format);
  809. if (!drv) {
  810. error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
  811. return;
  812. }
  813. }
  814. eject_device(bs, 0, &err);
  815. if (error_is_set(&err)) {
  816. error_propagate(errp, err);
  817. return;
  818. }
  819. bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
  820. bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
  821. qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp);
  822. }
  823. /* throttling disk I/O limits */
  824. void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd,
  825. int64_t bps_wr, int64_t iops, int64_t iops_rd,
  826. int64_t iops_wr, Error **errp)
  827. {
  828. BlockIOLimit io_limits;
  829. BlockDriverState *bs;
  830. bs = bdrv_find(device);
  831. if (!bs) {
  832. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  833. return;
  834. }
  835. io_limits.bps[BLOCK_IO_LIMIT_TOTAL] = bps;
  836. io_limits.bps[BLOCK_IO_LIMIT_READ] = bps_rd;
  837. io_limits.bps[BLOCK_IO_LIMIT_WRITE] = bps_wr;
  838. io_limits.iops[BLOCK_IO_LIMIT_TOTAL]= iops;
  839. io_limits.iops[BLOCK_IO_LIMIT_READ] = iops_rd;
  840. io_limits.iops[BLOCK_IO_LIMIT_WRITE]= iops_wr;
  841. if (!do_check_io_limits(&io_limits)) {
  842. error_set(errp, QERR_INVALID_PARAMETER_COMBINATION);
  843. return;
  844. }
  845. bs->io_limits = io_limits;
  846. bs->slice_time = BLOCK_IO_SLICE_TIME;
  847. if (!bs->io_limits_enabled && bdrv_io_limits_enabled(bs)) {
  848. bdrv_io_limits_enable(bs);
  849. } else if (bs->io_limits_enabled && !bdrv_io_limits_enabled(bs)) {
  850. bdrv_io_limits_disable(bs);
  851. } else {
  852. if (bs->block_timer) {
  853. qemu_mod_timer(bs->block_timer, qemu_get_clock_ns(vm_clock));
  854. }
  855. }
  856. }
  857. int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
  858. {
  859. const char *id = qdict_get_str(qdict, "id");
  860. BlockDriverState *bs;
  861. bs = bdrv_find(id);
  862. if (!bs) {
  863. qerror_report(QERR_DEVICE_NOT_FOUND, id);
  864. return -1;
  865. }
  866. if (bdrv_in_use(bs)) {
  867. qerror_report(QERR_DEVICE_IN_USE, id);
  868. return -1;
  869. }
  870. /* quiesce block driver; prevent further io */
  871. bdrv_drain_all();
  872. bdrv_flush(bs);
  873. bdrv_close(bs);
  874. /* if we have a device attached to this BlockDriverState
  875. * then we need to make the drive anonymous until the device
  876. * can be removed. If this is a drive with no device backing
  877. * then we can just get rid of the block driver state right here.
  878. */
  879. if (bdrv_get_attached_dev(bs)) {
  880. bdrv_make_anon(bs);
  881. } else {
  882. drive_uninit(drive_get_by_blockdev(bs));
  883. }
  884. return 0;
  885. }
  886. void qmp_block_resize(const char *device, int64_t size, Error **errp)
  887. {
  888. BlockDriverState *bs;
  889. bs = bdrv_find(device);
  890. if (!bs) {
  891. error_set(errp, QERR_DEVICE_NOT_FOUND, device);
  892. return;
  893. }
  894. if (size < 0) {
  895. error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size");
  896. return;
  897. }
  898. switch (bdrv_truncate(bs, size)) {
  899. case 0:
  900. break;
  901. case -ENOMEDIUM:
  902. error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
  903. break;
  904. case -ENOTSUP:
  905. error_set(errp, QERR_UNSUPPORTED);
  906. break;
  907. case -EACCES:
  908. error_set(errp, QERR_DEVICE_IS_READ_ONLY, device);
  909. break;
  910. case -EBUSY:
  911. error_set(errp, QERR_DEVICE_IN_USE, device);
  912. break;
  913. default:
  914. error_set(errp, QERR_UNDEFINED_ERROR);
  915. break;
  916. }
  917. }
  918. static QObject *qobject_from_block_job(BlockJob *job)
  919. {
  920. return qobject_from_jsonf("{ 'type': %s,"
  921. "'device': %s,"
  922. "'len': %" PRId64 ","
  923. "'offset': %" PRId64 ","
  924. "'speed': %" PRId64 " }",
  925. job->job_type->job_type,
  926. bdrv_get_device_name(job->bs),
  927. job->len,
  928. job->offset,
  929. job->speed);
  930. }
  931. static void block_stream_cb(void *opaque, int ret)
  932. {
  933. BlockDriverState *bs = opaque;
  934. QObject *obj;
  935. trace_block_stream_cb(bs, bs->job, ret);
  936. assert(bs->job);
  937. obj = qobject_from_block_job(bs->job);
  938. if (ret < 0) {
  939. QDict *dict = qobject_to_qdict(obj);
  940. qdict_put(dict, "error", qstring_from_str(strerror(-ret)));
  941. }
  942. if (block_job_is_cancelled(bs->job)) {
  943. monitor_protocol_event(QEVENT_BLOCK_JOB_CANCELLED, obj);
  944. } else {
  945. monitor_protocol_event(QEVENT_BLOCK_JOB_COMPLETED, obj);
  946. }
  947. qobject_decref(obj);
  948. drive_put_ref_bh_schedule(drive_get_by_blockdev(bs));
  949. }
  950. void qmp_block_stream(const char *device, bool has_base,
  951. const char *base, bool has_speed,
  952. int64_t speed, Error **errp)
  953. {
  954. BlockDriverState *bs;
  955. BlockDriverState *base_bs = NULL;
  956. Error *local_err = NULL;
  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. block_stream_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. static BlockJob *find_block_job(const char *device)
  982. {
  983. BlockDriverState *bs;
  984. bs = bdrv_find(device);
  985. if (!bs || !bs->job) {
  986. return NULL;
  987. }
  988. return bs->job;
  989. }
  990. void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp)
  991. {
  992. BlockJob *job = find_block_job(device);
  993. if (!job) {
  994. error_set(errp, QERR_DEVICE_NOT_ACTIVE, device);
  995. return;
  996. }
  997. block_job_set_speed(job, speed, errp);
  998. }
  999. void qmp_block_job_cancel(const char *device, Error **errp)
  1000. {
  1001. BlockJob *job = find_block_job(device);
  1002. if (!job) {
  1003. error_set(errp, QERR_DEVICE_NOT_ACTIVE, device);
  1004. return;
  1005. }
  1006. trace_qmp_block_job_cancel(job);
  1007. block_job_cancel(job);
  1008. }
  1009. static void do_qmp_query_block_jobs_one(void *opaque, BlockDriverState *bs)
  1010. {
  1011. BlockJobInfoList **prev = opaque;
  1012. BlockJob *job = bs->job;
  1013. if (job) {
  1014. BlockJobInfoList *elem;
  1015. BlockJobInfo *info = g_new(BlockJobInfo, 1);
  1016. *info = (BlockJobInfo){
  1017. .type = g_strdup(job->job_type->job_type),
  1018. .device = g_strdup(bdrv_get_device_name(bs)),
  1019. .len = job->len,
  1020. .offset = job->offset,
  1021. .speed = job->speed,
  1022. };
  1023. elem = g_new0(BlockJobInfoList, 1);
  1024. elem->value = info;
  1025. (*prev)->next = elem;
  1026. *prev = elem;
  1027. }
  1028. }
  1029. BlockJobInfoList *qmp_query_block_jobs(Error **errp)
  1030. {
  1031. /* Dummy is a fake list element for holding the head pointer */
  1032. BlockJobInfoList dummy = {};
  1033. BlockJobInfoList *prev = &dummy;
  1034. bdrv_iterate(do_qmp_query_block_jobs_one, &prev);
  1035. return dummy.next;
  1036. }