2 * Copyright (C) 2009-2010 Nippon Telegraph and Telephone Corporation.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License version
6 * 2 as published by the Free Software Foundation.
8 * You should have received a copy of the GNU General Public License
9 * along with this program. If not, see <http://www.gnu.org/licenses/>.
11 * Contributions after 2012-01-13 are licensed under the terms of the
12 * GNU GPL, version 2 or (at your option) any later version.
15 #include "qemu/osdep.h"
16 #include "qemu-common.h"
17 #include "qapi/error.h"
18 #include "qapi/qapi-visit-sockets.h"
19 #include "qapi/qapi-visit-block-core.h"
20 #include "qapi/qmp/qdict.h"
21 #include "qapi/qobject-input-visitor.h"
22 #include "qapi/qobject-output-visitor.h"
24 #include "qemu/error-report.h"
25 #include "qemu/module.h"
26 #include "qemu/option.h"
27 #include "qemu/sockets.h"
28 #include "block/block_int.h"
29 #include "block/qdict.h"
30 #include "sysemu/block-backend.h"
31 #include "qemu/bitops.h"
32 #include "qemu/cutils.h"
35 #define SD_PROTO_VER 0x01
37 #define SD_DEFAULT_ADDR "localhost"
38 #define SD_DEFAULT_PORT 7000
40 #define SD_OP_CREATE_AND_WRITE_OBJ 0x01
41 #define SD_OP_READ_OBJ 0x02
42 #define SD_OP_WRITE_OBJ 0x03
43 /* 0x04 is used internally by Sheepdog */
45 #define SD_OP_NEW_VDI 0x11
46 #define SD_OP_LOCK_VDI 0x12
47 #define SD_OP_RELEASE_VDI 0x13
48 #define SD_OP_GET_VDI_INFO 0x14
49 #define SD_OP_READ_VDIS 0x15
50 #define SD_OP_FLUSH_VDI 0x16
51 #define SD_OP_DEL_VDI 0x17
52 #define SD_OP_GET_CLUSTER_DEFAULT 0x18
54 #define SD_FLAG_CMD_WRITE 0x01
55 #define SD_FLAG_CMD_COW 0x02
56 #define SD_FLAG_CMD_CACHE 0x04 /* Writeback mode for cache */
57 #define SD_FLAG_CMD_DIRECT 0x08 /* Don't use cache */
59 #define SD_RES_SUCCESS 0x00 /* Success */
60 #define SD_RES_UNKNOWN 0x01 /* Unknown error */
61 #define SD_RES_NO_OBJ 0x02 /* No object found */
62 #define SD_RES_EIO 0x03 /* I/O error */
63 #define SD_RES_VDI_EXIST 0x04 /* Vdi exists already */
64 #define SD_RES_INVALID_PARMS 0x05 /* Invalid parameters */
65 #define SD_RES_SYSTEM_ERROR 0x06 /* System error */
66 #define SD_RES_VDI_LOCKED 0x07 /* Vdi is locked */
67 #define SD_RES_NO_VDI 0x08 /* No vdi found */
68 #define SD_RES_NO_BASE_VDI 0x09 /* No base vdi found */
69 #define SD_RES_VDI_READ 0x0A /* Cannot read requested vdi */
70 #define SD_RES_VDI_WRITE 0x0B /* Cannot write requested vdi */
71 #define SD_RES_BASE_VDI_READ 0x0C /* Cannot read base vdi */
72 #define SD_RES_BASE_VDI_WRITE 0x0D /* Cannot write base vdi */
73 #define SD_RES_NO_TAG 0x0E /* Requested tag is not found */
74 #define SD_RES_STARTUP 0x0F /* Sheepdog is on starting up */
75 #define SD_RES_VDI_NOT_LOCKED 0x10 /* Vdi is not locked */
76 #define SD_RES_SHUTDOWN 0x11 /* Sheepdog is shutting down */
77 #define SD_RES_NO_MEM 0x12 /* Cannot allocate memory */
78 #define SD_RES_FULL_VDI 0x13 /* we already have the maximum vdis */
79 #define SD_RES_VER_MISMATCH 0x14 /* Protocol version mismatch */
80 #define SD_RES_NO_SPACE 0x15 /* Server has no room for new objects */
81 #define SD_RES_WAIT_FOR_FORMAT 0x16 /* Waiting for a format operation */
82 #define SD_RES_WAIT_FOR_JOIN 0x17 /* Waiting for other nodes joining */
83 #define SD_RES_JOIN_FAILED 0x18 /* Target node had failed to join sheepdog */
84 #define SD_RES_HALT 0x19 /* Sheepdog is stopped serving IO request */
85 #define SD_RES_READONLY 0x1A /* Object is read-only */
90 * 0 - 19 (20 bits): data object space
91 * 20 - 31 (12 bits): reserved data object space
92 * 32 - 55 (24 bits): vdi object space
93 * 56 - 59 ( 4 bits): reserved vdi object space
94 * 60 - 63 ( 4 bits): object type identifier space
97 #define VDI_SPACE_SHIFT 32
98 #define VDI_BIT (UINT64_C(1) << 63)
99 #define VMSTATE_BIT (UINT64_C(1) << 62)
100 #define MAX_DATA_OBJS (UINT64_C(1) << 20)
101 #define MAX_CHILDREN 1024
102 #define SD_MAX_VDI_LEN 256
103 #define SD_MAX_VDI_TAG_LEN 256
104 #define SD_NR_VDIS (1U << 24)
105 #define SD_DATA_OBJ_SIZE (UINT64_C(1) << 22)
106 #define SD_MAX_VDI_SIZE (SD_DATA_OBJ_SIZE * MAX_DATA_OBJS)
107 #define SD_DEFAULT_BLOCK_SIZE_SHIFT 22
109 * For erasure coding, we use at most SD_EC_MAX_STRIP for data strips and
110 * (SD_EC_MAX_STRIP - 1) for parity strips
112 * SD_MAX_COPIES is sum of number of data strips and parity strips.
114 #define SD_EC_MAX_STRIP 16
115 #define SD_MAX_COPIES (SD_EC_MAX_STRIP * 2 - 1)
117 #define SD_INODE_SIZE (sizeof(SheepdogInode))
118 #define CURRENT_VDI_ID 0
120 #define LOCK_TYPE_NORMAL 0
121 #define LOCK_TYPE_SHARED 1 /* for iSCSI multipath */
123 typedef struct SheepdogReq
{
129 uint32_t data_length
;
130 uint32_t opcode_specific
[8];
133 typedef struct SheepdogRsp
{
139 uint32_t data_length
;
141 uint32_t opcode_specific
[7];
144 typedef struct SheepdogObjReq
{
150 uint32_t data_length
;
159 typedef struct SheepdogObjRsp
{
165 uint32_t data_length
;
173 typedef struct SheepdogVdiReq
{
179 uint32_t data_length
;
181 uint32_t base_vdi_id
;
184 uint8_t store_policy
;
185 uint8_t block_size_shift
;
191 typedef struct SheepdogVdiRsp
{
197 uint32_t data_length
;
204 typedef struct SheepdogClusterRsp
{
210 uint32_t data_length
;
214 uint8_t block_size_shift
;
217 } SheepdogClusterRsp
;
219 typedef struct SheepdogInode
{
220 char name
[SD_MAX_VDI_LEN
];
221 char tag
[SD_MAX_VDI_TAG_LEN
];
224 uint64_t vm_clock_nsec
;
226 uint64_t vm_state_size
;
227 uint16_t copy_policy
;
229 uint8_t block_size_shift
;
232 uint32_t parent_vdi_id
;
233 uint32_t child_vdi_id
[MAX_CHILDREN
];
234 uint32_t data_vdi_id
[MAX_DATA_OBJS
];
237 #define SD_INODE_HEADER_SIZE offsetof(SheepdogInode, data_vdi_id)
240 * 64 bit FNV-1a non-zero initial basis
242 #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
245 * 64 bit Fowler/Noll/Vo FNV-1a hash code
247 static inline uint64_t fnv_64a_buf(void *buf
, size_t len
, uint64_t hval
)
249 unsigned char *bp
= buf
;
250 unsigned char *be
= bp
+ len
;
252 hval
^= (uint64_t) *bp
++;
253 hval
+= (hval
<< 1) + (hval
<< 4) + (hval
<< 5) +
254 (hval
<< 7) + (hval
<< 8) + (hval
<< 40);
259 static inline bool is_data_obj_writable(SheepdogInode
*inode
, unsigned int idx
)
261 return inode
->vdi_id
== inode
->data_vdi_id
[idx
];
264 static inline bool is_data_obj(uint64_t oid
)
266 return !(VDI_BIT
& oid
);
269 static inline uint64_t data_oid_to_idx(uint64_t oid
)
271 return oid
& (MAX_DATA_OBJS
- 1);
274 static inline uint32_t oid_to_vid(uint64_t oid
)
276 return (oid
& ~VDI_BIT
) >> VDI_SPACE_SHIFT
;
279 static inline uint64_t vid_to_vdi_oid(uint32_t vid
)
281 return VDI_BIT
| ((uint64_t)vid
<< VDI_SPACE_SHIFT
);
284 static inline uint64_t vid_to_vmstate_oid(uint32_t vid
, uint32_t idx
)
286 return VMSTATE_BIT
| ((uint64_t)vid
<< VDI_SPACE_SHIFT
) | idx
;
289 static inline uint64_t vid_to_data_oid(uint32_t vid
, uint32_t idx
)
291 return ((uint64_t)vid
<< VDI_SPACE_SHIFT
) | idx
;
294 static inline bool is_snapshot(struct SheepdogInode
*inode
)
296 return !!inode
->snap_ctime
;
299 static inline size_t count_data_objs(const struct SheepdogInode
*inode
)
301 return DIV_ROUND_UP(inode
->vdi_size
,
302 (1UL << inode
->block_size_shift
));
305 typedef struct SheepdogAIOCB SheepdogAIOCB
;
306 typedef struct BDRVSheepdogState BDRVSheepdogState
;
308 typedef struct AIOReq
{
309 SheepdogAIOCB
*aiocb
;
310 unsigned int iov_offset
;
315 unsigned int data_len
;
320 QLIST_ENTRY(AIOReq
) aio_siblings
;
330 #define AIOCBOverlapping(x, y) \
331 (!(x->max_affect_data_idx < y->min_affect_data_idx \
332 || y->max_affect_data_idx < x->min_affect_data_idx))
334 struct SheepdogAIOCB
{
335 BDRVSheepdogState
*s
;
343 enum AIOCBState aiocb_type
;
345 Coroutine
*coroutine
;
348 uint32_t min_affect_data_idx
;
349 uint32_t max_affect_data_idx
;
352 * The difference between affect_data_idx and dirty_data_idx:
353 * affect_data_idx represents range of index of all request types.
354 * dirty_data_idx represents range of index updated by COW requests.
355 * dirty_data_idx is used for updating an inode object.
357 uint32_t min_dirty_data_idx
;
358 uint32_t max_dirty_data_idx
;
360 QLIST_ENTRY(SheepdogAIOCB
) aiocb_siblings
;
363 struct BDRVSheepdogState
{
364 BlockDriverState
*bs
;
365 AioContext
*aio_context
;
369 char name
[SD_MAX_VDI_LEN
];
371 uint32_t cache_flags
;
372 bool discard_supported
;
381 uint32_t aioreq_seq_num
;
383 /* Every aio request must be linked to either of these queues. */
384 QLIST_HEAD(, AIOReq
) inflight_aio_head
;
385 QLIST_HEAD(, AIOReq
) failed_aio_head
;
388 CoQueue overlapping_queue
;
389 QLIST_HEAD(, SheepdogAIOCB
) inflight_aiocb_head
;
392 typedef struct BDRVSheepdogReopenState
{
395 } BDRVSheepdogReopenState
;
397 static const char *sd_strerror(int err
)
401 static const struct {
405 {SD_RES_SUCCESS
, "Success"},
406 {SD_RES_UNKNOWN
, "Unknown error"},
407 {SD_RES_NO_OBJ
, "No object found"},
408 {SD_RES_EIO
, "I/O error"},
409 {SD_RES_VDI_EXIST
, "VDI exists already"},
410 {SD_RES_INVALID_PARMS
, "Invalid parameters"},
411 {SD_RES_SYSTEM_ERROR
, "System error"},
412 {SD_RES_VDI_LOCKED
, "VDI is already locked"},
413 {SD_RES_NO_VDI
, "No vdi found"},
414 {SD_RES_NO_BASE_VDI
, "No base VDI found"},
415 {SD_RES_VDI_READ
, "Failed read the requested VDI"},
416 {SD_RES_VDI_WRITE
, "Failed to write the requested VDI"},
417 {SD_RES_BASE_VDI_READ
, "Failed to read the base VDI"},
418 {SD_RES_BASE_VDI_WRITE
, "Failed to write the base VDI"},
419 {SD_RES_NO_TAG
, "Failed to find the requested tag"},
420 {SD_RES_STARTUP
, "The system is still booting"},
421 {SD_RES_VDI_NOT_LOCKED
, "VDI isn't locked"},
422 {SD_RES_SHUTDOWN
, "The system is shutting down"},
423 {SD_RES_NO_MEM
, "Out of memory on the server"},
424 {SD_RES_FULL_VDI
, "We already have the maximum vdis"},
425 {SD_RES_VER_MISMATCH
, "Protocol version mismatch"},
426 {SD_RES_NO_SPACE
, "Server has no space for new objects"},
427 {SD_RES_WAIT_FOR_FORMAT
, "Sheepdog is waiting for a format operation"},
428 {SD_RES_WAIT_FOR_JOIN
, "Sheepdog is waiting for other nodes joining"},
429 {SD_RES_JOIN_FAILED
, "Target node had failed to join sheepdog"},
430 {SD_RES_HALT
, "Sheepdog is stopped serving IO request"},
431 {SD_RES_READONLY
, "Object is read-only"},
434 for (i
= 0; i
< ARRAY_SIZE(errors
); ++i
) {
435 if (errors
[i
].err
== err
) {
436 return errors
[i
].desc
;
440 return "Invalid error code";
444 * Sheepdog I/O handling:
446 * 1. In sd_co_rw_vector, we send the I/O requests to the server and
447 * link the requests to the inflight_list in the
448 * BDRVSheepdogState. The function yields while waiting for
449 * receiving the response.
451 * 2. We receive the response in aio_read_response, the fd handler to
452 * the sheepdog connection. We switch back to sd_co_readv/sd_writev
453 * after all the requests belonging to the AIOCB are finished. If
454 * needed, sd_co_writev will send another requests for the vdi object.
457 static inline AIOReq
*alloc_aio_req(BDRVSheepdogState
*s
, SheepdogAIOCB
*acb
,
458 uint64_t oid
, unsigned int data_len
,
459 uint64_t offset
, uint8_t flags
, bool create
,
460 uint64_t base_oid
, unsigned int iov_offset
)
464 aio_req
= g_malloc(sizeof(*aio_req
));
465 aio_req
->aiocb
= acb
;
466 aio_req
->iov_offset
= iov_offset
;
468 aio_req
->base_oid
= base_oid
;
469 aio_req
->offset
= offset
;
470 aio_req
->data_len
= data_len
;
471 aio_req
->flags
= flags
;
472 aio_req
->id
= s
->aioreq_seq_num
++;
473 aio_req
->create
= create
;
479 static void wait_for_overlapping_aiocb(BDRVSheepdogState
*s
, SheepdogAIOCB
*acb
)
484 QLIST_FOREACH(cb
, &s
->inflight_aiocb_head
, aiocb_siblings
) {
485 if (AIOCBOverlapping(acb
, cb
)) {
486 qemu_co_queue_wait(&s
->overlapping_queue
, &s
->queue_lock
);
492 static void sd_aio_setup(SheepdogAIOCB
*acb
, BDRVSheepdogState
*s
,
493 QEMUIOVector
*qiov
, int64_t sector_num
, int nb_sectors
,
496 uint32_t object_size
;
498 object_size
= (UINT32_C(1) << s
->inode
.block_size_shift
);
504 acb
->sector_num
= sector_num
;
505 acb
->nb_sectors
= nb_sectors
;
507 acb
->coroutine
= qemu_coroutine_self();
511 acb
->min_affect_data_idx
= acb
->sector_num
* BDRV_SECTOR_SIZE
/ object_size
;
512 acb
->max_affect_data_idx
= (acb
->sector_num
* BDRV_SECTOR_SIZE
+
513 acb
->nb_sectors
* BDRV_SECTOR_SIZE
) / object_size
;
515 acb
->min_dirty_data_idx
= UINT32_MAX
;
516 acb
->max_dirty_data_idx
= 0;
517 acb
->aiocb_type
= type
;
519 if (type
== AIOCB_FLUSH_CACHE
) {
523 qemu_co_mutex_lock(&s
->queue_lock
);
524 wait_for_overlapping_aiocb(s
, acb
);
525 QLIST_INSERT_HEAD(&s
->inflight_aiocb_head
, acb
, aiocb_siblings
);
526 qemu_co_mutex_unlock(&s
->queue_lock
);
529 static SocketAddress
*sd_server_config(QDict
*options
, Error
**errp
)
531 QDict
*server
= NULL
;
533 SocketAddress
*saddr
= NULL
;
534 Error
*local_err
= NULL
;
536 qdict_extract_subqdict(options
, &server
, "server.");
538 iv
= qobject_input_visitor_new_flat_confused(server
, errp
);
543 visit_type_SocketAddress(iv
, NULL
, &saddr
, &local_err
);
545 error_propagate(errp
, local_err
);
551 qobject_unref(server
);
555 /* Return -EIO in case of error, file descriptor on success */
556 static int connect_to_sdog(BDRVSheepdogState
*s
, Error
**errp
)
560 fd
= socket_connect(s
->addr
, errp
);
562 if (s
->addr
->type
== SOCKET_ADDRESS_TYPE_INET
&& fd
>= 0) {
563 int ret
= socket_set_nodelay(fd
);
565 warn_report("can't set TCP_NODELAY: %s", strerror(errno
));
570 qemu_set_nonblock(fd
);
578 /* Return 0 on success and -errno in case of error */
579 static coroutine_fn
int send_co_req(int sockfd
, SheepdogReq
*hdr
, void *data
,
584 ret
= qemu_co_send(sockfd
, hdr
, sizeof(*hdr
));
585 if (ret
!= sizeof(*hdr
)) {
586 error_report("failed to send a req, %s", strerror(errno
));
590 ret
= qemu_co_send(sockfd
, data
, *wlen
);
592 error_report("failed to send a req, %s", strerror(errno
));
599 typedef struct SheepdogReqCo
{
601 BlockDriverState
*bs
;
602 AioContext
*aio_context
;
612 static void restart_co_req(void *opaque
)
614 SheepdogReqCo
*srco
= opaque
;
616 aio_co_wake(srco
->co
);
619 static coroutine_fn
void do_co_req(void *opaque
)
622 SheepdogReqCo
*srco
= opaque
;
623 int sockfd
= srco
->sockfd
;
624 SheepdogReq
*hdr
= srco
->hdr
;
625 void *data
= srco
->data
;
626 unsigned int *wlen
= srco
->wlen
;
627 unsigned int *rlen
= srco
->rlen
;
629 srco
->co
= qemu_coroutine_self();
630 aio_set_fd_handler(srco
->aio_context
, sockfd
, false,
631 NULL
, restart_co_req
, NULL
, srco
);
633 ret
= send_co_req(sockfd
, hdr
, data
, wlen
);
638 aio_set_fd_handler(srco
->aio_context
, sockfd
, false,
639 restart_co_req
, NULL
, NULL
, srco
);
641 ret
= qemu_co_recv(sockfd
, hdr
, sizeof(*hdr
));
642 if (ret
!= sizeof(*hdr
)) {
643 error_report("failed to get a rsp, %s", strerror(errno
));
648 if (*rlen
> hdr
->data_length
) {
649 *rlen
= hdr
->data_length
;
653 ret
= qemu_co_recv(sockfd
, data
, *rlen
);
655 error_report("failed to get the data, %s", strerror(errno
));
662 /* there is at most one request for this sockfd, so it is safe to
663 * set each handler to NULL. */
664 aio_set_fd_handler(srco
->aio_context
, sockfd
, false,
665 NULL
, NULL
, NULL
, NULL
);
669 /* Set srco->finished before reading bs->wakeup. */
670 atomic_mb_set(&srco
->finished
, true);
672 bdrv_wakeup(srco
->bs
);
677 * Send the request to the sheep in a synchronous manner.
679 * Return 0 on success, -errno in case of error.
681 static int do_req(int sockfd
, BlockDriverState
*bs
, SheepdogReq
*hdr
,
682 void *data
, unsigned int *wlen
, unsigned int *rlen
)
685 SheepdogReqCo srco
= {
687 .aio_context
= bs
? bdrv_get_aio_context(bs
) : qemu_get_aio_context(),
697 if (qemu_in_coroutine()) {
700 co
= qemu_coroutine_create(do_co_req
, &srco
);
702 bdrv_coroutine_enter(bs
, co
);
703 BDRV_POLL_WHILE(bs
, !srco
.finished
);
705 qemu_coroutine_enter(co
);
706 while (!srco
.finished
) {
707 aio_poll(qemu_get_aio_context(), true);
715 static void coroutine_fn
add_aio_request(BDRVSheepdogState
*s
, AIOReq
*aio_req
,
716 struct iovec
*iov
, int niov
,
717 enum AIOCBState aiocb_type
);
718 static void coroutine_fn
resend_aioreq(BDRVSheepdogState
*s
, AIOReq
*aio_req
);
719 static int reload_inode(BDRVSheepdogState
*s
, uint32_t snapid
, const char *tag
);
720 static int get_sheep_fd(BDRVSheepdogState
*s
, Error
**errp
);
721 static void co_write_request(void *opaque
);
723 static coroutine_fn
void reconnect_to_sdog(void *opaque
)
725 BDRVSheepdogState
*s
= opaque
;
726 AIOReq
*aio_req
, *next
;
728 aio_set_fd_handler(s
->aio_context
, s
->fd
, false, NULL
,
733 /* Wait for outstanding write requests to be completed. */
734 while (s
->co_send
!= NULL
) {
735 co_write_request(opaque
);
738 /* Try to reconnect the sheepdog server every one second. */
740 Error
*local_err
= NULL
;
741 s
->fd
= get_sheep_fd(s
, &local_err
);
743 trace_sheepdog_reconnect_to_sdog();
744 error_report_err(local_err
);
745 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME
, 1000000000ULL);
750 * Now we have to resend all the request in the inflight queue. However,
751 * resend_aioreq() can yield and newly created requests can be added to the
752 * inflight queue before the coroutine is resumed. To avoid mixing them, we
753 * have to move all the inflight requests to the failed queue before
754 * resend_aioreq() is called.
756 qemu_co_mutex_lock(&s
->queue_lock
);
757 QLIST_FOREACH_SAFE(aio_req
, &s
->inflight_aio_head
, aio_siblings
, next
) {
758 QLIST_REMOVE(aio_req
, aio_siblings
);
759 QLIST_INSERT_HEAD(&s
->failed_aio_head
, aio_req
, aio_siblings
);
762 /* Resend all the failed aio requests. */
763 while (!QLIST_EMPTY(&s
->failed_aio_head
)) {
764 aio_req
= QLIST_FIRST(&s
->failed_aio_head
);
765 QLIST_REMOVE(aio_req
, aio_siblings
);
766 qemu_co_mutex_unlock(&s
->queue_lock
);
767 resend_aioreq(s
, aio_req
);
768 qemu_co_mutex_lock(&s
->queue_lock
);
770 qemu_co_mutex_unlock(&s
->queue_lock
);
774 * Receive responses of the I/O requests.
776 * This function is registered as a fd handler, and called from the
777 * main loop when s->fd is ready for reading responses.
779 static void coroutine_fn
aio_read_response(void *opaque
)
782 BDRVSheepdogState
*s
= opaque
;
785 AIOReq
*aio_req
= NULL
;
790 ret
= qemu_co_recv(fd
, &rsp
, sizeof(rsp
));
791 if (ret
!= sizeof(rsp
)) {
792 error_report("failed to get the header, %s", strerror(errno
));
796 /* find the right aio_req from the inflight aio list */
797 QLIST_FOREACH(aio_req
, &s
->inflight_aio_head
, aio_siblings
) {
798 if (aio_req
->id
== rsp
.id
) {
803 error_report("cannot find aio_req %x", rsp
.id
);
807 acb
= aio_req
->aiocb
;
809 switch (acb
->aiocb_type
) {
810 case AIOCB_WRITE_UDATA
:
811 if (!is_data_obj(aio_req
->oid
)) {
814 idx
= data_oid_to_idx(aio_req
->oid
);
816 if (aio_req
->create
) {
818 * If the object is newly created one, we need to update
819 * the vdi object (metadata object). min_dirty_data_idx
820 * and max_dirty_data_idx are changed to include updated
821 * index between them.
823 if (rsp
.result
== SD_RES_SUCCESS
) {
824 s
->inode
.data_vdi_id
[idx
] = s
->inode
.vdi_id
;
825 acb
->max_dirty_data_idx
= MAX(idx
, acb
->max_dirty_data_idx
);
826 acb
->min_dirty_data_idx
= MIN(idx
, acb
->min_dirty_data_idx
);
830 case AIOCB_READ_UDATA
:
831 ret
= qemu_co_recvv(fd
, acb
->qiov
->iov
, acb
->qiov
->niov
,
832 aio_req
->iov_offset
, rsp
.data_length
);
833 if (ret
!= rsp
.data_length
) {
834 error_report("failed to get the data, %s", strerror(errno
));
838 case AIOCB_FLUSH_CACHE
:
839 if (rsp
.result
== SD_RES_INVALID_PARMS
) {
840 trace_sheepdog_aio_read_response();
841 s
->cache_flags
= SD_FLAG_CMD_DIRECT
;
842 rsp
.result
= SD_RES_SUCCESS
;
845 case AIOCB_DISCARD_OBJ
:
846 switch (rsp
.result
) {
847 case SD_RES_INVALID_PARMS
:
848 error_report("server doesn't support discard command");
849 rsp
.result
= SD_RES_SUCCESS
;
850 s
->discard_supported
= false;
857 /* No more data for this aio_req (reload_inode below uses its own file
858 * descriptor handler which doesn't use co_recv).
862 qemu_co_mutex_lock(&s
->queue_lock
);
863 QLIST_REMOVE(aio_req
, aio_siblings
);
864 qemu_co_mutex_unlock(&s
->queue_lock
);
866 switch (rsp
.result
) {
869 case SD_RES_READONLY
:
870 if (s
->inode
.vdi_id
== oid_to_vid(aio_req
->oid
)) {
871 ret
= reload_inode(s
, 0, "");
876 if (is_data_obj(aio_req
->oid
)) {
877 aio_req
->oid
= vid_to_data_oid(s
->inode
.vdi_id
,
878 data_oid_to_idx(aio_req
->oid
));
880 aio_req
->oid
= vid_to_vdi_oid(s
->inode
.vdi_id
);
882 resend_aioreq(s
, aio_req
);
886 error_report("%s", sd_strerror(rsp
.result
));
892 if (!--acb
->nr_pending
) {
894 * We've finished all requests which belong to the AIOCB, so
895 * we can switch back to sd_co_readv/writev now.
897 aio_co_wake(acb
->coroutine
);
903 reconnect_to_sdog(opaque
);
906 static void co_read_response(void *opaque
)
908 BDRVSheepdogState
*s
= opaque
;
911 s
->co_recv
= qemu_coroutine_create(aio_read_response
, opaque
);
914 aio_co_enter(s
->aio_context
, s
->co_recv
);
917 static void co_write_request(void *opaque
)
919 BDRVSheepdogState
*s
= opaque
;
921 aio_co_wake(s
->co_send
);
925 * Return a socket descriptor to read/write objects.
927 * We cannot use this descriptor for other operations because
928 * the block driver may be on waiting response from the server.
930 static int get_sheep_fd(BDRVSheepdogState
*s
, Error
**errp
)
934 fd
= connect_to_sdog(s
, errp
);
939 aio_set_fd_handler(s
->aio_context
, fd
, false,
940 co_read_response
, NULL
, NULL
, s
);
945 * Parse numeric snapshot ID in @str
946 * If @str can't be parsed as number, return false.
947 * Else, if the number is zero or too large, set *@snapid to zero and
949 * Else, set *@snapid to the number and return true.
951 static bool sd_parse_snapid(const char *str
, uint32_t *snapid
)
956 ret
= qemu_strtoul(str
, NULL
, 10, &ul
);
957 if (ret
== -ERANGE
) {
963 if (ul
> UINT32_MAX
) {
971 static bool sd_parse_snapid_or_tag(const char *str
,
972 uint32_t *snapid
, char tag
[])
974 if (!sd_parse_snapid(str
, snapid
)) {
976 if (g_strlcpy(tag
, str
, SD_MAX_VDI_TAG_LEN
) >= SD_MAX_VDI_TAG_LEN
) {
979 } else if (!*snapid
) {
988 const char *path
; /* non-null iff transport is tcp */
989 const char *host
; /* valid when transport is tcp */
990 int port
; /* valid when transport is tcp */
991 char vdi
[SD_MAX_VDI_LEN
];
992 char tag
[SD_MAX_VDI_TAG_LEN
];
994 /* Remainder is only for sd_config_done() */
999 static void sd_config_done(SheepdogConfig
*cfg
)
1002 query_params_free(cfg
->qp
);
1007 static void sd_parse_uri(SheepdogConfig
*cfg
, const char *filename
,
1011 QueryParams
*qp
= NULL
;
1015 memset(cfg
, 0, sizeof(*cfg
));
1017 cfg
->uri
= uri
= uri_parse(filename
);
1019 error_setg(&err
, "invalid URI '%s'", filename
);
1024 if (!g_strcmp0(uri
->scheme
, "sheepdog")) {
1026 } else if (!g_strcmp0(uri
->scheme
, "sheepdog+tcp")) {
1028 } else if (!g_strcmp0(uri
->scheme
, "sheepdog+unix")) {
1031 error_setg(&err
, "URI scheme must be 'sheepdog', 'sheepdog+tcp',"
1032 " or 'sheepdog+unix'");
1036 if (uri
->path
== NULL
|| !strcmp(uri
->path
, "/")) {
1037 error_setg(&err
, "missing file path in URI");
1040 if (g_strlcpy(cfg
->vdi
, uri
->path
+ 1, SD_MAX_VDI_LEN
)
1041 >= SD_MAX_VDI_LEN
) {
1042 error_setg(&err
, "VDI name is too long");
1046 cfg
->qp
= qp
= query_params_parse(uri
->query
);
1049 /* sheepdog+unix:///vdiname?socket=path */
1050 if (uri
->server
|| uri
->port
) {
1051 error_setg(&err
, "URI scheme %s doesn't accept a server address",
1057 "URI scheme %s requires query parameter 'socket'",
1061 if (qp
->n
!= 1 || strcmp(qp
->p
[0].name
, "socket")) {
1062 error_setg(&err
, "unexpected query parameters");
1065 cfg
->path
= qp
->p
[0].value
;
1067 /* sheepdog[+tcp]://[host:port]/vdiname */
1069 error_setg(&err
, "unexpected query parameters");
1072 cfg
->host
= uri
->server
;
1073 cfg
->port
= uri
->port
;
1077 if (uri
->fragment
) {
1078 if (!sd_parse_snapid_or_tag(uri
->fragment
,
1079 &cfg
->snap_id
, cfg
->tag
)) {
1080 error_setg(&err
, "'%s' is not a valid snapshot ID",
1085 cfg
->snap_id
= CURRENT_VDI_ID
; /* search current vdi */
1090 error_propagate(errp
, err
);
1091 sd_config_done(cfg
);
1096 * Parse a filename (old syntax)
1098 * filename must be one of the following formats:
1100 * 2. [vdiname]:[snapid]
1101 * 3. [vdiname]:[tag]
1102 * 4. [hostname]:[port]:[vdiname]
1103 * 5. [hostname]:[port]:[vdiname]:[snapid]
1104 * 6. [hostname]:[port]:[vdiname]:[tag]
1106 * You can boot from the snapshot images by specifying `snapid` or
1109 * You can run VMs outside the Sheepdog cluster by specifying
1110 * `hostname' and `port' (experimental).
1112 static void parse_vdiname(SheepdogConfig
*cfg
, const char *filename
,
1117 const char *host_spec
, *vdi_spec
;
1120 strstart(filename
, "sheepdog:", &filename
);
1121 p
= q
= g_strdup(filename
);
1123 /* count the number of separators */
1133 /* use the first two tokens as host_spec. */
1146 p
= strchr(vdi_spec
, ':');
1151 uri
= g_strdup_printf("sheepdog://%s/%s", host_spec
, vdi_spec
);
1154 * FIXME We to escape URI meta-characters, e.g. "x?y=z"
1155 * produces "sheepdog://x?y=z". Because of that ...
1157 sd_parse_uri(cfg
, uri
, &err
);
1160 * ... this can fail, but the error message is misleading.
1161 * Replace it by the traditional useless one until the
1162 * escaping is fixed.
1165 error_setg(errp
, "Can't parse filename");
1172 static void sd_parse_filename(const char *filename
, QDict
*options
,
1179 if (strstr(filename
, "://")) {
1180 sd_parse_uri(&cfg
, filename
, &err
);
1182 parse_vdiname(&cfg
, filename
, &err
);
1185 error_propagate(errp
, err
);
1190 qdict_set_default_str(options
, "server.path", cfg
.path
);
1191 qdict_set_default_str(options
, "server.type", "unix");
1193 qdict_set_default_str(options
, "server.type", "inet");
1194 qdict_set_default_str(options
, "server.host",
1195 cfg
.host
?: SD_DEFAULT_ADDR
);
1196 snprintf(buf
, sizeof(buf
), "%d", cfg
.port
?: SD_DEFAULT_PORT
);
1197 qdict_set_default_str(options
, "server.port", buf
);
1199 qdict_set_default_str(options
, "vdi", cfg
.vdi
);
1200 qdict_set_default_str(options
, "tag", cfg
.tag
);
1202 snprintf(buf
, sizeof(buf
), "%d", cfg
.snap_id
);
1203 qdict_set_default_str(options
, "snap-id", buf
);
1206 sd_config_done(&cfg
);
1209 static int find_vdi_name(BDRVSheepdogState
*s
, const char *filename
,
1210 uint32_t snapid
, const char *tag
, uint32_t *vid
,
1211 bool lock
, Error
**errp
)
1215 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1216 unsigned int wlen
, rlen
= 0;
1217 char buf
[SD_MAX_VDI_LEN
+ SD_MAX_VDI_TAG_LEN
] QEMU_NONSTRING
;
1219 fd
= connect_to_sdog(s
, errp
);
1224 /* This pair of strncpy calls ensures that the buffer is zero-filled,
1225 * which is desirable since we'll soon be sending those bytes, and
1226 * don't want the send_req to read uninitialized data.
1228 strncpy(buf
, filename
, SD_MAX_VDI_LEN
);
1229 strncpy(buf
+ SD_MAX_VDI_LEN
, tag
, SD_MAX_VDI_TAG_LEN
);
1231 memset(&hdr
, 0, sizeof(hdr
));
1233 hdr
.opcode
= SD_OP_LOCK_VDI
;
1234 hdr
.type
= LOCK_TYPE_NORMAL
;
1236 hdr
.opcode
= SD_OP_GET_VDI_INFO
;
1238 wlen
= SD_MAX_VDI_LEN
+ SD_MAX_VDI_TAG_LEN
;
1239 hdr
.proto_ver
= SD_PROTO_VER
;
1240 hdr
.data_length
= wlen
;
1241 hdr
.snapid
= snapid
;
1242 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1244 ret
= do_req(fd
, s
->bs
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1246 error_setg_errno(errp
, -ret
, "cannot get vdi info");
1250 if (rsp
->result
!= SD_RES_SUCCESS
) {
1251 error_setg(errp
, "cannot get vdi info, %s, %s %" PRIu32
" %s",
1252 sd_strerror(rsp
->result
), filename
, snapid
, tag
);
1253 if (rsp
->result
== SD_RES_NO_VDI
) {
1255 } else if (rsp
->result
== SD_RES_VDI_LOCKED
) {
1270 static void coroutine_fn
add_aio_request(BDRVSheepdogState
*s
, AIOReq
*aio_req
,
1271 struct iovec
*iov
, int niov
,
1272 enum AIOCBState aiocb_type
)
1274 int nr_copies
= s
->inode
.nr_copies
;
1276 unsigned int wlen
= 0;
1278 uint64_t oid
= aio_req
->oid
;
1279 unsigned int datalen
= aio_req
->data_len
;
1280 uint64_t offset
= aio_req
->offset
;
1281 uint8_t flags
= aio_req
->flags
;
1282 uint64_t old_oid
= aio_req
->base_oid
;
1283 bool create
= aio_req
->create
;
1285 qemu_co_mutex_lock(&s
->queue_lock
);
1286 QLIST_INSERT_HEAD(&s
->inflight_aio_head
, aio_req
, aio_siblings
);
1287 qemu_co_mutex_unlock(&s
->queue_lock
);
1290 error_report("bug");
1293 memset(&hdr
, 0, sizeof(hdr
));
1295 switch (aiocb_type
) {
1296 case AIOCB_FLUSH_CACHE
:
1297 hdr
.opcode
= SD_OP_FLUSH_VDI
;
1299 case AIOCB_READ_UDATA
:
1300 hdr
.opcode
= SD_OP_READ_OBJ
;
1303 case AIOCB_WRITE_UDATA
:
1305 hdr
.opcode
= SD_OP_CREATE_AND_WRITE_OBJ
;
1307 hdr
.opcode
= SD_OP_WRITE_OBJ
;
1310 hdr
.flags
= SD_FLAG_CMD_WRITE
| flags
;
1312 case AIOCB_DISCARD_OBJ
:
1313 hdr
.opcode
= SD_OP_WRITE_OBJ
;
1314 hdr
.flags
= SD_FLAG_CMD_WRITE
| flags
;
1315 s
->inode
.data_vdi_id
[data_oid_to_idx(oid
)] = 0;
1316 offset
= offsetof(SheepdogInode
,
1317 data_vdi_id
[data_oid_to_idx(oid
)]);
1318 oid
= vid_to_vdi_oid(s
->inode
.vdi_id
);
1319 wlen
= datalen
= sizeof(uint32_t);
1323 if (s
->cache_flags
) {
1324 hdr
.flags
|= s
->cache_flags
;
1328 hdr
.cow_oid
= old_oid
;
1329 hdr
.copies
= s
->inode
.nr_copies
;
1331 hdr
.data_length
= datalen
;
1332 hdr
.offset
= offset
;
1334 hdr
.id
= aio_req
->id
;
1336 qemu_co_mutex_lock(&s
->lock
);
1337 s
->co_send
= qemu_coroutine_self();
1338 aio_set_fd_handler(s
->aio_context
, s
->fd
, false,
1339 co_read_response
, co_write_request
, NULL
, s
);
1340 socket_set_cork(s
->fd
, 1);
1343 ret
= qemu_co_send(s
->fd
, &hdr
, sizeof(hdr
));
1344 if (ret
!= sizeof(hdr
)) {
1345 error_report("failed to send a req, %s", strerror(errno
));
1350 ret
= qemu_co_sendv(s
->fd
, iov
, niov
, aio_req
->iov_offset
, wlen
);
1352 error_report("failed to send a data, %s", strerror(errno
));
1356 socket_set_cork(s
->fd
, 0);
1357 aio_set_fd_handler(s
->aio_context
, s
->fd
, false,
1358 co_read_response
, NULL
, NULL
, s
);
1360 qemu_co_mutex_unlock(&s
->lock
);
1363 static int read_write_object(int fd
, BlockDriverState
*bs
, char *buf
,
1364 uint64_t oid
, uint8_t copies
,
1365 unsigned int datalen
, uint64_t offset
,
1366 bool write
, bool create
, uint32_t cache_flags
)
1369 SheepdogObjRsp
*rsp
= (SheepdogObjRsp
*)&hdr
;
1370 unsigned int wlen
, rlen
;
1373 memset(&hdr
, 0, sizeof(hdr
));
1378 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1380 hdr
.opcode
= SD_OP_CREATE_AND_WRITE_OBJ
;
1382 hdr
.opcode
= SD_OP_WRITE_OBJ
;
1387 hdr
.opcode
= SD_OP_READ_OBJ
;
1390 hdr
.flags
|= cache_flags
;
1393 hdr
.data_length
= datalen
;
1394 hdr
.offset
= offset
;
1395 hdr
.copies
= copies
;
1397 ret
= do_req(fd
, bs
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1399 error_report("failed to send a request to the sheep");
1403 switch (rsp
->result
) {
1404 case SD_RES_SUCCESS
:
1407 error_report("%s", sd_strerror(rsp
->result
));
1412 static int read_object(int fd
, BlockDriverState
*bs
, char *buf
,
1413 uint64_t oid
, uint8_t copies
,
1414 unsigned int datalen
, uint64_t offset
,
1415 uint32_t cache_flags
)
1417 return read_write_object(fd
, bs
, buf
, oid
, copies
,
1418 datalen
, offset
, false,
1419 false, cache_flags
);
1422 static int write_object(int fd
, BlockDriverState
*bs
, char *buf
,
1423 uint64_t oid
, uint8_t copies
,
1424 unsigned int datalen
, uint64_t offset
, bool create
,
1425 uint32_t cache_flags
)
1427 return read_write_object(fd
, bs
, buf
, oid
, copies
,
1428 datalen
, offset
, true,
1429 create
, cache_flags
);
1432 /* update inode with the latest state */
1433 static int reload_inode(BDRVSheepdogState
*s
, uint32_t snapid
, const char *tag
)
1435 Error
*local_err
= NULL
;
1436 SheepdogInode
*inode
;
1440 fd
= connect_to_sdog(s
, &local_err
);
1442 error_report_err(local_err
);
1446 inode
= g_malloc(SD_INODE_HEADER_SIZE
);
1448 ret
= find_vdi_name(s
, s
->name
, snapid
, tag
, &vid
, false, &local_err
);
1450 error_report_err(local_err
);
1454 ret
= read_object(fd
, s
->bs
, (char *)inode
, vid_to_vdi_oid(vid
),
1455 s
->inode
.nr_copies
, SD_INODE_HEADER_SIZE
, 0,
1461 if (inode
->vdi_id
!= s
->inode
.vdi_id
) {
1462 memcpy(&s
->inode
, inode
, SD_INODE_HEADER_SIZE
);
1472 static void coroutine_fn
resend_aioreq(BDRVSheepdogState
*s
, AIOReq
*aio_req
)
1474 SheepdogAIOCB
*acb
= aio_req
->aiocb
;
1476 aio_req
->create
= false;
1478 /* check whether this request becomes a CoW one */
1479 if (acb
->aiocb_type
== AIOCB_WRITE_UDATA
&& is_data_obj(aio_req
->oid
)) {
1480 int idx
= data_oid_to_idx(aio_req
->oid
);
1482 if (is_data_obj_writable(&s
->inode
, idx
)) {
1486 if (s
->inode
.data_vdi_id
[idx
]) {
1487 aio_req
->base_oid
= vid_to_data_oid(s
->inode
.data_vdi_id
[idx
], idx
);
1488 aio_req
->flags
|= SD_FLAG_CMD_COW
;
1490 aio_req
->create
= true;
1493 if (is_data_obj(aio_req
->oid
)) {
1494 add_aio_request(s
, aio_req
, acb
->qiov
->iov
, acb
->qiov
->niov
,
1498 iov
.iov_base
= &s
->inode
;
1499 iov
.iov_len
= sizeof(s
->inode
);
1500 add_aio_request(s
, aio_req
, &iov
, 1, AIOCB_WRITE_UDATA
);
1504 static void sd_detach_aio_context(BlockDriverState
*bs
)
1506 BDRVSheepdogState
*s
= bs
->opaque
;
1508 aio_set_fd_handler(s
->aio_context
, s
->fd
, false, NULL
,
1512 static void sd_attach_aio_context(BlockDriverState
*bs
,
1513 AioContext
*new_context
)
1515 BDRVSheepdogState
*s
= bs
->opaque
;
1517 s
->aio_context
= new_context
;
1518 aio_set_fd_handler(new_context
, s
->fd
, false,
1519 co_read_response
, NULL
, NULL
, s
);
1522 static QemuOptsList runtime_opts
= {
1524 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
1528 .type
= QEMU_OPT_STRING
,
1532 .type
= QEMU_OPT_NUMBER
,
1536 .type
= QEMU_OPT_STRING
,
1538 { /* end of list */ }
1542 static int sd_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1547 BDRVSheepdogState
*s
= bs
->opaque
;
1548 const char *vdi
, *snap_id_str
, *tag
;
1552 Error
*local_err
= NULL
;
1555 s
->aio_context
= bdrv_get_aio_context(bs
);
1557 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
1558 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
1560 error_propagate(errp
, local_err
);
1565 s
->addr
= sd_server_config(options
, errp
);
1571 vdi
= qemu_opt_get(opts
, "vdi");
1572 snap_id_str
= qemu_opt_get(opts
, "snap-id");
1573 snap_id
= qemu_opt_get_number(opts
, "snap-id", CURRENT_VDI_ID
);
1574 tag
= qemu_opt_get(opts
, "tag");
1577 error_setg(errp
, "parameter 'vdi' is missing");
1581 if (strlen(vdi
) >= SD_MAX_VDI_LEN
) {
1582 error_setg(errp
, "value of parameter 'vdi' is too long");
1587 if (snap_id
> UINT32_MAX
) {
1590 if (snap_id_str
&& !snap_id
) {
1591 error_setg(errp
, "'snap-id=%s' is not a valid snapshot ID",
1600 if (strlen(tag
) >= SD_MAX_VDI_TAG_LEN
) {
1601 error_setg(errp
, "value of parameter 'tag' is too long");
1606 QLIST_INIT(&s
->inflight_aio_head
);
1607 QLIST_INIT(&s
->failed_aio_head
);
1608 QLIST_INIT(&s
->inflight_aiocb_head
);
1610 s
->fd
= get_sheep_fd(s
, errp
);
1616 ret
= find_vdi_name(s
, vdi
, (uint32_t)snap_id
, tag
, &vid
, true, errp
);
1622 * QEMU block layer emulates writethrough cache as 'writeback + flush', so
1623 * we always set SD_FLAG_CMD_CACHE (writeback cache) as default.
1625 s
->cache_flags
= SD_FLAG_CMD_CACHE
;
1626 if (flags
& BDRV_O_NOCACHE
) {
1627 s
->cache_flags
= SD_FLAG_CMD_DIRECT
;
1629 s
->discard_supported
= true;
1631 if (snap_id
|| tag
[0]) {
1632 trace_sheepdog_open(vid
);
1633 s
->is_snapshot
= true;
1636 fd
= connect_to_sdog(s
, errp
);
1642 buf
= g_malloc(SD_INODE_SIZE
);
1643 ret
= read_object(fd
, s
->bs
, buf
, vid_to_vdi_oid(vid
),
1644 0, SD_INODE_SIZE
, 0, s
->cache_flags
);
1649 error_setg(errp
, "Can't read snapshot inode");
1653 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
1655 bs
->total_sectors
= s
->inode
.vdi_size
/ BDRV_SECTOR_SIZE
;
1656 pstrcpy(s
->name
, sizeof(s
->name
), vdi
);
1657 qemu_co_mutex_init(&s
->lock
);
1658 qemu_co_mutex_init(&s
->queue_lock
);
1659 qemu_co_queue_init(&s
->overlapping_queue
);
1660 qemu_opts_del(opts
);
1665 aio_set_fd_handler(bdrv_get_aio_context(bs
), s
->fd
,
1666 false, NULL
, NULL
, NULL
, NULL
);
1669 qemu_opts_del(opts
);
1674 static int sd_reopen_prepare(BDRVReopenState
*state
, BlockReopenQueue
*queue
,
1677 BDRVSheepdogState
*s
= state
->bs
->opaque
;
1678 BDRVSheepdogReopenState
*re_s
;
1681 re_s
= state
->opaque
= g_new0(BDRVSheepdogReopenState
, 1);
1683 re_s
->cache_flags
= SD_FLAG_CMD_CACHE
;
1684 if (state
->flags
& BDRV_O_NOCACHE
) {
1685 re_s
->cache_flags
= SD_FLAG_CMD_DIRECT
;
1688 re_s
->fd
= get_sheep_fd(s
, errp
);
1697 static void sd_reopen_commit(BDRVReopenState
*state
)
1699 BDRVSheepdogReopenState
*re_s
= state
->opaque
;
1700 BDRVSheepdogState
*s
= state
->bs
->opaque
;
1703 aio_set_fd_handler(s
->aio_context
, s
->fd
, false,
1704 NULL
, NULL
, NULL
, NULL
);
1709 s
->cache_flags
= re_s
->cache_flags
;
1711 g_free(state
->opaque
);
1712 state
->opaque
= NULL
;
1717 static void sd_reopen_abort(BDRVReopenState
*state
)
1719 BDRVSheepdogReopenState
*re_s
= state
->opaque
;
1720 BDRVSheepdogState
*s
= state
->bs
->opaque
;
1727 aio_set_fd_handler(s
->aio_context
, re_s
->fd
, false,
1728 NULL
, NULL
, NULL
, NULL
);
1729 closesocket(re_s
->fd
);
1732 g_free(state
->opaque
);
1733 state
->opaque
= NULL
;
1738 static int do_sd_create(BDRVSheepdogState
*s
, uint32_t *vdi_id
, int snapshot
,
1742 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1744 unsigned int wlen
, rlen
= 0;
1745 char buf
[SD_MAX_VDI_LEN
];
1747 fd
= connect_to_sdog(s
, errp
);
1752 /* FIXME: would it be better to fail (e.g., return -EIO) when filename
1753 * does not fit in buf? For now, just truncate and avoid buffer overrun.
1755 memset(buf
, 0, sizeof(buf
));
1756 pstrcpy(buf
, sizeof(buf
), s
->name
);
1758 memset(&hdr
, 0, sizeof(hdr
));
1759 hdr
.opcode
= SD_OP_NEW_VDI
;
1760 hdr
.base_vdi_id
= s
->inode
.vdi_id
;
1762 wlen
= SD_MAX_VDI_LEN
;
1764 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1765 hdr
.snapid
= snapshot
;
1767 hdr
.data_length
= wlen
;
1768 hdr
.vdi_size
= s
->inode
.vdi_size
;
1769 hdr
.copy_policy
= s
->inode
.copy_policy
;
1770 hdr
.copies
= s
->inode
.nr_copies
;
1771 hdr
.block_size_shift
= s
->inode
.block_size_shift
;
1773 ret
= do_req(fd
, NULL
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1778 error_setg_errno(errp
, -ret
, "create failed");
1782 if (rsp
->result
!= SD_RES_SUCCESS
) {
1783 error_setg(errp
, "%s, %s", sd_strerror(rsp
->result
), s
->inode
.name
);
1788 *vdi_id
= rsp
->vdi_id
;
1794 static int sd_prealloc(BlockDriverState
*bs
, int64_t old_size
, int64_t new_size
,
1797 BlockBackend
*blk
= NULL
;
1798 BDRVSheepdogState
*base
= bs
->opaque
;
1799 unsigned long buf_size
;
1800 uint32_t idx
, max_idx
;
1801 uint32_t object_size
;
1805 blk
= blk_new(bdrv_get_aio_context(bs
),
1806 BLK_PERM_CONSISTENT_READ
| BLK_PERM_WRITE
| BLK_PERM_RESIZE
,
1809 ret
= blk_insert_bs(blk
, bs
, errp
);
1811 goto out_with_err_set
;
1814 blk_set_allow_write_beyond_eof(blk
, true);
1816 object_size
= (UINT32_C(1) << base
->inode
.block_size_shift
);
1817 buf_size
= MIN(object_size
, SD_DATA_OBJ_SIZE
);
1818 buf
= g_malloc0(buf_size
);
1820 max_idx
= DIV_ROUND_UP(new_size
, buf_size
);
1822 for (idx
= old_size
/ buf_size
; idx
< max_idx
; idx
++) {
1824 * The created image can be a cloned image, so we need to read
1825 * a data from the source image.
1827 ret
= blk_pread(blk
, idx
* buf_size
, buf
, buf_size
);
1831 ret
= blk_pwrite(blk
, idx
* buf_size
, buf
, buf_size
, 0);
1840 error_setg_errno(errp
, -ret
, "Can't pre-allocate");
1849 static int sd_create_prealloc(BlockdevOptionsSheepdog
*location
, int64_t size
,
1852 BlockDriverState
*bs
;
1854 QObject
*obj
= NULL
;
1856 Error
*local_err
= NULL
;
1859 v
= qobject_output_visitor_new(&obj
);
1860 visit_type_BlockdevOptionsSheepdog(v
, NULL
, &location
, &local_err
);
1864 error_propagate(errp
, local_err
);
1869 qdict
= qobject_to(QDict
, obj
);
1870 qdict_flatten(qdict
);
1872 qdict_put_str(qdict
, "driver", "sheepdog");
1874 bs
= bdrv_open(NULL
, NULL
, qdict
, BDRV_O_PROTOCOL
| BDRV_O_RDWR
, errp
);
1880 ret
= sd_prealloc(bs
, 0, size
, errp
);
1883 qobject_unref(qdict
);
1887 static int parse_redundancy(BDRVSheepdogState
*s
, SheepdogRedundancy
*opt
)
1889 struct SheepdogInode
*inode
= &s
->inode
;
1891 switch (opt
->type
) {
1892 case SHEEPDOG_REDUNDANCY_TYPE_FULL
:
1893 if (opt
->u
.full
.copies
> SD_MAX_COPIES
|| opt
->u
.full
.copies
< 1) {
1896 inode
->copy_policy
= 0;
1897 inode
->nr_copies
= opt
->u
.full
.copies
;
1900 case SHEEPDOG_REDUNDANCY_TYPE_ERASURE_CODED
:
1902 int64_t copy
= opt
->u
.erasure_coded
.data_strips
;
1903 int64_t parity
= opt
->u
.erasure_coded
.parity_strips
;
1905 if (copy
!= 2 && copy
!= 4 && copy
!= 8 && copy
!= 16) {
1909 if (parity
>= SD_EC_MAX_STRIP
|| parity
< 1) {
1914 * 4 bits for parity and 4 bits for data.
1915 * We have to compress upper data bits because it can't represent 16
1917 inode
->copy_policy
= ((copy
/ 2) << 4) + parity
;
1918 inode
->nr_copies
= copy
+ parity
;
1923 g_assert_not_reached();
1930 * Sheepdog support two kinds of redundancy, full replication and erasure
1933 * # create a fully replicated vdi with x copies
1934 * -o redundancy=x (1 <= x <= SD_MAX_COPIES)
1936 * # create a erasure coded vdi with x data strips and y parity strips
1937 * -o redundancy=x:y (x must be one of {2,4,8,16} and 1 <= y < SD_EC_MAX_STRIP)
1939 static SheepdogRedundancy
*parse_redundancy_str(const char *opt
)
1941 SheepdogRedundancy
*redundancy
;
1942 const char *n1
, *n2
;
1947 pstrcpy(p
, sizeof(p
), opt
);
1948 n1
= strtok(p
, ":");
1949 n2
= strtok(NULL
, ":");
1955 ret
= qemu_strtol(n1
, NULL
, 10, ©
);
1960 redundancy
= g_new0(SheepdogRedundancy
, 1);
1962 *redundancy
= (SheepdogRedundancy
) {
1963 .type
= SHEEPDOG_REDUNDANCY_TYPE_FULL
,
1964 .u
.full
.copies
= copy
,
1967 ret
= qemu_strtol(n2
, NULL
, 10, &parity
);
1973 *redundancy
= (SheepdogRedundancy
) {
1974 .type
= SHEEPDOG_REDUNDANCY_TYPE_ERASURE_CODED
,
1975 .u
.erasure_coded
= {
1976 .data_strips
= copy
,
1977 .parity_strips
= parity
,
1985 static int parse_block_size_shift(BDRVSheepdogState
*s
,
1986 BlockdevCreateOptionsSheepdog
*opts
)
1988 struct SheepdogInode
*inode
= &s
->inode
;
1989 uint64_t object_size
;
1992 if (opts
->has_object_size
) {
1993 object_size
= opts
->object_size
;
1995 if ((object_size
- 1) & object_size
) { /* not a power of 2? */
1998 obj_order
= ctz32(object_size
);
1999 if (obj_order
< 20 || obj_order
> 31) {
2002 inode
->block_size_shift
= (uint8_t)obj_order
;
2008 static int sd_co_create(BlockdevCreateOptions
*options
, Error
**errp
)
2010 BlockdevCreateOptionsSheepdog
*opts
= &options
->u
.sheepdog
;
2013 char *backing_file
= NULL
;
2015 BDRVSheepdogState
*s
;
2016 uint64_t max_vdi_size
;
2017 bool prealloc
= false;
2019 assert(options
->driver
== BLOCKDEV_DRIVER_SHEEPDOG
);
2021 s
= g_new0(BDRVSheepdogState
, 1);
2023 /* Steal SocketAddress from QAPI, set NULL to prevent double free */
2024 s
->addr
= opts
->location
->server
;
2025 opts
->location
->server
= NULL
;
2027 if (strlen(opts
->location
->vdi
) >= sizeof(s
->name
)) {
2028 error_setg(errp
, "'vdi' string too long");
2032 pstrcpy(s
->name
, sizeof(s
->name
), opts
->location
->vdi
);
2034 s
->inode
.vdi_size
= opts
->size
;
2035 backing_file
= opts
->backing_file
;
2037 if (!opts
->has_preallocation
) {
2038 opts
->preallocation
= PREALLOC_MODE_OFF
;
2040 switch (opts
->preallocation
) {
2041 case PREALLOC_MODE_OFF
:
2044 case PREALLOC_MODE_FULL
:
2048 error_setg(errp
, "Preallocation mode not supported for Sheepdog");
2053 if (opts
->has_redundancy
) {
2054 ret
= parse_redundancy(s
, opts
->redundancy
);
2056 error_setg(errp
, "Invalid redundancy mode");
2060 ret
= parse_block_size_shift(s
, opts
);
2062 error_setg(errp
, "Invalid object_size."
2063 " obect_size needs to be power of 2"
2064 " and be limited from 2^20 to 2^31");
2068 if (opts
->has_backing_file
) {
2070 BDRVSheepdogState
*base
;
2073 /* Currently, only Sheepdog backing image is supported. */
2074 drv
= bdrv_find_protocol(opts
->backing_file
, true, NULL
);
2075 if (!drv
|| strcmp(drv
->protocol_name
, "sheepdog") != 0) {
2076 error_setg(errp
, "backing_file must be a sheepdog image");
2081 blk
= blk_new_open(opts
->backing_file
, NULL
, NULL
,
2082 BDRV_O_PROTOCOL
, errp
);
2088 base
= blk_bs(blk
)->opaque
;
2090 if (!is_snapshot(&base
->inode
)) {
2091 error_setg(errp
, "cannot clone from a non snapshot vdi");
2096 s
->inode
.vdi_id
= base
->inode
.vdi_id
;
2100 s
->aio_context
= qemu_get_aio_context();
2102 /* if block_size_shift is not specified, get cluster default value */
2103 if (s
->inode
.block_size_shift
== 0) {
2105 SheepdogClusterRsp
*rsp
= (SheepdogClusterRsp
*)&hdr
;
2107 unsigned int wlen
= 0, rlen
= 0;
2109 fd
= connect_to_sdog(s
, errp
);
2115 memset(&hdr
, 0, sizeof(hdr
));
2116 hdr
.opcode
= SD_OP_GET_CLUSTER_DEFAULT
;
2117 hdr
.proto_ver
= SD_PROTO_VER
;
2119 ret
= do_req(fd
, NULL
, (SheepdogReq
*)&hdr
,
2120 NULL
, &wlen
, &rlen
);
2123 error_setg_errno(errp
, -ret
, "failed to get cluster default");
2126 if (rsp
->result
== SD_RES_SUCCESS
) {
2127 s
->inode
.block_size_shift
= rsp
->block_size_shift
;
2129 s
->inode
.block_size_shift
= SD_DEFAULT_BLOCK_SIZE_SHIFT
;
2133 max_vdi_size
= (UINT64_C(1) << s
->inode
.block_size_shift
) * MAX_DATA_OBJS
;
2135 if (s
->inode
.vdi_size
> max_vdi_size
) {
2136 error_setg(errp
, "An image is too large."
2137 " The maximum image size is %"PRIu64
"GB",
2138 max_vdi_size
/ 1024 / 1024 / 1024);
2143 ret
= do_sd_create(s
, &vid
, 0, errp
);
2149 ret
= sd_create_prealloc(opts
->location
, opts
->size
, errp
);
2152 g_free(backing_file
);
2159 static int coroutine_fn
sd_co_create_opts(const char *filename
, QemuOpts
*opts
,
2162 BlockdevCreateOptions
*create_options
= NULL
;
2163 QDict
*qdict
, *location_qdict
;
2166 Error
*local_err
= NULL
;
2169 redundancy
= qemu_opt_get_del(opts
, BLOCK_OPT_REDUNDANCY
);
2171 qdict
= qemu_opts_to_qdict(opts
, NULL
);
2172 qdict_put_str(qdict
, "driver", "sheepdog");
2174 location_qdict
= qdict_new();
2175 qdict_put(qdict
, "location", location_qdict
);
2177 sd_parse_filename(filename
, location_qdict
, &local_err
);
2179 error_propagate(errp
, local_err
);
2184 qdict_flatten(qdict
);
2186 /* Change legacy command line options into QMP ones */
2187 static const QDictRenames opt_renames
[] = {
2188 { BLOCK_OPT_BACKING_FILE
, "backing-file" },
2189 { BLOCK_OPT_OBJECT_SIZE
, "object-size" },
2193 if (!qdict_rename_keys(qdict
, opt_renames
, errp
)) {
2198 /* Get the QAPI object */
2199 v
= qobject_input_visitor_new_flat_confused(qdict
, errp
);
2205 visit_type_BlockdevCreateOptions(v
, NULL
, &create_options
, &local_err
);
2209 error_propagate(errp
, local_err
);
2214 assert(create_options
->driver
== BLOCKDEV_DRIVER_SHEEPDOG
);
2215 create_options
->u
.sheepdog
.size
=
2216 ROUND_UP(create_options
->u
.sheepdog
.size
, BDRV_SECTOR_SIZE
);
2219 create_options
->u
.sheepdog
.has_redundancy
= true;
2220 create_options
->u
.sheepdog
.redundancy
=
2221 parse_redundancy_str(redundancy
);
2222 if (create_options
->u
.sheepdog
.redundancy
== NULL
) {
2223 error_setg(errp
, "Invalid redundancy mode");
2229 ret
= sd_co_create(create_options
, errp
);
2231 qapi_free_BlockdevCreateOptions(create_options
);
2232 qobject_unref(qdict
);
2237 static void sd_close(BlockDriverState
*bs
)
2239 Error
*local_err
= NULL
;
2240 BDRVSheepdogState
*s
= bs
->opaque
;
2242 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
2243 unsigned int wlen
, rlen
= 0;
2246 trace_sheepdog_close(s
->name
);
2248 fd
= connect_to_sdog(s
, &local_err
);
2250 error_report_err(local_err
);
2254 memset(&hdr
, 0, sizeof(hdr
));
2256 hdr
.opcode
= SD_OP_RELEASE_VDI
;
2257 hdr
.type
= LOCK_TYPE_NORMAL
;
2258 hdr
.base_vdi_id
= s
->inode
.vdi_id
;
2259 wlen
= strlen(s
->name
) + 1;
2260 hdr
.data_length
= wlen
;
2261 hdr
.flags
= SD_FLAG_CMD_WRITE
;
2263 ret
= do_req(fd
, s
->bs
, (SheepdogReq
*)&hdr
,
2264 s
->name
, &wlen
, &rlen
);
2268 if (!ret
&& rsp
->result
!= SD_RES_SUCCESS
&&
2269 rsp
->result
!= SD_RES_VDI_NOT_LOCKED
) {
2270 error_report("%s, %s", sd_strerror(rsp
->result
), s
->name
);
2273 aio_set_fd_handler(bdrv_get_aio_context(bs
), s
->fd
,
2274 false, NULL
, NULL
, NULL
, NULL
);
2276 qapi_free_SocketAddress(s
->addr
);
2279 static int64_t sd_getlength(BlockDriverState
*bs
)
2281 BDRVSheepdogState
*s
= bs
->opaque
;
2283 return s
->inode
.vdi_size
;
2286 static int coroutine_fn
sd_co_truncate(BlockDriverState
*bs
, int64_t offset
,
2287 PreallocMode prealloc
, Error
**errp
)
2289 BDRVSheepdogState
*s
= bs
->opaque
;
2291 unsigned int datalen
;
2292 uint64_t max_vdi_size
;
2293 int64_t old_size
= s
->inode
.vdi_size
;
2295 if (prealloc
!= PREALLOC_MODE_OFF
&& prealloc
!= PREALLOC_MODE_FULL
) {
2296 error_setg(errp
, "Unsupported preallocation mode '%s'",
2297 PreallocMode_str(prealloc
));
2301 max_vdi_size
= (UINT64_C(1) << s
->inode
.block_size_shift
) * MAX_DATA_OBJS
;
2302 if (offset
< old_size
) {
2303 error_setg(errp
, "shrinking is not supported");
2305 } else if (offset
> max_vdi_size
) {
2306 error_setg(errp
, "too big image size");
2310 fd
= connect_to_sdog(s
, errp
);
2315 /* we don't need to update entire object */
2316 datalen
= SD_INODE_HEADER_SIZE
;
2317 s
->inode
.vdi_size
= offset
;
2318 ret
= write_object(fd
, s
->bs
, (char *)&s
->inode
,
2319 vid_to_vdi_oid(s
->inode
.vdi_id
), s
->inode
.nr_copies
,
2320 datalen
, 0, false, s
->cache_flags
);
2324 error_setg_errno(errp
, -ret
, "failed to update an inode");
2328 if (prealloc
== PREALLOC_MODE_FULL
) {
2329 ret
= sd_prealloc(bs
, old_size
, offset
, errp
);
2339 * This function is called after writing data objects. If we need to
2340 * update metadata, this sends a write request to the vdi object.
2342 static void coroutine_fn
sd_write_done(SheepdogAIOCB
*acb
)
2344 BDRVSheepdogState
*s
= acb
->s
;
2347 uint32_t offset
, data_len
, mn
, mx
;
2349 mn
= acb
->min_dirty_data_idx
;
2350 mx
= acb
->max_dirty_data_idx
;
2352 /* we need to update the vdi object. */
2354 offset
= sizeof(s
->inode
) - sizeof(s
->inode
.data_vdi_id
) +
2355 mn
* sizeof(s
->inode
.data_vdi_id
[0]);
2356 data_len
= (mx
- mn
+ 1) * sizeof(s
->inode
.data_vdi_id
[0]);
2358 acb
->min_dirty_data_idx
= UINT32_MAX
;
2359 acb
->max_dirty_data_idx
= 0;
2361 iov
.iov_base
= &s
->inode
;
2362 iov
.iov_len
= sizeof(s
->inode
);
2363 aio_req
= alloc_aio_req(s
, acb
, vid_to_vdi_oid(s
->inode
.vdi_id
),
2364 data_len
, offset
, 0, false, 0, offset
);
2365 add_aio_request(s
, aio_req
, &iov
, 1, AIOCB_WRITE_UDATA
);
2366 if (--acb
->nr_pending
) {
2367 qemu_coroutine_yield();
2372 /* Delete current working VDI on the snapshot chain */
2373 static bool sd_delete(BDRVSheepdogState
*s
)
2375 Error
*local_err
= NULL
;
2376 unsigned int wlen
= SD_MAX_VDI_LEN
, rlen
= 0;
2377 SheepdogVdiReq hdr
= {
2378 .opcode
= SD_OP_DEL_VDI
,
2379 .base_vdi_id
= s
->inode
.vdi_id
,
2380 .data_length
= wlen
,
2381 .flags
= SD_FLAG_CMD_WRITE
,
2383 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
2386 fd
= connect_to_sdog(s
, &local_err
);
2388 error_report_err(local_err
);
2392 ret
= do_req(fd
, s
->bs
, (SheepdogReq
*)&hdr
,
2393 s
->name
, &wlen
, &rlen
);
2398 switch (rsp
->result
) {
2400 error_report("%s was already deleted", s
->name
);
2402 case SD_RES_SUCCESS
:
2405 error_report("%s, %s", sd_strerror(rsp
->result
), s
->name
);
2413 * Create a writable VDI from a snapshot
2415 static int sd_create_branch(BDRVSheepdogState
*s
)
2417 Error
*local_err
= NULL
;
2423 trace_sheepdog_create_branch_snapshot(s
->inode
.vdi_id
);
2425 buf
= g_malloc(SD_INODE_SIZE
);
2428 * Even If deletion fails, we will just create extra snapshot based on
2429 * the working VDI which was supposed to be deleted. So no need to
2432 deleted
= sd_delete(s
);
2433 ret
= do_sd_create(s
, &vid
, !deleted
, &local_err
);
2435 error_report_err(local_err
);
2439 trace_sheepdog_create_branch_created(vid
);
2441 fd
= connect_to_sdog(s
, &local_err
);
2443 error_report_err(local_err
);
2448 ret
= read_object(fd
, s
->bs
, buf
, vid_to_vdi_oid(vid
),
2449 s
->inode
.nr_copies
, SD_INODE_SIZE
, 0, s
->cache_flags
);
2457 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
2459 s
->is_snapshot
= false;
2461 trace_sheepdog_create_branch_new(s
->inode
.vdi_id
);
2470 * Send I/O requests to the server.
2472 * This function sends requests to the server, links the requests to
2473 * the inflight_list in BDRVSheepdogState, and exits without
2474 * waiting the response. The responses are received in the
2475 * `aio_read_response' function which is called from the main loop as
2478 * Returns 1 when we need to wait a response, 0 when there is no sent
2479 * request and -errno in error cases.
2481 static void coroutine_fn
sd_co_rw_vector(SheepdogAIOCB
*acb
)
2484 unsigned long len
, done
= 0, total
= acb
->nb_sectors
* BDRV_SECTOR_SIZE
;
2486 uint32_t object_size
;
2489 BDRVSheepdogState
*s
= acb
->s
;
2490 SheepdogInode
*inode
= &s
->inode
;
2493 if (acb
->aiocb_type
== AIOCB_WRITE_UDATA
&& s
->is_snapshot
) {
2495 * In the case we open the snapshot VDI, Sheepdog creates the
2496 * writable VDI when we do a write operation first.
2498 ret
= sd_create_branch(s
);
2505 object_size
= (UINT32_C(1) << inode
->block_size_shift
);
2506 idx
= acb
->sector_num
* BDRV_SECTOR_SIZE
/ object_size
;
2507 offset
= (acb
->sector_num
* BDRV_SECTOR_SIZE
) % object_size
;
2510 * Make sure we don't free the aiocb before we are done with all requests.
2511 * This additional reference is dropped at the end of this function.
2515 while (done
!= total
) {
2517 uint64_t old_oid
= 0;
2518 bool create
= false;
2520 oid
= vid_to_data_oid(inode
->data_vdi_id
[idx
], idx
);
2522 len
= MIN(total
- done
, object_size
- offset
);
2524 switch (acb
->aiocb_type
) {
2525 case AIOCB_READ_UDATA
:
2526 if (!inode
->data_vdi_id
[idx
]) {
2527 qemu_iovec_memset(acb
->qiov
, done
, 0, len
);
2531 case AIOCB_WRITE_UDATA
:
2532 if (!inode
->data_vdi_id
[idx
]) {
2534 } else if (!is_data_obj_writable(inode
, idx
)) {
2538 flags
= SD_FLAG_CMD_COW
;
2541 case AIOCB_DISCARD_OBJ
:
2543 * We discard the object only when the whole object is
2544 * 1) allocated 2) trimmed. Otherwise, simply skip it.
2546 if (len
!= object_size
|| inode
->data_vdi_id
[idx
] == 0) {
2555 trace_sheepdog_co_rw_vector_update(inode
->vdi_id
, oid
,
2556 vid_to_data_oid(inode
->data_vdi_id
[idx
], idx
),
2558 oid
= vid_to_data_oid(inode
->vdi_id
, idx
);
2559 trace_sheepdog_co_rw_vector_new(oid
);
2562 aio_req
= alloc_aio_req(s
, acb
, oid
, len
, offset
, flags
, create
,
2564 acb
->aiocb_type
== AIOCB_DISCARD_OBJ
?
2566 add_aio_request(s
, aio_req
, acb
->qiov
->iov
, acb
->qiov
->niov
,
2573 if (--acb
->nr_pending
) {
2574 qemu_coroutine_yield();
2578 static void sd_aio_complete(SheepdogAIOCB
*acb
)
2580 BDRVSheepdogState
*s
;
2581 if (acb
->aiocb_type
== AIOCB_FLUSH_CACHE
) {
2586 qemu_co_mutex_lock(&s
->queue_lock
);
2587 QLIST_REMOVE(acb
, aiocb_siblings
);
2588 qemu_co_queue_restart_all(&s
->overlapping_queue
);
2589 qemu_co_mutex_unlock(&s
->queue_lock
);
2592 static coroutine_fn
int sd_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
2593 int nb_sectors
, QEMUIOVector
*qiov
,
2598 int64_t offset
= (sector_num
+ nb_sectors
) * BDRV_SECTOR_SIZE
;
2599 BDRVSheepdogState
*s
= bs
->opaque
;
2602 if (offset
> s
->inode
.vdi_size
) {
2603 ret
= sd_co_truncate(bs
, offset
, PREALLOC_MODE_OFF
, NULL
);
2609 sd_aio_setup(&acb
, s
, qiov
, sector_num
, nb_sectors
, AIOCB_WRITE_UDATA
);
2610 sd_co_rw_vector(&acb
);
2611 sd_write_done(&acb
);
2612 sd_aio_complete(&acb
);
2617 static coroutine_fn
int sd_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
2618 int nb_sectors
, QEMUIOVector
*qiov
)
2621 BDRVSheepdogState
*s
= bs
->opaque
;
2623 sd_aio_setup(&acb
, s
, qiov
, sector_num
, nb_sectors
, AIOCB_READ_UDATA
);
2624 sd_co_rw_vector(&acb
);
2625 sd_aio_complete(&acb
);
2630 static int coroutine_fn
sd_co_flush_to_disk(BlockDriverState
*bs
)
2632 BDRVSheepdogState
*s
= bs
->opaque
;
2636 if (s
->cache_flags
!= SD_FLAG_CMD_CACHE
) {
2640 sd_aio_setup(&acb
, s
, NULL
, 0, 0, AIOCB_FLUSH_CACHE
);
2643 aio_req
= alloc_aio_req(s
, &acb
, vid_to_vdi_oid(s
->inode
.vdi_id
),
2644 0, 0, 0, false, 0, 0);
2645 add_aio_request(s
, aio_req
, NULL
, 0, acb
.aiocb_type
);
2647 if (--acb
.nr_pending
) {
2648 qemu_coroutine_yield();
2651 sd_aio_complete(&acb
);
2655 static int sd_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
)
2657 Error
*local_err
= NULL
;
2658 BDRVSheepdogState
*s
= bs
->opaque
;
2661 SheepdogInode
*inode
;
2662 unsigned int datalen
;
2664 trace_sheepdog_snapshot_create_info(sn_info
->name
, sn_info
->id_str
, s
->name
,
2665 sn_info
->vm_state_size
, s
->is_snapshot
);
2667 if (s
->is_snapshot
) {
2668 error_report("You can't create a snapshot of a snapshot VDI, "
2669 "%s (%" PRIu32
").", s
->name
, s
->inode
.vdi_id
);
2674 trace_sheepdog_snapshot_create(sn_info
->name
, sn_info
->id_str
);
2676 s
->inode
.vm_state_size
= sn_info
->vm_state_size
;
2677 s
->inode
.vm_clock_nsec
= sn_info
->vm_clock_nsec
;
2678 /* It appears that inode.tag does not require a NUL terminator,
2679 * which means this use of strncpy is ok.
2681 strncpy(s
->inode
.tag
, sn_info
->name
, sizeof(s
->inode
.tag
));
2682 /* we don't need to update entire object */
2683 datalen
= SD_INODE_HEADER_SIZE
;
2684 inode
= g_malloc(datalen
);
2686 /* refresh inode. */
2687 fd
= connect_to_sdog(s
, &local_err
);
2689 error_report_err(local_err
);
2694 ret
= write_object(fd
, s
->bs
, (char *)&s
->inode
,
2695 vid_to_vdi_oid(s
->inode
.vdi_id
), s
->inode
.nr_copies
,
2696 datalen
, 0, false, s
->cache_flags
);
2698 error_report("failed to write snapshot's inode.");
2702 ret
= do_sd_create(s
, &new_vid
, 1, &local_err
);
2704 error_reportf_err(local_err
,
2705 "failed to create inode for snapshot: ");
2709 ret
= read_object(fd
, s
->bs
, (char *)inode
,
2710 vid_to_vdi_oid(new_vid
), s
->inode
.nr_copies
, datalen
, 0,
2714 error_report("failed to read new inode info. %s", strerror(errno
));
2718 memcpy(&s
->inode
, inode
, datalen
);
2719 trace_sheepdog_snapshot_create_inode(s
->inode
.name
, s
->inode
.snap_id
,
2729 * We implement rollback(loadvm) operation to the specified snapshot by
2730 * 1) switch to the snapshot
2731 * 2) rely on sd_create_branch to delete working VDI and
2732 * 3) create a new working VDI based on the specified snapshot
2734 static int sd_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
)
2736 BDRVSheepdogState
*s
= bs
->opaque
;
2737 BDRVSheepdogState
*old_s
;
2738 char tag
[SD_MAX_VDI_TAG_LEN
];
2739 uint32_t snapid
= 0;
2742 if (!sd_parse_snapid_or_tag(snapshot_id
, &snapid
, tag
)) {
2746 old_s
= g_new(BDRVSheepdogState
, 1);
2748 memcpy(old_s
, s
, sizeof(BDRVSheepdogState
));
2750 ret
= reload_inode(s
, snapid
, tag
);
2755 ret
= sd_create_branch(s
);
2764 /* recover bdrv_sd_state */
2765 memcpy(s
, old_s
, sizeof(BDRVSheepdogState
));
2768 error_report("failed to open. recover old bdrv_sd_state.");
2773 #define NR_BATCHED_DISCARD 128
2775 static int remove_objects(BDRVSheepdogState
*s
, Error
**errp
)
2777 int fd
, i
= 0, nr_objs
= 0;
2779 SheepdogInode
*inode
= &s
->inode
;
2781 fd
= connect_to_sdog(s
, errp
);
2786 nr_objs
= count_data_objs(inode
);
2787 while (i
< nr_objs
) {
2788 int start_idx
, nr_filled_idx
;
2790 while (i
< nr_objs
&& !inode
->data_vdi_id
[i
]) {
2796 while (i
< nr_objs
&& nr_filled_idx
< NR_BATCHED_DISCARD
) {
2797 if (inode
->data_vdi_id
[i
]) {
2798 inode
->data_vdi_id
[i
] = 0;
2805 ret
= write_object(fd
, s
->bs
,
2806 (char *)&inode
->data_vdi_id
[start_idx
],
2807 vid_to_vdi_oid(s
->inode
.vdi_id
), inode
->nr_copies
,
2808 (i
- start_idx
) * sizeof(uint32_t),
2809 offsetof(struct SheepdogInode
,
2810 data_vdi_id
[start_idx
]),
2811 false, s
->cache_flags
);
2813 error_setg(errp
, "Failed to discard snapshot inode");
2824 static int sd_snapshot_delete(BlockDriverState
*bs
,
2825 const char *snapshot_id
,
2830 * FIXME should delete the snapshot matching both @snapshot_id and
2831 * @name, but @name not used here
2833 unsigned long snap_id
= 0;
2834 char snap_tag
[SD_MAX_VDI_TAG_LEN
];
2836 char buf
[SD_MAX_VDI_LEN
+ SD_MAX_VDI_TAG_LEN
];
2837 BDRVSheepdogState
*s
= bs
->opaque
;
2838 unsigned int wlen
= SD_MAX_VDI_LEN
+ SD_MAX_VDI_TAG_LEN
, rlen
= 0;
2840 SheepdogVdiReq hdr
= {
2841 .opcode
= SD_OP_DEL_VDI
,
2842 .data_length
= wlen
,
2843 .flags
= SD_FLAG_CMD_WRITE
,
2845 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
2847 ret
= remove_objects(s
, errp
);
2852 memset(buf
, 0, sizeof(buf
));
2853 memset(snap_tag
, 0, sizeof(snap_tag
));
2854 pstrcpy(buf
, SD_MAX_VDI_LEN
, s
->name
);
2855 /* TODO Use sd_parse_snapid() once this mess is cleaned up */
2856 ret
= qemu_strtoul(snapshot_id
, NULL
, 10, &snap_id
);
2857 if (ret
|| snap_id
> UINT32_MAX
) {
2859 * FIXME Since qemu_strtoul() returns -EINVAL when
2860 * @snapshot_id is null, @snapshot_id is mandatory. Correct
2861 * would be to require at least one of @snapshot_id and @name.
2863 error_setg(errp
, "Invalid snapshot ID: %s",
2864 snapshot_id
? snapshot_id
: "<null>");
2869 hdr
.snapid
= (uint32_t) snap_id
;
2871 /* FIXME I suspect we should use @name here */
2872 /* FIXME don't truncate silently */
2873 pstrcpy(snap_tag
, sizeof(snap_tag
), snapshot_id
);
2874 pstrcpy(buf
+ SD_MAX_VDI_LEN
, SD_MAX_VDI_TAG_LEN
, snap_tag
);
2877 ret
= find_vdi_name(s
, s
->name
, snap_id
, snap_tag
, &vid
, true, errp
);
2882 fd
= connect_to_sdog(s
, errp
);
2887 ret
= do_req(fd
, s
->bs
, (SheepdogReq
*)&hdr
,
2891 error_setg_errno(errp
, -ret
, "Couldn't send request to server");
2895 switch (rsp
->result
) {
2897 error_setg(errp
, "Can't find the snapshot");
2899 case SD_RES_SUCCESS
:
2902 error_setg(errp
, "%s", sd_strerror(rsp
->result
));
2909 static int sd_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
)
2911 Error
*local_err
= NULL
;
2912 BDRVSheepdogState
*s
= bs
->opaque
;
2914 int fd
, nr
= 1024, ret
, max
= BITS_TO_LONGS(SD_NR_VDIS
) * sizeof(long);
2915 QEMUSnapshotInfo
*sn_tab
= NULL
;
2916 unsigned wlen
, rlen
;
2918 SheepdogInode
*inode
;
2919 unsigned long *vdi_inuse
;
2920 unsigned int start_nr
;
2924 vdi_inuse
= g_malloc(max
);
2925 inode
= g_malloc(SD_INODE_HEADER_SIZE
);
2927 fd
= connect_to_sdog(s
, &local_err
);
2929 error_report_err(local_err
);
2937 memset(&req
, 0, sizeof(req
));
2939 req
.opcode
= SD_OP_READ_VDIS
;
2940 req
.data_length
= max
;
2942 ret
= do_req(fd
, s
->bs
, &req
, vdi_inuse
, &wlen
, &rlen
);
2949 sn_tab
= g_new0(QEMUSnapshotInfo
, nr
);
2951 /* calculate a vdi id with hash function */
2952 hval
= fnv_64a_buf(s
->name
, strlen(s
->name
), FNV1A_64_INIT
);
2953 start_nr
= hval
& (SD_NR_VDIS
- 1);
2955 fd
= connect_to_sdog(s
, &local_err
);
2957 error_report_err(local_err
);
2962 for (vid
= start_nr
; found
< nr
; vid
= (vid
+ 1) % SD_NR_VDIS
) {
2963 if (!test_bit(vid
, vdi_inuse
)) {
2967 /* we don't need to read entire object */
2968 ret
= read_object(fd
, s
->bs
, (char *)inode
,
2969 vid_to_vdi_oid(vid
),
2970 0, SD_INODE_HEADER_SIZE
, 0,
2977 if (!strcmp(inode
->name
, s
->name
) && is_snapshot(inode
)) {
2978 sn_tab
[found
].date_sec
= inode
->snap_ctime
>> 32;
2979 sn_tab
[found
].date_nsec
= inode
->snap_ctime
& 0xffffffff;
2980 sn_tab
[found
].vm_state_size
= inode
->vm_state_size
;
2981 sn_tab
[found
].vm_clock_nsec
= inode
->vm_clock_nsec
;
2983 snprintf(sn_tab
[found
].id_str
, sizeof(sn_tab
[found
].id_str
),
2984 "%" PRIu32
, inode
->snap_id
);
2985 pstrcpy(sn_tab
[found
].name
,
2986 MIN(sizeof(sn_tab
[found
].name
), sizeof(inode
->tag
)),
3006 static int do_load_save_vmstate(BDRVSheepdogState
*s
, uint8_t *data
,
3007 int64_t pos
, int size
, int load
)
3009 Error
*local_err
= NULL
;
3011 int fd
, ret
= 0, remaining
= size
;
3012 unsigned int data_len
;
3013 uint64_t vmstate_oid
;
3016 uint32_t vdi_id
= load
? s
->inode
.parent_vdi_id
: s
->inode
.vdi_id
;
3017 uint32_t object_size
= (UINT32_C(1) << s
->inode
.block_size_shift
);
3019 fd
= connect_to_sdog(s
, &local_err
);
3021 error_report_err(local_err
);
3026 vdi_index
= pos
/ object_size
;
3027 offset
= pos
% object_size
;
3029 data_len
= MIN(remaining
, object_size
- offset
);
3031 vmstate_oid
= vid_to_vmstate_oid(vdi_id
, vdi_index
);
3033 create
= (offset
== 0);
3035 ret
= read_object(fd
, s
->bs
, (char *)data
, vmstate_oid
,
3036 s
->inode
.nr_copies
, data_len
, offset
,
3039 ret
= write_object(fd
, s
->bs
, (char *)data
, vmstate_oid
,
3040 s
->inode
.nr_copies
, data_len
, offset
, create
,
3045 error_report("failed to save vmstate %s", strerror(errno
));
3051 remaining
-= data_len
;
3059 static int sd_save_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
3062 BDRVSheepdogState
*s
= bs
->opaque
;
3066 buf
= qemu_blockalign(bs
, qiov
->size
);
3067 qemu_iovec_to_buf(qiov
, 0, buf
, qiov
->size
);
3068 ret
= do_load_save_vmstate(s
, (uint8_t *) buf
, pos
, qiov
->size
, 0);
3074 static int sd_load_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
3077 BDRVSheepdogState
*s
= bs
->opaque
;
3081 buf
= qemu_blockalign(bs
, qiov
->size
);
3082 ret
= do_load_save_vmstate(s
, buf
, pos
, qiov
->size
, 1);
3083 qemu_iovec_from_buf(qiov
, 0, buf
, qiov
->size
);
3090 static coroutine_fn
int sd_co_pdiscard(BlockDriverState
*bs
, int64_t offset
,
3094 BDRVSheepdogState
*s
= bs
->opaque
;
3095 QEMUIOVector discard_iov
;
3099 if (!s
->discard_supported
) {
3103 memset(&discard_iov
, 0, sizeof(discard_iov
));
3104 memset(&iov
, 0, sizeof(iov
));
3105 iov
.iov_base
= &zero
;
3106 iov
.iov_len
= sizeof(zero
);
3107 discard_iov
.iov
= &iov
;
3108 discard_iov
.niov
= 1;
3109 if (!QEMU_IS_ALIGNED(offset
| bytes
, BDRV_SECTOR_SIZE
)) {
3112 sd_aio_setup(&acb
, s
, &discard_iov
, offset
>> BDRV_SECTOR_BITS
,
3113 bytes
>> BDRV_SECTOR_BITS
, AIOCB_DISCARD_OBJ
);
3114 sd_co_rw_vector(&acb
);
3115 sd_aio_complete(&acb
);
3120 static coroutine_fn
int
3121 sd_co_block_status(BlockDriverState
*bs
, bool want_zero
, int64_t offset
,
3122 int64_t bytes
, int64_t *pnum
, int64_t *map
,
3123 BlockDriverState
**file
)
3125 BDRVSheepdogState
*s
= bs
->opaque
;
3126 SheepdogInode
*inode
= &s
->inode
;
3127 uint32_t object_size
= (UINT32_C(1) << inode
->block_size_shift
);
3128 unsigned long start
= offset
/ object_size
,
3129 end
= DIV_ROUND_UP(offset
+ bytes
, object_size
);
3132 int ret
= BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
;
3134 for (idx
= start
; idx
< end
; idx
++) {
3135 if (inode
->data_vdi_id
[idx
] == 0) {
3140 /* Get the longest length of unallocated sectors */
3142 for (idx
= start
+ 1; idx
< end
; idx
++) {
3143 if (inode
->data_vdi_id
[idx
] != 0) {
3149 *pnum
= (idx
- start
) * object_size
;
3150 if (*pnum
> bytes
) {
3153 if (ret
> 0 && ret
& BDRV_BLOCK_OFFSET_VALID
) {
3159 static int64_t sd_get_allocated_file_size(BlockDriverState
*bs
)
3161 BDRVSheepdogState
*s
= bs
->opaque
;
3162 SheepdogInode
*inode
= &s
->inode
;
3163 uint32_t object_size
= (UINT32_C(1) << inode
->block_size_shift
);
3164 unsigned long i
, last
= DIV_ROUND_UP(inode
->vdi_size
, object_size
);
3167 for (i
= 0; i
< last
; i
++) {
3168 if (inode
->data_vdi_id
[i
] == 0) {
3171 size
+= object_size
;
3176 static QemuOptsList sd_create_opts
= {
3177 .name
= "sheepdog-create-opts",
3178 .head
= QTAILQ_HEAD_INITIALIZER(sd_create_opts
.head
),
3181 .name
= BLOCK_OPT_SIZE
,
3182 .type
= QEMU_OPT_SIZE
,
3183 .help
= "Virtual disk size"
3186 .name
= BLOCK_OPT_BACKING_FILE
,
3187 .type
= QEMU_OPT_STRING
,
3188 .help
= "File name of a base image"
3191 .name
= BLOCK_OPT_PREALLOC
,
3192 .type
= QEMU_OPT_STRING
,
3193 .help
= "Preallocation mode (allowed values: off, full)"
3196 .name
= BLOCK_OPT_REDUNDANCY
,
3197 .type
= QEMU_OPT_STRING
,
3198 .help
= "Redundancy of the image"
3201 .name
= BLOCK_OPT_OBJECT_SIZE
,
3202 .type
= QEMU_OPT_SIZE
,
3203 .help
= "Object size of the image"
3205 { /* end of list */ }
3209 static const char *const sd_strong_runtime_opts
[] = {
3218 static BlockDriver bdrv_sheepdog
= {
3219 .format_name
= "sheepdog",
3220 .protocol_name
= "sheepdog",
3221 .instance_size
= sizeof(BDRVSheepdogState
),
3222 .bdrv_parse_filename
= sd_parse_filename
,
3223 .bdrv_file_open
= sd_open
,
3224 .bdrv_reopen_prepare
= sd_reopen_prepare
,
3225 .bdrv_reopen_commit
= sd_reopen_commit
,
3226 .bdrv_reopen_abort
= sd_reopen_abort
,
3227 .bdrv_close
= sd_close
,
3228 .bdrv_co_create
= sd_co_create
,
3229 .bdrv_co_create_opts
= sd_co_create_opts
,
3230 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
3231 .bdrv_getlength
= sd_getlength
,
3232 .bdrv_get_allocated_file_size
= sd_get_allocated_file_size
,
3233 .bdrv_co_truncate
= sd_co_truncate
,
3235 .bdrv_co_readv
= sd_co_readv
,
3236 .bdrv_co_writev
= sd_co_writev
,
3237 .bdrv_co_flush_to_disk
= sd_co_flush_to_disk
,
3238 .bdrv_co_pdiscard
= sd_co_pdiscard
,
3239 .bdrv_co_block_status
= sd_co_block_status
,
3241 .bdrv_snapshot_create
= sd_snapshot_create
,
3242 .bdrv_snapshot_goto
= sd_snapshot_goto
,
3243 .bdrv_snapshot_delete
= sd_snapshot_delete
,
3244 .bdrv_snapshot_list
= sd_snapshot_list
,
3246 .bdrv_save_vmstate
= sd_save_vmstate
,
3247 .bdrv_load_vmstate
= sd_load_vmstate
,
3249 .bdrv_detach_aio_context
= sd_detach_aio_context
,
3250 .bdrv_attach_aio_context
= sd_attach_aio_context
,
3252 .create_opts
= &sd_create_opts
,
3253 .strong_runtime_opts
= sd_strong_runtime_opts
,
3256 static BlockDriver bdrv_sheepdog_tcp
= {
3257 .format_name
= "sheepdog",
3258 .protocol_name
= "sheepdog+tcp",
3259 .instance_size
= sizeof(BDRVSheepdogState
),
3260 .bdrv_parse_filename
= sd_parse_filename
,
3261 .bdrv_file_open
= sd_open
,
3262 .bdrv_reopen_prepare
= sd_reopen_prepare
,
3263 .bdrv_reopen_commit
= sd_reopen_commit
,
3264 .bdrv_reopen_abort
= sd_reopen_abort
,
3265 .bdrv_close
= sd_close
,
3266 .bdrv_co_create
= sd_co_create
,
3267 .bdrv_co_create_opts
= sd_co_create_opts
,
3268 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
3269 .bdrv_getlength
= sd_getlength
,
3270 .bdrv_get_allocated_file_size
= sd_get_allocated_file_size
,
3271 .bdrv_co_truncate
= sd_co_truncate
,
3273 .bdrv_co_readv
= sd_co_readv
,
3274 .bdrv_co_writev
= sd_co_writev
,
3275 .bdrv_co_flush_to_disk
= sd_co_flush_to_disk
,
3276 .bdrv_co_pdiscard
= sd_co_pdiscard
,
3277 .bdrv_co_block_status
= sd_co_block_status
,
3279 .bdrv_snapshot_create
= sd_snapshot_create
,
3280 .bdrv_snapshot_goto
= sd_snapshot_goto
,
3281 .bdrv_snapshot_delete
= sd_snapshot_delete
,
3282 .bdrv_snapshot_list
= sd_snapshot_list
,
3284 .bdrv_save_vmstate
= sd_save_vmstate
,
3285 .bdrv_load_vmstate
= sd_load_vmstate
,
3287 .bdrv_detach_aio_context
= sd_detach_aio_context
,
3288 .bdrv_attach_aio_context
= sd_attach_aio_context
,
3290 .create_opts
= &sd_create_opts
,
3291 .strong_runtime_opts
= sd_strong_runtime_opts
,
3294 static BlockDriver bdrv_sheepdog_unix
= {
3295 .format_name
= "sheepdog",
3296 .protocol_name
= "sheepdog+unix",
3297 .instance_size
= sizeof(BDRVSheepdogState
),
3298 .bdrv_parse_filename
= sd_parse_filename
,
3299 .bdrv_file_open
= sd_open
,
3300 .bdrv_reopen_prepare
= sd_reopen_prepare
,
3301 .bdrv_reopen_commit
= sd_reopen_commit
,
3302 .bdrv_reopen_abort
= sd_reopen_abort
,
3303 .bdrv_close
= sd_close
,
3304 .bdrv_co_create
= sd_co_create
,
3305 .bdrv_co_create_opts
= sd_co_create_opts
,
3306 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
3307 .bdrv_getlength
= sd_getlength
,
3308 .bdrv_get_allocated_file_size
= sd_get_allocated_file_size
,
3309 .bdrv_co_truncate
= sd_co_truncate
,
3311 .bdrv_co_readv
= sd_co_readv
,
3312 .bdrv_co_writev
= sd_co_writev
,
3313 .bdrv_co_flush_to_disk
= sd_co_flush_to_disk
,
3314 .bdrv_co_pdiscard
= sd_co_pdiscard
,
3315 .bdrv_co_block_status
= sd_co_block_status
,
3317 .bdrv_snapshot_create
= sd_snapshot_create
,
3318 .bdrv_snapshot_goto
= sd_snapshot_goto
,
3319 .bdrv_snapshot_delete
= sd_snapshot_delete
,
3320 .bdrv_snapshot_list
= sd_snapshot_list
,
3322 .bdrv_save_vmstate
= sd_save_vmstate
,
3323 .bdrv_load_vmstate
= sd_load_vmstate
,
3325 .bdrv_detach_aio_context
= sd_detach_aio_context
,
3326 .bdrv_attach_aio_context
= sd_attach_aio_context
,
3328 .create_opts
= &sd_create_opts
,
3329 .strong_runtime_opts
= sd_strong_runtime_opts
,
3332 static void bdrv_sheepdog_init(void)
3334 bdrv_register(&bdrv_sheepdog
);
3335 bdrv_register(&bdrv_sheepdog_tcp
);
3336 bdrv_register(&bdrv_sheepdog_unix
);
3338 block_init(bdrv_sheepdog_init
);