qapi-system.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*
  2. * QMP command handlers specific to the system emulators
  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. * This file incorporates work covered by the following copyright and
  10. * permission notice:
  11. *
  12. * Copyright (c) 2003-2008 Fabrice Bellard
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining a copy
  15. * of this software and associated documentation files (the "Software"), to deal
  16. * in the Software without restriction, including without limitation the rights
  17. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  18. * copies of the Software, and to permit persons to whom the Software is
  19. * furnished to do so, subject to the following conditions:
  20. *
  21. * The above copyright notice and this permission notice shall be included in
  22. * all copies or substantial portions of the Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  27. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  29. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  30. * THE SOFTWARE.
  31. */
  32. #include "qemu/osdep.h"
  33. #include "block/block_int.h"
  34. #include "qapi/error.h"
  35. #include "qapi/qapi-commands-block.h"
  36. #include "qobject/qdict.h"
  37. #include "system/block-backend.h"
  38. #include "system/blockdev.h"
  39. static BlockBackend *qmp_get_blk(const char *blk_name, const char *qdev_id,
  40. Error **errp)
  41. {
  42. BlockBackend *blk;
  43. if (!blk_name == !qdev_id) {
  44. error_setg(errp, "Need exactly one of 'device' and 'id'");
  45. return NULL;
  46. }
  47. if (qdev_id) {
  48. blk = blk_by_qdev_id(qdev_id, errp);
  49. } else {
  50. blk = blk_by_name(blk_name);
  51. if (blk == NULL) {
  52. error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
  53. "Device '%s' not found", blk_name);
  54. }
  55. }
  56. return blk;
  57. }
  58. /*
  59. * Attempt to open the tray of @device.
  60. * If @force, ignore its tray lock.
  61. * Else, if the tray is locked, don't open it, but ask the guest to open it.
  62. * On error, store an error through @errp and return -errno.
  63. * If @device does not exist, return -ENODEV.
  64. * If it has no removable media, return -ENOTSUP.
  65. * If it has no tray, return -ENOSYS.
  66. * If the guest was asked to open the tray, return -EINPROGRESS.
  67. * Else, return 0.
  68. */
  69. static int do_open_tray(const char *blk_name, const char *qdev_id,
  70. bool force, Error **errp)
  71. {
  72. BlockBackend *blk;
  73. const char *device = qdev_id ?: blk_name;
  74. bool locked;
  75. blk = qmp_get_blk(blk_name, qdev_id, errp);
  76. if (!blk) {
  77. return -ENODEV;
  78. }
  79. if (!blk_dev_has_removable_media(blk)) {
  80. error_setg(errp, "Device '%s' is not removable", device);
  81. return -ENOTSUP;
  82. }
  83. if (!blk_dev_has_tray(blk)) {
  84. error_setg(errp, "Device '%s' does not have a tray", device);
  85. return -ENOSYS;
  86. }
  87. if (blk_dev_is_tray_open(blk)) {
  88. return 0;
  89. }
  90. locked = blk_dev_is_medium_locked(blk);
  91. if (locked) {
  92. blk_dev_eject_request(blk, force);
  93. }
  94. if (!locked || force) {
  95. blk_dev_change_media_cb(blk, false, &error_abort);
  96. }
  97. if (locked && !force) {
  98. error_setg(errp, "Device '%s' is locked and force was not specified, "
  99. "wait for tray to open and try again", device);
  100. return -EINPROGRESS;
  101. }
  102. return 0;
  103. }
  104. void qmp_blockdev_open_tray(const char *device,
  105. const char *id,
  106. bool has_force, bool force,
  107. Error **errp)
  108. {
  109. Error *local_err = NULL;
  110. int rc;
  111. if (!has_force) {
  112. force = false;
  113. }
  114. rc = do_open_tray(device, id, force, &local_err);
  115. if (rc && rc != -ENOSYS && rc != -EINPROGRESS) {
  116. error_propagate(errp, local_err);
  117. return;
  118. }
  119. error_free(local_err);
  120. }
  121. void qmp_blockdev_close_tray(const char *device,
  122. const char *id,
  123. Error **errp)
  124. {
  125. BlockBackend *blk;
  126. Error *local_err = NULL;
  127. blk = qmp_get_blk(device, id, errp);
  128. if (!blk) {
  129. return;
  130. }
  131. if (!blk_dev_has_removable_media(blk)) {
  132. error_setg(errp, "Device '%s' is not removable", device ?: id);
  133. return;
  134. }
  135. if (!blk_dev_has_tray(blk)) {
  136. /* Ignore this command on tray-less devices */
  137. return;
  138. }
  139. if (!blk_dev_is_tray_open(blk)) {
  140. return;
  141. }
  142. blk_dev_change_media_cb(blk, true, &local_err);
  143. if (local_err) {
  144. error_propagate(errp, local_err);
  145. return;
  146. }
  147. }
  148. static void GRAPH_UNLOCKED
  149. blockdev_remove_medium(const char *device, const char *id, Error **errp)
  150. {
  151. BlockBackend *blk;
  152. BlockDriverState *bs;
  153. bool has_attached_device;
  154. GLOBAL_STATE_CODE();
  155. blk = qmp_get_blk(device, id, errp);
  156. if (!blk) {
  157. return;
  158. }
  159. /* For BBs without a device, we can exchange the BDS tree at will */
  160. has_attached_device = blk_get_attached_dev(blk);
  161. if (has_attached_device && !blk_dev_has_removable_media(blk)) {
  162. error_setg(errp, "Device '%s' is not removable", device ?: id);
  163. return;
  164. }
  165. if (has_attached_device && blk_dev_has_tray(blk) &&
  166. !blk_dev_is_tray_open(blk))
  167. {
  168. error_setg(errp, "Tray of device '%s' is not open", device ?: id);
  169. return;
  170. }
  171. bs = blk_bs(blk);
  172. if (!bs) {
  173. return;
  174. }
  175. bdrv_graph_rdlock_main_loop();
  176. if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) {
  177. bdrv_graph_rdunlock_main_loop();
  178. return;
  179. }
  180. bdrv_graph_rdunlock_main_loop();
  181. blk_remove_bs(blk);
  182. if (!blk_dev_has_tray(blk)) {
  183. /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
  184. * called at all); therefore, the medium needs to be ejected here.
  185. * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
  186. * value passed here (i.e. false). */
  187. blk_dev_change_media_cb(blk, false, &error_abort);
  188. }
  189. }
  190. void qmp_blockdev_remove_medium(const char *id, Error **errp)
  191. {
  192. blockdev_remove_medium(NULL, id, errp);
  193. }
  194. static void qmp_blockdev_insert_anon_medium(BlockBackend *blk,
  195. BlockDriverState *bs, Error **errp)
  196. {
  197. Error *local_err = NULL;
  198. bool has_device;
  199. int ret;
  200. /* For BBs without a device, we can exchange the BDS tree at will */
  201. has_device = blk_get_attached_dev(blk);
  202. if (has_device && !blk_dev_has_removable_media(blk)) {
  203. error_setg(errp, "Device is not removable");
  204. return;
  205. }
  206. if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) {
  207. error_setg(errp, "Tray of the device is not open");
  208. return;
  209. }
  210. if (blk_bs(blk)) {
  211. error_setg(errp, "There already is a medium in the device");
  212. return;
  213. }
  214. ret = blk_insert_bs(blk, bs, errp);
  215. if (ret < 0) {
  216. return;
  217. }
  218. if (!blk_dev_has_tray(blk)) {
  219. /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
  220. * called at all); therefore, the medium needs to be pushed into the
  221. * slot here.
  222. * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
  223. * value passed here (i.e. true). */
  224. blk_dev_change_media_cb(blk, true, &local_err);
  225. if (local_err) {
  226. error_propagate(errp, local_err);
  227. blk_remove_bs(blk);
  228. return;
  229. }
  230. }
  231. }
  232. static void blockdev_insert_medium(const char *device, const char *id,
  233. const char *node_name, Error **errp)
  234. {
  235. BlockBackend *blk;
  236. BlockDriverState *bs;
  237. GRAPH_RDLOCK_GUARD_MAINLOOP();
  238. blk = qmp_get_blk(device, id, errp);
  239. if (!blk) {
  240. return;
  241. }
  242. bs = bdrv_find_node(node_name);
  243. if (!bs) {
  244. error_setg(errp, "Node '%s' not found", node_name);
  245. return;
  246. }
  247. if (bdrv_has_blk(bs)) {
  248. error_setg(errp, "Node '%s' is already in use", node_name);
  249. return;
  250. }
  251. qmp_blockdev_insert_anon_medium(blk, bs, errp);
  252. }
  253. void qmp_blockdev_insert_medium(const char *id, const char *node_name,
  254. Error **errp)
  255. {
  256. blockdev_insert_medium(NULL, id, node_name, errp);
  257. }
  258. void qmp_blockdev_change_medium(const char *device,
  259. const char *id,
  260. const char *filename,
  261. const char *format,
  262. bool has_force, bool force,
  263. bool has_read_only,
  264. BlockdevChangeReadOnlyMode read_only,
  265. Error **errp)
  266. {
  267. BlockBackend *blk;
  268. BlockDriverState *medium_bs = NULL;
  269. int bdrv_flags;
  270. bool detect_zeroes;
  271. int rc;
  272. QDict *options = NULL;
  273. Error *err = NULL;
  274. blk = qmp_get_blk(device, id, errp);
  275. if (!blk) {
  276. goto fail;
  277. }
  278. if (blk_bs(blk)) {
  279. blk_update_root_state(blk);
  280. }
  281. bdrv_flags = blk_get_open_flags_from_root_state(blk);
  282. bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING |
  283. BDRV_O_PROTOCOL | BDRV_O_AUTO_RDONLY);
  284. if (!has_read_only) {
  285. read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN;
  286. }
  287. switch (read_only) {
  288. case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN:
  289. break;
  290. case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY:
  291. bdrv_flags &= ~BDRV_O_RDWR;
  292. break;
  293. case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE:
  294. bdrv_flags |= BDRV_O_RDWR;
  295. break;
  296. default:
  297. abort();
  298. }
  299. options = qdict_new();
  300. detect_zeroes = blk_get_detect_zeroes_from_root_state(blk);
  301. qdict_put_str(options, "detect-zeroes", detect_zeroes ? "on" : "off");
  302. if (format) {
  303. qdict_put_str(options, "driver", format);
  304. }
  305. medium_bs = bdrv_open(filename, NULL, options, bdrv_flags, errp);
  306. if (!medium_bs) {
  307. goto fail;
  308. }
  309. rc = do_open_tray(device, id, force, &err);
  310. if (rc && rc != -ENOSYS) {
  311. error_propagate(errp, err);
  312. goto fail;
  313. }
  314. error_free(err);
  315. err = NULL;
  316. blockdev_remove_medium(device, id, &err);
  317. if (err) {
  318. error_propagate(errp, err);
  319. goto fail;
  320. }
  321. qmp_blockdev_insert_anon_medium(blk, medium_bs, &err);
  322. if (err) {
  323. error_propagate(errp, err);
  324. goto fail;
  325. }
  326. qmp_blockdev_close_tray(device, id, errp);
  327. fail:
  328. /* If the medium has been inserted, the device has its own reference, so
  329. * ours must be relinquished; and if it has not been inserted successfully,
  330. * the reference must be relinquished anyway */
  331. bdrv_unref(medium_bs);
  332. }
  333. void qmp_eject(const char *device, const char *id,
  334. bool has_force, bool force, Error **errp)
  335. {
  336. Error *local_err = NULL;
  337. int rc;
  338. if (!has_force) {
  339. force = false;
  340. }
  341. rc = do_open_tray(device, id, force, &local_err);
  342. if (rc && rc != -ENOSYS) {
  343. error_propagate(errp, local_err);
  344. return;
  345. }
  346. error_free(local_err);
  347. blockdev_remove_medium(device, id, errp);
  348. }
  349. /* throttling disk I/O limits */
  350. void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp)
  351. {
  352. ThrottleConfig cfg;
  353. BlockDriverState *bs;
  354. BlockBackend *blk;
  355. blk = qmp_get_blk(arg->device, arg->id, errp);
  356. if (!blk) {
  357. return;
  358. }
  359. bs = blk_bs(blk);
  360. if (!bs) {
  361. error_setg(errp, "Device has no medium");
  362. return;
  363. }
  364. throttle_config_init(&cfg);
  365. cfg.buckets[THROTTLE_BPS_TOTAL].avg = arg->bps;
  366. cfg.buckets[THROTTLE_BPS_READ].avg = arg->bps_rd;
  367. cfg.buckets[THROTTLE_BPS_WRITE].avg = arg->bps_wr;
  368. cfg.buckets[THROTTLE_OPS_TOTAL].avg = arg->iops;
  369. cfg.buckets[THROTTLE_OPS_READ].avg = arg->iops_rd;
  370. cfg.buckets[THROTTLE_OPS_WRITE].avg = arg->iops_wr;
  371. if (arg->has_bps_max) {
  372. cfg.buckets[THROTTLE_BPS_TOTAL].max = arg->bps_max;
  373. }
  374. if (arg->has_bps_rd_max) {
  375. cfg.buckets[THROTTLE_BPS_READ].max = arg->bps_rd_max;
  376. }
  377. if (arg->has_bps_wr_max) {
  378. cfg.buckets[THROTTLE_BPS_WRITE].max = arg->bps_wr_max;
  379. }
  380. if (arg->has_iops_max) {
  381. cfg.buckets[THROTTLE_OPS_TOTAL].max = arg->iops_max;
  382. }
  383. if (arg->has_iops_rd_max) {
  384. cfg.buckets[THROTTLE_OPS_READ].max = arg->iops_rd_max;
  385. }
  386. if (arg->has_iops_wr_max) {
  387. cfg.buckets[THROTTLE_OPS_WRITE].max = arg->iops_wr_max;
  388. }
  389. if (arg->has_bps_max_length) {
  390. cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = arg->bps_max_length;
  391. }
  392. if (arg->has_bps_rd_max_length) {
  393. cfg.buckets[THROTTLE_BPS_READ].burst_length = arg->bps_rd_max_length;
  394. }
  395. if (arg->has_bps_wr_max_length) {
  396. cfg.buckets[THROTTLE_BPS_WRITE].burst_length = arg->bps_wr_max_length;
  397. }
  398. if (arg->has_iops_max_length) {
  399. cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = arg->iops_max_length;
  400. }
  401. if (arg->has_iops_rd_max_length) {
  402. cfg.buckets[THROTTLE_OPS_READ].burst_length = arg->iops_rd_max_length;
  403. }
  404. if (arg->has_iops_wr_max_length) {
  405. cfg.buckets[THROTTLE_OPS_WRITE].burst_length = arg->iops_wr_max_length;
  406. }
  407. if (arg->has_iops_size) {
  408. cfg.op_size = arg->iops_size;
  409. }
  410. if (!throttle_is_valid(&cfg, errp)) {
  411. return;
  412. }
  413. if (throttle_enabled(&cfg)) {
  414. /* Enable I/O limits if they're not enabled yet, otherwise
  415. * just update the throttling group. */
  416. if (!blk_get_public(blk)->throttle_group_member.throttle_state) {
  417. blk_io_limits_enable(blk, arg->group ?: arg->device ?: arg->id);
  418. } else if (arg->group) {
  419. blk_io_limits_update_group(blk, arg->group);
  420. }
  421. /* Set the new throttling configuration */
  422. blk_set_io_limits(blk, &cfg);
  423. } else if (blk_get_public(blk)->throttle_group_member.throttle_state) {
  424. /* If all throttling settings are set to 0, disable I/O limits */
  425. blk_io_limits_disable(blk);
  426. }
  427. }
  428. void qmp_block_latency_histogram_set(
  429. const char *id,
  430. bool has_boundaries, uint64List *boundaries,
  431. bool has_boundaries_read, uint64List *boundaries_read,
  432. bool has_boundaries_write, uint64List *boundaries_write,
  433. bool has_boundaries_append, uint64List *boundaries_append,
  434. bool has_boundaries_flush, uint64List *boundaries_flush,
  435. Error **errp)
  436. {
  437. BlockBackend *blk = qmp_get_blk(NULL, id, errp);
  438. BlockAcctStats *stats;
  439. int ret;
  440. if (!blk) {
  441. return;
  442. }
  443. stats = blk_get_stats(blk);
  444. if (!has_boundaries && !has_boundaries_read && !has_boundaries_write &&
  445. !has_boundaries_flush)
  446. {
  447. block_latency_histograms_clear(stats);
  448. return;
  449. }
  450. if (has_boundaries || has_boundaries_read) {
  451. ret = block_latency_histogram_set(
  452. stats, BLOCK_ACCT_READ,
  453. has_boundaries_read ? boundaries_read : boundaries);
  454. if (ret) {
  455. error_setg(errp, "Device '%s' set read boundaries fail", id);
  456. return;
  457. }
  458. }
  459. if (has_boundaries || has_boundaries_write) {
  460. ret = block_latency_histogram_set(
  461. stats, BLOCK_ACCT_WRITE,
  462. has_boundaries_write ? boundaries_write : boundaries);
  463. if (ret) {
  464. error_setg(errp, "Device '%s' set write boundaries fail", id);
  465. return;
  466. }
  467. }
  468. if (has_boundaries || has_boundaries_append) {
  469. ret = block_latency_histogram_set(
  470. stats, BLOCK_ACCT_ZONE_APPEND,
  471. has_boundaries_append ? boundaries_append : boundaries);
  472. if (ret) {
  473. error_setg(errp, "Device '%s' set append write boundaries fail", id);
  474. return;
  475. }
  476. }
  477. if (has_boundaries || has_boundaries_flush) {
  478. ret = block_latency_histogram_set(
  479. stats, BLOCK_ACCT_FLUSH,
  480. has_boundaries_flush ? boundaries_flush : boundaries);
  481. if (ret) {
  482. error_setg(errp, "Device '%s' set flush boundaries fail", id);
  483. return;
  484. }
  485. }
  486. }