blockdev.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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 "sysemu.h"
  16. #include "block_int.h"
  17. static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
  18. static const char *const if_name[IF_COUNT] = {
  19. [IF_NONE] = "none",
  20. [IF_IDE] = "ide",
  21. [IF_SCSI] = "scsi",
  22. [IF_FLOPPY] = "floppy",
  23. [IF_PFLASH] = "pflash",
  24. [IF_MTD] = "mtd",
  25. [IF_SD] = "sd",
  26. [IF_VIRTIO] = "virtio",
  27. [IF_XEN] = "xen",
  28. };
  29. static const int if_max_devs[IF_COUNT] = {
  30. /*
  31. * Do not change these numbers! They govern how drive option
  32. * index maps to unit and bus. That mapping is ABI.
  33. *
  34. * All controllers used to imlement if=T drives need to support
  35. * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
  36. * Otherwise, some index values map to "impossible" bus, unit
  37. * values.
  38. *
  39. * For instance, if you change [IF_SCSI] to 255, -drive
  40. * if=scsi,index=12 no longer means bus=1,unit=5, but
  41. * bus=0,unit=12. With an lsi53c895a controller (7 units max),
  42. * the drive can't be set up. Regression.
  43. */
  44. [IF_IDE] = 2,
  45. [IF_SCSI] = 7,
  46. };
  47. /*
  48. * We automatically delete the drive when a device using it gets
  49. * unplugged. Questionable feature, but we can't just drop it.
  50. * Device models call blockdev_mark_auto_del() to schedule the
  51. * automatic deletion, and generic qdev code calls blockdev_auto_del()
  52. * when deletion is actually safe.
  53. */
  54. void blockdev_mark_auto_del(BlockDriverState *bs)
  55. {
  56. DriveInfo *dinfo = drive_get_by_blockdev(bs);
  57. if (dinfo) {
  58. dinfo->auto_del = 1;
  59. }
  60. }
  61. void blockdev_auto_del(BlockDriverState *bs)
  62. {
  63. DriveInfo *dinfo = drive_get_by_blockdev(bs);
  64. if (dinfo && dinfo->auto_del) {
  65. drive_put_ref(dinfo);
  66. }
  67. }
  68. static int drive_index_to_bus_id(BlockInterfaceType type, int index)
  69. {
  70. int max_devs = if_max_devs[type];
  71. return max_devs ? index / max_devs : 0;
  72. }
  73. static int drive_index_to_unit_id(BlockInterfaceType type, int index)
  74. {
  75. int max_devs = if_max_devs[type];
  76. return max_devs ? index % max_devs : index;
  77. }
  78. QemuOpts *drive_def(const char *optstr)
  79. {
  80. return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
  81. }
  82. QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
  83. const char *optstr)
  84. {
  85. QemuOpts *opts;
  86. char buf[32];
  87. opts = drive_def(optstr);
  88. if (!opts) {
  89. return NULL;
  90. }
  91. if (type != IF_DEFAULT) {
  92. qemu_opt_set(opts, "if", if_name[type]);
  93. }
  94. if (index >= 0) {
  95. snprintf(buf, sizeof(buf), "%d", index);
  96. qemu_opt_set(opts, "index", buf);
  97. }
  98. if (file)
  99. qemu_opt_set(opts, "file", file);
  100. return opts;
  101. }
  102. DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
  103. {
  104. DriveInfo *dinfo;
  105. /* seek interface, bus and unit */
  106. QTAILQ_FOREACH(dinfo, &drives, next) {
  107. if (dinfo->type == type &&
  108. dinfo->bus == bus &&
  109. dinfo->unit == unit)
  110. return dinfo;
  111. }
  112. return NULL;
  113. }
  114. DriveInfo *drive_get_by_index(BlockInterfaceType type, int index)
  115. {
  116. return drive_get(type,
  117. drive_index_to_bus_id(type, index),
  118. drive_index_to_unit_id(type, index));
  119. }
  120. int drive_get_max_bus(BlockInterfaceType type)
  121. {
  122. int max_bus;
  123. DriveInfo *dinfo;
  124. max_bus = -1;
  125. QTAILQ_FOREACH(dinfo, &drives, next) {
  126. if(dinfo->type == type &&
  127. dinfo->bus > max_bus)
  128. max_bus = dinfo->bus;
  129. }
  130. return max_bus;
  131. }
  132. /* Get a block device. This should only be used for single-drive devices
  133. (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
  134. appropriate bus. */
  135. DriveInfo *drive_get_next(BlockInterfaceType type)
  136. {
  137. static int next_block_unit[IF_COUNT];
  138. return drive_get(type, 0, next_block_unit[type]++);
  139. }
  140. DriveInfo *drive_get_by_blockdev(BlockDriverState *bs)
  141. {
  142. DriveInfo *dinfo;
  143. QTAILQ_FOREACH(dinfo, &drives, next) {
  144. if (dinfo->bdrv == bs) {
  145. return dinfo;
  146. }
  147. }
  148. return NULL;
  149. }
  150. static void bdrv_format_print(void *opaque, const char *name)
  151. {
  152. error_printf(" %s", name);
  153. }
  154. static void drive_uninit(DriveInfo *dinfo)
  155. {
  156. qemu_opts_del(dinfo->opts);
  157. bdrv_delete(dinfo->bdrv);
  158. g_free(dinfo->id);
  159. QTAILQ_REMOVE(&drives, dinfo, next);
  160. g_free(dinfo);
  161. }
  162. void drive_put_ref(DriveInfo *dinfo)
  163. {
  164. assert(dinfo->refcount);
  165. if (--dinfo->refcount == 0) {
  166. drive_uninit(dinfo);
  167. }
  168. }
  169. void drive_get_ref(DriveInfo *dinfo)
  170. {
  171. dinfo->refcount++;
  172. }
  173. static int parse_block_error_action(const char *buf, int is_read)
  174. {
  175. if (!strcmp(buf, "ignore")) {
  176. return BLOCK_ERR_IGNORE;
  177. } else if (!is_read && !strcmp(buf, "enospc")) {
  178. return BLOCK_ERR_STOP_ENOSPC;
  179. } else if (!strcmp(buf, "stop")) {
  180. return BLOCK_ERR_STOP_ANY;
  181. } else if (!strcmp(buf, "report")) {
  182. return BLOCK_ERR_REPORT;
  183. } else {
  184. error_report("'%s' invalid %s error action",
  185. buf, is_read ? "read" : "write");
  186. return -1;
  187. }
  188. }
  189. DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)
  190. {
  191. const char *buf;
  192. const char *file = NULL;
  193. char devname[128];
  194. const char *serial;
  195. const char *mediastr = "";
  196. BlockInterfaceType type;
  197. enum { MEDIA_DISK, MEDIA_CDROM } media;
  198. int bus_id, unit_id;
  199. int cyls, heads, secs, translation;
  200. BlockDriver *drv = NULL;
  201. int max_devs;
  202. int index;
  203. int ro = 0;
  204. int bdrv_flags = 0;
  205. int on_read_error, on_write_error;
  206. const char *devaddr;
  207. DriveInfo *dinfo;
  208. int snapshot = 0;
  209. int ret;
  210. translation = BIOS_ATA_TRANSLATION_AUTO;
  211. media = MEDIA_DISK;
  212. /* extract parameters */
  213. bus_id = qemu_opt_get_number(opts, "bus", 0);
  214. unit_id = qemu_opt_get_number(opts, "unit", -1);
  215. index = qemu_opt_get_number(opts, "index", -1);
  216. cyls = qemu_opt_get_number(opts, "cyls", 0);
  217. heads = qemu_opt_get_number(opts, "heads", 0);
  218. secs = qemu_opt_get_number(opts, "secs", 0);
  219. snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
  220. ro = qemu_opt_get_bool(opts, "readonly", 0);
  221. file = qemu_opt_get(opts, "file");
  222. serial = qemu_opt_get(opts, "serial");
  223. if ((buf = qemu_opt_get(opts, "if")) != NULL) {
  224. pstrcpy(devname, sizeof(devname), buf);
  225. for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
  226. ;
  227. if (type == IF_COUNT) {
  228. error_report("unsupported bus type '%s'", buf);
  229. return NULL;
  230. }
  231. } else {
  232. type = default_to_scsi ? IF_SCSI : IF_IDE;
  233. pstrcpy(devname, sizeof(devname), if_name[type]);
  234. }
  235. max_devs = if_max_devs[type];
  236. if (cyls || heads || secs) {
  237. if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
  238. error_report("invalid physical cyls number");
  239. return NULL;
  240. }
  241. if (heads < 1 || (type == IF_IDE && heads > 16)) {
  242. error_report("invalid physical heads number");
  243. return NULL;
  244. }
  245. if (secs < 1 || (type == IF_IDE && secs > 63)) {
  246. error_report("invalid physical secs number");
  247. return NULL;
  248. }
  249. }
  250. if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
  251. if (!cyls) {
  252. error_report("'%s' trans must be used with cyls, heads and secs",
  253. buf);
  254. return NULL;
  255. }
  256. if (!strcmp(buf, "none"))
  257. translation = BIOS_ATA_TRANSLATION_NONE;
  258. else if (!strcmp(buf, "lba"))
  259. translation = BIOS_ATA_TRANSLATION_LBA;
  260. else if (!strcmp(buf, "auto"))
  261. translation = BIOS_ATA_TRANSLATION_AUTO;
  262. else {
  263. error_report("'%s' invalid translation type", buf);
  264. return NULL;
  265. }
  266. }
  267. if ((buf = qemu_opt_get(opts, "media")) != NULL) {
  268. if (!strcmp(buf, "disk")) {
  269. media = MEDIA_DISK;
  270. } else if (!strcmp(buf, "cdrom")) {
  271. if (cyls || secs || heads) {
  272. error_report("CHS can't be set with media=%s", buf);
  273. return NULL;
  274. }
  275. media = MEDIA_CDROM;
  276. } else {
  277. error_report("'%s' invalid media", buf);
  278. return NULL;
  279. }
  280. }
  281. if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
  282. if (bdrv_parse_cache_flags(buf, &bdrv_flags) != 0) {
  283. error_report("invalid cache option");
  284. return NULL;
  285. }
  286. }
  287. #ifdef CONFIG_LINUX_AIO
  288. if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
  289. if (!strcmp(buf, "native")) {
  290. bdrv_flags |= BDRV_O_NATIVE_AIO;
  291. } else if (!strcmp(buf, "threads")) {
  292. /* this is the default */
  293. } else {
  294. error_report("invalid aio option");
  295. return NULL;
  296. }
  297. }
  298. #endif
  299. if ((buf = qemu_opt_get(opts, "format")) != NULL) {
  300. if (strcmp(buf, "?") == 0) {
  301. error_printf("Supported formats:");
  302. bdrv_iterate_format(bdrv_format_print, NULL);
  303. error_printf("\n");
  304. return NULL;
  305. }
  306. drv = bdrv_find_whitelisted_format(buf);
  307. if (!drv) {
  308. error_report("'%s' invalid format", buf);
  309. return NULL;
  310. }
  311. }
  312. on_write_error = BLOCK_ERR_STOP_ENOSPC;
  313. if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
  314. if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
  315. error_report("werror is not supported by this bus type");
  316. return NULL;
  317. }
  318. on_write_error = parse_block_error_action(buf, 0);
  319. if (on_write_error < 0) {
  320. return NULL;
  321. }
  322. }
  323. on_read_error = BLOCK_ERR_REPORT;
  324. if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
  325. if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) {
  326. error_report("rerror is not supported by this bus type");
  327. return NULL;
  328. }
  329. on_read_error = parse_block_error_action(buf, 1);
  330. if (on_read_error < 0) {
  331. return NULL;
  332. }
  333. }
  334. if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
  335. if (type != IF_VIRTIO) {
  336. error_report("addr is not supported by this bus type");
  337. return NULL;
  338. }
  339. }
  340. /* compute bus and unit according index */
  341. if (index != -1) {
  342. if (bus_id != 0 || unit_id != -1) {
  343. error_report("index cannot be used with bus and unit");
  344. return NULL;
  345. }
  346. bus_id = drive_index_to_bus_id(type, index);
  347. unit_id = drive_index_to_unit_id(type, index);
  348. }
  349. /* if user doesn't specify a unit_id,
  350. * try to find the first free
  351. */
  352. if (unit_id == -1) {
  353. unit_id = 0;
  354. while (drive_get(type, bus_id, unit_id) != NULL) {
  355. unit_id++;
  356. if (max_devs && unit_id >= max_devs) {
  357. unit_id -= max_devs;
  358. bus_id++;
  359. }
  360. }
  361. }
  362. /* check unit id */
  363. if (max_devs && unit_id >= max_devs) {
  364. error_report("unit %d too big (max is %d)",
  365. unit_id, max_devs - 1);
  366. return NULL;
  367. }
  368. /*
  369. * catch multiple definitions
  370. */
  371. if (drive_get(type, bus_id, unit_id) != NULL) {
  372. error_report("drive with bus=%d, unit=%d (index=%d) exists",
  373. bus_id, unit_id, index);
  374. return NULL;
  375. }
  376. /* init */
  377. dinfo = g_malloc0(sizeof(*dinfo));
  378. if ((buf = qemu_opts_id(opts)) != NULL) {
  379. dinfo->id = g_strdup(buf);
  380. } else {
  381. /* no id supplied -> create one */
  382. dinfo->id = g_malloc0(32);
  383. if (type == IF_IDE || type == IF_SCSI)
  384. mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
  385. if (max_devs)
  386. snprintf(dinfo->id, 32, "%s%i%s%i",
  387. devname, bus_id, mediastr, unit_id);
  388. else
  389. snprintf(dinfo->id, 32, "%s%s%i",
  390. devname, mediastr, unit_id);
  391. }
  392. dinfo->bdrv = bdrv_new(dinfo->id);
  393. dinfo->devaddr = devaddr;
  394. dinfo->type = type;
  395. dinfo->bus = bus_id;
  396. dinfo->unit = unit_id;
  397. dinfo->opts = opts;
  398. dinfo->refcount = 1;
  399. if (serial)
  400. strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
  401. QTAILQ_INSERT_TAIL(&drives, dinfo, next);
  402. bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error);
  403. switch(type) {
  404. case IF_IDE:
  405. case IF_SCSI:
  406. case IF_XEN:
  407. case IF_NONE:
  408. switch(media) {
  409. case MEDIA_DISK:
  410. if (cyls != 0) {
  411. bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
  412. bdrv_set_translation_hint(dinfo->bdrv, translation);
  413. }
  414. break;
  415. case MEDIA_CDROM:
  416. dinfo->media_cd = 1;
  417. break;
  418. }
  419. break;
  420. case IF_SD:
  421. case IF_FLOPPY:
  422. case IF_PFLASH:
  423. case IF_MTD:
  424. break;
  425. case IF_VIRTIO:
  426. /* add virtio block device */
  427. opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
  428. qemu_opt_set(opts, "driver", "virtio-blk");
  429. qemu_opt_set(opts, "drive", dinfo->id);
  430. if (devaddr)
  431. qemu_opt_set(opts, "addr", devaddr);
  432. break;
  433. default:
  434. abort();
  435. }
  436. if (!file || !*file) {
  437. return dinfo;
  438. }
  439. if (snapshot) {
  440. /* always use cache=unsafe with snapshot */
  441. bdrv_flags &= ~BDRV_O_CACHE_MASK;
  442. bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
  443. }
  444. if (media == MEDIA_CDROM) {
  445. /* CDROM is fine for any interface, don't check. */
  446. ro = 1;
  447. } else if (ro == 1) {
  448. if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
  449. error_report("readonly not supported by this bus type");
  450. goto err;
  451. }
  452. }
  453. bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
  454. ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv);
  455. if (ret < 0) {
  456. error_report("could not open disk image %s: %s",
  457. file, strerror(-ret));
  458. goto err;
  459. }
  460. if (bdrv_key_required(dinfo->bdrv))
  461. autostart = 0;
  462. return dinfo;
  463. err:
  464. bdrv_delete(dinfo->bdrv);
  465. g_free(dinfo->id);
  466. QTAILQ_REMOVE(&drives, dinfo, next);
  467. g_free(dinfo);
  468. return NULL;
  469. }
  470. void do_commit(Monitor *mon, const QDict *qdict)
  471. {
  472. const char *device = qdict_get_str(qdict, "device");
  473. BlockDriverState *bs;
  474. if (!strcmp(device, "all")) {
  475. bdrv_commit_all();
  476. } else {
  477. bs = bdrv_find(device);
  478. if (!bs) {
  479. qerror_report(QERR_DEVICE_NOT_FOUND, device);
  480. return;
  481. }
  482. bdrv_commit(bs);
  483. }
  484. }
  485. int do_snapshot_blkdev(Monitor *mon, const QDict *qdict, QObject **ret_data)
  486. {
  487. const char *device = qdict_get_str(qdict, "device");
  488. const char *filename = qdict_get_try_str(qdict, "snapshot-file");
  489. const char *format = qdict_get_try_str(qdict, "format");
  490. BlockDriverState *bs;
  491. BlockDriver *drv, *old_drv, *proto_drv;
  492. int ret = 0;
  493. int flags;
  494. char old_filename[1024];
  495. if (!filename) {
  496. qerror_report(QERR_MISSING_PARAMETER, "snapshot-file");
  497. ret = -1;
  498. goto out;
  499. }
  500. bs = bdrv_find(device);
  501. if (!bs) {
  502. qerror_report(QERR_DEVICE_NOT_FOUND, device);
  503. ret = -1;
  504. goto out;
  505. }
  506. pstrcpy(old_filename, sizeof(old_filename), bs->filename);
  507. old_drv = bs->drv;
  508. flags = bs->open_flags;
  509. if (!format) {
  510. format = "qcow2";
  511. }
  512. drv = bdrv_find_format(format);
  513. if (!drv) {
  514. qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
  515. ret = -1;
  516. goto out;
  517. }
  518. proto_drv = bdrv_find_protocol(filename);
  519. if (!proto_drv) {
  520. qerror_report(QERR_INVALID_BLOCK_FORMAT, format);
  521. ret = -1;
  522. goto out;
  523. }
  524. ret = bdrv_img_create(filename, format, bs->filename,
  525. bs->drv->format_name, NULL, -1, flags);
  526. if (ret) {
  527. goto out;
  528. }
  529. qemu_aio_flush();
  530. bdrv_flush(bs);
  531. bdrv_close(bs);
  532. ret = bdrv_open(bs, filename, flags, drv);
  533. /*
  534. * If reopening the image file we just created fails, fall back
  535. * and try to re-open the original image. If that fails too, we
  536. * are in serious trouble.
  537. */
  538. if (ret != 0) {
  539. ret = bdrv_open(bs, old_filename, flags, old_drv);
  540. if (ret != 0) {
  541. qerror_report(QERR_OPEN_FILE_FAILED, old_filename);
  542. } else {
  543. qerror_report(QERR_OPEN_FILE_FAILED, filename);
  544. }
  545. }
  546. out:
  547. if (ret) {
  548. ret = -1;
  549. }
  550. return ret;
  551. }
  552. static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
  553. {
  554. if (!bdrv_dev_has_removable_media(bs)) {
  555. qerror_report(QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs));
  556. return -1;
  557. }
  558. if (bdrv_dev_is_medium_locked(bs) && !bdrv_dev_is_tray_open(bs)) {
  559. bdrv_dev_eject_request(bs, force);
  560. if (!force) {
  561. qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
  562. return -1;
  563. }
  564. }
  565. bdrv_close(bs);
  566. return 0;
  567. }
  568. int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
  569. {
  570. BlockDriverState *bs;
  571. int force = qdict_get_try_bool(qdict, "force", 0);
  572. const char *filename = qdict_get_str(qdict, "device");
  573. bs = bdrv_find(filename);
  574. if (!bs) {
  575. qerror_report(QERR_DEVICE_NOT_FOUND, filename);
  576. return -1;
  577. }
  578. return eject_device(mon, bs, force);
  579. }
  580. int do_block_set_passwd(Monitor *mon, const QDict *qdict,
  581. QObject **ret_data)
  582. {
  583. BlockDriverState *bs;
  584. int err;
  585. bs = bdrv_find(qdict_get_str(qdict, "device"));
  586. if (!bs) {
  587. qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
  588. return -1;
  589. }
  590. err = bdrv_set_key(bs, qdict_get_str(qdict, "password"));
  591. if (err == -EINVAL) {
  592. qerror_report(QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
  593. return -1;
  594. } else if (err < 0) {
  595. qerror_report(QERR_INVALID_PASSWORD);
  596. return -1;
  597. }
  598. return 0;
  599. }
  600. int do_change_block(Monitor *mon, const char *device,
  601. const char *filename, const char *fmt)
  602. {
  603. BlockDriverState *bs;
  604. BlockDriver *drv = NULL;
  605. int bdrv_flags;
  606. bs = bdrv_find(device);
  607. if (!bs) {
  608. qerror_report(QERR_DEVICE_NOT_FOUND, device);
  609. return -1;
  610. }
  611. if (fmt) {
  612. drv = bdrv_find_whitelisted_format(fmt);
  613. if (!drv) {
  614. qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
  615. return -1;
  616. }
  617. }
  618. if (eject_device(mon, bs, 0) < 0) {
  619. return -1;
  620. }
  621. bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
  622. bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
  623. if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
  624. qerror_report(QERR_OPEN_FILE_FAILED, filename);
  625. return -1;
  626. }
  627. return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
  628. }
  629. int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
  630. {
  631. const char *id = qdict_get_str(qdict, "id");
  632. BlockDriverState *bs;
  633. bs = bdrv_find(id);
  634. if (!bs) {
  635. qerror_report(QERR_DEVICE_NOT_FOUND, id);
  636. return -1;
  637. }
  638. if (bdrv_in_use(bs)) {
  639. qerror_report(QERR_DEVICE_IN_USE, id);
  640. return -1;
  641. }
  642. /* quiesce block driver; prevent further io */
  643. qemu_aio_flush();
  644. bdrv_flush(bs);
  645. bdrv_close(bs);
  646. /* if we have a device attached to this BlockDriverState
  647. * then we need to make the drive anonymous until the device
  648. * can be removed. If this is a drive with no device backing
  649. * then we can just get rid of the block driver state right here.
  650. */
  651. if (bdrv_get_attached_dev(bs)) {
  652. bdrv_make_anon(bs);
  653. } else {
  654. drive_uninit(drive_get_by_blockdev(bs));
  655. }
  656. return 0;
  657. }
  658. /*
  659. * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
  660. * existing QERR_ macro mess is cleaned up. A good example for better
  661. * error reports can be found in the qemu-img resize code.
  662. */
  663. int do_block_resize(Monitor *mon, const QDict *qdict, QObject **ret_data)
  664. {
  665. const char *device = qdict_get_str(qdict, "device");
  666. int64_t size = qdict_get_int(qdict, "size");
  667. BlockDriverState *bs;
  668. bs = bdrv_find(device);
  669. if (!bs) {
  670. qerror_report(QERR_DEVICE_NOT_FOUND, device);
  671. return -1;
  672. }
  673. if (size < 0) {
  674. qerror_report(QERR_UNDEFINED_ERROR);
  675. return -1;
  676. }
  677. if (bdrv_truncate(bs, size)) {
  678. qerror_report(QERR_UNDEFINED_ERROR);
  679. return -1;
  680. }
  681. return 0;
  682. }