export.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Common block export infrastructure
  3. *
  4. * Copyright (c) 2012, 2020 Red Hat, Inc.
  5. *
  6. * Authors:
  7. * Paolo Bonzini <pbonzini@redhat.com>
  8. * Kevin Wolf <kwolf@redhat.com>
  9. *
  10. * This work is licensed under the terms of the GNU GPL, version 2 or
  11. * later. See the COPYING file in the top-level directory.
  12. */
  13. #include "qemu/osdep.h"
  14. #include "block/block.h"
  15. #include "system/block-backend.h"
  16. #include "system/iothread.h"
  17. #include "block/export.h"
  18. #include "block/fuse.h"
  19. #include "block/nbd.h"
  20. #include "qapi/error.h"
  21. #include "qapi/qapi-commands-block-export.h"
  22. #include "qapi/qapi-events-block-export.h"
  23. #include "qemu/id.h"
  24. #ifdef CONFIG_VHOST_USER_BLK_SERVER
  25. #include "vhost-user-blk-server.h"
  26. #endif
  27. #ifdef CONFIG_VDUSE_BLK_EXPORT
  28. #include "vduse-blk.h"
  29. #endif
  30. static const BlockExportDriver *blk_exp_drivers[] = {
  31. &blk_exp_nbd,
  32. #ifdef CONFIG_VHOST_USER_BLK_SERVER
  33. &blk_exp_vhost_user_blk,
  34. #endif
  35. #ifdef CONFIG_FUSE
  36. &blk_exp_fuse,
  37. #endif
  38. #ifdef CONFIG_VDUSE_BLK_EXPORT
  39. &blk_exp_vduse_blk,
  40. #endif
  41. };
  42. /* Only accessed from the main thread */
  43. static QLIST_HEAD(, BlockExport) block_exports =
  44. QLIST_HEAD_INITIALIZER(block_exports);
  45. BlockExport *blk_exp_find(const char *id)
  46. {
  47. BlockExport *exp;
  48. QLIST_FOREACH(exp, &block_exports, next) {
  49. if (strcmp(id, exp->id) == 0) {
  50. return exp;
  51. }
  52. }
  53. return NULL;
  54. }
  55. static const BlockExportDriver *blk_exp_find_driver(BlockExportType type)
  56. {
  57. int i;
  58. for (i = 0; i < ARRAY_SIZE(blk_exp_drivers); i++) {
  59. if (blk_exp_drivers[i]->type == type) {
  60. return blk_exp_drivers[i];
  61. }
  62. }
  63. return NULL;
  64. }
  65. BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
  66. {
  67. bool fixed_iothread = export->has_fixed_iothread && export->fixed_iothread;
  68. const BlockExportDriver *drv;
  69. BlockExport *exp = NULL;
  70. BlockDriverState *bs;
  71. BlockBackend *blk = NULL;
  72. AioContext *ctx;
  73. uint64_t perm;
  74. int ret;
  75. GLOBAL_STATE_CODE();
  76. if (!id_wellformed(export->id)) {
  77. error_setg(errp, "Invalid block export id");
  78. return NULL;
  79. }
  80. if (blk_exp_find(export->id)) {
  81. error_setg(errp, "Block export id '%s' is already in use", export->id);
  82. return NULL;
  83. }
  84. drv = blk_exp_find_driver(export->type);
  85. if (!drv) {
  86. error_setg(errp, "No driver found for the requested export type");
  87. return NULL;
  88. }
  89. bs = bdrv_lookup_bs(NULL, export->node_name, errp);
  90. if (!bs) {
  91. return NULL;
  92. }
  93. if (!export->has_writable) {
  94. export->writable = false;
  95. }
  96. if (bdrv_is_read_only(bs) && export->writable) {
  97. error_setg(errp, "Cannot export read-only node as writable");
  98. return NULL;
  99. }
  100. ctx = bdrv_get_aio_context(bs);
  101. if (export->iothread) {
  102. IOThread *iothread;
  103. AioContext *new_ctx;
  104. Error **set_context_errp;
  105. iothread = iothread_by_id(export->iothread);
  106. if (!iothread) {
  107. error_setg(errp, "iothread \"%s\" not found", export->iothread);
  108. goto fail;
  109. }
  110. new_ctx = iothread_get_aio_context(iothread);
  111. /* Ignore errors with fixed-iothread=false */
  112. set_context_errp = fixed_iothread ? errp : NULL;
  113. ret = bdrv_try_change_aio_context(bs, new_ctx, NULL, set_context_errp);
  114. if (ret == 0) {
  115. ctx = new_ctx;
  116. } else if (fixed_iothread) {
  117. goto fail;
  118. }
  119. }
  120. /*
  121. * Block exports are used for non-shared storage migration. Make sure
  122. * that BDRV_O_INACTIVE is cleared and the image is ready for write
  123. * access since the export could be available before migration handover.
  124. * ctx was acquired in the caller.
  125. */
  126. bdrv_graph_rdlock_main_loop();
  127. bdrv_activate(bs, NULL);
  128. bdrv_graph_rdunlock_main_loop();
  129. perm = BLK_PERM_CONSISTENT_READ;
  130. if (export->writable) {
  131. perm |= BLK_PERM_WRITE;
  132. }
  133. blk = blk_new(ctx, perm, BLK_PERM_ALL);
  134. if (!fixed_iothread) {
  135. blk_set_allow_aio_context_change(blk, true);
  136. }
  137. ret = blk_insert_bs(blk, bs, errp);
  138. if (ret < 0) {
  139. goto fail;
  140. }
  141. if (!export->has_writethrough) {
  142. export->writethrough = false;
  143. }
  144. blk_set_enable_write_cache(blk, !export->writethrough);
  145. assert(drv->instance_size >= sizeof(BlockExport));
  146. exp = g_malloc0(drv->instance_size);
  147. *exp = (BlockExport) {
  148. .drv = drv,
  149. .refcount = 1,
  150. .user_owned = true,
  151. .id = g_strdup(export->id),
  152. .ctx = ctx,
  153. .blk = blk,
  154. };
  155. ret = drv->create(exp, export, errp);
  156. if (ret < 0) {
  157. goto fail;
  158. }
  159. assert(exp->blk != NULL);
  160. QLIST_INSERT_HEAD(&block_exports, exp, next);
  161. return exp;
  162. fail:
  163. if (blk) {
  164. blk_set_dev_ops(blk, NULL, NULL);
  165. blk_unref(blk);
  166. }
  167. if (exp) {
  168. g_free(exp->id);
  169. g_free(exp);
  170. }
  171. return NULL;
  172. }
  173. void blk_exp_ref(BlockExport *exp)
  174. {
  175. assert(qatomic_read(&exp->refcount) > 0);
  176. qatomic_inc(&exp->refcount);
  177. }
  178. /* Runs in the main thread */
  179. static void blk_exp_delete_bh(void *opaque)
  180. {
  181. BlockExport *exp = opaque;
  182. assert(exp->refcount == 0);
  183. QLIST_REMOVE(exp, next);
  184. exp->drv->delete(exp);
  185. blk_set_dev_ops(exp->blk, NULL, NULL);
  186. blk_unref(exp->blk);
  187. qapi_event_send_block_export_deleted(exp->id);
  188. g_free(exp->id);
  189. g_free(exp);
  190. }
  191. void blk_exp_unref(BlockExport *exp)
  192. {
  193. assert(qatomic_read(&exp->refcount) > 0);
  194. if (qatomic_fetch_dec(&exp->refcount) == 1) {
  195. /* Touch the block_exports list only in the main thread */
  196. aio_bh_schedule_oneshot(qemu_get_aio_context(), blk_exp_delete_bh,
  197. exp);
  198. }
  199. }
  200. /*
  201. * Drops the user reference to the export and requests that all client
  202. * connections and other internally held references start to shut down. When
  203. * the function returns, there may still be active references while the export
  204. * is in the process of shutting down.
  205. */
  206. void blk_exp_request_shutdown(BlockExport *exp)
  207. {
  208. /*
  209. * If the user doesn't own the export any more, it is already shutting
  210. * down. We must not call .request_shutdown and decrease the refcount a
  211. * second time.
  212. */
  213. if (!exp->user_owned) {
  214. return;
  215. }
  216. exp->drv->request_shutdown(exp);
  217. assert(exp->user_owned);
  218. exp->user_owned = false;
  219. blk_exp_unref(exp);
  220. }
  221. /*
  222. * Returns whether a block export of the given type exists.
  223. * type == BLOCK_EXPORT_TYPE__MAX checks for an export of any type.
  224. */
  225. static bool blk_exp_has_type(BlockExportType type)
  226. {
  227. BlockExport *exp;
  228. if (type == BLOCK_EXPORT_TYPE__MAX) {
  229. return !QLIST_EMPTY(&block_exports);
  230. }
  231. QLIST_FOREACH(exp, &block_exports, next) {
  232. if (exp->drv->type == type) {
  233. return true;
  234. }
  235. }
  236. return false;
  237. }
  238. /* type == BLOCK_EXPORT_TYPE__MAX for all types */
  239. void blk_exp_close_all_type(BlockExportType type)
  240. {
  241. BlockExport *exp, *next;
  242. assert(in_aio_context_home_thread(qemu_get_aio_context()));
  243. QLIST_FOREACH_SAFE(exp, &block_exports, next, next) {
  244. if (type != BLOCK_EXPORT_TYPE__MAX && exp->drv->type != type) {
  245. continue;
  246. }
  247. blk_exp_request_shutdown(exp);
  248. }
  249. AIO_WAIT_WHILE_UNLOCKED(NULL, blk_exp_has_type(type));
  250. }
  251. void blk_exp_close_all(void)
  252. {
  253. blk_exp_close_all_type(BLOCK_EXPORT_TYPE__MAX);
  254. }
  255. void qmp_block_export_add(BlockExportOptions *export, Error **errp)
  256. {
  257. blk_exp_add(export, errp);
  258. }
  259. void qmp_block_export_del(const char *id,
  260. bool has_mode, BlockExportRemoveMode mode,
  261. Error **errp)
  262. {
  263. ERRP_GUARD();
  264. BlockExport *exp;
  265. exp = blk_exp_find(id);
  266. if (exp == NULL) {
  267. error_setg(errp, "Export '%s' is not found", id);
  268. return;
  269. }
  270. if (!exp->user_owned) {
  271. error_setg(errp, "Export '%s' is already shutting down", id);
  272. return;
  273. }
  274. if (!has_mode) {
  275. mode = BLOCK_EXPORT_REMOVE_MODE_SAFE;
  276. }
  277. if (mode == BLOCK_EXPORT_REMOVE_MODE_SAFE &&
  278. qatomic_read(&exp->refcount) > 1) {
  279. error_setg(errp, "export '%s' still in use", exp->id);
  280. error_append_hint(errp, "Use mode='hard' to force client "
  281. "disconnect\n");
  282. return;
  283. }
  284. blk_exp_request_shutdown(exp);
  285. }
  286. BlockExportInfoList *qmp_query_block_exports(Error **errp)
  287. {
  288. BlockExportInfoList *head = NULL, **tail = &head;
  289. BlockExport *exp;
  290. QLIST_FOREACH(exp, &block_exports, next) {
  291. BlockExportInfo *info = g_new(BlockExportInfo, 1);
  292. *info = (BlockExportInfo) {
  293. .id = g_strdup(exp->id),
  294. .type = exp->drv->type,
  295. .node_name = g_strdup(bdrv_get_node_name(blk_bs(exp->blk))),
  296. .shutting_down = !exp->user_owned,
  297. };
  298. QAPI_LIST_APPEND(tail, info);
  299. }
  300. return head;
  301. }