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/main-loop.h"
26 #include "qemu/module.h"
27 #include "qemu/option.h"
28 #include "qemu/sockets.h"
29 #include "block/block_int.h"
30 #include "block/qdict.h"
31 #include "sysemu/block-backend.h"
32 #include "qemu/bitops.h"
33 #include "qemu/cutils.h"
36 #define SD_PROTO_VER 0x01
38 #define SD_DEFAULT_ADDR "localhost"
39 #define SD_DEFAULT_PORT 7000
41 #define SD_OP_CREATE_AND_WRITE_OBJ 0x01
42 #define SD_OP_READ_OBJ 0x02
43 #define SD_OP_WRITE_OBJ 0x03
44 /* 0x04 is used internally by Sheepdog */
46 #define SD_OP_NEW_VDI 0x11
47 #define SD_OP_LOCK_VDI 0x12
48 #define SD_OP_RELEASE_VDI 0x13
49 #define SD_OP_GET_VDI_INFO 0x14
50 #define SD_OP_READ_VDIS 0x15
51 #define SD_OP_FLUSH_VDI 0x16
52 #define SD_OP_DEL_VDI 0x17
53 #define SD_OP_GET_CLUSTER_DEFAULT 0x18
55 #define SD_FLAG_CMD_WRITE 0x01
56 #define SD_FLAG_CMD_COW 0x02
57 #define SD_FLAG_CMD_CACHE 0x04 /* Writeback mode for cache */
58 #define SD_FLAG_CMD_DIRECT 0x08 /* Don't use cache */
60 #define SD_RES_SUCCESS 0x00 /* Success */
61 #define SD_RES_UNKNOWN 0x01 /* Unknown error */
62 #define SD_RES_NO_OBJ 0x02 /* No object found */
63 #define SD_RES_EIO 0x03 /* I/O error */
64 #define SD_RES_VDI_EXIST 0x04 /* Vdi exists already */
65 #define SD_RES_INVALID_PARMS 0x05 /* Invalid parameters */
66 #define SD_RES_SYSTEM_ERROR 0x06 /* System error */
67 #define SD_RES_VDI_LOCKED 0x07 /* Vdi is locked */
68 #define SD_RES_NO_VDI 0x08 /* No vdi found */
69 #define SD_RES_NO_BASE_VDI 0x09 /* No base vdi found */
70 #define SD_RES_VDI_READ 0x0A /* Cannot read requested vdi */
71 #define SD_RES_VDI_WRITE 0x0B /* Cannot write requested vdi */
72 #define SD_RES_BASE_VDI_READ 0x0C /* Cannot read base vdi */
73 #define SD_RES_BASE_VDI_WRITE 0x0D /* Cannot write base vdi */
74 #define SD_RES_NO_TAG 0x0E /* Requested tag is not found */
75 #define SD_RES_STARTUP 0x0F /* Sheepdog is on starting up */
76 #define SD_RES_VDI_NOT_LOCKED 0x10 /* Vdi is not locked */
77 #define SD_RES_SHUTDOWN 0x11 /* Sheepdog is shutting down */
78 #define SD_RES_NO_MEM 0x12 /* Cannot allocate memory */
79 #define SD_RES_FULL_VDI 0x13 /* we already have the maximum vdis */
80 #define SD_RES_VER_MISMATCH 0x14 /* Protocol version mismatch */
81 #define SD_RES_NO_SPACE 0x15 /* Server has no room for new objects */
82 #define SD_RES_WAIT_FOR_FORMAT 0x16 /* Waiting for a format operation */
83 #define SD_RES_WAIT_FOR_JOIN 0x17 /* Waiting for other nodes joining */
84 #define SD_RES_JOIN_FAILED 0x18 /* Target node had failed to join sheepdog */
85 #define SD_RES_HALT 0x19 /* Sheepdog is stopped serving IO request */
86 #define SD_RES_READONLY 0x1A /* Object is read-only */
91 * 0 - 19 (20 bits): data object space
92 * 20 - 31 (12 bits): reserved data object space
93 * 32 - 55 (24 bits): vdi object space
94 * 56 - 59 ( 4 bits): reserved vdi object space
95 * 60 - 63 ( 4 bits): object type identifier space
98 #define VDI_SPACE_SHIFT 32
99 #define VDI_BIT (UINT64_C(1) << 63)
100 #define VMSTATE_BIT (UINT64_C(1) << 62)
101 #define MAX_DATA_OBJS (UINT64_C(1) << 20)
102 #define MAX_CHILDREN 1024
103 #define SD_MAX_VDI_LEN 256
104 #define SD_MAX_VDI_TAG_LEN 256
105 #define SD_NR_VDIS (1U << 24)
106 #define SD_DATA_OBJ_SIZE (UINT64_C(1) << 22)
107 #define SD_MAX_VDI_SIZE (SD_DATA_OBJ_SIZE * MAX_DATA_OBJS)
108 #define SD_DEFAULT_BLOCK_SIZE_SHIFT 22
110 * For erasure coding, we use at most SD_EC_MAX_STRIP for data strips and
111 * (SD_EC_MAX_STRIP - 1) for parity strips
113 * SD_MAX_COPIES is sum of number of data strips and parity strips.
115 #define SD_EC_MAX_STRIP 16
116 #define SD_MAX_COPIES (SD_EC_MAX_STRIP * 2 - 1)
118 #define SD_INODE_SIZE (sizeof(SheepdogInode))
119 #define CURRENT_VDI_ID 0
121 #define LOCK_TYPE_NORMAL 0
122 #define LOCK_TYPE_SHARED 1 /* for iSCSI multipath */
124 typedef struct SheepdogReq
{
130 uint32_t data_length
;
131 uint32_t opcode_specific
[8];
134 typedef struct SheepdogRsp
{
140 uint32_t data_length
;
142 uint32_t opcode_specific
[7];
145 typedef struct SheepdogObjReq
{
151 uint32_t data_length
;
160 typedef struct SheepdogObjRsp
{
166 uint32_t data_length
;
174 typedef struct SheepdogVdiReq
{
180 uint32_t data_length
;
182 uint32_t base_vdi_id
;
185 uint8_t store_policy
;
186 uint8_t block_size_shift
;
192 typedef struct SheepdogVdiRsp
{
198 uint32_t data_length
;
205 typedef struct SheepdogClusterRsp
{
211 uint32_t data_length
;
215 uint8_t block_size_shift
;
218 } SheepdogClusterRsp
;
220 typedef struct SheepdogInode
{
221 char name
[SD_MAX_VDI_LEN
];
222 char tag
[SD_MAX_VDI_TAG_LEN
];
225 uint64_t vm_clock_nsec
;
227 uint64_t vm_state_size
;
228 uint16_t copy_policy
;
230 uint8_t block_size_shift
;
233 uint32_t parent_vdi_id
;
234 uint32_t child_vdi_id
[MAX_CHILDREN
];
235 uint32_t data_vdi_id
[MAX_DATA_OBJS
];
238 #define SD_INODE_HEADER_SIZE offsetof(SheepdogInode, data_vdi_id)
241 * 64 bit FNV-1a non-zero initial basis
243 #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
246 * 64 bit Fowler/Noll/Vo FNV-1a hash code
248 static inline uint64_t fnv_64a_buf(void *buf
, size_t len
, uint64_t hval
)
250 unsigned char *bp
= buf
;
251 unsigned char *be
= bp
+ len
;
253 hval
^= (uint64_t) *bp
++;
254 hval
+= (hval
<< 1) + (hval
<< 4) + (hval
<< 5) +
255 (hval
<< 7) + (hval
<< 8) + (hval
<< 40);
260 static inline bool is_data_obj_writable(SheepdogInode
*inode
, unsigned int idx
)
262 return inode
->vdi_id
== inode
->data_vdi_id
[idx
];
265 static inline bool is_data_obj(uint64_t oid
)
267 return !(VDI_BIT
& oid
);
270 static inline uint64_t data_oid_to_idx(uint64_t oid
)
272 return oid
& (MAX_DATA_OBJS
- 1);
275 static inline uint32_t oid_to_vid(uint64_t oid
)
277 return (oid
& ~VDI_BIT
) >> VDI_SPACE_SHIFT
;
280 static inline uint64_t vid_to_vdi_oid(uint32_t vid
)
282 return VDI_BIT
| ((uint64_t)vid
<< VDI_SPACE_SHIFT
);
285 static inline uint64_t vid_to_vmstate_oid(uint32_t vid
, uint32_t idx
)
287 return VMSTATE_BIT
| ((uint64_t)vid
<< VDI_SPACE_SHIFT
) | idx
;
290 static inline uint64_t vid_to_data_oid(uint32_t vid
, uint32_t idx
)
292 return ((uint64_t)vid
<< VDI_SPACE_SHIFT
) | idx
;
295 static inline bool is_snapshot(struct SheepdogInode
*inode
)
297 return !!inode
->snap_ctime
;
300 static inline size_t count_data_objs(const struct SheepdogInode
*inode
)
302 return DIV_ROUND_UP(inode
->vdi_size
,
303 (1UL << inode
->block_size_shift
));
306 typedef struct SheepdogAIOCB SheepdogAIOCB
;
307 typedef struct BDRVSheepdogState BDRVSheepdogState
;
309 typedef struct AIOReq
{
310 SheepdogAIOCB
*aiocb
;
311 unsigned int iov_offset
;
316 unsigned int data_len
;
321 QLIST_ENTRY(AIOReq
) aio_siblings
;
331 #define AIOCBOverlapping(x, y) \
332 (!(x->max_affect_data_idx < y->min_affect_data_idx \
333 || y->max_affect_data_idx < x->min_affect_data_idx))
335 struct SheepdogAIOCB
{
336 BDRVSheepdogState
*s
;
344 enum AIOCBState aiocb_type
;
346 Coroutine
*coroutine
;
349 uint32_t min_affect_data_idx
;
350 uint32_t max_affect_data_idx
;
353 * The difference between affect_data_idx and dirty_data_idx:
354 * affect_data_idx represents range of index of all request types.
355 * dirty_data_idx represents range of index updated by COW requests.
356 * dirty_data_idx is used for updating an inode object.
358 uint32_t min_dirty_data_idx
;
359 uint32_t max_dirty_data_idx
;
361 QLIST_ENTRY(SheepdogAIOCB
) aiocb_siblings
;
364 struct BDRVSheepdogState
{
365 BlockDriverState
*bs
;
366 AioContext
*aio_context
;
370 char name
[SD_MAX_VDI_LEN
];
372 uint32_t cache_flags
;
373 bool discard_supported
;
382 uint32_t aioreq_seq_num
;
384 /* Every aio request must be linked to either of these queues. */
385 QLIST_HEAD(, AIOReq
) inflight_aio_head
;
386 QLIST_HEAD(, AIOReq
) failed_aio_head
;
389 CoQueue overlapping_queue
;
390 QLIST_HEAD(, SheepdogAIOCB
) inflight_aiocb_head
;
393 typedef struct BDRVSheepdogReopenState
{
396 } BDRVSheepdogReopenState
;
398 static const char *sd_strerror(int err
)
402 static const struct {
406 {SD_RES_SUCCESS
, "Success"},
407 {SD_RES_UNKNOWN
, "Unknown error"},
408 {SD_RES_NO_OBJ
, "No object found"},
409 {SD_RES_EIO
, "I/O error"},
410 {SD_RES_VDI_EXIST
, "VDI exists already"},
411 {SD_RES_INVALID_PARMS
, "Invalid parameters"},
412 {SD_RES_SYSTEM_ERROR
, "System error"},
413 {SD_RES_VDI_LOCKED
, "VDI is already locked"},
414 {SD_RES_NO_VDI
, "No vdi found"},
415 {SD_RES_NO_BASE_VDI
, "No base VDI found"},
416 {SD_RES_VDI_READ
, "Failed read the requested VDI"},
417 {SD_RES_VDI_WRITE
, "Failed to write the requested VDI"},
418 {SD_RES_BASE_VDI_READ
, "Failed to read the base VDI"},
419 {SD_RES_BASE_VDI_WRITE
, "Failed to write the base VDI"},
420 {SD_RES_NO_TAG
, "Failed to find the requested tag"},
421 {SD_RES_STARTUP
, "The system is still booting"},
422 {SD_RES_VDI_NOT_LOCKED
, "VDI isn't locked"},
423 {SD_RES_SHUTDOWN
, "The system is shutting down"},
424 {SD_RES_NO_MEM
, "Out of memory on the server"},
425 {SD_RES_FULL_VDI
, "We already have the maximum vdis"},
426 {SD_RES_VER_MISMATCH
, "Protocol version mismatch"},
427 {SD_RES_NO_SPACE
, "Server has no space for new objects"},
428 {SD_RES_WAIT_FOR_FORMAT
, "Sheepdog is waiting for a format operation"},
429 {SD_RES_WAIT_FOR_JOIN
, "Sheepdog is waiting for other nodes joining"},
430 {SD_RES_JOIN_FAILED
, "Target node had failed to join sheepdog"},
431 {SD_RES_HALT
, "Sheepdog is stopped serving IO request"},
432 {SD_RES_READONLY
, "Object is read-only"},
435 for (i
= 0; i
< ARRAY_SIZE(errors
); ++i
) {
436 if (errors
[i
].err
== err
) {
437 return errors
[i
].desc
;
441 return "Invalid error code";
445 * Sheepdog I/O handling:
447 * 1. In sd_co_rw_vector, we send the I/O requests to the server and
448 * link the requests to the inflight_list in the
449 * BDRVSheepdogState. The function yields while waiting for
450 * receiving the response.
452 * 2. We receive the response in aio_read_response, the fd handler to
453 * the sheepdog connection. We switch back to sd_co_readv/sd_writev
454 * after all the requests belonging to the AIOCB are finished. If
455 * needed, sd_co_writev will send another requests for the vdi object.
458 static inline AIOReq
*alloc_aio_req(BDRVSheepdogState
*s
, SheepdogAIOCB
*acb
,
459 uint64_t oid
, unsigned int data_len
,
460 uint64_t offset
, uint8_t flags
, bool create
,
461 uint64_t base_oid
, unsigned int iov_offset
)
465 aio_req
= g_malloc(sizeof(*aio_req
));
466 aio_req
->aiocb
= acb
;
467 aio_req
->iov_offset
= iov_offset
;
469 aio_req
->base_oid
= base_oid
;
470 aio_req
->offset
= offset
;
471 aio_req
->data_len
= data_len
;
472 aio_req
->flags
= flags
;
473 aio_req
->id
= s
->aioreq_seq_num
++;
474 aio_req
->create
= create
;
480 static void wait_for_overlapping_aiocb(BDRVSheepdogState
*s
, SheepdogAIOCB
*acb
)
485 QLIST_FOREACH(cb
, &s
->inflight_aiocb_head
, aiocb_siblings
) {
486 if (AIOCBOverlapping(acb
, cb
)) {
487 qemu_co_queue_wait(&s
->overlapping_queue
, &s
->queue_lock
);
493 static void sd_aio_setup(SheepdogAIOCB
*acb
, BDRVSheepdogState
*s
,
494 QEMUIOVector
*qiov
, int64_t sector_num
, int nb_sectors
,
497 uint32_t object_size
;
499 object_size
= (UINT32_C(1) << s
->inode
.block_size_shift
);
505 acb
->sector_num
= sector_num
;
506 acb
->nb_sectors
= nb_sectors
;
508 acb
->coroutine
= qemu_coroutine_self();
512 acb
->min_affect_data_idx
= acb
->sector_num
* BDRV_SECTOR_SIZE
/ object_size
;
513 acb
->max_affect_data_idx
= (acb
->sector_num
* BDRV_SECTOR_SIZE
+
514 acb
->nb_sectors
* BDRV_SECTOR_SIZE
) / object_size
;
516 acb
->min_dirty_data_idx
= UINT32_MAX
;
517 acb
->max_dirty_data_idx
= 0;
518 acb
->aiocb_type
= type
;
520 if (type
== AIOCB_FLUSH_CACHE
) {
524 qemu_co_mutex_lock(&s
->queue_lock
);
525 wait_for_overlapping_aiocb(s
, acb
);
526 QLIST_INSERT_HEAD(&s
->inflight_aiocb_head
, acb
, aiocb_siblings
);
527 qemu_co_mutex_unlock(&s
->queue_lock
);
530 static SocketAddress
*sd_server_config(QDict
*options
, Error
**errp
)
532 QDict
*server
= NULL
;
534 SocketAddress
*saddr
= NULL
;
535 Error
*local_err
= NULL
;
537 qdict_extract_subqdict(options
, &server
, "server.");
539 iv
= qobject_input_visitor_new_flat_confused(server
, errp
);
544 visit_type_SocketAddress(iv
, NULL
, &saddr
, &local_err
);
546 error_propagate(errp
, local_err
);
552 qobject_unref(server
);
556 /* Return -EIO in case of error, file descriptor on success */
557 static int connect_to_sdog(BDRVSheepdogState
*s
, Error
**errp
)
561 fd
= socket_connect(s
->addr
, errp
);
563 if (s
->addr
->type
== SOCKET_ADDRESS_TYPE_INET
&& fd
>= 0) {
564 int ret
= socket_set_nodelay(fd
);
566 warn_report("can't set TCP_NODELAY: %s", strerror(errno
));
571 qemu_set_nonblock(fd
);
579 /* Return 0 on success and -errno in case of error */
580 static coroutine_fn
int send_co_req(int sockfd
, SheepdogReq
*hdr
, void *data
,
585 ret
= qemu_co_send(sockfd
, hdr
, sizeof(*hdr
));
586 if (ret
!= sizeof(*hdr
)) {
587 error_report("failed to send a req, %s", strerror(errno
));
591 ret
= qemu_co_send(sockfd
, data
, *wlen
);
593 error_report("failed to send a req, %s", strerror(errno
));
600 typedef struct SheepdogReqCo
{
602 BlockDriverState
*bs
;
603 AioContext
*aio_context
;
613 static void restart_co_req(void *opaque
)
615 SheepdogReqCo
*srco
= opaque
;
617 aio_co_wake(srco
->co
);
620 static coroutine_fn
void do_co_req(void *opaque
)
623 SheepdogReqCo
*srco
= opaque
;
624 int sockfd
= srco
->sockfd
;
625 SheepdogReq
*hdr
= srco
->hdr
;
626 void *data
= srco
->data
;
627 unsigned int *wlen
= srco
->wlen
;
628 unsigned int *rlen
= srco
->rlen
;
630 srco
->co
= qemu_coroutine_self();
631 aio_set_fd_handler(srco
->aio_context
, sockfd
, false,
632 NULL
, restart_co_req
, NULL
, srco
);
634 ret
= send_co_req(sockfd
, hdr
, data
, wlen
);
639 aio_set_fd_handler(srco
->aio_context
, sockfd
, false,
640 restart_co_req
, NULL
, NULL
, srco
);
642 ret
= qemu_co_recv(sockfd
, hdr
, sizeof(*hdr
));
643 if (ret
!= sizeof(*hdr
)) {
644 error_report("failed to get a rsp, %s", strerror(errno
));
649 if (*rlen
> hdr
->data_length
) {
650 *rlen
= hdr
->data_length
;
654 ret
= qemu_co_recv(sockfd
, data
, *rlen
);
656 error_report("failed to get the data, %s", strerror(errno
));
663 /* there is at most one request for this sockfd, so it is safe to
664 * set each handler to NULL. */
665 aio_set_fd_handler(srco
->aio_context
, sockfd
, false,
666 NULL
, NULL
, NULL
, NULL
);
670 /* Set srco->finished before reading bs->wakeup. */
671 atomic_mb_set(&srco
->finished
, true);
673 bdrv_wakeup(srco
->bs
);
678 * Send the request to the sheep in a synchronous manner.
680 * Return 0 on success, -errno in case of error.
682 static int do_req(int sockfd
, BlockDriverState
*bs
, SheepdogReq
*hdr
,
683 void *data
, unsigned int *wlen
, unsigned int *rlen
)
686 SheepdogReqCo srco
= {
688 .aio_context
= bs
? bdrv_get_aio_context(bs
) : qemu_get_aio_context(),
698 if (qemu_in_coroutine()) {
701 co
= qemu_coroutine_create(do_co_req
, &srco
);
703 bdrv_coroutine_enter(bs
, co
);
704 BDRV_POLL_WHILE(bs
, !srco
.finished
);
706 qemu_coroutine_enter(co
);
707 while (!srco
.finished
) {
708 aio_poll(qemu_get_aio_context(), true);
716 static void coroutine_fn
add_aio_request(BDRVSheepdogState
*s
, AIOReq
*aio_req
,
717 struct iovec
*iov
, int niov
,
718 enum AIOCBState aiocb_type
);
719 static void coroutine_fn
resend_aioreq(BDRVSheepdogState
*s
, AIOReq
*aio_req
);
720 static int reload_inode(BDRVSheepdogState
*s
, uint32_t snapid
, const char *tag
);
721 static int get_sheep_fd(BDRVSheepdogState
*s
, Error
**errp
);
722 static void co_write_request(void *opaque
);
724 static coroutine_fn
void reconnect_to_sdog(void *opaque
)
726 BDRVSheepdogState
*s
= opaque
;
727 AIOReq
*aio_req
, *next
;
729 aio_set_fd_handler(s
->aio_context
, s
->fd
, false, NULL
,
734 /* Wait for outstanding write requests to be completed. */
735 while (s
->co_send
!= NULL
) {
736 co_write_request(opaque
);
739 /* Try to reconnect the sheepdog server every one second. */
741 Error
*local_err
= NULL
;
742 s
->fd
= get_sheep_fd(s
, &local_err
);
744 trace_sheepdog_reconnect_to_sdog();
745 error_report_err(local_err
);
746 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME
, 1000000000ULL);
751 * Now we have to resend all the request in the inflight queue. However,
752 * resend_aioreq() can yield and newly created requests can be added to the
753 * inflight queue before the coroutine is resumed. To avoid mixing them, we
754 * have to move all the inflight requests to the failed queue before
755 * resend_aioreq() is called.
757 qemu_co_mutex_lock(&s
->queue_lock
);
758 QLIST_FOREACH_SAFE(aio_req
, &s
->inflight_aio_head
, aio_siblings
, next
) {
759 QLIST_REMOVE(aio_req
, aio_siblings
);
760 QLIST_INSERT_HEAD(&s
->failed_aio_head
, aio_req
, aio_siblings
);
763 /* Resend all the failed aio requests. */
764 while (!QLIST_EMPTY(&s
->failed_aio_head
)) {
765 aio_req
= QLIST_FIRST(&s
->failed_aio_head
);
766 QLIST_REMOVE(aio_req
, aio_siblings
);
767 qemu_co_mutex_unlock(&s
->queue_lock
);
768 resend_aioreq(s
, aio_req
);
769 qemu_co_mutex_lock(&s
->queue_lock
);
771 qemu_co_mutex_unlock(&s
->queue_lock
);
775 * Receive responses of the I/O requests.
777 * This function is registered as a fd handler, and called from the
778 * main loop when s->fd is ready for reading responses.
780 static void coroutine_fn
aio_read_response(void *opaque
)
783 BDRVSheepdogState
*s
= opaque
;
786 AIOReq
*aio_req
= NULL
;
791 ret
= qemu_co_recv(fd
, &rsp
, sizeof(rsp
));
792 if (ret
!= sizeof(rsp
)) {
793 error_report("failed to get the header, %s", strerror(errno
));
797 /* find the right aio_req from the inflight aio list */
798 QLIST_FOREACH(aio_req
, &s
->inflight_aio_head
, aio_siblings
) {
799 if (aio_req
->id
== rsp
.id
) {
804 error_report("cannot find aio_req %x", rsp
.id
);
808 acb
= aio_req
->aiocb
;
810 switch (acb
->aiocb_type
) {
811 case AIOCB_WRITE_UDATA
:
812 if (!is_data_obj(aio_req
->oid
)) {
815 idx
= data_oid_to_idx(aio_req
->oid
);
817 if (aio_req
->create
) {
819 * If the object is newly created one, we need to update
820 * the vdi object (metadata object). min_dirty_data_idx
821 * and max_dirty_data_idx are changed to include updated
822 * index between them.
824 if (rsp
.result
== SD_RES_SUCCESS
) {
825 s
->inode
.data_vdi_id
[idx
] = s
->inode
.vdi_id
;
826 acb
->max_dirty_data_idx
= MAX(idx
, acb
->max_dirty_data_idx
);
827 acb
->min_dirty_data_idx
= MIN(idx
, acb
->min_dirty_data_idx
);
831 case AIOCB_READ_UDATA
:
832 ret
= qemu_co_recvv(fd
, acb
->qiov
->iov
, acb
->qiov
->niov
,
833 aio_req
->iov_offset
, rsp
.data_length
);
834 if (ret
!= rsp
.data_length
) {
835 error_report("failed to get the data, %s", strerror(errno
));
839 case AIOCB_FLUSH_CACHE
:
840 if (rsp
.result
== SD_RES_INVALID_PARMS
) {
841 trace_sheepdog_aio_read_response();
842 s
->cache_flags
= SD_FLAG_CMD_DIRECT
;
843 rsp
.result
= SD_RES_SUCCESS
;
846 case AIOCB_DISCARD_OBJ
:
847 switch (rsp
.result
) {
848 case SD_RES_INVALID_PARMS
:
849 error_report("server doesn't support discard command");
850 rsp
.result
= SD_RES_SUCCESS
;
851 s
->discard_supported
= false;
858 /* No more data for this aio_req (reload_inode below uses its own file
859 * descriptor handler which doesn't use co_recv).
863 qemu_co_mutex_lock(&s
->queue_lock
);
864 QLIST_REMOVE(aio_req
, aio_siblings
);
865 qemu_co_mutex_unlock(&s
->queue_lock
);
867 switch (rsp
.result
) {
870 case SD_RES_READONLY
:
871 if (s
->inode
.vdi_id
== oid_to_vid(aio_req
->oid
)) {
872 ret
= reload_inode(s
, 0, "");
877 if (is_data_obj(aio_req
->oid
)) {
878 aio_req
->oid
= vid_to_data_oid(s
->inode
.vdi_id
,
879 data_oid_to_idx(aio_req
->oid
));
881 aio_req
->oid
= vid_to_vdi_oid(s
->inode
.vdi_id
);
883 resend_aioreq(s
, aio_req
);
887 error_report("%s", sd_strerror(rsp
.result
));
893 if (!--acb
->nr_pending
) {
895 * We've finished all requests which belong to the AIOCB, so
896 * we can switch back to sd_co_readv/writev now.
898 aio_co_wake(acb
->coroutine
);
904 reconnect_to_sdog(opaque
);
907 static void co_read_response(void *opaque
)
909 BDRVSheepdogState
*s
= opaque
;
912 s
->co_recv
= qemu_coroutine_create(aio_read_response
, opaque
);
915 aio_co_enter(s
->aio_context
, s
->co_recv
);
918 static void co_write_request(void *opaque
)
920 BDRVSheepdogState
*s
= opaque
;
922 aio_co_wake(s
->co_send
);
926 * Return a socket descriptor to read/write objects.
928 * We cannot use this descriptor for other operations because
929 * the block driver may be on waiting response from the server.
931 static int get_sheep_fd(BDRVSheepdogState
*s
, Error
**errp
)
935 fd
= connect_to_sdog(s
, errp
);
940 aio_set_fd_handler(s
->aio_context
, fd
, false,
941 co_read_response
, NULL
, NULL
, s
);
946 * Parse numeric snapshot ID in @str
947 * If @str can't be parsed as number, return false.
948 * Else, if the number is zero or too large, set *@snapid to zero and
950 * Else, set *@snapid to the number and return true.
952 static bool sd_parse_snapid(const char *str
, uint32_t *snapid
)
957 ret
= qemu_strtoul(str
, NULL
, 10, &ul
);
958 if (ret
== -ERANGE
) {
964 if (ul
> UINT32_MAX
) {
972 static bool sd_parse_snapid_or_tag(const char *str
,
973 uint32_t *snapid
, char tag
[])
975 if (!sd_parse_snapid(str
, snapid
)) {
977 if (g_strlcpy(tag
, str
, SD_MAX_VDI_TAG_LEN
) >= SD_MAX_VDI_TAG_LEN
) {
980 } else if (!*snapid
) {
989 const char *path
; /* non-null iff transport is tcp */
990 const char *host
; /* valid when transport is tcp */
991 int port
; /* valid when transport is tcp */
992 char vdi
[SD_MAX_VDI_LEN
];
993 char tag
[SD_MAX_VDI_TAG_LEN
];
995 /* Remainder is only for sd_config_done() */
1000 static void sd_config_done(SheepdogConfig
*cfg
)
1003 query_params_free(cfg
->qp
);
1008 static void sd_parse_uri(SheepdogConfig
*cfg
, const char *filename
,
1012 QueryParams
*qp
= NULL
;
1016 memset(cfg
, 0, sizeof(*cfg
));
1018 cfg
->uri
= uri
= uri_parse(filename
);
1020 error_setg(&err
, "invalid URI '%s'", filename
);
1025 if (!g_strcmp0(uri
->scheme
, "sheepdog")) {
1027 } else if (!g_strcmp0(uri
->scheme
, "sheepdog+tcp")) {
1029 } else if (!g_strcmp0(uri
->scheme
, "sheepdog+unix")) {
1032 error_setg(&err
, "URI scheme must be 'sheepdog', 'sheepdog+tcp',"
1033 " or 'sheepdog+unix'");
1037 if (uri
->path
== NULL
|| !strcmp(uri
->path
, "/")) {
1038 error_setg(&err
, "missing file path in URI");
1041 if (g_strlcpy(cfg
->vdi
, uri
->path
+ 1, SD_MAX_VDI_LEN
)
1042 >= SD_MAX_VDI_LEN
) {
1043 error_setg(&err
, "VDI name is too long");
1047 cfg
->qp
= qp
= query_params_parse(uri
->query
);
1050 /* sheepdog+unix:///vdiname?socket=path */
1051 if (uri
->server
|| uri
->port
) {
1052 error_setg(&err
, "URI scheme %s doesn't accept a server address",
1058 "URI scheme %s requires query parameter 'socket'",
1062 if (qp
->n
!= 1 || strcmp(qp
->p
[0].name
, "socket")) {
1063 error_setg(&err
, "unexpected query parameters");
1066 cfg
->path
= qp
->p
[0].value
;
1068 /* sheepdog[+tcp]://[host:port]/vdiname */
1070 error_setg(&err
, "unexpected query parameters");
1073 cfg
->host
= uri
->server
;
1074 cfg
->port
= uri
->port
;
1078 if (uri
->fragment
) {
1079 if (!sd_parse_snapid_or_tag(uri
->fragment
,
1080 &cfg
->snap_id
, cfg
->tag
)) {
1081 error_setg(&err
, "'%s' is not a valid snapshot ID",
1086 cfg
->snap_id
= CURRENT_VDI_ID
; /* search current vdi */
1091 error_propagate(errp
, err
);
1092 sd_config_done(cfg
);
1097 * Parse a filename (old syntax)
1099 * filename must be one of the following formats:
1101 * 2. [vdiname]:[snapid]
1102 * 3. [vdiname]:[tag]
1103 * 4. [hostname]:[port]:[vdiname]
1104 * 5. [hostname]:[port]:[vdiname]:[snapid]
1105 * 6. [hostname]:[port]:[vdiname]:[tag]
1107 * You can boot from the snapshot images by specifying `snapid` or
1110 * You can run VMs outside the Sheepdog cluster by specifying
1111 * `hostname' and `port' (experimental).
1113 static void parse_vdiname(SheepdogConfig
*cfg
, const char *filename
,
1118 const char *host_spec
, *vdi_spec
;
1121 strstart(filename
, "sheepdog:", &filename
);
1122 p
= q
= g_strdup(filename
);
1124 /* count the number of separators */
1134 /* use the first two tokens as host_spec. */
1147 p
= strchr(vdi_spec
, ':');
1152 uri
= g_strdup_printf("sheepdog://%s/%s", host_spec
, vdi_spec
);
1155 * FIXME We to escape URI meta-characters, e.g. "x?y=z"
1156 * produces "sheepdog://x?y=z". Because of that ...
1158 sd_parse_uri(cfg
, uri
, &err
);
1161 * ... this can fail, but the error message is misleading.
1162 * Replace it by the traditional useless one until the
1163 * escaping is fixed.
1166 error_setg(errp
, "Can't parse filename");
1173 static void sd_parse_filename(const char *filename
, QDict
*options
,
1180 if (strstr(filename
, "://")) {
1181 sd_parse_uri(&cfg
, filename
, &err
);
1183 parse_vdiname(&cfg
, filename
, &err
);
1186 error_propagate(errp
, err
);
1191 qdict_set_default_str(options
, "server.path", cfg
.path
);
1192 qdict_set_default_str(options
, "server.type", "unix");
1194 qdict_set_default_str(options
, "server.type", "inet");
1195 qdict_set_default_str(options
, "server.host",
1196 cfg
.host
?: SD_DEFAULT_ADDR
);
1197 snprintf(buf
, sizeof(buf
), "%d", cfg
.port
?: SD_DEFAULT_PORT
);
1198 qdict_set_default_str(options
, "server.port", buf
);
1200 qdict_set_default_str(options
, "vdi", cfg
.vdi
);
1201 qdict_set_default_str(options
, "tag", cfg
.tag
);
1203 snprintf(buf
, sizeof(buf
), "%d", cfg
.snap_id
);
1204 qdict_set_default_str(options
, "snap-id", buf
);
1207 sd_config_done(&cfg
);
1210 static int find_vdi_name(BDRVSheepdogState
*s
, const char *filename
,
1211 uint32_t snapid
, const char *tag
, uint32_t *vid
,
1212 bool lock
, Error
**errp
)
1216 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1217 unsigned int wlen
, rlen
= 0;
1218 char buf
[SD_MAX_VDI_LEN
+ SD_MAX_VDI_TAG_LEN
] QEMU_NONSTRING
;
1220 fd
= connect_to_sdog(s
, errp
);
1225 /* This pair of strncpy calls ensures that the buffer is zero-filled,
1226 * which is desirable since we'll soon be sending those bytes, and
1227 * don't want the send_req to read uninitialized data.
1229 strncpy(buf
, filename
, SD_MAX_VDI_LEN
);
1230 strncpy(buf
+ SD_MAX_VDI_LEN
, tag
, SD_MAX_VDI_TAG_LEN
);
1232 memset(&hdr
, 0, sizeof(hdr
));
1234 hdr
.opcode
= SD_OP_LOCK_VDI
;
1235 hdr
.type
= LOCK_TYPE_NORMAL
;
1237 hdr
.opcode
= SD_OP_GET_VDI_INFO
;
1239 wlen
= SD_MAX_VDI_LEN
+ SD_MAX_VDI_TAG_LEN
;
1240 hdr
.proto_ver
= SD_PROTO_VER
;
1241 hdr
.data_length
= wlen
;
1242 hdr
.snapid
= snapid
;
1243 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1245 ret
= do_req(fd
, s
->bs
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1247 error_setg_errno(errp
, -ret
, "cannot get vdi info");
1251 if (rsp
->result
!= SD_RES_SUCCESS
) {
1252 error_setg(errp
, "cannot get vdi info, %s, %s %" PRIu32
" %s",
1253 sd_strerror(rsp
->result
), filename
, snapid
, tag
);
1254 if (rsp
->result
== SD_RES_NO_VDI
) {
1256 } else if (rsp
->result
== SD_RES_VDI_LOCKED
) {
1271 static void coroutine_fn
add_aio_request(BDRVSheepdogState
*s
, AIOReq
*aio_req
,
1272 struct iovec
*iov
, int niov
,
1273 enum AIOCBState aiocb_type
)
1275 int nr_copies
= s
->inode
.nr_copies
;
1277 unsigned int wlen
= 0;
1279 uint64_t oid
= aio_req
->oid
;
1280 unsigned int datalen
= aio_req
->data_len
;
1281 uint64_t offset
= aio_req
->offset
;
1282 uint8_t flags
= aio_req
->flags
;
1283 uint64_t old_oid
= aio_req
->base_oid
;
1284 bool create
= aio_req
->create
;
1286 qemu_co_mutex_lock(&s
->queue_lock
);
1287 QLIST_INSERT_HEAD(&s
->inflight_aio_head
, aio_req
, aio_siblings
);
1288 qemu_co_mutex_unlock(&s
->queue_lock
);
1291 error_report("bug");
1294 memset(&hdr
, 0, sizeof(hdr
));
1296 switch (aiocb_type
) {
1297 case AIOCB_FLUSH_CACHE
:
1298 hdr
.opcode
= SD_OP_FLUSH_VDI
;
1300 case AIOCB_READ_UDATA
:
1301 hdr
.opcode
= SD_OP_READ_OBJ
;
1304 case AIOCB_WRITE_UDATA
:
1306 hdr
.opcode
= SD_OP_CREATE_AND_WRITE_OBJ
;
1308 hdr
.opcode
= SD_OP_WRITE_OBJ
;
1311 hdr
.flags
= SD_FLAG_CMD_WRITE
| flags
;
1313 case AIOCB_DISCARD_OBJ
:
1314 hdr
.opcode
= SD_OP_WRITE_OBJ
;
1315 hdr
.flags
= SD_FLAG_CMD_WRITE
| flags
;
1316 s
->inode
.data_vdi_id
[data_oid_to_idx(oid
)] = 0;
1317 offset
= offsetof(SheepdogInode
,
1318 data_vdi_id
[data_oid_to_idx(oid
)]);
1319 oid
= vid_to_vdi_oid(s
->inode
.vdi_id
);
1320 wlen
= datalen
= sizeof(uint32_t);
1324 if (s
->cache_flags
) {
1325 hdr
.flags
|= s
->cache_flags
;
1329 hdr
.cow_oid
= old_oid
;
1330 hdr
.copies
= s
->inode
.nr_copies
;
1332 hdr
.data_length
= datalen
;
1333 hdr
.offset
= offset
;
1335 hdr
.id
= aio_req
->id
;
1337 qemu_co_mutex_lock(&s
->lock
);
1338 s
->co_send
= qemu_coroutine_self();
1339 aio_set_fd_handler(s
->aio_context
, s
->fd
, false,
1340 co_read_response
, co_write_request
, NULL
, s
);
1341 socket_set_cork(s
->fd
, 1);
1344 ret
= qemu_co_send(s
->fd
, &hdr
, sizeof(hdr
));
1345 if (ret
!= sizeof(hdr
)) {
1346 error_report("failed to send a req, %s", strerror(errno
));
1351 ret
= qemu_co_sendv(s
->fd
, iov
, niov
, aio_req
->iov_offset
, wlen
);
1353 error_report("failed to send a data, %s", strerror(errno
));
1357 socket_set_cork(s
->fd
, 0);
1358 aio_set_fd_handler(s
->aio_context
, s
->fd
, false,
1359 co_read_response
, NULL
, NULL
, s
);
1361 qemu_co_mutex_unlock(&s
->lock
);
1364 static int read_write_object(int fd
, BlockDriverState
*bs
, char *buf
,
1365 uint64_t oid
, uint8_t copies
,
1366 unsigned int datalen
, uint64_t offset
,
1367 bool write
, bool create
, uint32_t cache_flags
)
1370 SheepdogObjRsp
*rsp
= (SheepdogObjRsp
*)&hdr
;
1371 unsigned int wlen
, rlen
;
1374 memset(&hdr
, 0, sizeof(hdr
));
1379 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1381 hdr
.opcode
= SD_OP_CREATE_AND_WRITE_OBJ
;
1383 hdr
.opcode
= SD_OP_WRITE_OBJ
;
1388 hdr
.opcode
= SD_OP_READ_OBJ
;
1391 hdr
.flags
|= cache_flags
;
1394 hdr
.data_length
= datalen
;
1395 hdr
.offset
= offset
;
1396 hdr
.copies
= copies
;
1398 ret
= do_req(fd
, bs
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1400 error_report("failed to send a request to the sheep");
1404 switch (rsp
->result
) {
1405 case SD_RES_SUCCESS
:
1408 error_report("%s", sd_strerror(rsp
->result
));
1413 static int read_object(int fd
, BlockDriverState
*bs
, char *buf
,
1414 uint64_t oid
, uint8_t copies
,
1415 unsigned int datalen
, uint64_t offset
,
1416 uint32_t cache_flags
)
1418 return read_write_object(fd
, bs
, buf
, oid
, copies
,
1419 datalen
, offset
, false,
1420 false, cache_flags
);
1423 static int write_object(int fd
, BlockDriverState
*bs
, char *buf
,
1424 uint64_t oid
, uint8_t copies
,
1425 unsigned int datalen
, uint64_t offset
, bool create
,
1426 uint32_t cache_flags
)
1428 return read_write_object(fd
, bs
, buf
, oid
, copies
,
1429 datalen
, offset
, true,
1430 create
, cache_flags
);
1433 /* update inode with the latest state */
1434 static int reload_inode(BDRVSheepdogState
*s
, uint32_t snapid
, const char *tag
)
1436 Error
*local_err
= NULL
;
1437 SheepdogInode
*inode
;
1441 fd
= connect_to_sdog(s
, &local_err
);
1443 error_report_err(local_err
);
1447 inode
= g_malloc(SD_INODE_HEADER_SIZE
);
1449 ret
= find_vdi_name(s
, s
->name
, snapid
, tag
, &vid
, false, &local_err
);
1451 error_report_err(local_err
);
1455 ret
= read_object(fd
, s
->bs
, (char *)inode
, vid_to_vdi_oid(vid
),
1456 s
->inode
.nr_copies
, SD_INODE_HEADER_SIZE
, 0,
1462 if (inode
->vdi_id
!= s
->inode
.vdi_id
) {
1463 memcpy(&s
->inode
, inode
, SD_INODE_HEADER_SIZE
);
1473 static void coroutine_fn
resend_aioreq(BDRVSheepdogState
*s
, AIOReq
*aio_req
)
1475 SheepdogAIOCB
*acb
= aio_req
->aiocb
;
1477 aio_req
->create
= false;
1479 /* check whether this request becomes a CoW one */
1480 if (acb
->aiocb_type
== AIOCB_WRITE_UDATA
&& is_data_obj(aio_req
->oid
)) {
1481 int idx
= data_oid_to_idx(aio_req
->oid
);
1483 if (is_data_obj_writable(&s
->inode
, idx
)) {
1487 if (s
->inode
.data_vdi_id
[idx
]) {
1488 aio_req
->base_oid
= vid_to_data_oid(s
->inode
.data_vdi_id
[idx
], idx
);
1489 aio_req
->flags
|= SD_FLAG_CMD_COW
;
1491 aio_req
->create
= true;
1494 if (is_data_obj(aio_req
->oid
)) {
1495 add_aio_request(s
, aio_req
, acb
->qiov
->iov
, acb
->qiov
->niov
,
1499 iov
.iov_base
= &s
->inode
;
1500 iov
.iov_len
= sizeof(s
->inode
);
1501 add_aio_request(s
, aio_req
, &iov
, 1, AIOCB_WRITE_UDATA
);
1505 static void sd_detach_aio_context(BlockDriverState
*bs
)
1507 BDRVSheepdogState
*s
= bs
->opaque
;
1509 aio_set_fd_handler(s
->aio_context
, s
->fd
, false, NULL
,
1513 static void sd_attach_aio_context(BlockDriverState
*bs
,
1514 AioContext
*new_context
)
1516 BDRVSheepdogState
*s
= bs
->opaque
;
1518 s
->aio_context
= new_context
;
1519 aio_set_fd_handler(new_context
, s
->fd
, false,
1520 co_read_response
, NULL
, NULL
, s
);
1523 static QemuOptsList runtime_opts
= {
1525 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
1529 .type
= QEMU_OPT_STRING
,
1533 .type
= QEMU_OPT_NUMBER
,
1537 .type
= QEMU_OPT_STRING
,
1539 { /* end of list */ }
1543 static int sd_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1548 BDRVSheepdogState
*s
= bs
->opaque
;
1549 const char *vdi
, *snap_id_str
, *tag
;
1553 Error
*local_err
= NULL
;
1556 s
->aio_context
= bdrv_get_aio_context(bs
);
1558 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
1559 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
1561 error_propagate(errp
, local_err
);
1566 s
->addr
= sd_server_config(options
, errp
);
1572 vdi
= qemu_opt_get(opts
, "vdi");
1573 snap_id_str
= qemu_opt_get(opts
, "snap-id");
1574 snap_id
= qemu_opt_get_number(opts
, "snap-id", CURRENT_VDI_ID
);
1575 tag
= qemu_opt_get(opts
, "tag");
1578 error_setg(errp
, "parameter 'vdi' is missing");
1582 if (strlen(vdi
) >= SD_MAX_VDI_LEN
) {
1583 error_setg(errp
, "value of parameter 'vdi' is too long");
1588 if (snap_id
> UINT32_MAX
) {
1591 if (snap_id_str
&& !snap_id
) {
1592 error_setg(errp
, "'snap-id=%s' is not a valid snapshot ID",
1601 if (strlen(tag
) >= SD_MAX_VDI_TAG_LEN
) {
1602 error_setg(errp
, "value of parameter 'tag' is too long");
1607 QLIST_INIT(&s
->inflight_aio_head
);
1608 QLIST_INIT(&s
->failed_aio_head
);
1609 QLIST_INIT(&s
->inflight_aiocb_head
);
1611 s
->fd
= get_sheep_fd(s
, errp
);
1617 ret
= find_vdi_name(s
, vdi
, (uint32_t)snap_id
, tag
, &vid
, true, errp
);
1623 * QEMU block layer emulates writethrough cache as 'writeback + flush', so
1624 * we always set SD_FLAG_CMD_CACHE (writeback cache) as default.
1626 s
->cache_flags
= SD_FLAG_CMD_CACHE
;
1627 if (flags
& BDRV_O_NOCACHE
) {
1628 s
->cache_flags
= SD_FLAG_CMD_DIRECT
;
1630 s
->discard_supported
= true;
1632 if (snap_id
|| tag
[0]) {
1633 trace_sheepdog_open(vid
);
1634 s
->is_snapshot
= true;
1637 fd
= connect_to_sdog(s
, errp
);
1643 buf
= g_malloc(SD_INODE_SIZE
);
1644 ret
= read_object(fd
, s
->bs
, buf
, vid_to_vdi_oid(vid
),
1645 0, SD_INODE_SIZE
, 0, s
->cache_flags
);
1650 error_setg(errp
, "Can't read snapshot inode");
1654 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
1656 bs
->total_sectors
= s
->inode
.vdi_size
/ BDRV_SECTOR_SIZE
;
1657 pstrcpy(s
->name
, sizeof(s
->name
), vdi
);
1658 qemu_co_mutex_init(&s
->lock
);
1659 qemu_co_mutex_init(&s
->queue_lock
);
1660 qemu_co_queue_init(&s
->overlapping_queue
);
1661 qemu_opts_del(opts
);
1666 aio_set_fd_handler(bdrv_get_aio_context(bs
), s
->fd
,
1667 false, NULL
, NULL
, NULL
, NULL
);
1670 qemu_opts_del(opts
);
1675 static int sd_reopen_prepare(BDRVReopenState
*state
, BlockReopenQueue
*queue
,
1678 BDRVSheepdogState
*s
= state
->bs
->opaque
;
1679 BDRVSheepdogReopenState
*re_s
;
1682 re_s
= state
->opaque
= g_new0(BDRVSheepdogReopenState
, 1);
1684 re_s
->cache_flags
= SD_FLAG_CMD_CACHE
;
1685 if (state
->flags
& BDRV_O_NOCACHE
) {
1686 re_s
->cache_flags
= SD_FLAG_CMD_DIRECT
;
1689 re_s
->fd
= get_sheep_fd(s
, errp
);
1698 static void sd_reopen_commit(BDRVReopenState
*state
)
1700 BDRVSheepdogReopenState
*re_s
= state
->opaque
;
1701 BDRVSheepdogState
*s
= state
->bs
->opaque
;
1704 aio_set_fd_handler(s
->aio_context
, s
->fd
, false,
1705 NULL
, NULL
, NULL
, NULL
);
1710 s
->cache_flags
= re_s
->cache_flags
;
1712 g_free(state
->opaque
);
1713 state
->opaque
= NULL
;
1718 static void sd_reopen_abort(BDRVReopenState
*state
)
1720 BDRVSheepdogReopenState
*re_s
= state
->opaque
;
1721 BDRVSheepdogState
*s
= state
->bs
->opaque
;
1728 aio_set_fd_handler(s
->aio_context
, re_s
->fd
, false,
1729 NULL
, NULL
, NULL
, NULL
);
1730 closesocket(re_s
->fd
);
1733 g_free(state
->opaque
);
1734 state
->opaque
= NULL
;
1739 static int do_sd_create(BDRVSheepdogState
*s
, uint32_t *vdi_id
, int snapshot
,
1743 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1745 unsigned int wlen
, rlen
= 0;
1746 char buf
[SD_MAX_VDI_LEN
];
1748 fd
= connect_to_sdog(s
, errp
);
1753 /* FIXME: would it be better to fail (e.g., return -EIO) when filename
1754 * does not fit in buf? For now, just truncate and avoid buffer overrun.
1756 memset(buf
, 0, sizeof(buf
));
1757 pstrcpy(buf
, sizeof(buf
), s
->name
);
1759 memset(&hdr
, 0, sizeof(hdr
));
1760 hdr
.opcode
= SD_OP_NEW_VDI
;
1761 hdr
.base_vdi_id
= s
->inode
.vdi_id
;
1763 wlen
= SD_MAX_VDI_LEN
;
1765 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1766 hdr
.snapid
= snapshot
;
1768 hdr
.data_length
= wlen
;
1769 hdr
.vdi_size
= s
->inode
.vdi_size
;
1770 hdr
.copy_policy
= s
->inode
.copy_policy
;
1771 hdr
.copies
= s
->inode
.nr_copies
;
1772 hdr
.block_size_shift
= s
->inode
.block_size_shift
;
1774 ret
= do_req(fd
, NULL
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1779 error_setg_errno(errp
, -ret
, "create failed");
1783 if (rsp
->result
!= SD_RES_SUCCESS
) {
1784 error_setg(errp
, "%s, %s", sd_strerror(rsp
->result
), s
->inode
.name
);
1789 *vdi_id
= rsp
->vdi_id
;
1795 static int sd_prealloc(BlockDriverState
*bs
, int64_t old_size
, int64_t new_size
,
1798 BlockBackend
*blk
= NULL
;
1799 BDRVSheepdogState
*base
= bs
->opaque
;
1800 unsigned long buf_size
;
1801 uint32_t idx
, max_idx
;
1802 uint32_t object_size
;
1806 blk
= blk_new_with_bs(bs
,
1807 BLK_PERM_CONSISTENT_READ
| BLK_PERM_WRITE
| BLK_PERM_RESIZE
,
1808 BLK_PERM_ALL
, errp
);
1812 goto out_with_err_set
;
1815 blk_set_allow_write_beyond_eof(blk
, true);
1817 object_size
= (UINT32_C(1) << base
->inode
.block_size_shift
);
1818 buf_size
= MIN(object_size
, SD_DATA_OBJ_SIZE
);
1819 buf
= g_malloc0(buf_size
);
1821 max_idx
= DIV_ROUND_UP(new_size
, buf_size
);
1823 for (idx
= old_size
/ buf_size
; idx
< max_idx
; idx
++) {
1825 * The created image can be a cloned image, so we need to read
1826 * a data from the source image.
1828 ret
= blk_pread(blk
, idx
* buf_size
, buf
, buf_size
);
1832 ret
= blk_pwrite(blk
, idx
* buf_size
, buf
, buf_size
, 0);
1841 error_setg_errno(errp
, -ret
, "Can't pre-allocate");
1850 static int sd_create_prealloc(BlockdevOptionsSheepdog
*location
, int64_t size
,
1853 BlockDriverState
*bs
;
1855 QObject
*obj
= NULL
;
1859 v
= qobject_output_visitor_new(&obj
);
1860 visit_type_BlockdevOptionsSheepdog(v
, NULL
, &location
, &error_abort
);
1863 qdict
= qobject_to(QDict
, obj
);
1864 qdict_flatten(qdict
);
1866 qdict_put_str(qdict
, "driver", "sheepdog");
1868 bs
= bdrv_open(NULL
, NULL
, qdict
, BDRV_O_PROTOCOL
| BDRV_O_RDWR
, errp
);
1874 ret
= sd_prealloc(bs
, 0, size
, errp
);
1877 qobject_unref(qdict
);
1881 static int parse_redundancy(BDRVSheepdogState
*s
, SheepdogRedundancy
*opt
)
1883 struct SheepdogInode
*inode
= &s
->inode
;
1885 switch (opt
->type
) {
1886 case SHEEPDOG_REDUNDANCY_TYPE_FULL
:
1887 if (opt
->u
.full
.copies
> SD_MAX_COPIES
|| opt
->u
.full
.copies
< 1) {
1890 inode
->copy_policy
= 0;
1891 inode
->nr_copies
= opt
->u
.full
.copies
;
1894 case SHEEPDOG_REDUNDANCY_TYPE_ERASURE_CODED
:
1896 int64_t copy
= opt
->u
.erasure_coded
.data_strips
;
1897 int64_t parity
= opt
->u
.erasure_coded
.parity_strips
;
1899 if (copy
!= 2 && copy
!= 4 && copy
!= 8 && copy
!= 16) {
1903 if (parity
>= SD_EC_MAX_STRIP
|| parity
< 1) {
1908 * 4 bits for parity and 4 bits for data.
1909 * We have to compress upper data bits because it can't represent 16
1911 inode
->copy_policy
= ((copy
/ 2) << 4) + parity
;
1912 inode
->nr_copies
= copy
+ parity
;
1917 g_assert_not_reached();
1924 * Sheepdog support two kinds of redundancy, full replication and erasure
1927 * # create a fully replicated vdi with x copies
1928 * -o redundancy=x (1 <= x <= SD_MAX_COPIES)
1930 * # create a erasure coded vdi with x data strips and y parity strips
1931 * -o redundancy=x:y (x must be one of {2,4,8,16} and 1 <= y < SD_EC_MAX_STRIP)
1933 static SheepdogRedundancy
*parse_redundancy_str(const char *opt
)
1935 SheepdogRedundancy
*redundancy
;
1936 const char *n1
, *n2
;
1941 pstrcpy(p
, sizeof(p
), opt
);
1942 n1
= strtok(p
, ":");
1943 n2
= strtok(NULL
, ":");
1949 ret
= qemu_strtol(n1
, NULL
, 10, ©
);
1954 redundancy
= g_new0(SheepdogRedundancy
, 1);
1956 *redundancy
= (SheepdogRedundancy
) {
1957 .type
= SHEEPDOG_REDUNDANCY_TYPE_FULL
,
1958 .u
.full
.copies
= copy
,
1961 ret
= qemu_strtol(n2
, NULL
, 10, &parity
);
1967 *redundancy
= (SheepdogRedundancy
) {
1968 .type
= SHEEPDOG_REDUNDANCY_TYPE_ERASURE_CODED
,
1969 .u
.erasure_coded
= {
1970 .data_strips
= copy
,
1971 .parity_strips
= parity
,
1979 static int parse_block_size_shift(BDRVSheepdogState
*s
,
1980 BlockdevCreateOptionsSheepdog
*opts
)
1982 struct SheepdogInode
*inode
= &s
->inode
;
1983 uint64_t object_size
;
1986 if (opts
->has_object_size
) {
1987 object_size
= opts
->object_size
;
1989 if ((object_size
- 1) & object_size
) { /* not a power of 2? */
1992 obj_order
= ctz32(object_size
);
1993 if (obj_order
< 20 || obj_order
> 31) {
1996 inode
->block_size_shift
= (uint8_t)obj_order
;
2002 static int sd_co_create(BlockdevCreateOptions
*options
, Error
**errp
)
2004 BlockdevCreateOptionsSheepdog
*opts
= &options
->u
.sheepdog
;
2007 char *backing_file
= NULL
;
2009 BDRVSheepdogState
*s
;
2010 uint64_t max_vdi_size
;
2011 bool prealloc
= false;
2013 assert(options
->driver
== BLOCKDEV_DRIVER_SHEEPDOG
);
2015 s
= g_new0(BDRVSheepdogState
, 1);
2017 /* Steal SocketAddress from QAPI, set NULL to prevent double free */
2018 s
->addr
= opts
->location
->server
;
2019 opts
->location
->server
= NULL
;
2021 if (strlen(opts
->location
->vdi
) >= sizeof(s
->name
)) {
2022 error_setg(errp
, "'vdi' string too long");
2026 pstrcpy(s
->name
, sizeof(s
->name
), opts
->location
->vdi
);
2028 s
->inode
.vdi_size
= opts
->size
;
2029 backing_file
= opts
->backing_file
;
2031 if (!opts
->has_preallocation
) {
2032 opts
->preallocation
= PREALLOC_MODE_OFF
;
2034 switch (opts
->preallocation
) {
2035 case PREALLOC_MODE_OFF
:
2038 case PREALLOC_MODE_FULL
:
2042 error_setg(errp
, "Preallocation mode not supported for Sheepdog");
2047 if (opts
->has_redundancy
) {
2048 ret
= parse_redundancy(s
, opts
->redundancy
);
2050 error_setg(errp
, "Invalid redundancy mode");
2054 ret
= parse_block_size_shift(s
, opts
);
2056 error_setg(errp
, "Invalid object_size."
2057 " obect_size needs to be power of 2"
2058 " and be limited from 2^20 to 2^31");
2062 if (opts
->has_backing_file
) {
2064 BDRVSheepdogState
*base
;
2067 /* Currently, only Sheepdog backing image is supported. */
2068 drv
= bdrv_find_protocol(opts
->backing_file
, true, NULL
);
2069 if (!drv
|| strcmp(drv
->protocol_name
, "sheepdog") != 0) {
2070 error_setg(errp
, "backing_file must be a sheepdog image");
2075 blk
= blk_new_open(opts
->backing_file
, NULL
, NULL
,
2076 BDRV_O_PROTOCOL
, errp
);
2082 base
= blk_bs(blk
)->opaque
;
2084 if (!is_snapshot(&base
->inode
)) {
2085 error_setg(errp
, "cannot clone from a non snapshot vdi");
2090 s
->inode
.vdi_id
= base
->inode
.vdi_id
;
2094 s
->aio_context
= qemu_get_aio_context();
2096 /* if block_size_shift is not specified, get cluster default value */
2097 if (s
->inode
.block_size_shift
== 0) {
2099 SheepdogClusterRsp
*rsp
= (SheepdogClusterRsp
*)&hdr
;
2101 unsigned int wlen
= 0, rlen
= 0;
2103 fd
= connect_to_sdog(s
, errp
);
2109 memset(&hdr
, 0, sizeof(hdr
));
2110 hdr
.opcode
= SD_OP_GET_CLUSTER_DEFAULT
;
2111 hdr
.proto_ver
= SD_PROTO_VER
;
2113 ret
= do_req(fd
, NULL
, (SheepdogReq
*)&hdr
,
2114 NULL
, &wlen
, &rlen
);
2117 error_setg_errno(errp
, -ret
, "failed to get cluster default");
2120 if (rsp
->result
== SD_RES_SUCCESS
) {
2121 s
->inode
.block_size_shift
= rsp
->block_size_shift
;
2123 s
->inode
.block_size_shift
= SD_DEFAULT_BLOCK_SIZE_SHIFT
;
2127 max_vdi_size
= (UINT64_C(1) << s
->inode
.block_size_shift
) * MAX_DATA_OBJS
;
2129 if (s
->inode
.vdi_size
> max_vdi_size
) {
2130 error_setg(errp
, "An image is too large."
2131 " The maximum image size is %"PRIu64
"GB",
2132 max_vdi_size
/ 1024 / 1024 / 1024);
2137 ret
= do_sd_create(s
, &vid
, 0, errp
);
2143 ret
= sd_create_prealloc(opts
->location
, opts
->size
, errp
);
2146 g_free(backing_file
);
2153 static int coroutine_fn
sd_co_create_opts(BlockDriver
*drv
,
2154 const char *filename
,
2158 BlockdevCreateOptions
*create_options
= NULL
;
2159 QDict
*qdict
, *location_qdict
;
2162 Error
*local_err
= NULL
;
2165 redundancy
= qemu_opt_get_del(opts
, BLOCK_OPT_REDUNDANCY
);
2167 qdict
= qemu_opts_to_qdict(opts
, NULL
);
2168 qdict_put_str(qdict
, "driver", "sheepdog");
2170 location_qdict
= qdict_new();
2171 qdict_put(qdict
, "location", location_qdict
);
2173 sd_parse_filename(filename
, location_qdict
, &local_err
);
2175 error_propagate(errp
, local_err
);
2180 qdict_flatten(qdict
);
2182 /* Change legacy command line options into QMP ones */
2183 static const QDictRenames opt_renames
[] = {
2184 { BLOCK_OPT_BACKING_FILE
, "backing-file" },
2185 { BLOCK_OPT_OBJECT_SIZE
, "object-size" },
2189 if (!qdict_rename_keys(qdict
, opt_renames
, errp
)) {
2194 /* Get the QAPI object */
2195 v
= qobject_input_visitor_new_flat_confused(qdict
, errp
);
2201 visit_type_BlockdevCreateOptions(v
, NULL
, &create_options
, &local_err
);
2205 error_propagate(errp
, local_err
);
2210 assert(create_options
->driver
== BLOCKDEV_DRIVER_SHEEPDOG
);
2211 create_options
->u
.sheepdog
.size
=
2212 ROUND_UP(create_options
->u
.sheepdog
.size
, BDRV_SECTOR_SIZE
);
2215 create_options
->u
.sheepdog
.has_redundancy
= true;
2216 create_options
->u
.sheepdog
.redundancy
=
2217 parse_redundancy_str(redundancy
);
2218 if (create_options
->u
.sheepdog
.redundancy
== NULL
) {
2219 error_setg(errp
, "Invalid redundancy mode");
2225 ret
= sd_co_create(create_options
, errp
);
2227 qapi_free_BlockdevCreateOptions(create_options
);
2228 qobject_unref(qdict
);
2233 static void sd_close(BlockDriverState
*bs
)
2235 Error
*local_err
= NULL
;
2236 BDRVSheepdogState
*s
= bs
->opaque
;
2238 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
2239 unsigned int wlen
, rlen
= 0;
2242 trace_sheepdog_close(s
->name
);
2244 fd
= connect_to_sdog(s
, &local_err
);
2246 error_report_err(local_err
);
2250 memset(&hdr
, 0, sizeof(hdr
));
2252 hdr
.opcode
= SD_OP_RELEASE_VDI
;
2253 hdr
.type
= LOCK_TYPE_NORMAL
;
2254 hdr
.base_vdi_id
= s
->inode
.vdi_id
;
2255 wlen
= strlen(s
->name
) + 1;
2256 hdr
.data_length
= wlen
;
2257 hdr
.flags
= SD_FLAG_CMD_WRITE
;
2259 ret
= do_req(fd
, s
->bs
, (SheepdogReq
*)&hdr
,
2260 s
->name
, &wlen
, &rlen
);
2264 if (!ret
&& rsp
->result
!= SD_RES_SUCCESS
&&
2265 rsp
->result
!= SD_RES_VDI_NOT_LOCKED
) {
2266 error_report("%s, %s", sd_strerror(rsp
->result
), s
->name
);
2269 aio_set_fd_handler(bdrv_get_aio_context(bs
), s
->fd
,
2270 false, NULL
, NULL
, NULL
, NULL
);
2272 qapi_free_SocketAddress(s
->addr
);
2275 static int64_t sd_getlength(BlockDriverState
*bs
)
2277 BDRVSheepdogState
*s
= bs
->opaque
;
2279 return s
->inode
.vdi_size
;
2282 static int coroutine_fn
sd_co_truncate(BlockDriverState
*bs
, int64_t offset
,
2283 bool exact
, PreallocMode prealloc
,
2284 BdrvRequestFlags flags
, Error
**errp
)
2286 BDRVSheepdogState
*s
= bs
->opaque
;
2288 unsigned int datalen
;
2289 uint64_t max_vdi_size
;
2290 int64_t old_size
= s
->inode
.vdi_size
;
2292 if (prealloc
!= PREALLOC_MODE_OFF
&& prealloc
!= PREALLOC_MODE_FULL
) {
2293 error_setg(errp
, "Unsupported preallocation mode '%s'",
2294 PreallocMode_str(prealloc
));
2298 max_vdi_size
= (UINT64_C(1) << s
->inode
.block_size_shift
) * MAX_DATA_OBJS
;
2299 if (offset
< old_size
) {
2300 error_setg(errp
, "shrinking is not supported");
2302 } else if (offset
> max_vdi_size
) {
2303 error_setg(errp
, "too big image size");
2307 fd
= connect_to_sdog(s
, errp
);
2312 /* we don't need to update entire object */
2313 datalen
= SD_INODE_HEADER_SIZE
;
2314 s
->inode
.vdi_size
= offset
;
2315 ret
= write_object(fd
, s
->bs
, (char *)&s
->inode
,
2316 vid_to_vdi_oid(s
->inode
.vdi_id
), s
->inode
.nr_copies
,
2317 datalen
, 0, false, s
->cache_flags
);
2321 error_setg_errno(errp
, -ret
, "failed to update an inode");
2325 if (prealloc
== PREALLOC_MODE_FULL
) {
2326 ret
= sd_prealloc(bs
, old_size
, offset
, errp
);
2336 * This function is called after writing data objects. If we need to
2337 * update metadata, this sends a write request to the vdi object.
2339 static void coroutine_fn
sd_write_done(SheepdogAIOCB
*acb
)
2341 BDRVSheepdogState
*s
= acb
->s
;
2344 uint32_t offset
, data_len
, mn
, mx
;
2346 mn
= acb
->min_dirty_data_idx
;
2347 mx
= acb
->max_dirty_data_idx
;
2349 /* we need to update the vdi object. */
2351 offset
= sizeof(s
->inode
) - sizeof(s
->inode
.data_vdi_id
) +
2352 mn
* sizeof(s
->inode
.data_vdi_id
[0]);
2353 data_len
= (mx
- mn
+ 1) * sizeof(s
->inode
.data_vdi_id
[0]);
2355 acb
->min_dirty_data_idx
= UINT32_MAX
;
2356 acb
->max_dirty_data_idx
= 0;
2358 iov
.iov_base
= &s
->inode
;
2359 iov
.iov_len
= sizeof(s
->inode
);
2360 aio_req
= alloc_aio_req(s
, acb
, vid_to_vdi_oid(s
->inode
.vdi_id
),
2361 data_len
, offset
, 0, false, 0, offset
);
2362 add_aio_request(s
, aio_req
, &iov
, 1, AIOCB_WRITE_UDATA
);
2363 if (--acb
->nr_pending
) {
2364 qemu_coroutine_yield();
2369 /* Delete current working VDI on the snapshot chain */
2370 static bool sd_delete(BDRVSheepdogState
*s
)
2372 Error
*local_err
= NULL
;
2373 unsigned int wlen
= SD_MAX_VDI_LEN
, rlen
= 0;
2374 SheepdogVdiReq hdr
= {
2375 .opcode
= SD_OP_DEL_VDI
,
2376 .base_vdi_id
= s
->inode
.vdi_id
,
2377 .data_length
= wlen
,
2378 .flags
= SD_FLAG_CMD_WRITE
,
2380 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
2383 fd
= connect_to_sdog(s
, &local_err
);
2385 error_report_err(local_err
);
2389 ret
= do_req(fd
, s
->bs
, (SheepdogReq
*)&hdr
,
2390 s
->name
, &wlen
, &rlen
);
2395 switch (rsp
->result
) {
2397 error_report("%s was already deleted", s
->name
);
2399 case SD_RES_SUCCESS
:
2402 error_report("%s, %s", sd_strerror(rsp
->result
), s
->name
);
2410 * Create a writable VDI from a snapshot
2412 static int sd_create_branch(BDRVSheepdogState
*s
)
2414 Error
*local_err
= NULL
;
2420 trace_sheepdog_create_branch_snapshot(s
->inode
.vdi_id
);
2422 buf
= g_malloc(SD_INODE_SIZE
);
2425 * Even If deletion fails, we will just create extra snapshot based on
2426 * the working VDI which was supposed to be deleted. So no need to
2429 deleted
= sd_delete(s
);
2430 ret
= do_sd_create(s
, &vid
, !deleted
, &local_err
);
2432 error_report_err(local_err
);
2436 trace_sheepdog_create_branch_created(vid
);
2438 fd
= connect_to_sdog(s
, &local_err
);
2440 error_report_err(local_err
);
2445 ret
= read_object(fd
, s
->bs
, buf
, vid_to_vdi_oid(vid
),
2446 s
->inode
.nr_copies
, SD_INODE_SIZE
, 0, s
->cache_flags
);
2454 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
2456 s
->is_snapshot
= false;
2458 trace_sheepdog_create_branch_new(s
->inode
.vdi_id
);
2467 * Send I/O requests to the server.
2469 * This function sends requests to the server, links the requests to
2470 * the inflight_list in BDRVSheepdogState, and exits without
2471 * waiting the response. The responses are received in the
2472 * `aio_read_response' function which is called from the main loop as
2475 * Returns 1 when we need to wait a response, 0 when there is no sent
2476 * request and -errno in error cases.
2478 static void coroutine_fn
sd_co_rw_vector(SheepdogAIOCB
*acb
)
2481 unsigned long len
, done
= 0, total
= acb
->nb_sectors
* BDRV_SECTOR_SIZE
;
2483 uint32_t object_size
;
2486 BDRVSheepdogState
*s
= acb
->s
;
2487 SheepdogInode
*inode
= &s
->inode
;
2490 if (acb
->aiocb_type
== AIOCB_WRITE_UDATA
&& s
->is_snapshot
) {
2492 * In the case we open the snapshot VDI, Sheepdog creates the
2493 * writable VDI when we do a write operation first.
2495 ret
= sd_create_branch(s
);
2502 object_size
= (UINT32_C(1) << inode
->block_size_shift
);
2503 idx
= acb
->sector_num
* BDRV_SECTOR_SIZE
/ object_size
;
2504 offset
= (acb
->sector_num
* BDRV_SECTOR_SIZE
) % object_size
;
2507 * Make sure we don't free the aiocb before we are done with all requests.
2508 * This additional reference is dropped at the end of this function.
2512 while (done
!= total
) {
2514 uint64_t old_oid
= 0;
2515 bool create
= false;
2517 oid
= vid_to_data_oid(inode
->data_vdi_id
[idx
], idx
);
2519 len
= MIN(total
- done
, object_size
- offset
);
2521 switch (acb
->aiocb_type
) {
2522 case AIOCB_READ_UDATA
:
2523 if (!inode
->data_vdi_id
[idx
]) {
2524 qemu_iovec_memset(acb
->qiov
, done
, 0, len
);
2528 case AIOCB_WRITE_UDATA
:
2529 if (!inode
->data_vdi_id
[idx
]) {
2531 } else if (!is_data_obj_writable(inode
, idx
)) {
2535 flags
= SD_FLAG_CMD_COW
;
2538 case AIOCB_DISCARD_OBJ
:
2540 * We discard the object only when the whole object is
2541 * 1) allocated 2) trimmed. Otherwise, simply skip it.
2543 if (len
!= object_size
|| inode
->data_vdi_id
[idx
] == 0) {
2552 trace_sheepdog_co_rw_vector_update(inode
->vdi_id
, oid
,
2553 vid_to_data_oid(inode
->data_vdi_id
[idx
], idx
),
2555 oid
= vid_to_data_oid(inode
->vdi_id
, idx
);
2556 trace_sheepdog_co_rw_vector_new(oid
);
2559 aio_req
= alloc_aio_req(s
, acb
, oid
, len
, offset
, flags
, create
,
2561 acb
->aiocb_type
== AIOCB_DISCARD_OBJ
?
2563 add_aio_request(s
, aio_req
, acb
->qiov
->iov
, acb
->qiov
->niov
,
2570 if (--acb
->nr_pending
) {
2571 qemu_coroutine_yield();
2575 static void sd_aio_complete(SheepdogAIOCB
*acb
)
2577 BDRVSheepdogState
*s
;
2578 if (acb
->aiocb_type
== AIOCB_FLUSH_CACHE
) {
2583 qemu_co_mutex_lock(&s
->queue_lock
);
2584 QLIST_REMOVE(acb
, aiocb_siblings
);
2585 qemu_co_queue_restart_all(&s
->overlapping_queue
);
2586 qemu_co_mutex_unlock(&s
->queue_lock
);
2589 static coroutine_fn
int sd_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
2590 int nb_sectors
, QEMUIOVector
*qiov
,
2595 int64_t offset
= (sector_num
+ nb_sectors
) * BDRV_SECTOR_SIZE
;
2596 BDRVSheepdogState
*s
= bs
->opaque
;
2599 if (offset
> s
->inode
.vdi_size
) {
2600 ret
= sd_co_truncate(bs
, offset
, false, PREALLOC_MODE_OFF
, 0, NULL
);
2606 sd_aio_setup(&acb
, s
, qiov
, sector_num
, nb_sectors
, AIOCB_WRITE_UDATA
);
2607 sd_co_rw_vector(&acb
);
2608 sd_write_done(&acb
);
2609 sd_aio_complete(&acb
);
2614 static coroutine_fn
int sd_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
2615 int nb_sectors
, QEMUIOVector
*qiov
)
2618 BDRVSheepdogState
*s
= bs
->opaque
;
2620 sd_aio_setup(&acb
, s
, qiov
, sector_num
, nb_sectors
, AIOCB_READ_UDATA
);
2621 sd_co_rw_vector(&acb
);
2622 sd_aio_complete(&acb
);
2627 static int coroutine_fn
sd_co_flush_to_disk(BlockDriverState
*bs
)
2629 BDRVSheepdogState
*s
= bs
->opaque
;
2633 if (s
->cache_flags
!= SD_FLAG_CMD_CACHE
) {
2637 sd_aio_setup(&acb
, s
, NULL
, 0, 0, AIOCB_FLUSH_CACHE
);
2640 aio_req
= alloc_aio_req(s
, &acb
, vid_to_vdi_oid(s
->inode
.vdi_id
),
2641 0, 0, 0, false, 0, 0);
2642 add_aio_request(s
, aio_req
, NULL
, 0, acb
.aiocb_type
);
2644 if (--acb
.nr_pending
) {
2645 qemu_coroutine_yield();
2648 sd_aio_complete(&acb
);
2652 static int sd_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
)
2654 Error
*local_err
= NULL
;
2655 BDRVSheepdogState
*s
= bs
->opaque
;
2658 SheepdogInode
*inode
;
2659 unsigned int datalen
;
2661 trace_sheepdog_snapshot_create_info(sn_info
->name
, sn_info
->id_str
, s
->name
,
2662 sn_info
->vm_state_size
, s
->is_snapshot
);
2664 if (s
->is_snapshot
) {
2665 error_report("You can't create a snapshot of a snapshot VDI, "
2666 "%s (%" PRIu32
").", s
->name
, s
->inode
.vdi_id
);
2671 trace_sheepdog_snapshot_create(sn_info
->name
, sn_info
->id_str
);
2673 s
->inode
.vm_state_size
= sn_info
->vm_state_size
;
2674 s
->inode
.vm_clock_nsec
= sn_info
->vm_clock_nsec
;
2675 /* It appears that inode.tag does not require a NUL terminator,
2676 * which means this use of strncpy is ok.
2678 strncpy(s
->inode
.tag
, sn_info
->name
, sizeof(s
->inode
.tag
));
2679 /* we don't need to update entire object */
2680 datalen
= SD_INODE_HEADER_SIZE
;
2681 inode
= g_malloc(datalen
);
2683 /* refresh inode. */
2684 fd
= connect_to_sdog(s
, &local_err
);
2686 error_report_err(local_err
);
2691 ret
= write_object(fd
, s
->bs
, (char *)&s
->inode
,
2692 vid_to_vdi_oid(s
->inode
.vdi_id
), s
->inode
.nr_copies
,
2693 datalen
, 0, false, s
->cache_flags
);
2695 error_report("failed to write snapshot's inode.");
2699 ret
= do_sd_create(s
, &new_vid
, 1, &local_err
);
2701 error_reportf_err(local_err
,
2702 "failed to create inode for snapshot: ");
2706 ret
= read_object(fd
, s
->bs
, (char *)inode
,
2707 vid_to_vdi_oid(new_vid
), s
->inode
.nr_copies
, datalen
, 0,
2711 error_report("failed to read new inode info. %s", strerror(errno
));
2715 memcpy(&s
->inode
, inode
, datalen
);
2716 trace_sheepdog_snapshot_create_inode(s
->inode
.name
, s
->inode
.snap_id
,
2726 * We implement rollback(loadvm) operation to the specified snapshot by
2727 * 1) switch to the snapshot
2728 * 2) rely on sd_create_branch to delete working VDI and
2729 * 3) create a new working VDI based on the specified snapshot
2731 static int sd_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
)
2733 BDRVSheepdogState
*s
= bs
->opaque
;
2734 BDRVSheepdogState
*old_s
;
2735 char tag
[SD_MAX_VDI_TAG_LEN
];
2736 uint32_t snapid
= 0;
2739 if (!sd_parse_snapid_or_tag(snapshot_id
, &snapid
, tag
)) {
2743 old_s
= g_new(BDRVSheepdogState
, 1);
2745 memcpy(old_s
, s
, sizeof(BDRVSheepdogState
));
2747 ret
= reload_inode(s
, snapid
, tag
);
2752 ret
= sd_create_branch(s
);
2761 /* recover bdrv_sd_state */
2762 memcpy(s
, old_s
, sizeof(BDRVSheepdogState
));
2765 error_report("failed to open. recover old bdrv_sd_state.");
2770 #define NR_BATCHED_DISCARD 128
2772 static int remove_objects(BDRVSheepdogState
*s
, Error
**errp
)
2774 int fd
, i
= 0, nr_objs
= 0;
2776 SheepdogInode
*inode
= &s
->inode
;
2778 fd
= connect_to_sdog(s
, errp
);
2783 nr_objs
= count_data_objs(inode
);
2784 while (i
< nr_objs
) {
2785 int start_idx
, nr_filled_idx
;
2787 while (i
< nr_objs
&& !inode
->data_vdi_id
[i
]) {
2793 while (i
< nr_objs
&& nr_filled_idx
< NR_BATCHED_DISCARD
) {
2794 if (inode
->data_vdi_id
[i
]) {
2795 inode
->data_vdi_id
[i
] = 0;
2802 ret
= write_object(fd
, s
->bs
,
2803 (char *)&inode
->data_vdi_id
[start_idx
],
2804 vid_to_vdi_oid(s
->inode
.vdi_id
), inode
->nr_copies
,
2805 (i
- start_idx
) * sizeof(uint32_t),
2806 offsetof(struct SheepdogInode
,
2807 data_vdi_id
[start_idx
]),
2808 false, s
->cache_flags
);
2810 error_setg(errp
, "Failed to discard snapshot inode");
2821 static int sd_snapshot_delete(BlockDriverState
*bs
,
2822 const char *snapshot_id
,
2827 * FIXME should delete the snapshot matching both @snapshot_id and
2828 * @name, but @name not used here
2830 unsigned long snap_id
= 0;
2831 char snap_tag
[SD_MAX_VDI_TAG_LEN
];
2833 char buf
[SD_MAX_VDI_LEN
+ SD_MAX_VDI_TAG_LEN
];
2834 BDRVSheepdogState
*s
= bs
->opaque
;
2835 unsigned int wlen
= SD_MAX_VDI_LEN
+ SD_MAX_VDI_TAG_LEN
, rlen
= 0;
2837 SheepdogVdiReq hdr
= {
2838 .opcode
= SD_OP_DEL_VDI
,
2839 .data_length
= wlen
,
2840 .flags
= SD_FLAG_CMD_WRITE
,
2842 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
2844 ret
= remove_objects(s
, errp
);
2849 memset(buf
, 0, sizeof(buf
));
2850 memset(snap_tag
, 0, sizeof(snap_tag
));
2851 pstrcpy(buf
, SD_MAX_VDI_LEN
, s
->name
);
2852 /* TODO Use sd_parse_snapid() once this mess is cleaned up */
2853 ret
= qemu_strtoul(snapshot_id
, NULL
, 10, &snap_id
);
2854 if (ret
|| snap_id
> UINT32_MAX
) {
2856 * FIXME Since qemu_strtoul() returns -EINVAL when
2857 * @snapshot_id is null, @snapshot_id is mandatory. Correct
2858 * would be to require at least one of @snapshot_id and @name.
2860 error_setg(errp
, "Invalid snapshot ID: %s",
2861 snapshot_id
? snapshot_id
: "<null>");
2866 hdr
.snapid
= (uint32_t) snap_id
;
2868 /* FIXME I suspect we should use @name here */
2869 /* FIXME don't truncate silently */
2870 pstrcpy(snap_tag
, sizeof(snap_tag
), snapshot_id
);
2871 pstrcpy(buf
+ SD_MAX_VDI_LEN
, SD_MAX_VDI_TAG_LEN
, snap_tag
);
2874 ret
= find_vdi_name(s
, s
->name
, snap_id
, snap_tag
, &vid
, true, errp
);
2879 fd
= connect_to_sdog(s
, errp
);
2884 ret
= do_req(fd
, s
->bs
, (SheepdogReq
*)&hdr
,
2888 error_setg_errno(errp
, -ret
, "Couldn't send request to server");
2892 switch (rsp
->result
) {
2894 error_setg(errp
, "Can't find the snapshot");
2896 case SD_RES_SUCCESS
:
2899 error_setg(errp
, "%s", sd_strerror(rsp
->result
));
2906 static int sd_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
)
2908 Error
*local_err
= NULL
;
2909 BDRVSheepdogState
*s
= bs
->opaque
;
2911 int fd
, nr
= 1024, ret
, max
= BITS_TO_LONGS(SD_NR_VDIS
) * sizeof(long);
2912 QEMUSnapshotInfo
*sn_tab
= NULL
;
2913 unsigned wlen
, rlen
;
2915 SheepdogInode
*inode
;
2916 unsigned long *vdi_inuse
;
2917 unsigned int start_nr
;
2921 vdi_inuse
= g_malloc(max
);
2922 inode
= g_malloc(SD_INODE_HEADER_SIZE
);
2924 fd
= connect_to_sdog(s
, &local_err
);
2926 error_report_err(local_err
);
2934 memset(&req
, 0, sizeof(req
));
2936 req
.opcode
= SD_OP_READ_VDIS
;
2937 req
.data_length
= max
;
2939 ret
= do_req(fd
, s
->bs
, &req
, vdi_inuse
, &wlen
, &rlen
);
2946 sn_tab
= g_new0(QEMUSnapshotInfo
, nr
);
2948 /* calculate a vdi id with hash function */
2949 hval
= fnv_64a_buf(s
->name
, strlen(s
->name
), FNV1A_64_INIT
);
2950 start_nr
= hval
& (SD_NR_VDIS
- 1);
2952 fd
= connect_to_sdog(s
, &local_err
);
2954 error_report_err(local_err
);
2959 for (vid
= start_nr
; found
< nr
; vid
= (vid
+ 1) % SD_NR_VDIS
) {
2960 if (!test_bit(vid
, vdi_inuse
)) {
2964 /* we don't need to read entire object */
2965 ret
= read_object(fd
, s
->bs
, (char *)inode
,
2966 vid_to_vdi_oid(vid
),
2967 0, SD_INODE_HEADER_SIZE
, 0,
2974 if (!strcmp(inode
->name
, s
->name
) && is_snapshot(inode
)) {
2975 sn_tab
[found
].date_sec
= inode
->snap_ctime
>> 32;
2976 sn_tab
[found
].date_nsec
= inode
->snap_ctime
& 0xffffffff;
2977 sn_tab
[found
].vm_state_size
= inode
->vm_state_size
;
2978 sn_tab
[found
].vm_clock_nsec
= inode
->vm_clock_nsec
;
2980 snprintf(sn_tab
[found
].id_str
, sizeof(sn_tab
[found
].id_str
),
2981 "%" PRIu32
, inode
->snap_id
);
2982 pstrcpy(sn_tab
[found
].name
,
2983 MIN(sizeof(sn_tab
[found
].name
), sizeof(inode
->tag
)),
3003 static int do_load_save_vmstate(BDRVSheepdogState
*s
, uint8_t *data
,
3004 int64_t pos
, int size
, int load
)
3006 Error
*local_err
= NULL
;
3008 int fd
, ret
= 0, remaining
= size
;
3009 unsigned int data_len
;
3010 uint64_t vmstate_oid
;
3013 uint32_t vdi_id
= load
? s
->inode
.parent_vdi_id
: s
->inode
.vdi_id
;
3014 uint32_t object_size
= (UINT32_C(1) << s
->inode
.block_size_shift
);
3016 fd
= connect_to_sdog(s
, &local_err
);
3018 error_report_err(local_err
);
3023 vdi_index
= pos
/ object_size
;
3024 offset
= pos
% object_size
;
3026 data_len
= MIN(remaining
, object_size
- offset
);
3028 vmstate_oid
= vid_to_vmstate_oid(vdi_id
, vdi_index
);
3030 create
= (offset
== 0);
3032 ret
= read_object(fd
, s
->bs
, (char *)data
, vmstate_oid
,
3033 s
->inode
.nr_copies
, data_len
, offset
,
3036 ret
= write_object(fd
, s
->bs
, (char *)data
, vmstate_oid
,
3037 s
->inode
.nr_copies
, data_len
, offset
, create
,
3042 error_report("failed to save vmstate %s", strerror(errno
));
3048 remaining
-= data_len
;
3056 static int sd_save_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
3059 BDRVSheepdogState
*s
= bs
->opaque
;
3063 buf
= qemu_blockalign(bs
, qiov
->size
);
3064 qemu_iovec_to_buf(qiov
, 0, buf
, qiov
->size
);
3065 ret
= do_load_save_vmstate(s
, (uint8_t *) buf
, pos
, qiov
->size
, 0);
3071 static int sd_load_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
3074 BDRVSheepdogState
*s
= bs
->opaque
;
3078 buf
= qemu_blockalign(bs
, qiov
->size
);
3079 ret
= do_load_save_vmstate(s
, buf
, pos
, qiov
->size
, 1);
3080 qemu_iovec_from_buf(qiov
, 0, buf
, qiov
->size
);
3087 static coroutine_fn
int sd_co_pdiscard(BlockDriverState
*bs
, int64_t offset
,
3091 BDRVSheepdogState
*s
= bs
->opaque
;
3092 QEMUIOVector discard_iov
;
3096 if (!s
->discard_supported
) {
3100 memset(&discard_iov
, 0, sizeof(discard_iov
));
3101 memset(&iov
, 0, sizeof(iov
));
3102 iov
.iov_base
= &zero
;
3103 iov
.iov_len
= sizeof(zero
);
3104 discard_iov
.iov
= &iov
;
3105 discard_iov
.niov
= 1;
3106 if (!QEMU_IS_ALIGNED(offset
| bytes
, BDRV_SECTOR_SIZE
)) {
3109 sd_aio_setup(&acb
, s
, &discard_iov
, offset
>> BDRV_SECTOR_BITS
,
3110 bytes
>> BDRV_SECTOR_BITS
, AIOCB_DISCARD_OBJ
);
3111 sd_co_rw_vector(&acb
);
3112 sd_aio_complete(&acb
);
3117 static coroutine_fn
int
3118 sd_co_block_status(BlockDriverState
*bs
, bool want_zero
, int64_t offset
,
3119 int64_t bytes
, int64_t *pnum
, int64_t *map
,
3120 BlockDriverState
**file
)
3122 BDRVSheepdogState
*s
= bs
->opaque
;
3123 SheepdogInode
*inode
= &s
->inode
;
3124 uint32_t object_size
= (UINT32_C(1) << inode
->block_size_shift
);
3125 unsigned long start
= offset
/ object_size
,
3126 end
= DIV_ROUND_UP(offset
+ bytes
, object_size
);
3129 int ret
= BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
;
3131 for (idx
= start
; idx
< end
; idx
++) {
3132 if (inode
->data_vdi_id
[idx
] == 0) {
3137 /* Get the longest length of unallocated sectors */
3139 for (idx
= start
+ 1; idx
< end
; idx
++) {
3140 if (inode
->data_vdi_id
[idx
] != 0) {
3146 *pnum
= (idx
- start
) * object_size
;
3147 if (*pnum
> bytes
) {
3150 if (ret
> 0 && ret
& BDRV_BLOCK_OFFSET_VALID
) {
3156 static int64_t sd_get_allocated_file_size(BlockDriverState
*bs
)
3158 BDRVSheepdogState
*s
= bs
->opaque
;
3159 SheepdogInode
*inode
= &s
->inode
;
3160 uint32_t object_size
= (UINT32_C(1) << inode
->block_size_shift
);
3161 unsigned long i
, last
= DIV_ROUND_UP(inode
->vdi_size
, object_size
);
3164 for (i
= 0; i
< last
; i
++) {
3165 if (inode
->data_vdi_id
[i
] == 0) {
3168 size
+= object_size
;
3173 static QemuOptsList sd_create_opts
= {
3174 .name
= "sheepdog-create-opts",
3175 .head
= QTAILQ_HEAD_INITIALIZER(sd_create_opts
.head
),
3178 .name
= BLOCK_OPT_SIZE
,
3179 .type
= QEMU_OPT_SIZE
,
3180 .help
= "Virtual disk size"
3183 .name
= BLOCK_OPT_BACKING_FILE
,
3184 .type
= QEMU_OPT_STRING
,
3185 .help
= "File name of a base image"
3188 .name
= BLOCK_OPT_PREALLOC
,
3189 .type
= QEMU_OPT_STRING
,
3190 .help
= "Preallocation mode (allowed values: off, full)"
3193 .name
= BLOCK_OPT_REDUNDANCY
,
3194 .type
= QEMU_OPT_STRING
,
3195 .help
= "Redundancy of the image"
3198 .name
= BLOCK_OPT_OBJECT_SIZE
,
3199 .type
= QEMU_OPT_SIZE
,
3200 .help
= "Object size of the image"
3202 { /* end of list */ }
3206 static const char *const sd_strong_runtime_opts
[] = {
3215 static BlockDriver bdrv_sheepdog
= {
3216 .format_name
= "sheepdog",
3217 .protocol_name
= "sheepdog",
3218 .instance_size
= sizeof(BDRVSheepdogState
),
3219 .bdrv_parse_filename
= sd_parse_filename
,
3220 .bdrv_file_open
= sd_open
,
3221 .bdrv_reopen_prepare
= sd_reopen_prepare
,
3222 .bdrv_reopen_commit
= sd_reopen_commit
,
3223 .bdrv_reopen_abort
= sd_reopen_abort
,
3224 .bdrv_close
= sd_close
,
3225 .bdrv_co_create
= sd_co_create
,
3226 .bdrv_co_create_opts
= sd_co_create_opts
,
3227 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
3228 .bdrv_has_zero_init_truncate
= bdrv_has_zero_init_1
,
3229 .bdrv_getlength
= sd_getlength
,
3230 .bdrv_get_allocated_file_size
= sd_get_allocated_file_size
,
3231 .bdrv_co_truncate
= sd_co_truncate
,
3233 .bdrv_co_readv
= sd_co_readv
,
3234 .bdrv_co_writev
= sd_co_writev
,
3235 .bdrv_co_flush_to_disk
= sd_co_flush_to_disk
,
3236 .bdrv_co_pdiscard
= sd_co_pdiscard
,
3237 .bdrv_co_block_status
= sd_co_block_status
,
3239 .bdrv_snapshot_create
= sd_snapshot_create
,
3240 .bdrv_snapshot_goto
= sd_snapshot_goto
,
3241 .bdrv_snapshot_delete
= sd_snapshot_delete
,
3242 .bdrv_snapshot_list
= sd_snapshot_list
,
3244 .bdrv_save_vmstate
= sd_save_vmstate
,
3245 .bdrv_load_vmstate
= sd_load_vmstate
,
3247 .bdrv_detach_aio_context
= sd_detach_aio_context
,
3248 .bdrv_attach_aio_context
= sd_attach_aio_context
,
3250 .create_opts
= &sd_create_opts
,
3251 .strong_runtime_opts
= sd_strong_runtime_opts
,
3254 static BlockDriver bdrv_sheepdog_tcp
= {
3255 .format_name
= "sheepdog",
3256 .protocol_name
= "sheepdog+tcp",
3257 .instance_size
= sizeof(BDRVSheepdogState
),
3258 .bdrv_parse_filename
= sd_parse_filename
,
3259 .bdrv_file_open
= sd_open
,
3260 .bdrv_reopen_prepare
= sd_reopen_prepare
,
3261 .bdrv_reopen_commit
= sd_reopen_commit
,
3262 .bdrv_reopen_abort
= sd_reopen_abort
,
3263 .bdrv_close
= sd_close
,
3264 .bdrv_co_create
= sd_co_create
,
3265 .bdrv_co_create_opts
= sd_co_create_opts
,
3266 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
3267 .bdrv_has_zero_init_truncate
= bdrv_has_zero_init_1
,
3268 .bdrv_getlength
= sd_getlength
,
3269 .bdrv_get_allocated_file_size
= sd_get_allocated_file_size
,
3270 .bdrv_co_truncate
= sd_co_truncate
,
3272 .bdrv_co_readv
= sd_co_readv
,
3273 .bdrv_co_writev
= sd_co_writev
,
3274 .bdrv_co_flush_to_disk
= sd_co_flush_to_disk
,
3275 .bdrv_co_pdiscard
= sd_co_pdiscard
,
3276 .bdrv_co_block_status
= sd_co_block_status
,
3278 .bdrv_snapshot_create
= sd_snapshot_create
,
3279 .bdrv_snapshot_goto
= sd_snapshot_goto
,
3280 .bdrv_snapshot_delete
= sd_snapshot_delete
,
3281 .bdrv_snapshot_list
= sd_snapshot_list
,
3283 .bdrv_save_vmstate
= sd_save_vmstate
,
3284 .bdrv_load_vmstate
= sd_load_vmstate
,
3286 .bdrv_detach_aio_context
= sd_detach_aio_context
,
3287 .bdrv_attach_aio_context
= sd_attach_aio_context
,
3289 .create_opts
= &sd_create_opts
,
3290 .strong_runtime_opts
= sd_strong_runtime_opts
,
3293 static BlockDriver bdrv_sheepdog_unix
= {
3294 .format_name
= "sheepdog",
3295 .protocol_name
= "sheepdog+unix",
3296 .instance_size
= sizeof(BDRVSheepdogState
),
3297 .bdrv_parse_filename
= sd_parse_filename
,
3298 .bdrv_file_open
= sd_open
,
3299 .bdrv_reopen_prepare
= sd_reopen_prepare
,
3300 .bdrv_reopen_commit
= sd_reopen_commit
,
3301 .bdrv_reopen_abort
= sd_reopen_abort
,
3302 .bdrv_close
= sd_close
,
3303 .bdrv_co_create
= sd_co_create
,
3304 .bdrv_co_create_opts
= sd_co_create_opts
,
3305 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
3306 .bdrv_has_zero_init_truncate
= 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
);