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