]>
Commit | Line | Data |
---|---|---|
26f54e9a MA |
1 | /* |
2 | * QEMU Block backends | |
3 | * | |
4 | * Copyright (C) 2014 Red Hat, Inc. | |
5 | * | |
6 | * Authors: | |
7 | * Markus Armbruster <[email protected]>, | |
8 | * | |
9 | * This work is licensed under the terms of the GNU LGPL, version 2.1 | |
10 | * or later. See the COPYING.LIB file in the top-level directory. | |
11 | */ | |
12 | ||
13 | #include "sysemu/block-backend.h" | |
14 | #include "block/block_int.h" | |
18e46a03 | 15 | #include "sysemu/blockdev.h" |
26f54e9a MA |
16 | |
17 | struct BlockBackend { | |
18 | char *name; | |
19 | int refcnt; | |
7e7d56d9 | 20 | BlockDriverState *bs; |
18e46a03 | 21 | DriveInfo *legacy_dinfo; |
26f54e9a MA |
22 | QTAILQ_ENTRY(BlockBackend) link; /* for blk_backends */ |
23 | }; | |
24 | ||
8fb3c76c MA |
25 | static void drive_info_del(DriveInfo *dinfo); |
26 | ||
7e7d56d9 | 27 | /* All the BlockBackends (except for hidden ones) */ |
26f54e9a MA |
28 | static QTAILQ_HEAD(, BlockBackend) blk_backends = |
29 | QTAILQ_HEAD_INITIALIZER(blk_backends); | |
30 | ||
31 | /* | |
32 | * Create a new BlockBackend with @name, with a reference count of one. | |
33 | * @name must not be null or empty. | |
34 | * Fail if a BlockBackend with this name already exists. | |
35 | * Store an error through @errp on failure, unless it's null. | |
36 | * Return the new BlockBackend on success, null on failure. | |
37 | */ | |
38 | BlockBackend *blk_new(const char *name, Error **errp) | |
39 | { | |
40 | BlockBackend *blk; | |
41 | ||
42 | assert(name && name[0]); | |
7f06d47e MA |
43 | if (!id_wellformed(name)) { |
44 | error_setg(errp, "Invalid device name"); | |
45 | return NULL; | |
46 | } | |
26f54e9a MA |
47 | if (blk_by_name(name)) { |
48 | error_setg(errp, "Device with id '%s' already exists", name); | |
49 | return NULL; | |
50 | } | |
7f06d47e MA |
51 | if (bdrv_find_node(name)) { |
52 | error_setg(errp, | |
53 | "Device name '%s' conflicts with an existing node name", | |
54 | name); | |
55 | return NULL; | |
56 | } | |
26f54e9a MA |
57 | |
58 | blk = g_new0(BlockBackend, 1); | |
59 | blk->name = g_strdup(name); | |
60 | blk->refcnt = 1; | |
61 | QTAILQ_INSERT_TAIL(&blk_backends, blk, link); | |
62 | return blk; | |
63 | } | |
64 | ||
7e7d56d9 MA |
65 | /* |
66 | * Create a new BlockBackend with a new BlockDriverState attached. | |
7e7d56d9 MA |
67 | * Otherwise just like blk_new(), which see. |
68 | */ | |
69 | BlockBackend *blk_new_with_bs(const char *name, Error **errp) | |
70 | { | |
71 | BlockBackend *blk; | |
72 | BlockDriverState *bs; | |
73 | ||
74 | blk = blk_new(name, errp); | |
75 | if (!blk) { | |
76 | return NULL; | |
77 | } | |
78 | ||
7f06d47e | 79 | bs = bdrv_new_root(); |
7e7d56d9 MA |
80 | blk->bs = bs; |
81 | bs->blk = blk; | |
82 | return blk; | |
83 | } | |
84 | ||
26f54e9a MA |
85 | static void blk_delete(BlockBackend *blk) |
86 | { | |
87 | assert(!blk->refcnt); | |
7e7d56d9 | 88 | if (blk->bs) { |
9ba10c95 | 89 | assert(blk->bs->blk == blk); |
7e7d56d9 | 90 | blk->bs->blk = NULL; |
9ba10c95 | 91 | bdrv_unref(blk->bs); |
7e7d56d9 MA |
92 | blk->bs = NULL; |
93 | } | |
94 | /* Avoid double-remove after blk_hide_on_behalf_of_do_drive_del() */ | |
95 | if (blk->name[0]) { | |
96 | QTAILQ_REMOVE(&blk_backends, blk, link); | |
97 | } | |
26f54e9a | 98 | g_free(blk->name); |
18e46a03 | 99 | drive_info_del(blk->legacy_dinfo); |
26f54e9a MA |
100 | g_free(blk); |
101 | } | |
102 | ||
8fb3c76c MA |
103 | static void drive_info_del(DriveInfo *dinfo) |
104 | { | |
105 | if (!dinfo) { | |
106 | return; | |
107 | } | |
108 | qemu_opts_del(dinfo->opts); | |
109 | g_free(dinfo->id); | |
110 | g_free(dinfo->serial); | |
111 | g_free(dinfo); | |
112 | } | |
113 | ||
26f54e9a MA |
114 | /* |
115 | * Increment @blk's reference count. | |
116 | * @blk must not be null. | |
117 | */ | |
118 | void blk_ref(BlockBackend *blk) | |
119 | { | |
120 | blk->refcnt++; | |
121 | } | |
122 | ||
123 | /* | |
124 | * Decrement @blk's reference count. | |
125 | * If this drops it to zero, destroy @blk. | |
126 | * For convenience, do nothing if @blk is null. | |
127 | */ | |
128 | void blk_unref(BlockBackend *blk) | |
129 | { | |
130 | if (blk) { | |
131 | assert(blk->refcnt > 0); | |
132 | if (!--blk->refcnt) { | |
133 | blk_delete(blk); | |
134 | } | |
135 | } | |
136 | } | |
137 | ||
138 | /* | |
139 | * Return the BlockBackend after @blk. | |
140 | * If @blk is null, return the first one. | |
141 | * Else, return @blk's next sibling, which may be null. | |
142 | * | |
143 | * To iterate over all BlockBackends, do | |
144 | * for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { | |
145 | * ... | |
146 | * } | |
147 | */ | |
148 | BlockBackend *blk_next(BlockBackend *blk) | |
149 | { | |
150 | return blk ? QTAILQ_NEXT(blk, link) : QTAILQ_FIRST(&blk_backends); | |
151 | } | |
152 | ||
153 | /* | |
7e7d56d9 MA |
154 | * Return @blk's name, a non-null string. |
155 | * Wart: the name is empty iff @blk has been hidden with | |
156 | * blk_hide_on_behalf_of_do_drive_del(). | |
26f54e9a MA |
157 | */ |
158 | const char *blk_name(BlockBackend *blk) | |
159 | { | |
160 | return blk->name; | |
161 | } | |
162 | ||
163 | /* | |
164 | * Return the BlockBackend with name @name if it exists, else null. | |
165 | * @name must not be null. | |
166 | */ | |
167 | BlockBackend *blk_by_name(const char *name) | |
168 | { | |
169 | BlockBackend *blk; | |
170 | ||
171 | assert(name); | |
172 | QTAILQ_FOREACH(blk, &blk_backends, link) { | |
173 | if (!strcmp(name, blk->name)) { | |
174 | return blk; | |
175 | } | |
176 | } | |
177 | return NULL; | |
178 | } | |
7e7d56d9 MA |
179 | |
180 | /* | |
181 | * Return the BlockDriverState attached to @blk if any, else null. | |
182 | */ | |
183 | BlockDriverState *blk_bs(BlockBackend *blk) | |
184 | { | |
185 | return blk->bs; | |
186 | } | |
187 | ||
18e46a03 MA |
188 | /* |
189 | * Return @blk's DriveInfo if any, else null. | |
190 | */ | |
191 | DriveInfo *blk_legacy_dinfo(BlockBackend *blk) | |
192 | { | |
193 | return blk->legacy_dinfo; | |
194 | } | |
195 | ||
196 | /* | |
197 | * Set @blk's DriveInfo to @dinfo, and return it. | |
198 | * @blk must not have a DriveInfo set already. | |
199 | * No other BlockBackend may have the same DriveInfo set. | |
200 | */ | |
201 | DriveInfo *blk_set_legacy_dinfo(BlockBackend *blk, DriveInfo *dinfo) | |
202 | { | |
203 | assert(!blk->legacy_dinfo); | |
204 | return blk->legacy_dinfo = dinfo; | |
205 | } | |
206 | ||
207 | /* | |
208 | * Return the BlockBackend with DriveInfo @dinfo. | |
209 | * It must exist. | |
210 | */ | |
211 | BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo) | |
212 | { | |
213 | BlockBackend *blk; | |
214 | ||
215 | QTAILQ_FOREACH(blk, &blk_backends, link) { | |
216 | if (blk->legacy_dinfo == dinfo) { | |
217 | return blk; | |
218 | } | |
219 | } | |
220 | abort(); | |
221 | } | |
222 | ||
7e7d56d9 MA |
223 | /* |
224 | * Hide @blk. | |
225 | * @blk must not have been hidden already. | |
226 | * Make attached BlockDriverState, if any, anonymous. | |
227 | * Once hidden, @blk is invisible to all functions that don't receive | |
228 | * it as argument. For example, blk_by_name() won't return it. | |
229 | * Strictly for use by do_drive_del(). | |
230 | * TODO get rid of it! | |
231 | */ | |
232 | void blk_hide_on_behalf_of_do_drive_del(BlockBackend *blk) | |
233 | { | |
234 | QTAILQ_REMOVE(&blk_backends, blk, link); | |
235 | blk->name[0] = 0; | |
236 | if (blk->bs) { | |
237 | bdrv_make_anon(blk->bs); | |
238 | } | |
239 | } | |
4be74634 MA |
240 | |
241 | void blk_iostatus_enable(BlockBackend *blk) | |
242 | { | |
243 | bdrv_iostatus_enable(blk->bs); | |
244 | } | |
245 | ||
246 | int blk_attach_dev(BlockBackend *blk, void *dev) | |
247 | { | |
248 | return bdrv_attach_dev(blk->bs, dev); | |
249 | } | |
250 | ||
251 | void blk_attach_dev_nofail(BlockBackend *blk, void *dev) | |
252 | { | |
253 | bdrv_attach_dev_nofail(blk->bs, dev); | |
254 | } | |
255 | ||
256 | void blk_detach_dev(BlockBackend *blk, void *dev) | |
257 | { | |
258 | bdrv_detach_dev(blk->bs, dev); | |
259 | } | |
260 | ||
261 | void *blk_get_attached_dev(BlockBackend *blk) | |
262 | { | |
263 | return bdrv_get_attached_dev(blk->bs); | |
264 | } | |
265 | ||
266 | void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops, void *opaque) | |
267 | { | |
268 | bdrv_set_dev_ops(blk->bs, ops, opaque); | |
269 | } | |
270 | ||
271 | int blk_read(BlockBackend *blk, int64_t sector_num, uint8_t *buf, | |
272 | int nb_sectors) | |
273 | { | |
274 | return bdrv_read(blk->bs, sector_num, buf, nb_sectors); | |
275 | } | |
276 | ||
277 | int blk_read_unthrottled(BlockBackend *blk, int64_t sector_num, uint8_t *buf, | |
278 | int nb_sectors) | |
279 | { | |
280 | return bdrv_read_unthrottled(blk->bs, sector_num, buf, nb_sectors); | |
281 | } | |
282 | ||
283 | int blk_write(BlockBackend *blk, int64_t sector_num, const uint8_t *buf, | |
284 | int nb_sectors) | |
285 | { | |
286 | return bdrv_write(blk->bs, sector_num, buf, nb_sectors); | |
287 | } | |
288 | ||
289 | BlockAIOCB *blk_aio_write_zeroes(BlockBackend *blk, int64_t sector_num, | |
290 | int nb_sectors, BdrvRequestFlags flags, | |
291 | BlockCompletionFunc *cb, void *opaque) | |
292 | { | |
293 | return bdrv_aio_write_zeroes(blk->bs, sector_num, nb_sectors, flags, | |
294 | cb, opaque); | |
295 | } | |
296 | ||
297 | int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count) | |
298 | { | |
299 | return bdrv_pread(blk->bs, offset, buf, count); | |
300 | } | |
301 | ||
302 | int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count) | |
303 | { | |
304 | return bdrv_pwrite(blk->bs, offset, buf, count); | |
305 | } | |
306 | ||
307 | int64_t blk_getlength(BlockBackend *blk) | |
308 | { | |
309 | return bdrv_getlength(blk->bs); | |
310 | } | |
311 | ||
312 | void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr) | |
313 | { | |
314 | bdrv_get_geometry(blk->bs, nb_sectors_ptr); | |
315 | } | |
316 | ||
317 | BlockAIOCB *blk_aio_readv(BlockBackend *blk, int64_t sector_num, | |
318 | QEMUIOVector *iov, int nb_sectors, | |
319 | BlockCompletionFunc *cb, void *opaque) | |
320 | { | |
321 | return bdrv_aio_readv(blk->bs, sector_num, iov, nb_sectors, cb, opaque); | |
322 | } | |
323 | ||
324 | BlockAIOCB *blk_aio_writev(BlockBackend *blk, int64_t sector_num, | |
325 | QEMUIOVector *iov, int nb_sectors, | |
326 | BlockCompletionFunc *cb, void *opaque) | |
327 | { | |
328 | return bdrv_aio_writev(blk->bs, sector_num, iov, nb_sectors, cb, opaque); | |
329 | } | |
330 | ||
331 | BlockAIOCB *blk_aio_flush(BlockBackend *blk, | |
332 | BlockCompletionFunc *cb, void *opaque) | |
333 | { | |
334 | return bdrv_aio_flush(blk->bs, cb, opaque); | |
335 | } | |
336 | ||
337 | BlockAIOCB *blk_aio_discard(BlockBackend *blk, | |
338 | int64_t sector_num, int nb_sectors, | |
339 | BlockCompletionFunc *cb, void *opaque) | |
340 | { | |
341 | return bdrv_aio_discard(blk->bs, sector_num, nb_sectors, cb, opaque); | |
342 | } | |
343 | ||
344 | void blk_aio_cancel(BlockAIOCB *acb) | |
345 | { | |
346 | bdrv_aio_cancel(acb); | |
347 | } | |
348 | ||
349 | void blk_aio_cancel_async(BlockAIOCB *acb) | |
350 | { | |
351 | bdrv_aio_cancel_async(acb); | |
352 | } | |
353 | ||
354 | int blk_aio_multiwrite(BlockBackend *blk, BlockRequest *reqs, int num_reqs) | |
355 | { | |
356 | return bdrv_aio_multiwrite(blk->bs, reqs, num_reqs); | |
357 | } | |
358 | ||
359 | int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf) | |
360 | { | |
361 | return bdrv_ioctl(blk->bs, req, buf); | |
362 | } | |
363 | ||
364 | BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf, | |
365 | BlockCompletionFunc *cb, void *opaque) | |
366 | { | |
367 | return bdrv_aio_ioctl(blk->bs, req, buf, cb, opaque); | |
368 | } | |
369 | ||
370 | int blk_flush(BlockBackend *blk) | |
371 | { | |
372 | return bdrv_flush(blk->bs); | |
373 | } | |
374 | ||
375 | int blk_flush_all(void) | |
376 | { | |
377 | return bdrv_flush_all(); | |
378 | } | |
379 | ||
380 | void blk_drain_all(void) | |
381 | { | |
382 | bdrv_drain_all(); | |
383 | } | |
384 | ||
385 | BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read) | |
386 | { | |
387 | return bdrv_get_on_error(blk->bs, is_read); | |
388 | } | |
389 | ||
390 | BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read, | |
391 | int error) | |
392 | { | |
393 | return bdrv_get_error_action(blk->bs, is_read, error); | |
394 | } | |
395 | ||
396 | void blk_error_action(BlockBackend *blk, BlockErrorAction action, | |
397 | bool is_read, int error) | |
398 | { | |
399 | bdrv_error_action(blk->bs, action, is_read, error); | |
400 | } | |
401 | ||
402 | int blk_is_read_only(BlockBackend *blk) | |
403 | { | |
404 | return bdrv_is_read_only(blk->bs); | |
405 | } | |
406 | ||
407 | int blk_is_sg(BlockBackend *blk) | |
408 | { | |
409 | return bdrv_is_sg(blk->bs); | |
410 | } | |
411 | ||
412 | int blk_enable_write_cache(BlockBackend *blk) | |
413 | { | |
414 | return bdrv_enable_write_cache(blk->bs); | |
415 | } | |
416 | ||
417 | void blk_set_enable_write_cache(BlockBackend *blk, bool wce) | |
418 | { | |
419 | bdrv_set_enable_write_cache(blk->bs, wce); | |
420 | } | |
421 | ||
422 | int blk_is_inserted(BlockBackend *blk) | |
423 | { | |
424 | return bdrv_is_inserted(blk->bs); | |
425 | } | |
426 | ||
427 | void blk_lock_medium(BlockBackend *blk, bool locked) | |
428 | { | |
429 | bdrv_lock_medium(blk->bs, locked); | |
430 | } | |
431 | ||
432 | void blk_eject(BlockBackend *blk, bool eject_flag) | |
433 | { | |
434 | bdrv_eject(blk->bs, eject_flag); | |
435 | } | |
436 | ||
437 | int blk_get_flags(BlockBackend *blk) | |
438 | { | |
439 | return bdrv_get_flags(blk->bs); | |
440 | } | |
441 | ||
442 | void blk_set_guest_block_size(BlockBackend *blk, int align) | |
443 | { | |
444 | bdrv_set_guest_block_size(blk->bs, align); | |
445 | } | |
446 | ||
447 | void *blk_blockalign(BlockBackend *blk, size_t size) | |
448 | { | |
449 | return qemu_blockalign(blk ? blk->bs : NULL, size); | |
450 | } | |
451 | ||
452 | bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp) | |
453 | { | |
454 | return bdrv_op_is_blocked(blk->bs, op, errp); | |
455 | } | |
456 | ||
457 | void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason) | |
458 | { | |
459 | bdrv_op_unblock(blk->bs, op, reason); | |
460 | } | |
461 | ||
462 | void blk_op_block_all(BlockBackend *blk, Error *reason) | |
463 | { | |
464 | bdrv_op_block_all(blk->bs, reason); | |
465 | } | |
466 | ||
467 | void blk_op_unblock_all(BlockBackend *blk, Error *reason) | |
468 | { | |
469 | bdrv_op_unblock_all(blk->bs, reason); | |
470 | } | |
471 | ||
472 | AioContext *blk_get_aio_context(BlockBackend *blk) | |
473 | { | |
474 | return bdrv_get_aio_context(blk->bs); | |
475 | } | |
476 | ||
477 | void blk_set_aio_context(BlockBackend *blk, AioContext *new_context) | |
478 | { | |
479 | bdrv_set_aio_context(blk->bs, new_context); | |
480 | } | |
481 | ||
482 | void blk_io_plug(BlockBackend *blk) | |
483 | { | |
484 | bdrv_io_plug(blk->bs); | |
485 | } | |
486 | ||
487 | void blk_io_unplug(BlockBackend *blk) | |
488 | { | |
489 | bdrv_io_unplug(blk->bs); | |
490 | } | |
491 | ||
492 | BlockAcctStats *blk_get_stats(BlockBackend *blk) | |
493 | { | |
494 | return bdrv_get_stats(blk->bs); | |
495 | } | |
496 | ||
497 | void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk, | |
498 | BlockCompletionFunc *cb, void *opaque) | |
499 | { | |
500 | return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque); | |
501 | } |