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-common.h"
17 #include "qemu/error-report.h"
18 #include "qemu/sockets.h"
19 #include "block/block_int.h"
20 #include "qemu/bitops.h"
22 #define SD_PROTO_VER 0x01
24 #define SD_DEFAULT_ADDR "localhost"
25 #define SD_DEFAULT_PORT 7000
27 #define SD_OP_CREATE_AND_WRITE_OBJ 0x01
28 #define SD_OP_READ_OBJ 0x02
29 #define SD_OP_WRITE_OBJ 0x03
30 /* 0x04 is used internally by Sheepdog */
31 #define SD_OP_DISCARD_OBJ 0x05
33 #define SD_OP_NEW_VDI 0x11
34 #define SD_OP_LOCK_VDI 0x12
35 #define SD_OP_RELEASE_VDI 0x13
36 #define SD_OP_GET_VDI_INFO 0x14
37 #define SD_OP_READ_VDIS 0x15
38 #define SD_OP_FLUSH_VDI 0x16
39 #define SD_OP_DEL_VDI 0x17
40 #define SD_OP_GET_CLUSTER_DEFAULT 0x18
42 #define SD_FLAG_CMD_WRITE 0x01
43 #define SD_FLAG_CMD_COW 0x02
44 #define SD_FLAG_CMD_CACHE 0x04 /* Writeback mode for cache */
45 #define SD_FLAG_CMD_DIRECT 0x08 /* Don't use cache */
47 #define SD_RES_SUCCESS 0x00 /* Success */
48 #define SD_RES_UNKNOWN 0x01 /* Unknown error */
49 #define SD_RES_NO_OBJ 0x02 /* No object found */
50 #define SD_RES_EIO 0x03 /* I/O error */
51 #define SD_RES_VDI_EXIST 0x04 /* Vdi exists already */
52 #define SD_RES_INVALID_PARMS 0x05 /* Invalid parameters */
53 #define SD_RES_SYSTEM_ERROR 0x06 /* System error */
54 #define SD_RES_VDI_LOCKED 0x07 /* Vdi is locked */
55 #define SD_RES_NO_VDI 0x08 /* No vdi found */
56 #define SD_RES_NO_BASE_VDI 0x09 /* No base vdi found */
57 #define SD_RES_VDI_READ 0x0A /* Cannot read requested vdi */
58 #define SD_RES_VDI_WRITE 0x0B /* Cannot write requested vdi */
59 #define SD_RES_BASE_VDI_READ 0x0C /* Cannot read base vdi */
60 #define SD_RES_BASE_VDI_WRITE 0x0D /* Cannot write base vdi */
61 #define SD_RES_NO_TAG 0x0E /* Requested tag is not found */
62 #define SD_RES_STARTUP 0x0F /* Sheepdog is on starting up */
63 #define SD_RES_VDI_NOT_LOCKED 0x10 /* Vdi is not locked */
64 #define SD_RES_SHUTDOWN 0x11 /* Sheepdog is shutting down */
65 #define SD_RES_NO_MEM 0x12 /* Cannot allocate memory */
66 #define SD_RES_FULL_VDI 0x13 /* we already have the maximum vdis */
67 #define SD_RES_VER_MISMATCH 0x14 /* Protocol version mismatch */
68 #define SD_RES_NO_SPACE 0x15 /* Server has no room for new objects */
69 #define SD_RES_WAIT_FOR_FORMAT 0x16 /* Waiting for a format operation */
70 #define SD_RES_WAIT_FOR_JOIN 0x17 /* Waiting for other nodes joining */
71 #define SD_RES_JOIN_FAILED 0x18 /* Target node had failed to join sheepdog */
72 #define SD_RES_HALT 0x19 /* Sheepdog is stopped serving IO request */
73 #define SD_RES_READONLY 0x1A /* Object is read-only */
78 * 0 - 19 (20 bits): data object space
79 * 20 - 31 (12 bits): reserved data object space
80 * 32 - 55 (24 bits): vdi object space
81 * 56 - 59 ( 4 bits): reserved vdi object space
82 * 60 - 63 ( 4 bits): object type identifier space
85 #define VDI_SPACE_SHIFT 32
86 #define VDI_BIT (UINT64_C(1) << 63)
87 #define VMSTATE_BIT (UINT64_C(1) << 62)
88 #define MAX_DATA_OBJS (UINT64_C(1) << 20)
89 #define MAX_CHILDREN 1024
90 #define SD_MAX_VDI_LEN 256
91 #define SD_MAX_VDI_TAG_LEN 256
92 #define SD_NR_VDIS (1U << 24)
93 #define SD_DATA_OBJ_SIZE (UINT64_C(1) << 22)
94 #define SD_MAX_VDI_SIZE (SD_DATA_OBJ_SIZE * MAX_DATA_OBJS)
95 #define SD_DEFAULT_BLOCK_SIZE_SHIFT 22
97 * For erasure coding, we use at most SD_EC_MAX_STRIP for data strips and
98 * (SD_EC_MAX_STRIP - 1) for parity strips
100 * SD_MAX_COPIES is sum of number of data strips and parity strips.
102 #define SD_EC_MAX_STRIP 16
103 #define SD_MAX_COPIES (SD_EC_MAX_STRIP * 2 - 1)
105 #define SD_INODE_SIZE (sizeof(SheepdogInode))
106 #define CURRENT_VDI_ID 0
108 #define LOCK_TYPE_NORMAL 0
109 #define LOCK_TYPE_SHARED 1 /* for iSCSI multipath */
111 typedef struct SheepdogReq
{
117 uint32_t data_length
;
118 uint32_t opcode_specific
[8];
121 typedef struct SheepdogRsp
{
127 uint32_t data_length
;
129 uint32_t opcode_specific
[7];
132 typedef struct SheepdogObjReq
{
138 uint32_t data_length
;
147 typedef struct SheepdogObjRsp
{
153 uint32_t data_length
;
161 typedef struct SheepdogVdiReq
{
167 uint32_t data_length
;
169 uint32_t base_vdi_id
;
172 uint8_t store_policy
;
173 uint8_t block_size_shift
;
179 typedef struct SheepdogVdiRsp
{
185 uint32_t data_length
;
192 typedef struct SheepdogClusterRsp
{
198 uint32_t data_length
;
202 uint8_t block_size_shift
;
205 } SheepdogClusterRsp
;
207 typedef struct SheepdogInode
{
208 char name
[SD_MAX_VDI_LEN
];
209 char tag
[SD_MAX_VDI_TAG_LEN
];
212 uint64_t vm_clock_nsec
;
214 uint64_t vm_state_size
;
215 uint16_t copy_policy
;
217 uint8_t block_size_shift
;
220 uint32_t parent_vdi_id
;
221 uint32_t child_vdi_id
[MAX_CHILDREN
];
222 uint32_t data_vdi_id
[MAX_DATA_OBJS
];
225 #define SD_INODE_HEADER_SIZE offsetof(SheepdogInode, data_vdi_id)
228 * 64 bit FNV-1a non-zero initial basis
230 #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
233 * 64 bit Fowler/Noll/Vo FNV-1a hash code
235 static inline uint64_t fnv_64a_buf(void *buf
, size_t len
, uint64_t hval
)
237 unsigned char *bp
= buf
;
238 unsigned char *be
= bp
+ len
;
240 hval
^= (uint64_t) *bp
++;
241 hval
+= (hval
<< 1) + (hval
<< 4) + (hval
<< 5) +
242 (hval
<< 7) + (hval
<< 8) + (hval
<< 40);
247 static inline bool is_data_obj_writable(SheepdogInode
*inode
, unsigned int idx
)
249 return inode
->vdi_id
== inode
->data_vdi_id
[idx
];
252 static inline bool is_data_obj(uint64_t oid
)
254 return !(VDI_BIT
& oid
);
257 static inline uint64_t data_oid_to_idx(uint64_t oid
)
259 return oid
& (MAX_DATA_OBJS
- 1);
262 static inline uint32_t oid_to_vid(uint64_t oid
)
264 return (oid
& ~VDI_BIT
) >> VDI_SPACE_SHIFT
;
267 static inline uint64_t vid_to_vdi_oid(uint32_t vid
)
269 return VDI_BIT
| ((uint64_t)vid
<< VDI_SPACE_SHIFT
);
272 static inline uint64_t vid_to_vmstate_oid(uint32_t vid
, uint32_t idx
)
274 return VMSTATE_BIT
| ((uint64_t)vid
<< VDI_SPACE_SHIFT
) | idx
;
277 static inline uint64_t vid_to_data_oid(uint32_t vid
, uint32_t idx
)
279 return ((uint64_t)vid
<< VDI_SPACE_SHIFT
) | idx
;
282 static inline bool is_snapshot(struct SheepdogInode
*inode
)
284 return !!inode
->snap_ctime
;
289 #define DPRINTF(fmt, args...) \
291 fprintf(stdout, "%s %d: " fmt, __func__, __LINE__, ##args); \
294 #define DPRINTF(fmt, args...)
297 typedef struct SheepdogAIOCB SheepdogAIOCB
;
299 typedef struct AIOReq
{
300 SheepdogAIOCB
*aiocb
;
301 unsigned int iov_offset
;
306 unsigned int data_len
;
311 QLIST_ENTRY(AIOReq
) aio_siblings
;
321 #define AIOCBOverwrapping(x, y) \
322 (!(x->max_affect_data_idx < y->min_affect_data_idx \
323 || y->max_affect_data_idx < x->min_affect_data_idx))
325 struct SheepdogAIOCB
{
334 enum AIOCBState aiocb_type
;
336 Coroutine
*coroutine
;
337 void (*aio_done_func
)(SheepdogAIOCB
*);
342 uint32_t min_affect_data_idx
;
343 uint32_t max_affect_data_idx
;
345 QLIST_ENTRY(SheepdogAIOCB
) aiocb_siblings
;
348 typedef struct BDRVSheepdogState
{
349 BlockDriverState
*bs
;
350 AioContext
*aio_context
;
354 uint32_t min_dirty_data_idx
;
355 uint32_t max_dirty_data_idx
;
357 char name
[SD_MAX_VDI_LEN
];
359 uint32_t cache_flags
;
360 bool discard_supported
;
370 uint32_t aioreq_seq_num
;
372 /* Every aio request must be linked to either of these queues. */
373 QLIST_HEAD(inflight_aio_head
, AIOReq
) inflight_aio_head
;
374 QLIST_HEAD(failed_aio_head
, AIOReq
) failed_aio_head
;
376 CoQueue overwrapping_queue
;
377 QLIST_HEAD(inflight_aiocb_head
, SheepdogAIOCB
) inflight_aiocb_head
;
380 static const char * sd_strerror(int err
)
384 static const struct {
388 {SD_RES_SUCCESS
, "Success"},
389 {SD_RES_UNKNOWN
, "Unknown error"},
390 {SD_RES_NO_OBJ
, "No object found"},
391 {SD_RES_EIO
, "I/O error"},
392 {SD_RES_VDI_EXIST
, "VDI exists already"},
393 {SD_RES_INVALID_PARMS
, "Invalid parameters"},
394 {SD_RES_SYSTEM_ERROR
, "System error"},
395 {SD_RES_VDI_LOCKED
, "VDI is already locked"},
396 {SD_RES_NO_VDI
, "No vdi found"},
397 {SD_RES_NO_BASE_VDI
, "No base VDI found"},
398 {SD_RES_VDI_READ
, "Failed read the requested VDI"},
399 {SD_RES_VDI_WRITE
, "Failed to write the requested VDI"},
400 {SD_RES_BASE_VDI_READ
, "Failed to read the base VDI"},
401 {SD_RES_BASE_VDI_WRITE
, "Failed to write the base VDI"},
402 {SD_RES_NO_TAG
, "Failed to find the requested tag"},
403 {SD_RES_STARTUP
, "The system is still booting"},
404 {SD_RES_VDI_NOT_LOCKED
, "VDI isn't locked"},
405 {SD_RES_SHUTDOWN
, "The system is shutting down"},
406 {SD_RES_NO_MEM
, "Out of memory on the server"},
407 {SD_RES_FULL_VDI
, "We already have the maximum vdis"},
408 {SD_RES_VER_MISMATCH
, "Protocol version mismatch"},
409 {SD_RES_NO_SPACE
, "Server has no space for new objects"},
410 {SD_RES_WAIT_FOR_FORMAT
, "Sheepdog is waiting for a format operation"},
411 {SD_RES_WAIT_FOR_JOIN
, "Sheepdog is waiting for other nodes joining"},
412 {SD_RES_JOIN_FAILED
, "Target node had failed to join sheepdog"},
413 {SD_RES_HALT
, "Sheepdog is stopped serving IO request"},
414 {SD_RES_READONLY
, "Object is read-only"},
417 for (i
= 0; i
< ARRAY_SIZE(errors
); ++i
) {
418 if (errors
[i
].err
== err
) {
419 return errors
[i
].desc
;
423 return "Invalid error code";
427 * Sheepdog I/O handling:
429 * 1. In sd_co_rw_vector, we send the I/O requests to the server and
430 * link the requests to the inflight_list in the
431 * BDRVSheepdogState. The function exits without waiting for
432 * receiving the response.
434 * 2. We receive the response in aio_read_response, the fd handler to
435 * the sheepdog connection. If metadata update is needed, we send
436 * the write request to the vdi object in sd_write_done, the write
437 * completion function. We switch back to sd_co_readv/writev after
438 * all the requests belonging to the AIOCB are finished.
441 static inline AIOReq
*alloc_aio_req(BDRVSheepdogState
*s
, SheepdogAIOCB
*acb
,
442 uint64_t oid
, unsigned int data_len
,
443 uint64_t offset
, uint8_t flags
, bool create
,
444 uint64_t base_oid
, unsigned int iov_offset
)
448 aio_req
= g_malloc(sizeof(*aio_req
));
449 aio_req
->aiocb
= acb
;
450 aio_req
->iov_offset
= iov_offset
;
452 aio_req
->base_oid
= base_oid
;
453 aio_req
->offset
= offset
;
454 aio_req
->data_len
= data_len
;
455 aio_req
->flags
= flags
;
456 aio_req
->id
= s
->aioreq_seq_num
++;
457 aio_req
->create
= create
;
463 static inline void free_aio_req(BDRVSheepdogState
*s
, AIOReq
*aio_req
)
465 SheepdogAIOCB
*acb
= aio_req
->aiocb
;
467 acb
->cancelable
= false;
468 QLIST_REMOVE(aio_req
, aio_siblings
);
474 static void coroutine_fn
sd_finish_aiocb(SheepdogAIOCB
*acb
)
476 qemu_coroutine_enter(acb
->coroutine
, NULL
);
481 * Check whether the specified acb can be canceled
483 * We can cancel aio when any request belonging to the acb is:
484 * - Not processed by the sheepdog server.
485 * - Not linked to the inflight queue.
487 static bool sd_acb_cancelable(const SheepdogAIOCB
*acb
)
489 BDRVSheepdogState
*s
= acb
->common
.bs
->opaque
;
492 if (!acb
->cancelable
) {
496 QLIST_FOREACH(aioreq
, &s
->inflight_aio_head
, aio_siblings
) {
497 if (aioreq
->aiocb
== acb
) {
505 static void sd_aio_cancel(BlockAIOCB
*blockacb
)
507 SheepdogAIOCB
*acb
= (SheepdogAIOCB
*)blockacb
;
508 BDRVSheepdogState
*s
= acb
->common
.bs
->opaque
;
509 AIOReq
*aioreq
, *next
;
511 if (sd_acb_cancelable(acb
)) {
512 /* Remove outstanding requests from failed queue. */
513 QLIST_FOREACH_SAFE(aioreq
, &s
->failed_aio_head
, aio_siblings
,
515 if (aioreq
->aiocb
== acb
) {
516 free_aio_req(s
, aioreq
);
520 assert(acb
->nr_pending
== 0);
521 if (acb
->common
.cb
) {
522 acb
->common
.cb(acb
->common
.opaque
, -ECANCELED
);
524 sd_finish_aiocb(acb
);
528 static const AIOCBInfo sd_aiocb_info
= {
529 .aiocb_size
= sizeof(SheepdogAIOCB
),
530 .cancel_async
= sd_aio_cancel
,
533 static SheepdogAIOCB
*sd_aio_setup(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
534 int64_t sector_num
, int nb_sectors
)
537 uint32_t object_size
;
538 BDRVSheepdogState
*s
= bs
->opaque
;
540 object_size
= (UINT32_C(1) << s
->inode
.block_size_shift
);
542 acb
= qemu_aio_get(&sd_aiocb_info
, bs
, NULL
, NULL
);
546 acb
->sector_num
= sector_num
;
547 acb
->nb_sectors
= nb_sectors
;
549 acb
->aio_done_func
= NULL
;
550 acb
->cancelable
= true;
551 acb
->coroutine
= qemu_coroutine_self();
555 acb
->min_affect_data_idx
= acb
->sector_num
* BDRV_SECTOR_SIZE
/ object_size
;
556 acb
->max_affect_data_idx
= (acb
->sector_num
* BDRV_SECTOR_SIZE
+
557 acb
->nb_sectors
* BDRV_SECTOR_SIZE
) / object_size
;
562 /* Return -EIO in case of error, file descriptor on success */
563 static int connect_to_sdog(BDRVSheepdogState
*s
, Error
**errp
)
568 fd
= unix_connect(s
->host_spec
, errp
);
570 fd
= inet_connect(s
->host_spec
, errp
);
573 int ret
= socket_set_nodelay(fd
);
575 error_report("%s", strerror(errno
));
581 qemu_set_nonblock(fd
);
589 /* Return 0 on success and -errno in case of error */
590 static coroutine_fn
int send_co_req(int sockfd
, SheepdogReq
*hdr
, void *data
,
595 ret
= qemu_co_send(sockfd
, hdr
, sizeof(*hdr
));
596 if (ret
!= sizeof(*hdr
)) {
597 error_report("failed to send a req, %s", strerror(errno
));
598 ret
= -socket_error();
602 ret
= qemu_co_send(sockfd
, data
, *wlen
);
604 ret
= -socket_error();
605 error_report("failed to send a req, %s", strerror(errno
));
611 static void restart_co_req(void *opaque
)
613 Coroutine
*co
= opaque
;
615 qemu_coroutine_enter(co
, NULL
);
618 typedef struct SheepdogReqCo
{
620 AioContext
*aio_context
;
629 static coroutine_fn
void do_co_req(void *opaque
)
633 SheepdogReqCo
*srco
= opaque
;
634 int sockfd
= srco
->sockfd
;
635 SheepdogReq
*hdr
= srco
->hdr
;
636 void *data
= srco
->data
;
637 unsigned int *wlen
= srco
->wlen
;
638 unsigned int *rlen
= srco
->rlen
;
640 co
= qemu_coroutine_self();
641 aio_set_fd_handler(srco
->aio_context
, sockfd
, NULL
, restart_co_req
, co
);
643 ret
= send_co_req(sockfd
, hdr
, data
, wlen
);
648 aio_set_fd_handler(srco
->aio_context
, sockfd
, restart_co_req
, NULL
, co
);
650 ret
= qemu_co_recv(sockfd
, hdr
, sizeof(*hdr
));
651 if (ret
!= sizeof(*hdr
)) {
652 error_report("failed to get a rsp, %s", strerror(errno
));
657 if (*rlen
> hdr
->data_length
) {
658 *rlen
= hdr
->data_length
;
662 ret
= qemu_co_recv(sockfd
, data
, *rlen
);
664 error_report("failed to get the data, %s", strerror(errno
));
671 /* there is at most one request for this sockfd, so it is safe to
672 * set each handler to NULL. */
673 aio_set_fd_handler(srco
->aio_context
, sockfd
, NULL
, NULL
, NULL
);
676 srco
->finished
= true;
680 * Send the request to the sheep in a synchronous manner.
682 * Return 0 on success, -errno in case of error.
684 static int do_req(int sockfd
, AioContext
*aio_context
, SheepdogReq
*hdr
,
685 void *data
, unsigned int *wlen
, unsigned int *rlen
)
688 SheepdogReqCo srco
= {
690 .aio_context
= aio_context
,
699 if (qemu_in_coroutine()) {
702 co
= qemu_coroutine_create(do_co_req
);
703 qemu_coroutine_enter(co
, &srco
);
704 while (!srco
.finished
) {
705 aio_poll(aio_context
, true);
712 static void coroutine_fn
add_aio_request(BDRVSheepdogState
*s
, AIOReq
*aio_req
,
713 struct iovec
*iov
, int niov
,
714 enum AIOCBState aiocb_type
);
715 static void coroutine_fn
resend_aioreq(BDRVSheepdogState
*s
, AIOReq
*aio_req
);
716 static int reload_inode(BDRVSheepdogState
*s
, uint32_t snapid
, const char *tag
);
717 static int get_sheep_fd(BDRVSheepdogState
*s
, Error
**errp
);
718 static void co_write_request(void *opaque
);
720 static coroutine_fn
void reconnect_to_sdog(void *opaque
)
722 BDRVSheepdogState
*s
= opaque
;
723 AIOReq
*aio_req
, *next
;
725 aio_set_fd_handler(s
->aio_context
, s
->fd
, NULL
, NULL
, NULL
);
729 /* Wait for outstanding write requests to be completed. */
730 while (s
->co_send
!= NULL
) {
731 co_write_request(opaque
);
734 /* Try to reconnect the sheepdog server every one second. */
736 Error
*local_err
= NULL
;
737 s
->fd
= get_sheep_fd(s
, &local_err
);
739 DPRINTF("Wait for connection to be established\n");
740 error_report_err(local_err
);
741 co_aio_sleep_ns(bdrv_get_aio_context(s
->bs
), QEMU_CLOCK_REALTIME
,
747 * Now we have to resend all the request in the inflight queue. However,
748 * resend_aioreq() can yield and newly created requests can be added to the
749 * inflight queue before the coroutine is resumed. To avoid mixing them, we
750 * have to move all the inflight requests to the failed queue before
751 * resend_aioreq() is called.
753 QLIST_FOREACH_SAFE(aio_req
, &s
->inflight_aio_head
, aio_siblings
, next
) {
754 QLIST_REMOVE(aio_req
, aio_siblings
);
755 QLIST_INSERT_HEAD(&s
->failed_aio_head
, aio_req
, aio_siblings
);
758 /* Resend all the failed aio requests. */
759 while (!QLIST_EMPTY(&s
->failed_aio_head
)) {
760 aio_req
= QLIST_FIRST(&s
->failed_aio_head
);
761 QLIST_REMOVE(aio_req
, aio_siblings
);
762 QLIST_INSERT_HEAD(&s
->inflight_aio_head
, aio_req
, aio_siblings
);
763 resend_aioreq(s
, aio_req
);
768 * Receive responses of the I/O requests.
770 * This function is registered as a fd handler, and called from the
771 * main loop when s->fd is ready for reading responses.
773 static void coroutine_fn
aio_read_response(void *opaque
)
776 BDRVSheepdogState
*s
= opaque
;
779 AIOReq
*aio_req
= NULL
;
784 ret
= qemu_co_recv(fd
, &rsp
, sizeof(rsp
));
785 if (ret
!= sizeof(rsp
)) {
786 error_report("failed to get the header, %s", strerror(errno
));
790 /* find the right aio_req from the inflight aio list */
791 QLIST_FOREACH(aio_req
, &s
->inflight_aio_head
, aio_siblings
) {
792 if (aio_req
->id
== rsp
.id
) {
797 error_report("cannot find aio_req %x", rsp
.id
);
801 acb
= aio_req
->aiocb
;
803 switch (acb
->aiocb_type
) {
804 case AIOCB_WRITE_UDATA
:
805 /* this coroutine context is no longer suitable for co_recv
806 * because we may send data to update vdi objects */
808 if (!is_data_obj(aio_req
->oid
)) {
811 idx
= data_oid_to_idx(aio_req
->oid
);
813 if (aio_req
->create
) {
815 * If the object is newly created one, we need to update
816 * the vdi object (metadata object). min_dirty_data_idx
817 * and max_dirty_data_idx are changed to include updated
818 * index between them.
820 if (rsp
.result
== SD_RES_SUCCESS
) {
821 s
->inode
.data_vdi_id
[idx
] = s
->inode
.vdi_id
;
822 s
->max_dirty_data_idx
= MAX(idx
, s
->max_dirty_data_idx
);
823 s
->min_dirty_data_idx
= MIN(idx
, s
->min_dirty_data_idx
);
827 case AIOCB_READ_UDATA
:
828 ret
= qemu_co_recvv(fd
, acb
->qiov
->iov
, acb
->qiov
->niov
,
829 aio_req
->iov_offset
, rsp
.data_length
);
830 if (ret
!= rsp
.data_length
) {
831 error_report("failed to get the data, %s", strerror(errno
));
835 case AIOCB_FLUSH_CACHE
:
836 if (rsp
.result
== SD_RES_INVALID_PARMS
) {
837 DPRINTF("disable cache since the server doesn't support it\n");
838 s
->cache_flags
= SD_FLAG_CMD_DIRECT
;
839 rsp
.result
= SD_RES_SUCCESS
;
842 case AIOCB_DISCARD_OBJ
:
843 switch (rsp
.result
) {
844 case SD_RES_INVALID_PARMS
:
845 error_report("sheep(%s) doesn't support discard command",
847 rsp
.result
= SD_RES_SUCCESS
;
848 s
->discard_supported
= false;
851 idx
= data_oid_to_idx(aio_req
->oid
);
852 s
->inode
.data_vdi_id
[idx
] = 0;
859 switch (rsp
.result
) {
862 case SD_RES_READONLY
:
863 if (s
->inode
.vdi_id
== oid_to_vid(aio_req
->oid
)) {
864 ret
= reload_inode(s
, 0, "");
869 if (is_data_obj(aio_req
->oid
)) {
870 aio_req
->oid
= vid_to_data_oid(s
->inode
.vdi_id
,
871 data_oid_to_idx(aio_req
->oid
));
873 aio_req
->oid
= vid_to_vdi_oid(s
->inode
.vdi_id
);
875 resend_aioreq(s
, aio_req
);
879 error_report("%s", sd_strerror(rsp
.result
));
883 free_aio_req(s
, aio_req
);
884 if (!acb
->nr_pending
) {
886 * We've finished all requests which belong to the AIOCB, so
887 * we can switch back to sd_co_readv/writev now.
889 acb
->aio_done_func(acb
);
896 reconnect_to_sdog(opaque
);
899 static void co_read_response(void *opaque
)
901 BDRVSheepdogState
*s
= opaque
;
904 s
->co_recv
= qemu_coroutine_create(aio_read_response
);
907 qemu_coroutine_enter(s
->co_recv
, opaque
);
910 static void co_write_request(void *opaque
)
912 BDRVSheepdogState
*s
= opaque
;
914 qemu_coroutine_enter(s
->co_send
, NULL
);
918 * Return a socket descriptor to read/write objects.
920 * We cannot use this descriptor for other operations because
921 * the block driver may be on waiting response from the server.
923 static int get_sheep_fd(BDRVSheepdogState
*s
, Error
**errp
)
927 fd
= connect_to_sdog(s
, errp
);
932 aio_set_fd_handler(s
->aio_context
, fd
, co_read_response
, NULL
, s
);
936 static int sd_parse_uri(BDRVSheepdogState
*s
, const char *filename
,
937 char *vdi
, uint32_t *snapid
, char *tag
)
940 QueryParams
*qp
= NULL
;
943 uri
= uri_parse(filename
);
949 if (!strcmp(uri
->scheme
, "sheepdog")) {
951 } else if (!strcmp(uri
->scheme
, "sheepdog+tcp")) {
953 } else if (!strcmp(uri
->scheme
, "sheepdog+unix")) {
960 if (uri
->path
== NULL
|| !strcmp(uri
->path
, "/")) {
964 pstrcpy(vdi
, SD_MAX_VDI_LEN
, uri
->path
+ 1);
966 qp
= query_params_parse(uri
->query
);
967 if (qp
->n
> 1 || (s
->is_unix
&& !qp
->n
) || (!s
->is_unix
&& qp
->n
)) {
973 /* sheepdog+unix:///vdiname?socket=path */
974 if (uri
->server
|| uri
->port
|| strcmp(qp
->p
[0].name
, "socket")) {
978 s
->host_spec
= g_strdup(qp
->p
[0].value
);
980 /* sheepdog[+tcp]://[host:port]/vdiname */
981 s
->host_spec
= g_strdup_printf("%s:%d", uri
->server
?: SD_DEFAULT_ADDR
,
982 uri
->port
?: SD_DEFAULT_PORT
);
987 *snapid
= strtoul(uri
->fragment
, NULL
, 10);
989 pstrcpy(tag
, SD_MAX_VDI_TAG_LEN
, uri
->fragment
);
992 *snapid
= CURRENT_VDI_ID
; /* search current vdi */
997 query_params_free(qp
);
1004 * Parse a filename (old syntax)
1006 * filename must be one of the following formats:
1008 * 2. [vdiname]:[snapid]
1009 * 3. [vdiname]:[tag]
1010 * 4. [hostname]:[port]:[vdiname]
1011 * 5. [hostname]:[port]:[vdiname]:[snapid]
1012 * 6. [hostname]:[port]:[vdiname]:[tag]
1014 * You can boot from the snapshot images by specifying `snapid` or
1017 * You can run VMs outside the Sheepdog cluster by specifying
1018 * `hostname' and `port' (experimental).
1020 static int parse_vdiname(BDRVSheepdogState
*s
, const char *filename
,
1021 char *vdi
, uint32_t *snapid
, char *tag
)
1024 const char *host_spec
, *vdi_spec
;
1027 strstart(filename
, "sheepdog:", (const char **)&filename
);
1028 p
= q
= g_strdup(filename
);
1030 /* count the number of separators */
1040 /* use the first two tokens as host_spec. */
1053 p
= strchr(vdi_spec
, ':');
1058 uri
= g_strdup_printf("sheepdog://%s/%s", host_spec
, vdi_spec
);
1060 ret
= sd_parse_uri(s
, uri
, vdi
, snapid
, tag
);
1068 static int find_vdi_name(BDRVSheepdogState
*s
, const char *filename
,
1069 uint32_t snapid
, const char *tag
, uint32_t *vid
,
1070 bool lock
, Error
**errp
)
1074 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1075 unsigned int wlen
, rlen
= 0;
1076 char buf
[SD_MAX_VDI_LEN
+ SD_MAX_VDI_TAG_LEN
];
1078 fd
= connect_to_sdog(s
, errp
);
1083 /* This pair of strncpy calls ensures that the buffer is zero-filled,
1084 * which is desirable since we'll soon be sending those bytes, and
1085 * don't want the send_req to read uninitialized data.
1087 strncpy(buf
, filename
, SD_MAX_VDI_LEN
);
1088 strncpy(buf
+ SD_MAX_VDI_LEN
, tag
, SD_MAX_VDI_TAG_LEN
);
1090 memset(&hdr
, 0, sizeof(hdr
));
1092 hdr
.opcode
= SD_OP_LOCK_VDI
;
1093 hdr
.type
= LOCK_TYPE_NORMAL
;
1095 hdr
.opcode
= SD_OP_GET_VDI_INFO
;
1097 wlen
= SD_MAX_VDI_LEN
+ SD_MAX_VDI_TAG_LEN
;
1098 hdr
.proto_ver
= SD_PROTO_VER
;
1099 hdr
.data_length
= wlen
;
1100 hdr
.snapid
= snapid
;
1101 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1103 ret
= do_req(fd
, s
->aio_context
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1105 error_setg_errno(errp
, -ret
, "cannot get vdi info");
1109 if (rsp
->result
!= SD_RES_SUCCESS
) {
1110 error_setg(errp
, "cannot get vdi info, %s, %s %" PRIu32
" %s",
1111 sd_strerror(rsp
->result
), filename
, snapid
, tag
);
1112 if (rsp
->result
== SD_RES_NO_VDI
) {
1114 } else if (rsp
->result
== SD_RES_VDI_LOCKED
) {
1129 static void coroutine_fn
add_aio_request(BDRVSheepdogState
*s
, AIOReq
*aio_req
,
1130 struct iovec
*iov
, int niov
,
1131 enum AIOCBState aiocb_type
)
1133 int nr_copies
= s
->inode
.nr_copies
;
1135 unsigned int wlen
= 0;
1137 uint64_t oid
= aio_req
->oid
;
1138 unsigned int datalen
= aio_req
->data_len
;
1139 uint64_t offset
= aio_req
->offset
;
1140 uint8_t flags
= aio_req
->flags
;
1141 uint64_t old_oid
= aio_req
->base_oid
;
1142 bool create
= aio_req
->create
;
1145 error_report("bug");
1148 memset(&hdr
, 0, sizeof(hdr
));
1150 switch (aiocb_type
) {
1151 case AIOCB_FLUSH_CACHE
:
1152 hdr
.opcode
= SD_OP_FLUSH_VDI
;
1154 case AIOCB_READ_UDATA
:
1155 hdr
.opcode
= SD_OP_READ_OBJ
;
1158 case AIOCB_WRITE_UDATA
:
1160 hdr
.opcode
= SD_OP_CREATE_AND_WRITE_OBJ
;
1162 hdr
.opcode
= SD_OP_WRITE_OBJ
;
1165 hdr
.flags
= SD_FLAG_CMD_WRITE
| flags
;
1167 case AIOCB_DISCARD_OBJ
:
1168 hdr
.opcode
= SD_OP_DISCARD_OBJ
;
1172 if (s
->cache_flags
) {
1173 hdr
.flags
|= s
->cache_flags
;
1177 hdr
.cow_oid
= old_oid
;
1178 hdr
.copies
= s
->inode
.nr_copies
;
1180 hdr
.data_length
= datalen
;
1181 hdr
.offset
= offset
;
1183 hdr
.id
= aio_req
->id
;
1185 qemu_co_mutex_lock(&s
->lock
);
1186 s
->co_send
= qemu_coroutine_self();
1187 aio_set_fd_handler(s
->aio_context
, s
->fd
,
1188 co_read_response
, co_write_request
, s
);
1189 socket_set_cork(s
->fd
, 1);
1192 ret
= qemu_co_send(s
->fd
, &hdr
, sizeof(hdr
));
1193 if (ret
!= sizeof(hdr
)) {
1194 error_report("failed to send a req, %s", strerror(errno
));
1199 ret
= qemu_co_sendv(s
->fd
, iov
, niov
, aio_req
->iov_offset
, wlen
);
1201 error_report("failed to send a data, %s", strerror(errno
));
1205 socket_set_cork(s
->fd
, 0);
1206 aio_set_fd_handler(s
->aio_context
, s
->fd
, co_read_response
, NULL
, s
);
1208 qemu_co_mutex_unlock(&s
->lock
);
1211 static int read_write_object(int fd
, AioContext
*aio_context
, char *buf
,
1212 uint64_t oid
, uint8_t copies
,
1213 unsigned int datalen
, uint64_t offset
,
1214 bool write
, bool create
, uint32_t cache_flags
)
1217 SheepdogObjRsp
*rsp
= (SheepdogObjRsp
*)&hdr
;
1218 unsigned int wlen
, rlen
;
1221 memset(&hdr
, 0, sizeof(hdr
));
1226 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1228 hdr
.opcode
= SD_OP_CREATE_AND_WRITE_OBJ
;
1230 hdr
.opcode
= SD_OP_WRITE_OBJ
;
1235 hdr
.opcode
= SD_OP_READ_OBJ
;
1238 hdr
.flags
|= cache_flags
;
1241 hdr
.data_length
= datalen
;
1242 hdr
.offset
= offset
;
1243 hdr
.copies
= copies
;
1245 ret
= do_req(fd
, aio_context
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1247 error_report("failed to send a request to the sheep");
1251 switch (rsp
->result
) {
1252 case SD_RES_SUCCESS
:
1255 error_report("%s", sd_strerror(rsp
->result
));
1260 static int read_object(int fd
, AioContext
*aio_context
, char *buf
,
1261 uint64_t oid
, uint8_t copies
,
1262 unsigned int datalen
, uint64_t offset
,
1263 uint32_t cache_flags
)
1265 return read_write_object(fd
, aio_context
, buf
, oid
, copies
,
1266 datalen
, offset
, false,
1267 false, cache_flags
);
1270 static int write_object(int fd
, AioContext
*aio_context
, char *buf
,
1271 uint64_t oid
, uint8_t copies
,
1272 unsigned int datalen
, uint64_t offset
, bool create
,
1273 uint32_t cache_flags
)
1275 return read_write_object(fd
, aio_context
, buf
, oid
, copies
,
1276 datalen
, offset
, true,
1277 create
, cache_flags
);
1280 /* update inode with the latest state */
1281 static int reload_inode(BDRVSheepdogState
*s
, uint32_t snapid
, const char *tag
)
1283 Error
*local_err
= NULL
;
1284 SheepdogInode
*inode
;
1288 fd
= connect_to_sdog(s
, &local_err
);
1290 error_report_err(local_err
);
1294 inode
= g_malloc(SD_INODE_HEADER_SIZE
);
1296 ret
= find_vdi_name(s
, s
->name
, snapid
, tag
, &vid
, false, &local_err
);
1298 error_report_err(local_err
);
1302 ret
= read_object(fd
, s
->aio_context
, (char *)inode
, vid_to_vdi_oid(vid
),
1303 s
->inode
.nr_copies
, SD_INODE_HEADER_SIZE
, 0,
1309 if (inode
->vdi_id
!= s
->inode
.vdi_id
) {
1310 memcpy(&s
->inode
, inode
, SD_INODE_HEADER_SIZE
);
1320 static void coroutine_fn
resend_aioreq(BDRVSheepdogState
*s
, AIOReq
*aio_req
)
1322 SheepdogAIOCB
*acb
= aio_req
->aiocb
;
1324 aio_req
->create
= false;
1326 /* check whether this request becomes a CoW one */
1327 if (acb
->aiocb_type
== AIOCB_WRITE_UDATA
&& is_data_obj(aio_req
->oid
)) {
1328 int idx
= data_oid_to_idx(aio_req
->oid
);
1330 if (is_data_obj_writable(&s
->inode
, idx
)) {
1334 if (s
->inode
.data_vdi_id
[idx
]) {
1335 aio_req
->base_oid
= vid_to_data_oid(s
->inode
.data_vdi_id
[idx
], idx
);
1336 aio_req
->flags
|= SD_FLAG_CMD_COW
;
1338 aio_req
->create
= true;
1341 if (is_data_obj(aio_req
->oid
)) {
1342 add_aio_request(s
, aio_req
, acb
->qiov
->iov
, acb
->qiov
->niov
,
1346 iov
.iov_base
= &s
->inode
;
1347 iov
.iov_len
= sizeof(s
->inode
);
1348 add_aio_request(s
, aio_req
, &iov
, 1, AIOCB_WRITE_UDATA
);
1352 static void sd_detach_aio_context(BlockDriverState
*bs
)
1354 BDRVSheepdogState
*s
= bs
->opaque
;
1356 aio_set_fd_handler(s
->aio_context
, s
->fd
, NULL
, NULL
, NULL
);
1359 static void sd_attach_aio_context(BlockDriverState
*bs
,
1360 AioContext
*new_context
)
1362 BDRVSheepdogState
*s
= bs
->opaque
;
1364 s
->aio_context
= new_context
;
1365 aio_set_fd_handler(new_context
, s
->fd
, co_read_response
, NULL
, s
);
1368 /* TODO Convert to fine grained options */
1369 static QemuOptsList runtime_opts
= {
1371 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
1375 .type
= QEMU_OPT_STRING
,
1376 .help
= "URL to the sheepdog image",
1378 { /* end of list */ }
1382 static int sd_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1387 BDRVSheepdogState
*s
= bs
->opaque
;
1388 char vdi
[SD_MAX_VDI_LEN
], tag
[SD_MAX_VDI_TAG_LEN
];
1392 Error
*local_err
= NULL
;
1393 const char *filename
;
1396 s
->aio_context
= bdrv_get_aio_context(bs
);
1398 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
1399 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
1401 error_propagate(errp
, local_err
);
1406 filename
= qemu_opt_get(opts
, "filename");
1408 QLIST_INIT(&s
->inflight_aio_head
);
1409 QLIST_INIT(&s
->failed_aio_head
);
1410 QLIST_INIT(&s
->inflight_aiocb_head
);
1413 memset(vdi
, 0, sizeof(vdi
));
1414 memset(tag
, 0, sizeof(tag
));
1416 if (strstr(filename
, "://")) {
1417 ret
= sd_parse_uri(s
, filename
, vdi
, &snapid
, tag
);
1419 ret
= parse_vdiname(s
, filename
, vdi
, &snapid
, tag
);
1422 error_setg(errp
, "Can't parse filename");
1425 s
->fd
= get_sheep_fd(s
, errp
);
1431 ret
= find_vdi_name(s
, vdi
, snapid
, tag
, &vid
, true, errp
);
1437 * QEMU block layer emulates writethrough cache as 'writeback + flush', so
1438 * we always set SD_FLAG_CMD_CACHE (writeback cache) as default.
1440 s
->cache_flags
= SD_FLAG_CMD_CACHE
;
1441 if (flags
& BDRV_O_NOCACHE
) {
1442 s
->cache_flags
= SD_FLAG_CMD_DIRECT
;
1444 s
->discard_supported
= true;
1446 if (snapid
|| tag
[0] != '\0') {
1447 DPRINTF("%" PRIx32
" snapshot inode was open.\n", vid
);
1448 s
->is_snapshot
= true;
1451 fd
= connect_to_sdog(s
, errp
);
1457 buf
= g_malloc(SD_INODE_SIZE
);
1458 ret
= read_object(fd
, s
->aio_context
, buf
, vid_to_vdi_oid(vid
),
1459 0, SD_INODE_SIZE
, 0, s
->cache_flags
);
1464 error_setg(errp
, "Can't read snapshot inode");
1468 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
1469 s
->min_dirty_data_idx
= UINT32_MAX
;
1470 s
->max_dirty_data_idx
= 0;
1472 bs
->total_sectors
= s
->inode
.vdi_size
/ BDRV_SECTOR_SIZE
;
1473 pstrcpy(s
->name
, sizeof(s
->name
), vdi
);
1474 qemu_co_mutex_init(&s
->lock
);
1475 qemu_co_queue_init(&s
->overwrapping_queue
);
1476 qemu_opts_del(opts
);
1480 aio_set_fd_handler(bdrv_get_aio_context(bs
), s
->fd
, NULL
, NULL
, NULL
);
1484 qemu_opts_del(opts
);
1489 static int do_sd_create(BDRVSheepdogState
*s
, uint32_t *vdi_id
, int snapshot
,
1493 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1495 unsigned int wlen
, rlen
= 0;
1496 char buf
[SD_MAX_VDI_LEN
];
1498 fd
= connect_to_sdog(s
, errp
);
1503 /* FIXME: would it be better to fail (e.g., return -EIO) when filename
1504 * does not fit in buf? For now, just truncate and avoid buffer overrun.
1506 memset(buf
, 0, sizeof(buf
));
1507 pstrcpy(buf
, sizeof(buf
), s
->name
);
1509 memset(&hdr
, 0, sizeof(hdr
));
1510 hdr
.opcode
= SD_OP_NEW_VDI
;
1511 hdr
.base_vdi_id
= s
->inode
.vdi_id
;
1513 wlen
= SD_MAX_VDI_LEN
;
1515 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1516 hdr
.snapid
= snapshot
;
1518 hdr
.data_length
= wlen
;
1519 hdr
.vdi_size
= s
->inode
.vdi_size
;
1520 hdr
.copy_policy
= s
->inode
.copy_policy
;
1521 hdr
.copies
= s
->inode
.nr_copies
;
1522 hdr
.block_size_shift
= s
->inode
.block_size_shift
;
1524 ret
= do_req(fd
, s
->aio_context
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1529 error_setg_errno(errp
, -ret
, "create failed");
1533 if (rsp
->result
!= SD_RES_SUCCESS
) {
1534 error_setg(errp
, "%s, %s", sd_strerror(rsp
->result
), s
->inode
.name
);
1539 *vdi_id
= rsp
->vdi_id
;
1545 static int sd_prealloc(const char *filename
, Error
**errp
)
1547 BlockDriverState
*bs
= NULL
;
1548 BDRVSheepdogState
*base
= NULL
;
1549 unsigned long buf_size
;
1550 uint32_t idx
, max_idx
;
1551 uint32_t object_size
;
1556 ret
= bdrv_open(&bs
, filename
, NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
1559 goto out_with_err_set
;
1562 vdi_size
= bdrv_getlength(bs
);
1569 object_size
= (UINT32_C(1) << base
->inode
.block_size_shift
);
1570 buf_size
= MIN(object_size
, SD_DATA_OBJ_SIZE
);
1571 buf
= g_malloc0(buf_size
);
1573 max_idx
= DIV_ROUND_UP(vdi_size
, buf_size
);
1575 for (idx
= 0; idx
< max_idx
; idx
++) {
1577 * The created image can be a cloned image, so we need to read
1578 * a data from the source image.
1580 ret
= bdrv_pread(bs
, idx
* buf_size
, buf
, buf_size
);
1584 ret
= bdrv_pwrite(bs
, idx
* buf_size
, buf
, buf_size
);
1592 error_setg_errno(errp
, -ret
, "Can't pre-allocate");
1604 * Sheepdog support two kinds of redundancy, full replication and erasure
1607 * # create a fully replicated vdi with x copies
1608 * -o redundancy=x (1 <= x <= SD_MAX_COPIES)
1610 * # create a erasure coded vdi with x data strips and y parity strips
1611 * -o redundancy=x:y (x must be one of {2,4,8,16} and 1 <= y < SD_EC_MAX_STRIP)
1613 static int parse_redundancy(BDRVSheepdogState
*s
, const char *opt
)
1615 struct SheepdogInode
*inode
= &s
->inode
;
1616 const char *n1
, *n2
;
1620 pstrcpy(p
, sizeof(p
), opt
);
1621 n1
= strtok(p
, ":");
1622 n2
= strtok(NULL
, ":");
1628 copy
= strtol(n1
, NULL
, 10);
1629 if (copy
> SD_MAX_COPIES
|| copy
< 1) {
1633 inode
->copy_policy
= 0;
1634 inode
->nr_copies
= copy
;
1638 if (copy
!= 2 && copy
!= 4 && copy
!= 8 && copy
!= 16) {
1642 parity
= strtol(n2
, NULL
, 10);
1643 if (parity
>= SD_EC_MAX_STRIP
|| parity
< 1) {
1648 * 4 bits for parity and 4 bits for data.
1649 * We have to compress upper data bits because it can't represent 16
1651 inode
->copy_policy
= ((copy
/ 2) << 4) + parity
;
1652 inode
->nr_copies
= copy
+ parity
;
1657 static int parse_block_size_shift(BDRVSheepdogState
*s
, QemuOpts
*opt
)
1659 struct SheepdogInode
*inode
= &s
->inode
;
1660 uint64_t object_size
;
1663 object_size
= qemu_opt_get_size_del(opt
, BLOCK_OPT_OBJECT_SIZE
, 0);
1665 if ((object_size
- 1) & object_size
) { /* not a power of 2? */
1668 obj_order
= ctz32(object_size
);
1669 if (obj_order
< 20 || obj_order
> 31) {
1672 inode
->block_size_shift
= (uint8_t)obj_order
;
1678 static int sd_create(const char *filename
, QemuOpts
*opts
,
1683 char *backing_file
= NULL
;
1685 BDRVSheepdogState
*s
;
1686 char tag
[SD_MAX_VDI_TAG_LEN
];
1688 uint64_t max_vdi_size
;
1689 bool prealloc
= false;
1691 s
= g_new0(BDRVSheepdogState
, 1);
1693 memset(tag
, 0, sizeof(tag
));
1694 if (strstr(filename
, "://")) {
1695 ret
= sd_parse_uri(s
, filename
, s
->name
, &snapid
, tag
);
1697 ret
= parse_vdiname(s
, filename
, s
->name
, &snapid
, tag
);
1700 error_setg(errp
, "Can't parse filename");
1704 s
->inode
.vdi_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
1706 backing_file
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FILE
);
1707 buf
= qemu_opt_get_del(opts
, BLOCK_OPT_PREALLOC
);
1708 if (!buf
|| !strcmp(buf
, "off")) {
1710 } else if (!strcmp(buf
, "full")) {
1713 error_setg(errp
, "Invalid preallocation mode: '%s'", buf
);
1719 buf
= qemu_opt_get_del(opts
, BLOCK_OPT_REDUNDANCY
);
1721 ret
= parse_redundancy(s
, buf
);
1723 error_setg(errp
, "Invalid redundancy mode: '%s'", buf
);
1727 ret
= parse_block_size_shift(s
, opts
);
1729 error_setg(errp
, "Invalid object_size."
1730 " obect_size needs to be power of 2"
1731 " and be limited from 2^20 to 2^31");
1736 BlockDriverState
*bs
;
1737 BDRVSheepdogState
*base
;
1740 /* Currently, only Sheepdog backing image is supported. */
1741 drv
= bdrv_find_protocol(backing_file
, true, NULL
);
1742 if (!drv
|| strcmp(drv
->protocol_name
, "sheepdog") != 0) {
1743 error_setg(errp
, "backing_file must be a sheepdog image");
1749 ret
= bdrv_open(&bs
, backing_file
, NULL
, NULL
, BDRV_O_PROTOCOL
, errp
);
1756 if (!is_snapshot(&base
->inode
)) {
1757 error_setg(errp
, "cannot clone from a non snapshot vdi");
1762 s
->inode
.vdi_id
= base
->inode
.vdi_id
;
1766 s
->aio_context
= qemu_get_aio_context();
1768 /* if block_size_shift is not specified, get cluster default value */
1769 if (s
->inode
.block_size_shift
== 0) {
1771 SheepdogClusterRsp
*rsp
= (SheepdogClusterRsp
*)&hdr
;
1772 Error
*local_err
= NULL
;
1774 unsigned int wlen
= 0, rlen
= 0;
1776 fd
= connect_to_sdog(s
, &local_err
);
1778 error_report("%s", error_get_pretty(local_err
));
1779 error_free(local_err
);
1784 memset(&hdr
, 0, sizeof(hdr
));
1785 hdr
.opcode
= SD_OP_GET_CLUSTER_DEFAULT
;
1786 hdr
.proto_ver
= SD_PROTO_VER
;
1788 ret
= do_req(fd
, s
->aio_context
, (SheepdogReq
*)&hdr
,
1789 NULL
, &wlen
, &rlen
);
1792 error_setg_errno(errp
, -ret
, "failed to get cluster default");
1795 if (rsp
->result
== SD_RES_SUCCESS
) {
1796 s
->inode
.block_size_shift
= rsp
->block_size_shift
;
1798 s
->inode
.block_size_shift
= SD_DEFAULT_BLOCK_SIZE_SHIFT
;
1802 max_vdi_size
= (UINT64_C(1) << s
->inode
.block_size_shift
) * MAX_DATA_OBJS
;
1804 if (s
->inode
.vdi_size
> max_vdi_size
) {
1805 error_setg(errp
, "An image is too large."
1806 " The maximum image size is %"PRIu64
"GB",
1807 max_vdi_size
/ 1024 / 1024 / 1024);
1812 ret
= do_sd_create(s
, &vid
, 0, errp
);
1818 ret
= sd_prealloc(filename
, errp
);
1821 g_free(backing_file
);
1827 static void sd_close(BlockDriverState
*bs
)
1829 Error
*local_err
= NULL
;
1830 BDRVSheepdogState
*s
= bs
->opaque
;
1832 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1833 unsigned int wlen
, rlen
= 0;
1836 DPRINTF("%s\n", s
->name
);
1838 fd
= connect_to_sdog(s
, &local_err
);
1840 error_report_err(local_err
);
1844 memset(&hdr
, 0, sizeof(hdr
));
1846 hdr
.opcode
= SD_OP_RELEASE_VDI
;
1847 hdr
.type
= LOCK_TYPE_NORMAL
;
1848 hdr
.base_vdi_id
= s
->inode
.vdi_id
;
1849 wlen
= strlen(s
->name
) + 1;
1850 hdr
.data_length
= wlen
;
1851 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1853 ret
= do_req(fd
, s
->aio_context
, (SheepdogReq
*)&hdr
,
1854 s
->name
, &wlen
, &rlen
);
1858 if (!ret
&& rsp
->result
!= SD_RES_SUCCESS
&&
1859 rsp
->result
!= SD_RES_VDI_NOT_LOCKED
) {
1860 error_report("%s, %s", sd_strerror(rsp
->result
), s
->name
);
1863 aio_set_fd_handler(bdrv_get_aio_context(bs
), s
->fd
, NULL
, NULL
, NULL
);
1865 g_free(s
->host_spec
);
1868 static int64_t sd_getlength(BlockDriverState
*bs
)
1870 BDRVSheepdogState
*s
= bs
->opaque
;
1872 return s
->inode
.vdi_size
;
1875 static int sd_truncate(BlockDriverState
*bs
, int64_t offset
)
1877 Error
*local_err
= NULL
;
1878 BDRVSheepdogState
*s
= bs
->opaque
;
1880 unsigned int datalen
;
1881 uint64_t max_vdi_size
;
1883 max_vdi_size
= (UINT64_C(1) << s
->inode
.block_size_shift
) * MAX_DATA_OBJS
;
1884 if (offset
< s
->inode
.vdi_size
) {
1885 error_report("shrinking is not supported");
1887 } else if (offset
> max_vdi_size
) {
1888 error_report("too big image size");
1892 fd
= connect_to_sdog(s
, &local_err
);
1894 error_report_err(local_err
);
1898 /* we don't need to update entire object */
1899 datalen
= SD_INODE_SIZE
- sizeof(s
->inode
.data_vdi_id
);
1900 s
->inode
.vdi_size
= offset
;
1901 ret
= write_object(fd
, s
->aio_context
, (char *)&s
->inode
,
1902 vid_to_vdi_oid(s
->inode
.vdi_id
), s
->inode
.nr_copies
,
1903 datalen
, 0, false, s
->cache_flags
);
1907 error_report("failed to update an inode.");
1914 * This function is called after writing data objects. If we need to
1915 * update metadata, this sends a write request to the vdi object.
1916 * Otherwise, this switches back to sd_co_readv/writev.
1918 static void coroutine_fn
sd_write_done(SheepdogAIOCB
*acb
)
1920 BDRVSheepdogState
*s
= acb
->common
.bs
->opaque
;
1923 uint32_t offset
, data_len
, mn
, mx
;
1925 mn
= s
->min_dirty_data_idx
;
1926 mx
= s
->max_dirty_data_idx
;
1928 /* we need to update the vdi object. */
1929 offset
= sizeof(s
->inode
) - sizeof(s
->inode
.data_vdi_id
) +
1930 mn
* sizeof(s
->inode
.data_vdi_id
[0]);
1931 data_len
= (mx
- mn
+ 1) * sizeof(s
->inode
.data_vdi_id
[0]);
1933 s
->min_dirty_data_idx
= UINT32_MAX
;
1934 s
->max_dirty_data_idx
= 0;
1936 iov
.iov_base
= &s
->inode
;
1937 iov
.iov_len
= sizeof(s
->inode
);
1938 aio_req
= alloc_aio_req(s
, acb
, vid_to_vdi_oid(s
->inode
.vdi_id
),
1939 data_len
, offset
, 0, false, 0, offset
);
1940 QLIST_INSERT_HEAD(&s
->inflight_aio_head
, aio_req
, aio_siblings
);
1941 add_aio_request(s
, aio_req
, &iov
, 1, AIOCB_WRITE_UDATA
);
1943 acb
->aio_done_func
= sd_finish_aiocb
;
1944 acb
->aiocb_type
= AIOCB_WRITE_UDATA
;
1948 sd_finish_aiocb(acb
);
1951 /* Delete current working VDI on the snapshot chain */
1952 static bool sd_delete(BDRVSheepdogState
*s
)
1954 Error
*local_err
= NULL
;
1955 unsigned int wlen
= SD_MAX_VDI_LEN
, rlen
= 0;
1956 SheepdogVdiReq hdr
= {
1957 .opcode
= SD_OP_DEL_VDI
,
1958 .base_vdi_id
= s
->inode
.vdi_id
,
1959 .data_length
= wlen
,
1960 .flags
= SD_FLAG_CMD_WRITE
,
1962 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1965 fd
= connect_to_sdog(s
, &local_err
);
1967 error_report_err(local_err
);
1971 ret
= do_req(fd
, s
->aio_context
, (SheepdogReq
*)&hdr
,
1972 s
->name
, &wlen
, &rlen
);
1977 switch (rsp
->result
) {
1979 error_report("%s was already deleted", s
->name
);
1981 case SD_RES_SUCCESS
:
1984 error_report("%s, %s", sd_strerror(rsp
->result
), s
->name
);
1992 * Create a writable VDI from a snapshot
1994 static int sd_create_branch(BDRVSheepdogState
*s
)
1996 Error
*local_err
= NULL
;
2002 DPRINTF("%" PRIx32
" is snapshot.\n", s
->inode
.vdi_id
);
2004 buf
= g_malloc(SD_INODE_SIZE
);
2007 * Even If deletion fails, we will just create extra snapshot based on
2008 * the working VDI which was supposed to be deleted. So no need to
2011 deleted
= sd_delete(s
);
2012 ret
= do_sd_create(s
, &vid
, !deleted
, &local_err
);
2014 error_report_err(local_err
);
2018 DPRINTF("%" PRIx32
" is created.\n", vid
);
2020 fd
= connect_to_sdog(s
, &local_err
);
2022 error_report_err(local_err
);
2027 ret
= read_object(fd
, s
->aio_context
, buf
, vid_to_vdi_oid(vid
),
2028 s
->inode
.nr_copies
, SD_INODE_SIZE
, 0, s
->cache_flags
);
2036 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
2038 s
->is_snapshot
= false;
2040 DPRINTF("%" PRIx32
" was newly created.\n", s
->inode
.vdi_id
);
2049 * Send I/O requests to the server.
2051 * This function sends requests to the server, links the requests to
2052 * the inflight_list in BDRVSheepdogState, and exits without
2053 * waiting the response. The responses are received in the
2054 * `aio_read_response' function which is called from the main loop as
2057 * Returns 1 when we need to wait a response, 0 when there is no sent
2058 * request and -errno in error cases.
2060 static int coroutine_fn
sd_co_rw_vector(void *p
)
2062 SheepdogAIOCB
*acb
= p
;
2064 unsigned long len
, done
= 0, total
= acb
->nb_sectors
* BDRV_SECTOR_SIZE
;
2066 uint32_t object_size
;
2069 BDRVSheepdogState
*s
= acb
->common
.bs
->opaque
;
2070 SheepdogInode
*inode
= &s
->inode
;
2073 if (acb
->aiocb_type
== AIOCB_WRITE_UDATA
&& s
->is_snapshot
) {
2075 * In the case we open the snapshot VDI, Sheepdog creates the
2076 * writable VDI when we do a write operation first.
2078 ret
= sd_create_branch(s
);
2085 object_size
= (UINT32_C(1) << inode
->block_size_shift
);
2086 idx
= acb
->sector_num
* BDRV_SECTOR_SIZE
/ object_size
;
2087 offset
= (acb
->sector_num
* BDRV_SECTOR_SIZE
) % object_size
;
2090 * Make sure we don't free the aiocb before we are done with all requests.
2091 * This additional reference is dropped at the end of this function.
2095 while (done
!= total
) {
2097 uint64_t old_oid
= 0;
2098 bool create
= false;
2100 oid
= vid_to_data_oid(inode
->data_vdi_id
[idx
], idx
);
2102 len
= MIN(total
- done
, object_size
- offset
);
2104 switch (acb
->aiocb_type
) {
2105 case AIOCB_READ_UDATA
:
2106 if (!inode
->data_vdi_id
[idx
]) {
2107 qemu_iovec_memset(acb
->qiov
, done
, 0, len
);
2111 case AIOCB_WRITE_UDATA
:
2112 if (!inode
->data_vdi_id
[idx
]) {
2114 } else if (!is_data_obj_writable(inode
, idx
)) {
2118 flags
= SD_FLAG_CMD_COW
;
2121 case AIOCB_DISCARD_OBJ
:
2123 * We discard the object only when the whole object is
2124 * 1) allocated 2) trimmed. Otherwise, simply skip it.
2126 if (len
!= object_size
|| inode
->data_vdi_id
[idx
] == 0) {
2135 DPRINTF("update ino (%" PRIu32
") %" PRIu64
" %" PRIu64
" %ld\n",
2137 vid_to_data_oid(inode
->data_vdi_id
[idx
], idx
), idx
);
2138 oid
= vid_to_data_oid(inode
->vdi_id
, idx
);
2139 DPRINTF("new oid %" PRIx64
"\n", oid
);
2142 aio_req
= alloc_aio_req(s
, acb
, oid
, len
, offset
, flags
, create
,
2144 QLIST_INSERT_HEAD(&s
->inflight_aio_head
, aio_req
, aio_siblings
);
2146 add_aio_request(s
, aio_req
, acb
->qiov
->iov
, acb
->qiov
->niov
,
2154 if (!--acb
->nr_pending
) {
2160 static bool check_overwrapping_aiocb(BDRVSheepdogState
*s
, SheepdogAIOCB
*aiocb
)
2164 QLIST_FOREACH(cb
, &s
->inflight_aiocb_head
, aiocb_siblings
) {
2165 if (AIOCBOverwrapping(aiocb
, cb
)) {
2170 QLIST_INSERT_HEAD(&s
->inflight_aiocb_head
, aiocb
, aiocb_siblings
);
2174 static coroutine_fn
int sd_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
2175 int nb_sectors
, QEMUIOVector
*qiov
)
2179 int64_t offset
= (sector_num
+ nb_sectors
) * BDRV_SECTOR_SIZE
;
2180 BDRVSheepdogState
*s
= bs
->opaque
;
2182 if (offset
> s
->inode
.vdi_size
) {
2183 ret
= sd_truncate(bs
, offset
);
2189 acb
= sd_aio_setup(bs
, qiov
, sector_num
, nb_sectors
);
2190 acb
->aio_done_func
= sd_write_done
;
2191 acb
->aiocb_type
= AIOCB_WRITE_UDATA
;
2194 if (check_overwrapping_aiocb(s
, acb
)) {
2195 qemu_co_queue_wait(&s
->overwrapping_queue
);
2199 ret
= sd_co_rw_vector(acb
);
2201 QLIST_REMOVE(acb
, aiocb_siblings
);
2202 qemu_co_queue_restart_all(&s
->overwrapping_queue
);
2203 qemu_aio_unref(acb
);
2207 qemu_coroutine_yield();
2209 QLIST_REMOVE(acb
, aiocb_siblings
);
2210 qemu_co_queue_restart_all(&s
->overwrapping_queue
);
2215 static coroutine_fn
int sd_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
2216 int nb_sectors
, QEMUIOVector
*qiov
)
2220 BDRVSheepdogState
*s
= bs
->opaque
;
2222 acb
= sd_aio_setup(bs
, qiov
, sector_num
, nb_sectors
);
2223 acb
->aiocb_type
= AIOCB_READ_UDATA
;
2224 acb
->aio_done_func
= sd_finish_aiocb
;
2227 if (check_overwrapping_aiocb(s
, acb
)) {
2228 qemu_co_queue_wait(&s
->overwrapping_queue
);
2232 ret
= sd_co_rw_vector(acb
);
2234 QLIST_REMOVE(acb
, aiocb_siblings
);
2235 qemu_co_queue_restart_all(&s
->overwrapping_queue
);
2236 qemu_aio_unref(acb
);
2240 qemu_coroutine_yield();
2242 QLIST_REMOVE(acb
, aiocb_siblings
);
2243 qemu_co_queue_restart_all(&s
->overwrapping_queue
);
2247 static int coroutine_fn
sd_co_flush_to_disk(BlockDriverState
*bs
)
2249 BDRVSheepdogState
*s
= bs
->opaque
;
2253 if (s
->cache_flags
!= SD_FLAG_CMD_CACHE
) {
2257 acb
= sd_aio_setup(bs
, NULL
, 0, 0);
2258 acb
->aiocb_type
= AIOCB_FLUSH_CACHE
;
2259 acb
->aio_done_func
= sd_finish_aiocb
;
2261 aio_req
= alloc_aio_req(s
, acb
, vid_to_vdi_oid(s
->inode
.vdi_id
),
2262 0, 0, 0, false, 0, 0);
2263 QLIST_INSERT_HEAD(&s
->inflight_aio_head
, aio_req
, aio_siblings
);
2264 add_aio_request(s
, aio_req
, NULL
, 0, acb
->aiocb_type
);
2266 qemu_coroutine_yield();
2270 static int sd_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
)
2272 Error
*local_err
= NULL
;
2273 BDRVSheepdogState
*s
= bs
->opaque
;
2276 SheepdogInode
*inode
;
2277 unsigned int datalen
;
2279 DPRINTF("sn_info: name %s id_str %s s: name %s vm_state_size %" PRId64
" "
2280 "is_snapshot %d\n", sn_info
->name
, sn_info
->id_str
,
2281 s
->name
, sn_info
->vm_state_size
, s
->is_snapshot
);
2283 if (s
->is_snapshot
) {
2284 error_report("You can't create a snapshot of a snapshot VDI, "
2285 "%s (%" PRIu32
").", s
->name
, s
->inode
.vdi_id
);
2290 DPRINTF("%s %s\n", sn_info
->name
, sn_info
->id_str
);
2292 s
->inode
.vm_state_size
= sn_info
->vm_state_size
;
2293 s
->inode
.vm_clock_nsec
= sn_info
->vm_clock_nsec
;
2294 /* It appears that inode.tag does not require a NUL terminator,
2295 * which means this use of strncpy is ok.
2297 strncpy(s
->inode
.tag
, sn_info
->name
, sizeof(s
->inode
.tag
));
2298 /* we don't need to update entire object */
2299 datalen
= SD_INODE_SIZE
- sizeof(s
->inode
.data_vdi_id
);
2300 inode
= g_malloc(datalen
);
2302 /* refresh inode. */
2303 fd
= connect_to_sdog(s
, &local_err
);
2305 error_report_err(local_err
);
2310 ret
= write_object(fd
, s
->aio_context
, (char *)&s
->inode
,
2311 vid_to_vdi_oid(s
->inode
.vdi_id
), s
->inode
.nr_copies
,
2312 datalen
, 0, false, s
->cache_flags
);
2314 error_report("failed to write snapshot's inode.");
2318 ret
= do_sd_create(s
, &new_vid
, 1, &local_err
);
2320 error_report("failed to create inode for snapshot: %s",
2321 error_get_pretty(local_err
));
2322 error_free(local_err
);
2326 ret
= read_object(fd
, s
->aio_context
, (char *)inode
,
2327 vid_to_vdi_oid(new_vid
), s
->inode
.nr_copies
, datalen
, 0,
2331 error_report("failed to read new inode info. %s", strerror(errno
));
2335 memcpy(&s
->inode
, inode
, datalen
);
2336 DPRINTF("s->inode: name %s snap_id %x oid %x\n",
2337 s
->inode
.name
, s
->inode
.snap_id
, s
->inode
.vdi_id
);
2346 * We implement rollback(loadvm) operation to the specified snapshot by
2347 * 1) switch to the snapshot
2348 * 2) rely on sd_create_branch to delete working VDI and
2349 * 3) create a new working VDI based on the specified snapshot
2351 static int sd_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
)
2353 BDRVSheepdogState
*s
= bs
->opaque
;
2354 BDRVSheepdogState
*old_s
;
2355 char tag
[SD_MAX_VDI_TAG_LEN
];
2356 uint32_t snapid
= 0;
2359 old_s
= g_new(BDRVSheepdogState
, 1);
2361 memcpy(old_s
, s
, sizeof(BDRVSheepdogState
));
2363 snapid
= strtoul(snapshot_id
, NULL
, 10);
2367 pstrcpy(tag
, sizeof(tag
), snapshot_id
);
2370 ret
= reload_inode(s
, snapid
, tag
);
2375 ret
= sd_create_branch(s
);
2384 /* recover bdrv_sd_state */
2385 memcpy(s
, old_s
, sizeof(BDRVSheepdogState
));
2388 error_report("failed to open. recover old bdrv_sd_state.");
2393 static int sd_snapshot_delete(BlockDriverState
*bs
,
2394 const char *snapshot_id
,
2398 /* FIXME: Delete specified snapshot id. */
2402 static int sd_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
)
2404 Error
*local_err
= NULL
;
2405 BDRVSheepdogState
*s
= bs
->opaque
;
2407 int fd
, nr
= 1024, ret
, max
= BITS_TO_LONGS(SD_NR_VDIS
) * sizeof(long);
2408 QEMUSnapshotInfo
*sn_tab
= NULL
;
2409 unsigned wlen
, rlen
;
2411 static SheepdogInode inode
;
2412 unsigned long *vdi_inuse
;
2413 unsigned int start_nr
;
2417 vdi_inuse
= g_malloc(max
);
2419 fd
= connect_to_sdog(s
, &local_err
);
2421 error_report_err(local_err
);
2429 memset(&req
, 0, sizeof(req
));
2431 req
.opcode
= SD_OP_READ_VDIS
;
2432 req
.data_length
= max
;
2434 ret
= do_req(fd
, s
->aio_context
, (SheepdogReq
*)&req
,
2435 vdi_inuse
, &wlen
, &rlen
);
2442 sn_tab
= g_new0(QEMUSnapshotInfo
, nr
);
2444 /* calculate a vdi id with hash function */
2445 hval
= fnv_64a_buf(s
->name
, strlen(s
->name
), FNV1A_64_INIT
);
2446 start_nr
= hval
& (SD_NR_VDIS
- 1);
2448 fd
= connect_to_sdog(s
, &local_err
);
2450 error_report_err(local_err
);
2455 for (vid
= start_nr
; found
< nr
; vid
= (vid
+ 1) % SD_NR_VDIS
) {
2456 if (!test_bit(vid
, vdi_inuse
)) {
2460 /* we don't need to read entire object */
2461 ret
= read_object(fd
, s
->aio_context
, (char *)&inode
,
2462 vid_to_vdi_oid(vid
),
2463 0, SD_INODE_SIZE
- sizeof(inode
.data_vdi_id
), 0,
2470 if (!strcmp(inode
.name
, s
->name
) && is_snapshot(&inode
)) {
2471 sn_tab
[found
].date_sec
= inode
.snap_ctime
>> 32;
2472 sn_tab
[found
].date_nsec
= inode
.snap_ctime
& 0xffffffff;
2473 sn_tab
[found
].vm_state_size
= inode
.vm_state_size
;
2474 sn_tab
[found
].vm_clock_nsec
= inode
.vm_clock_nsec
;
2476 snprintf(sn_tab
[found
].id_str
, sizeof(sn_tab
[found
].id_str
),
2477 "%" PRIu32
, inode
.snap_id
);
2478 pstrcpy(sn_tab
[found
].name
,
2479 MIN(sizeof(sn_tab
[found
].name
), sizeof(inode
.tag
)),
2498 static int do_load_save_vmstate(BDRVSheepdogState
*s
, uint8_t *data
,
2499 int64_t pos
, int size
, int load
)
2501 Error
*local_err
= NULL
;
2503 int fd
, ret
= 0, remaining
= size
;
2504 unsigned int data_len
;
2505 uint64_t vmstate_oid
;
2508 uint32_t vdi_id
= load
? s
->inode
.parent_vdi_id
: s
->inode
.vdi_id
;
2509 uint32_t object_size
= (UINT32_C(1) << s
->inode
.block_size_shift
);
2511 fd
= connect_to_sdog(s
, &local_err
);
2513 error_report_err(local_err
);
2518 vdi_index
= pos
/ object_size
;
2519 offset
= pos
% object_size
;
2521 data_len
= MIN(remaining
, object_size
- offset
);
2523 vmstate_oid
= vid_to_vmstate_oid(vdi_id
, vdi_index
);
2525 create
= (offset
== 0);
2527 ret
= read_object(fd
, s
->aio_context
, (char *)data
, vmstate_oid
,
2528 s
->inode
.nr_copies
, data_len
, offset
,
2531 ret
= write_object(fd
, s
->aio_context
, (char *)data
, vmstate_oid
,
2532 s
->inode
.nr_copies
, data_len
, offset
, create
,
2537 error_report("failed to save vmstate %s", strerror(errno
));
2543 remaining
-= data_len
;
2551 static int sd_save_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
2554 BDRVSheepdogState
*s
= bs
->opaque
;
2558 buf
= qemu_blockalign(bs
, qiov
->size
);
2559 qemu_iovec_to_buf(qiov
, 0, buf
, qiov
->size
);
2560 ret
= do_load_save_vmstate(s
, (uint8_t *) buf
, pos
, qiov
->size
, 0);
2566 static int sd_load_vmstate(BlockDriverState
*bs
, uint8_t *data
,
2567 int64_t pos
, int size
)
2569 BDRVSheepdogState
*s
= bs
->opaque
;
2571 return do_load_save_vmstate(s
, data
, pos
, size
, 1);
2575 static coroutine_fn
int sd_co_discard(BlockDriverState
*bs
, int64_t sector_num
,
2580 BDRVSheepdogState
*s
= bs
->opaque
;
2583 if (!s
->discard_supported
) {
2587 acb
= sd_aio_setup(bs
, &dummy
, sector_num
, nb_sectors
);
2588 acb
->aiocb_type
= AIOCB_DISCARD_OBJ
;
2589 acb
->aio_done_func
= sd_finish_aiocb
;
2592 if (check_overwrapping_aiocb(s
, acb
)) {
2593 qemu_co_queue_wait(&s
->overwrapping_queue
);
2597 ret
= sd_co_rw_vector(acb
);
2599 QLIST_REMOVE(acb
, aiocb_siblings
);
2600 qemu_co_queue_restart_all(&s
->overwrapping_queue
);
2601 qemu_aio_unref(acb
);
2605 qemu_coroutine_yield();
2607 QLIST_REMOVE(acb
, aiocb_siblings
);
2608 qemu_co_queue_restart_all(&s
->overwrapping_queue
);
2613 static coroutine_fn
int64_t
2614 sd_co_get_block_status(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
2617 BDRVSheepdogState
*s
= bs
->opaque
;
2618 SheepdogInode
*inode
= &s
->inode
;
2619 uint32_t object_size
= (UINT32_C(1) << inode
->block_size_shift
);
2620 uint64_t offset
= sector_num
* BDRV_SECTOR_SIZE
;
2621 unsigned long start
= offset
/ object_size
,
2622 end
= DIV_ROUND_UP((sector_num
+ nb_sectors
) *
2623 BDRV_SECTOR_SIZE
, object_size
);
2625 int64_t ret
= BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
| offset
;
2627 for (idx
= start
; idx
< end
; idx
++) {
2628 if (inode
->data_vdi_id
[idx
] == 0) {
2633 /* Get the longest length of unallocated sectors */
2635 for (idx
= start
+ 1; idx
< end
; idx
++) {
2636 if (inode
->data_vdi_id
[idx
] != 0) {
2642 *pnum
= (idx
- start
) * object_size
/ BDRV_SECTOR_SIZE
;
2643 if (*pnum
> nb_sectors
) {
2649 static int64_t sd_get_allocated_file_size(BlockDriverState
*bs
)
2651 BDRVSheepdogState
*s
= bs
->opaque
;
2652 SheepdogInode
*inode
= &s
->inode
;
2653 uint32_t object_size
= (UINT32_C(1) << inode
->block_size_shift
);
2654 unsigned long i
, last
= DIV_ROUND_UP(inode
->vdi_size
, object_size
);
2657 for (i
= 0; i
< last
; i
++) {
2658 if (inode
->data_vdi_id
[i
] == 0) {
2661 size
+= object_size
;
2666 static QemuOptsList sd_create_opts
= {
2667 .name
= "sheepdog-create-opts",
2668 .head
= QTAILQ_HEAD_INITIALIZER(sd_create_opts
.head
),
2671 .name
= BLOCK_OPT_SIZE
,
2672 .type
= QEMU_OPT_SIZE
,
2673 .help
= "Virtual disk size"
2676 .name
= BLOCK_OPT_BACKING_FILE
,
2677 .type
= QEMU_OPT_STRING
,
2678 .help
= "File name of a base image"
2681 .name
= BLOCK_OPT_PREALLOC
,
2682 .type
= QEMU_OPT_STRING
,
2683 .help
= "Preallocation mode (allowed values: off, full)"
2686 .name
= BLOCK_OPT_REDUNDANCY
,
2687 .type
= QEMU_OPT_STRING
,
2688 .help
= "Redundancy of the image"
2691 .name
= BLOCK_OPT_OBJECT_SIZE
,
2692 .type
= QEMU_OPT_SIZE
,
2693 .help
= "Object size of the image"
2695 { /* end of list */ }
2699 static BlockDriver bdrv_sheepdog
= {
2700 .format_name
= "sheepdog",
2701 .protocol_name
= "sheepdog",
2702 .instance_size
= sizeof(BDRVSheepdogState
),
2703 .bdrv_needs_filename
= true,
2704 .bdrv_file_open
= sd_open
,
2705 .bdrv_close
= sd_close
,
2706 .bdrv_create
= sd_create
,
2707 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
2708 .bdrv_getlength
= sd_getlength
,
2709 .bdrv_get_allocated_file_size
= sd_get_allocated_file_size
,
2710 .bdrv_truncate
= sd_truncate
,
2712 .bdrv_co_readv
= sd_co_readv
,
2713 .bdrv_co_writev
= sd_co_writev
,
2714 .bdrv_co_flush_to_disk
= sd_co_flush_to_disk
,
2715 .bdrv_co_discard
= sd_co_discard
,
2716 .bdrv_co_get_block_status
= sd_co_get_block_status
,
2718 .bdrv_snapshot_create
= sd_snapshot_create
,
2719 .bdrv_snapshot_goto
= sd_snapshot_goto
,
2720 .bdrv_snapshot_delete
= sd_snapshot_delete
,
2721 .bdrv_snapshot_list
= sd_snapshot_list
,
2723 .bdrv_save_vmstate
= sd_save_vmstate
,
2724 .bdrv_load_vmstate
= sd_load_vmstate
,
2726 .bdrv_detach_aio_context
= sd_detach_aio_context
,
2727 .bdrv_attach_aio_context
= sd_attach_aio_context
,
2729 .create_opts
= &sd_create_opts
,
2732 static BlockDriver bdrv_sheepdog_tcp
= {
2733 .format_name
= "sheepdog",
2734 .protocol_name
= "sheepdog+tcp",
2735 .instance_size
= sizeof(BDRVSheepdogState
),
2736 .bdrv_needs_filename
= true,
2737 .bdrv_file_open
= sd_open
,
2738 .bdrv_close
= sd_close
,
2739 .bdrv_create
= sd_create
,
2740 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
2741 .bdrv_getlength
= sd_getlength
,
2742 .bdrv_get_allocated_file_size
= sd_get_allocated_file_size
,
2743 .bdrv_truncate
= sd_truncate
,
2745 .bdrv_co_readv
= sd_co_readv
,
2746 .bdrv_co_writev
= sd_co_writev
,
2747 .bdrv_co_flush_to_disk
= sd_co_flush_to_disk
,
2748 .bdrv_co_discard
= sd_co_discard
,
2749 .bdrv_co_get_block_status
= sd_co_get_block_status
,
2751 .bdrv_snapshot_create
= sd_snapshot_create
,
2752 .bdrv_snapshot_goto
= sd_snapshot_goto
,
2753 .bdrv_snapshot_delete
= sd_snapshot_delete
,
2754 .bdrv_snapshot_list
= sd_snapshot_list
,
2756 .bdrv_save_vmstate
= sd_save_vmstate
,
2757 .bdrv_load_vmstate
= sd_load_vmstate
,
2759 .bdrv_detach_aio_context
= sd_detach_aio_context
,
2760 .bdrv_attach_aio_context
= sd_attach_aio_context
,
2762 .create_opts
= &sd_create_opts
,
2765 static BlockDriver bdrv_sheepdog_unix
= {
2766 .format_name
= "sheepdog",
2767 .protocol_name
= "sheepdog+unix",
2768 .instance_size
= sizeof(BDRVSheepdogState
),
2769 .bdrv_needs_filename
= true,
2770 .bdrv_file_open
= sd_open
,
2771 .bdrv_close
= sd_close
,
2772 .bdrv_create
= sd_create
,
2773 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
2774 .bdrv_getlength
= sd_getlength
,
2775 .bdrv_get_allocated_file_size
= sd_get_allocated_file_size
,
2776 .bdrv_truncate
= sd_truncate
,
2778 .bdrv_co_readv
= sd_co_readv
,
2779 .bdrv_co_writev
= sd_co_writev
,
2780 .bdrv_co_flush_to_disk
= sd_co_flush_to_disk
,
2781 .bdrv_co_discard
= sd_co_discard
,
2782 .bdrv_co_get_block_status
= sd_co_get_block_status
,
2784 .bdrv_snapshot_create
= sd_snapshot_create
,
2785 .bdrv_snapshot_goto
= sd_snapshot_goto
,
2786 .bdrv_snapshot_delete
= sd_snapshot_delete
,
2787 .bdrv_snapshot_list
= sd_snapshot_list
,
2789 .bdrv_save_vmstate
= sd_save_vmstate
,
2790 .bdrv_load_vmstate
= sd_load_vmstate
,
2792 .bdrv_detach_aio_context
= sd_detach_aio_context
,
2793 .bdrv_attach_aio_context
= sd_attach_aio_context
,
2795 .create_opts
= &sd_create_opts
,
2798 static void bdrv_sheepdog_init(void)
2800 bdrv_register(&bdrv_sheepdog
);
2801 bdrv_register(&bdrv_sheepdog_tcp
);
2802 bdrv_register(&bdrv_sheepdog_unix
);
2804 block_init(bdrv_sheepdog_init
);