blockdev.c 21 KB

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