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 */
32 #define SD_OP_NEW_VDI 0x11
33 #define SD_OP_LOCK_VDI 0x12
34 #define SD_OP_RELEASE_VDI 0x13
35 #define SD_OP_GET_VDI_INFO 0x14
36 #define SD_OP_READ_VDIS 0x15
37 #define SD_OP_FLUSH_VDI 0x16
38 #define SD_OP_DEL_VDI 0x17
39 #define SD_OP_GET_CLUSTER_DEFAULT 0x18
41 #define SD_FLAG_CMD_WRITE 0x01
42 #define SD_FLAG_CMD_COW 0x02
43 #define SD_FLAG_CMD_CACHE 0x04 /* Writeback mode for cache */
44 #define SD_FLAG_CMD_DIRECT 0x08 /* Don't use cache */
46 #define SD_RES_SUCCESS 0x00 /* Success */
47 #define SD_RES_UNKNOWN 0x01 /* Unknown error */
48 #define SD_RES_NO_OBJ 0x02 /* No object found */
49 #define SD_RES_EIO 0x03 /* I/O error */
50 #define SD_RES_VDI_EXIST 0x04 /* Vdi exists already */
51 #define SD_RES_INVALID_PARMS 0x05 /* Invalid parameters */
52 #define SD_RES_SYSTEM_ERROR 0x06 /* System error */
53 #define SD_RES_VDI_LOCKED 0x07 /* Vdi is locked */
54 #define SD_RES_NO_VDI 0x08 /* No vdi found */
55 #define SD_RES_NO_BASE_VDI 0x09 /* No base vdi found */
56 #define SD_RES_VDI_READ 0x0A /* Cannot read requested vdi */
57 #define SD_RES_VDI_WRITE 0x0B /* Cannot write requested vdi */
58 #define SD_RES_BASE_VDI_READ 0x0C /* Cannot read base vdi */
59 #define SD_RES_BASE_VDI_WRITE 0x0D /* Cannot write base vdi */
60 #define SD_RES_NO_TAG 0x0E /* Requested tag is not found */
61 #define SD_RES_STARTUP 0x0F /* Sheepdog is on starting up */
62 #define SD_RES_VDI_NOT_LOCKED 0x10 /* Vdi is not locked */
63 #define SD_RES_SHUTDOWN 0x11 /* Sheepdog is shutting down */
64 #define SD_RES_NO_MEM 0x12 /* Cannot allocate memory */
65 #define SD_RES_FULL_VDI 0x13 /* we already have the maximum vdis */
66 #define SD_RES_VER_MISMATCH 0x14 /* Protocol version mismatch */
67 #define SD_RES_NO_SPACE 0x15 /* Server has no room for new objects */
68 #define SD_RES_WAIT_FOR_FORMAT 0x16 /* Waiting for a format operation */
69 #define SD_RES_WAIT_FOR_JOIN 0x17 /* Waiting for other nodes joining */
70 #define SD_RES_JOIN_FAILED 0x18 /* Target node had failed to join sheepdog */
71 #define SD_RES_HALT 0x19 /* Sheepdog is stopped serving IO request */
72 #define SD_RES_READONLY 0x1A /* Object is read-only */
77 * 0 - 19 (20 bits): data object space
78 * 20 - 31 (12 bits): reserved data object space
79 * 32 - 55 (24 bits): vdi object space
80 * 56 - 59 ( 4 bits): reserved vdi object space
81 * 60 - 63 ( 4 bits): object type identifier space
84 #define VDI_SPACE_SHIFT 32
85 #define VDI_BIT (UINT64_C(1) << 63)
86 #define VMSTATE_BIT (UINT64_C(1) << 62)
87 #define MAX_DATA_OBJS (UINT64_C(1) << 20)
88 #define MAX_CHILDREN 1024
89 #define SD_MAX_VDI_LEN 256
90 #define SD_MAX_VDI_TAG_LEN 256
91 #define SD_NR_VDIS (1U << 24)
92 #define SD_DATA_OBJ_SIZE (UINT64_C(1) << 22)
93 #define SD_MAX_VDI_SIZE (SD_DATA_OBJ_SIZE * MAX_DATA_OBJS)
94 #define SD_DEFAULT_BLOCK_SIZE_SHIFT 22
96 * For erasure coding, we use at most SD_EC_MAX_STRIP for data strips and
97 * (SD_EC_MAX_STRIP - 1) for parity strips
99 * SD_MAX_COPIES is sum of number of data strips and parity strips.
101 #define SD_EC_MAX_STRIP 16
102 #define SD_MAX_COPIES (SD_EC_MAX_STRIP * 2 - 1)
104 #define SD_INODE_SIZE (sizeof(SheepdogInode))
105 #define CURRENT_VDI_ID 0
107 #define LOCK_TYPE_NORMAL 0
108 #define LOCK_TYPE_SHARED 1 /* for iSCSI multipath */
110 typedef struct SheepdogReq
{
116 uint32_t data_length
;
117 uint32_t opcode_specific
[8];
120 typedef struct SheepdogRsp
{
126 uint32_t data_length
;
128 uint32_t opcode_specific
[7];
131 typedef struct SheepdogObjReq
{
137 uint32_t data_length
;
146 typedef struct SheepdogObjRsp
{
152 uint32_t data_length
;
160 typedef struct SheepdogVdiReq
{
166 uint32_t data_length
;
168 uint32_t base_vdi_id
;
171 uint8_t store_policy
;
172 uint8_t block_size_shift
;
178 typedef struct SheepdogVdiRsp
{
184 uint32_t data_length
;
191 typedef struct SheepdogClusterRsp
{
197 uint32_t data_length
;
201 uint8_t block_size_shift
;
204 } SheepdogClusterRsp
;
206 typedef struct SheepdogInode
{
207 char name
[SD_MAX_VDI_LEN
];
208 char tag
[SD_MAX_VDI_TAG_LEN
];
211 uint64_t vm_clock_nsec
;
213 uint64_t vm_state_size
;
214 uint16_t copy_policy
;
216 uint8_t block_size_shift
;
219 uint32_t parent_vdi_id
;
220 uint32_t child_vdi_id
[MAX_CHILDREN
];
221 uint32_t data_vdi_id
[MAX_DATA_OBJS
];
224 #define SD_INODE_HEADER_SIZE offsetof(SheepdogInode, data_vdi_id)
227 * 64 bit FNV-1a non-zero initial basis
229 #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
232 * 64 bit Fowler/Noll/Vo FNV-1a hash code
234 static inline uint64_t fnv_64a_buf(void *buf
, size_t len
, uint64_t hval
)
236 unsigned char *bp
= buf
;
237 unsigned char *be
= bp
+ len
;
239 hval
^= (uint64_t) *bp
++;
240 hval
+= (hval
<< 1) + (hval
<< 4) + (hval
<< 5) +
241 (hval
<< 7) + (hval
<< 8) + (hval
<< 40);
246 static inline bool is_data_obj_writable(SheepdogInode
*inode
, unsigned int idx
)
248 return inode
->vdi_id
== inode
->data_vdi_id
[idx
];
251 static inline bool is_data_obj(uint64_t oid
)
253 return !(VDI_BIT
& oid
);
256 static inline uint64_t data_oid_to_idx(uint64_t oid
)
258 return oid
& (MAX_DATA_OBJS
- 1);
261 static inline uint32_t oid_to_vid(uint64_t oid
)
263 return (oid
& ~VDI_BIT
) >> VDI_SPACE_SHIFT
;
266 static inline uint64_t vid_to_vdi_oid(uint32_t vid
)
268 return VDI_BIT
| ((uint64_t)vid
<< VDI_SPACE_SHIFT
);
271 static inline uint64_t vid_to_vmstate_oid(uint32_t vid
, uint32_t idx
)
273 return VMSTATE_BIT
| ((uint64_t)vid
<< VDI_SPACE_SHIFT
) | idx
;
276 static inline uint64_t vid_to_data_oid(uint32_t vid
, uint32_t idx
)
278 return ((uint64_t)vid
<< VDI_SPACE_SHIFT
) | idx
;
281 static inline bool is_snapshot(struct SheepdogInode
*inode
)
283 return !!inode
->snap_ctime
;
288 #define DPRINTF(fmt, args...) \
290 fprintf(stdout, "%s %d: " fmt, __func__, __LINE__, ##args); \
293 #define DPRINTF(fmt, args...)
296 typedef struct SheepdogAIOCB SheepdogAIOCB
;
298 typedef struct AIOReq
{
299 SheepdogAIOCB
*aiocb
;
300 unsigned int iov_offset
;
305 unsigned int data_len
;
310 QLIST_ENTRY(AIOReq
) aio_siblings
;
320 #define AIOCBOverlapping(x, y) \
321 (!(x->max_affect_data_idx < y->min_affect_data_idx \
322 || y->max_affect_data_idx < x->min_affect_data_idx))
324 struct SheepdogAIOCB
{
333 enum AIOCBState aiocb_type
;
335 Coroutine
*coroutine
;
336 void (*aio_done_func
)(SheepdogAIOCB
*);
341 uint32_t min_affect_data_idx
;
342 uint32_t max_affect_data_idx
;
345 * The difference between affect_data_idx and dirty_data_idx:
346 * affect_data_idx represents range of index of all request types.
347 * dirty_data_idx represents range of index updated by COW requests.
348 * dirty_data_idx is used for updating an inode object.
350 uint32_t min_dirty_data_idx
;
351 uint32_t max_dirty_data_idx
;
353 QLIST_ENTRY(SheepdogAIOCB
) aiocb_siblings
;
356 typedef struct BDRVSheepdogState
{
357 BlockDriverState
*bs
;
358 AioContext
*aio_context
;
362 char name
[SD_MAX_VDI_LEN
];
364 uint32_t cache_flags
;
365 bool discard_supported
;
375 uint32_t aioreq_seq_num
;
377 /* Every aio request must be linked to either of these queues. */
378 QLIST_HEAD(inflight_aio_head
, AIOReq
) inflight_aio_head
;
379 QLIST_HEAD(failed_aio_head
, AIOReq
) failed_aio_head
;
381 CoQueue overlapping_queue
;
382 QLIST_HEAD(inflight_aiocb_head
, SheepdogAIOCB
) inflight_aiocb_head
;
385 typedef struct BDRVSheepdogReopenState
{
388 } BDRVSheepdogReopenState
;
390 static const char * sd_strerror(int err
)
394 static const struct {
398 {SD_RES_SUCCESS
, "Success"},
399 {SD_RES_UNKNOWN
, "Unknown error"},
400 {SD_RES_NO_OBJ
, "No object found"},
401 {SD_RES_EIO
, "I/O error"},
402 {SD_RES_VDI_EXIST
, "VDI exists already"},
403 {SD_RES_INVALID_PARMS
, "Invalid parameters"},
404 {SD_RES_SYSTEM_ERROR
, "System error"},
405 {SD_RES_VDI_LOCKED
, "VDI is already locked"},
406 {SD_RES_NO_VDI
, "No vdi found"},
407 {SD_RES_NO_BASE_VDI
, "No base VDI found"},
408 {SD_RES_VDI_READ
, "Failed read the requested VDI"},
409 {SD_RES_VDI_WRITE
, "Failed to write the requested VDI"},
410 {SD_RES_BASE_VDI_READ
, "Failed to read the base VDI"},
411 {SD_RES_BASE_VDI_WRITE
, "Failed to write the base VDI"},
412 {SD_RES_NO_TAG
, "Failed to find the requested tag"},
413 {SD_RES_STARTUP
, "The system is still booting"},
414 {SD_RES_VDI_NOT_LOCKED
, "VDI isn't locked"},
415 {SD_RES_SHUTDOWN
, "The system is shutting down"},
416 {SD_RES_NO_MEM
, "Out of memory on the server"},
417 {SD_RES_FULL_VDI
, "We already have the maximum vdis"},
418 {SD_RES_VER_MISMATCH
, "Protocol version mismatch"},
419 {SD_RES_NO_SPACE
, "Server has no space for new objects"},
420 {SD_RES_WAIT_FOR_FORMAT
, "Sheepdog is waiting for a format operation"},
421 {SD_RES_WAIT_FOR_JOIN
, "Sheepdog is waiting for other nodes joining"},
422 {SD_RES_JOIN_FAILED
, "Target node had failed to join sheepdog"},
423 {SD_RES_HALT
, "Sheepdog is stopped serving IO request"},
424 {SD_RES_READONLY
, "Object is read-only"},
427 for (i
= 0; i
< ARRAY_SIZE(errors
); ++i
) {
428 if (errors
[i
].err
== err
) {
429 return errors
[i
].desc
;
433 return "Invalid error code";
437 * Sheepdog I/O handling:
439 * 1. In sd_co_rw_vector, we send the I/O requests to the server and
440 * link the requests to the inflight_list in the
441 * BDRVSheepdogState. The function exits without waiting for
442 * receiving the response.
444 * 2. We receive the response in aio_read_response, the fd handler to
445 * the sheepdog connection. If metadata update is needed, we send
446 * the write request to the vdi object in sd_write_done, the write
447 * completion function. We switch back to sd_co_readv/writev after
448 * all the requests belonging to the AIOCB are finished.
451 static inline AIOReq
*alloc_aio_req(BDRVSheepdogState
*s
, SheepdogAIOCB
*acb
,
452 uint64_t oid
, unsigned int data_len
,
453 uint64_t offset
, uint8_t flags
, bool create
,
454 uint64_t base_oid
, unsigned int iov_offset
)
458 aio_req
= g_malloc(sizeof(*aio_req
));
459 aio_req
->aiocb
= acb
;
460 aio_req
->iov_offset
= iov_offset
;
462 aio_req
->base_oid
= base_oid
;
463 aio_req
->offset
= offset
;
464 aio_req
->data_len
= data_len
;
465 aio_req
->flags
= flags
;
466 aio_req
->id
= s
->aioreq_seq_num
++;
467 aio_req
->create
= create
;
473 static inline void free_aio_req(BDRVSheepdogState
*s
, AIOReq
*aio_req
)
475 SheepdogAIOCB
*acb
= aio_req
->aiocb
;
477 acb
->cancelable
= false;
478 QLIST_REMOVE(aio_req
, aio_siblings
);
484 static void coroutine_fn
sd_finish_aiocb(SheepdogAIOCB
*acb
)
486 qemu_coroutine_enter(acb
->coroutine
, NULL
);
491 * Check whether the specified acb can be canceled
493 * We can cancel aio when any request belonging to the acb is:
494 * - Not processed by the sheepdog server.
495 * - Not linked to the inflight queue.
497 static bool sd_acb_cancelable(const SheepdogAIOCB
*acb
)
499 BDRVSheepdogState
*s
= acb
->common
.bs
->opaque
;
502 if (!acb
->cancelable
) {
506 QLIST_FOREACH(aioreq
, &s
->inflight_aio_head
, aio_siblings
) {
507 if (aioreq
->aiocb
== acb
) {
515 static void sd_aio_cancel(BlockAIOCB
*blockacb
)
517 SheepdogAIOCB
*acb
= (SheepdogAIOCB
*)blockacb
;
518 BDRVSheepdogState
*s
= acb
->common
.bs
->opaque
;
519 AIOReq
*aioreq
, *next
;
521 if (sd_acb_cancelable(acb
)) {
522 /* Remove outstanding requests from failed queue. */
523 QLIST_FOREACH_SAFE(aioreq
, &s
->failed_aio_head
, aio_siblings
,
525 if (aioreq
->aiocb
== acb
) {
526 free_aio_req(s
, aioreq
);
530 assert(acb
->nr_pending
== 0);
531 if (acb
->common
.cb
) {
532 acb
->common
.cb(acb
->common
.opaque
, -ECANCELED
);
534 sd_finish_aiocb(acb
);
538 static const AIOCBInfo sd_aiocb_info
= {
539 .aiocb_size
= sizeof(SheepdogAIOCB
),
540 .cancel_async
= sd_aio_cancel
,
543 static SheepdogAIOCB
*sd_aio_setup(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
544 int64_t sector_num
, int nb_sectors
)
547 uint32_t object_size
;
548 BDRVSheepdogState
*s
= bs
->opaque
;
550 object_size
= (UINT32_C(1) << s
->inode
.block_size_shift
);
552 acb
= qemu_aio_get(&sd_aiocb_info
, bs
, NULL
, NULL
);
556 acb
->sector_num
= sector_num
;
557 acb
->nb_sectors
= nb_sectors
;
559 acb
->aio_done_func
= NULL
;
560 acb
->cancelable
= true;
561 acb
->coroutine
= qemu_coroutine_self();
565 acb
->min_affect_data_idx
= acb
->sector_num
* BDRV_SECTOR_SIZE
/ object_size
;
566 acb
->max_affect_data_idx
= (acb
->sector_num
* BDRV_SECTOR_SIZE
+
567 acb
->nb_sectors
* BDRV_SECTOR_SIZE
) / object_size
;
569 acb
->min_dirty_data_idx
= UINT32_MAX
;
570 acb
->max_dirty_data_idx
= 0;
575 /* Return -EIO in case of error, file descriptor on success */
576 static int connect_to_sdog(BDRVSheepdogState
*s
, Error
**errp
)
581 fd
= unix_connect(s
->host_spec
, errp
);
583 fd
= inet_connect(s
->host_spec
, errp
);
586 int ret
= socket_set_nodelay(fd
);
588 error_report("%s", strerror(errno
));
594 qemu_set_nonblock(fd
);
602 /* Return 0 on success and -errno in case of error */
603 static coroutine_fn
int send_co_req(int sockfd
, SheepdogReq
*hdr
, void *data
,
608 ret
= qemu_co_send(sockfd
, hdr
, sizeof(*hdr
));
609 if (ret
!= sizeof(*hdr
)) {
610 error_report("failed to send a req, %s", strerror(errno
));
611 ret
= -socket_error();
615 ret
= qemu_co_send(sockfd
, data
, *wlen
);
617 ret
= -socket_error();
618 error_report("failed to send a req, %s", strerror(errno
));
624 static void restart_co_req(void *opaque
)
626 Coroutine
*co
= opaque
;
628 qemu_coroutine_enter(co
, NULL
);
631 typedef struct SheepdogReqCo
{
633 AioContext
*aio_context
;
642 static coroutine_fn
void do_co_req(void *opaque
)
646 SheepdogReqCo
*srco
= opaque
;
647 int sockfd
= srco
->sockfd
;
648 SheepdogReq
*hdr
= srco
->hdr
;
649 void *data
= srco
->data
;
650 unsigned int *wlen
= srco
->wlen
;
651 unsigned int *rlen
= srco
->rlen
;
653 co
= qemu_coroutine_self();
654 aio_set_fd_handler(srco
->aio_context
, sockfd
, NULL
, restart_co_req
, co
);
656 ret
= send_co_req(sockfd
, hdr
, data
, wlen
);
661 aio_set_fd_handler(srco
->aio_context
, sockfd
, restart_co_req
, NULL
, co
);
663 ret
= qemu_co_recv(sockfd
, hdr
, sizeof(*hdr
));
664 if (ret
!= sizeof(*hdr
)) {
665 error_report("failed to get a rsp, %s", strerror(errno
));
670 if (*rlen
> hdr
->data_length
) {
671 *rlen
= hdr
->data_length
;
675 ret
= qemu_co_recv(sockfd
, data
, *rlen
);
677 error_report("failed to get the data, %s", strerror(errno
));
684 /* there is at most one request for this sockfd, so it is safe to
685 * set each handler to NULL. */
686 aio_set_fd_handler(srco
->aio_context
, sockfd
, NULL
, NULL
, NULL
);
689 srco
->finished
= true;
693 * Send the request to the sheep in a synchronous manner.
695 * Return 0 on success, -errno in case of error.
697 static int do_req(int sockfd
, AioContext
*aio_context
, SheepdogReq
*hdr
,
698 void *data
, unsigned int *wlen
, unsigned int *rlen
)
701 SheepdogReqCo srco
= {
703 .aio_context
= aio_context
,
712 if (qemu_in_coroutine()) {
715 co
= qemu_coroutine_create(do_co_req
);
716 qemu_coroutine_enter(co
, &srco
);
717 while (!srco
.finished
) {
718 aio_poll(aio_context
, true);
725 static void coroutine_fn
add_aio_request(BDRVSheepdogState
*s
, AIOReq
*aio_req
,
726 struct iovec
*iov
, int niov
,
727 enum AIOCBState aiocb_type
);
728 static void coroutine_fn
resend_aioreq(BDRVSheepdogState
*s
, AIOReq
*aio_req
);
729 static int reload_inode(BDRVSheepdogState
*s
, uint32_t snapid
, const char *tag
);
730 static int get_sheep_fd(BDRVSheepdogState
*s
, Error
**errp
);
731 static void co_write_request(void *opaque
);
733 static coroutine_fn
void reconnect_to_sdog(void *opaque
)
735 BDRVSheepdogState
*s
= opaque
;
736 AIOReq
*aio_req
, *next
;
738 aio_set_fd_handler(s
->aio_context
, s
->fd
, NULL
, NULL
, NULL
);
742 /* Wait for outstanding write requests to be completed. */
743 while (s
->co_send
!= NULL
) {
744 co_write_request(opaque
);
747 /* Try to reconnect the sheepdog server every one second. */
749 Error
*local_err
= NULL
;
750 s
->fd
= get_sheep_fd(s
, &local_err
);
752 DPRINTF("Wait for connection to be established\n");
753 error_report_err(local_err
);
754 co_aio_sleep_ns(bdrv_get_aio_context(s
->bs
), QEMU_CLOCK_REALTIME
,
760 * Now we have to resend all the request in the inflight queue. However,
761 * resend_aioreq() can yield and newly created requests can be added to the
762 * inflight queue before the coroutine is resumed. To avoid mixing them, we
763 * have to move all the inflight requests to the failed queue before
764 * resend_aioreq() is called.
766 QLIST_FOREACH_SAFE(aio_req
, &s
->inflight_aio_head
, aio_siblings
, next
) {
767 QLIST_REMOVE(aio_req
, aio_siblings
);
768 QLIST_INSERT_HEAD(&s
->failed_aio_head
, aio_req
, aio_siblings
);
771 /* Resend all the failed aio requests. */
772 while (!QLIST_EMPTY(&s
->failed_aio_head
)) {
773 aio_req
= QLIST_FIRST(&s
->failed_aio_head
);
774 QLIST_REMOVE(aio_req
, aio_siblings
);
775 QLIST_INSERT_HEAD(&s
->inflight_aio_head
, aio_req
, aio_siblings
);
776 resend_aioreq(s
, aio_req
);
781 * Receive responses of the I/O requests.
783 * This function is registered as a fd handler, and called from the
784 * main loop when s->fd is ready for reading responses.
786 static void coroutine_fn
aio_read_response(void *opaque
)
789 BDRVSheepdogState
*s
= opaque
;
792 AIOReq
*aio_req
= NULL
;
797 ret
= qemu_co_recv(fd
, &rsp
, sizeof(rsp
));
798 if (ret
!= sizeof(rsp
)) {
799 error_report("failed to get the header, %s", strerror(errno
));
803 /* find the right aio_req from the inflight aio list */
804 QLIST_FOREACH(aio_req
, &s
->inflight_aio_head
, aio_siblings
) {
805 if (aio_req
->id
== rsp
.id
) {
810 error_report("cannot find aio_req %x", rsp
.id
);
814 acb
= aio_req
->aiocb
;
816 switch (acb
->aiocb_type
) {
817 case AIOCB_WRITE_UDATA
:
818 /* this coroutine context is no longer suitable for co_recv
819 * because we may send data to update vdi objects */
821 if (!is_data_obj(aio_req
->oid
)) {
824 idx
= data_oid_to_idx(aio_req
->oid
);
826 if (aio_req
->create
) {
828 * If the object is newly created one, we need to update
829 * the vdi object (metadata object). min_dirty_data_idx
830 * and max_dirty_data_idx are changed to include updated
831 * index between them.
833 if (rsp
.result
== SD_RES_SUCCESS
) {
834 s
->inode
.data_vdi_id
[idx
] = s
->inode
.vdi_id
;
835 acb
->max_dirty_data_idx
= MAX(idx
, acb
->max_dirty_data_idx
);
836 acb
->min_dirty_data_idx
= MIN(idx
, acb
->min_dirty_data_idx
);
840 case AIOCB_READ_UDATA
:
841 ret
= qemu_co_recvv(fd
, acb
->qiov
->iov
, acb
->qiov
->niov
,
842 aio_req
->iov_offset
, rsp
.data_length
);
843 if (ret
!= rsp
.data_length
) {
844 error_report("failed to get the data, %s", strerror(errno
));
848 case AIOCB_FLUSH_CACHE
:
849 if (rsp
.result
== SD_RES_INVALID_PARMS
) {
850 DPRINTF("disable cache since the server doesn't support it\n");
851 s
->cache_flags
= SD_FLAG_CMD_DIRECT
;
852 rsp
.result
= SD_RES_SUCCESS
;
855 case AIOCB_DISCARD_OBJ
:
856 switch (rsp
.result
) {
857 case SD_RES_INVALID_PARMS
:
858 error_report("sheep(%s) doesn't support discard command",
860 rsp
.result
= SD_RES_SUCCESS
;
861 s
->discard_supported
= false;
868 switch (rsp
.result
) {
871 case SD_RES_READONLY
:
872 if (s
->inode
.vdi_id
== oid_to_vid(aio_req
->oid
)) {
873 ret
= reload_inode(s
, 0, "");
878 if (is_data_obj(aio_req
->oid
)) {
879 aio_req
->oid
= vid_to_data_oid(s
->inode
.vdi_id
,
880 data_oid_to_idx(aio_req
->oid
));
882 aio_req
->oid
= vid_to_vdi_oid(s
->inode
.vdi_id
);
884 resend_aioreq(s
, aio_req
);
888 error_report("%s", sd_strerror(rsp
.result
));
892 free_aio_req(s
, aio_req
);
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 acb
->aio_done_func(acb
);
905 reconnect_to_sdog(opaque
);
908 static void co_read_response(void *opaque
)
910 BDRVSheepdogState
*s
= opaque
;
913 s
->co_recv
= qemu_coroutine_create(aio_read_response
);
916 qemu_coroutine_enter(s
->co_recv
, opaque
);
919 static void co_write_request(void *opaque
)
921 BDRVSheepdogState
*s
= opaque
;
923 qemu_coroutine_enter(s
->co_send
, NULL
);
927 * Return a socket descriptor to read/write objects.
929 * We cannot use this descriptor for other operations because
930 * the block driver may be on waiting response from the server.
932 static int get_sheep_fd(BDRVSheepdogState
*s
, Error
**errp
)
936 fd
= connect_to_sdog(s
, errp
);
941 aio_set_fd_handler(s
->aio_context
, fd
, co_read_response
, NULL
, s
);
945 static int sd_parse_uri(BDRVSheepdogState
*s
, const char *filename
,
946 char *vdi
, uint32_t *snapid
, char *tag
)
949 QueryParams
*qp
= NULL
;
952 uri
= uri_parse(filename
);
958 if (!strcmp(uri
->scheme
, "sheepdog")) {
960 } else if (!strcmp(uri
->scheme
, "sheepdog+tcp")) {
962 } else if (!strcmp(uri
->scheme
, "sheepdog+unix")) {
969 if (uri
->path
== NULL
|| !strcmp(uri
->path
, "/")) {
973 pstrcpy(vdi
, SD_MAX_VDI_LEN
, uri
->path
+ 1);
975 qp
= query_params_parse(uri
->query
);
976 if (qp
->n
> 1 || (s
->is_unix
&& !qp
->n
) || (!s
->is_unix
&& qp
->n
)) {
982 /* sheepdog+unix:///vdiname?socket=path */
983 if (uri
->server
|| uri
->port
|| strcmp(qp
->p
[0].name
, "socket")) {
987 s
->host_spec
= g_strdup(qp
->p
[0].value
);
989 /* sheepdog[+tcp]://[host:port]/vdiname */
990 s
->host_spec
= g_strdup_printf("%s:%d", uri
->server
?: SD_DEFAULT_ADDR
,
991 uri
->port
?: SD_DEFAULT_PORT
);
996 *snapid
= strtoul(uri
->fragment
, NULL
, 10);
998 pstrcpy(tag
, SD_MAX_VDI_TAG_LEN
, uri
->fragment
);
1001 *snapid
= CURRENT_VDI_ID
; /* search current vdi */
1006 query_params_free(qp
);
1013 * Parse a filename (old syntax)
1015 * filename must be one of the following formats:
1017 * 2. [vdiname]:[snapid]
1018 * 3. [vdiname]:[tag]
1019 * 4. [hostname]:[port]:[vdiname]
1020 * 5. [hostname]:[port]:[vdiname]:[snapid]
1021 * 6. [hostname]:[port]:[vdiname]:[tag]
1023 * You can boot from the snapshot images by specifying `snapid` or
1026 * You can run VMs outside the Sheepdog cluster by specifying
1027 * `hostname' and `port' (experimental).
1029 static int parse_vdiname(BDRVSheepdogState
*s
, const char *filename
,
1030 char *vdi
, uint32_t *snapid
, char *tag
)
1033 const char *host_spec
, *vdi_spec
;
1036 strstart(filename
, "sheepdog:", (const char **)&filename
);
1037 p
= q
= g_strdup(filename
);
1039 /* count the number of separators */
1049 /* use the first two tokens as host_spec. */
1062 p
= strchr(vdi_spec
, ':');
1067 uri
= g_strdup_printf("sheepdog://%s/%s", host_spec
, vdi_spec
);
1069 ret
= sd_parse_uri(s
, uri
, vdi
, snapid
, tag
);
1077 static int find_vdi_name(BDRVSheepdogState
*s
, const char *filename
,
1078 uint32_t snapid
, const char *tag
, uint32_t *vid
,
1079 bool lock
, Error
**errp
)
1083 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1084 unsigned int wlen
, rlen
= 0;
1085 char buf
[SD_MAX_VDI_LEN
+ SD_MAX_VDI_TAG_LEN
];
1087 fd
= connect_to_sdog(s
, errp
);
1092 /* This pair of strncpy calls ensures that the buffer is zero-filled,
1093 * which is desirable since we'll soon be sending those bytes, and
1094 * don't want the send_req to read uninitialized data.
1096 strncpy(buf
, filename
, SD_MAX_VDI_LEN
);
1097 strncpy(buf
+ SD_MAX_VDI_LEN
, tag
, SD_MAX_VDI_TAG_LEN
);
1099 memset(&hdr
, 0, sizeof(hdr
));
1101 hdr
.opcode
= SD_OP_LOCK_VDI
;
1102 hdr
.type
= LOCK_TYPE_NORMAL
;
1104 hdr
.opcode
= SD_OP_GET_VDI_INFO
;
1106 wlen
= SD_MAX_VDI_LEN
+ SD_MAX_VDI_TAG_LEN
;
1107 hdr
.proto_ver
= SD_PROTO_VER
;
1108 hdr
.data_length
= wlen
;
1109 hdr
.snapid
= snapid
;
1110 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1112 ret
= do_req(fd
, s
->aio_context
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1114 error_setg_errno(errp
, -ret
, "cannot get vdi info");
1118 if (rsp
->result
!= SD_RES_SUCCESS
) {
1119 error_setg(errp
, "cannot get vdi info, %s, %s %" PRIu32
" %s",
1120 sd_strerror(rsp
->result
), filename
, snapid
, tag
);
1121 if (rsp
->result
== SD_RES_NO_VDI
) {
1123 } else if (rsp
->result
== SD_RES_VDI_LOCKED
) {
1138 static void coroutine_fn
add_aio_request(BDRVSheepdogState
*s
, AIOReq
*aio_req
,
1139 struct iovec
*iov
, int niov
,
1140 enum AIOCBState aiocb_type
)
1142 int nr_copies
= s
->inode
.nr_copies
;
1144 unsigned int wlen
= 0;
1146 uint64_t oid
= aio_req
->oid
;
1147 unsigned int datalen
= aio_req
->data_len
;
1148 uint64_t offset
= aio_req
->offset
;
1149 uint8_t flags
= aio_req
->flags
;
1150 uint64_t old_oid
= aio_req
->base_oid
;
1151 bool create
= aio_req
->create
;
1154 error_report("bug");
1157 memset(&hdr
, 0, sizeof(hdr
));
1159 switch (aiocb_type
) {
1160 case AIOCB_FLUSH_CACHE
:
1161 hdr
.opcode
= SD_OP_FLUSH_VDI
;
1163 case AIOCB_READ_UDATA
:
1164 hdr
.opcode
= SD_OP_READ_OBJ
;
1167 case AIOCB_WRITE_UDATA
:
1169 hdr
.opcode
= SD_OP_CREATE_AND_WRITE_OBJ
;
1171 hdr
.opcode
= SD_OP_WRITE_OBJ
;
1174 hdr
.flags
= SD_FLAG_CMD_WRITE
| flags
;
1176 case AIOCB_DISCARD_OBJ
:
1177 hdr
.opcode
= SD_OP_WRITE_OBJ
;
1178 hdr
.flags
= SD_FLAG_CMD_WRITE
| flags
;
1179 s
->inode
.data_vdi_id
[data_oid_to_idx(oid
)] = 0;
1180 offset
= offsetof(SheepdogInode
,
1181 data_vdi_id
[data_oid_to_idx(oid
)]);
1182 oid
= vid_to_vdi_oid(s
->inode
.vdi_id
);
1183 wlen
= datalen
= sizeof(uint32_t);
1187 if (s
->cache_flags
) {
1188 hdr
.flags
|= s
->cache_flags
;
1192 hdr
.cow_oid
= old_oid
;
1193 hdr
.copies
= s
->inode
.nr_copies
;
1195 hdr
.data_length
= datalen
;
1196 hdr
.offset
= offset
;
1198 hdr
.id
= aio_req
->id
;
1200 qemu_co_mutex_lock(&s
->lock
);
1201 s
->co_send
= qemu_coroutine_self();
1202 aio_set_fd_handler(s
->aio_context
, s
->fd
,
1203 co_read_response
, co_write_request
, s
);
1204 socket_set_cork(s
->fd
, 1);
1207 ret
= qemu_co_send(s
->fd
, &hdr
, sizeof(hdr
));
1208 if (ret
!= sizeof(hdr
)) {
1209 error_report("failed to send a req, %s", strerror(errno
));
1214 ret
= qemu_co_sendv(s
->fd
, iov
, niov
, aio_req
->iov_offset
, wlen
);
1216 error_report("failed to send a data, %s", strerror(errno
));
1220 socket_set_cork(s
->fd
, 0);
1221 aio_set_fd_handler(s
->aio_context
, s
->fd
, co_read_response
, NULL
, s
);
1223 qemu_co_mutex_unlock(&s
->lock
);
1226 static int read_write_object(int fd
, AioContext
*aio_context
, char *buf
,
1227 uint64_t oid
, uint8_t copies
,
1228 unsigned int datalen
, uint64_t offset
,
1229 bool write
, bool create
, uint32_t cache_flags
)
1232 SheepdogObjRsp
*rsp
= (SheepdogObjRsp
*)&hdr
;
1233 unsigned int wlen
, rlen
;
1236 memset(&hdr
, 0, sizeof(hdr
));
1241 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1243 hdr
.opcode
= SD_OP_CREATE_AND_WRITE_OBJ
;
1245 hdr
.opcode
= SD_OP_WRITE_OBJ
;
1250 hdr
.opcode
= SD_OP_READ_OBJ
;
1253 hdr
.flags
|= cache_flags
;
1256 hdr
.data_length
= datalen
;
1257 hdr
.offset
= offset
;
1258 hdr
.copies
= copies
;
1260 ret
= do_req(fd
, aio_context
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1262 error_report("failed to send a request to the sheep");
1266 switch (rsp
->result
) {
1267 case SD_RES_SUCCESS
:
1270 error_report("%s", sd_strerror(rsp
->result
));
1275 static int read_object(int fd
, AioContext
*aio_context
, char *buf
,
1276 uint64_t oid
, uint8_t copies
,
1277 unsigned int datalen
, uint64_t offset
,
1278 uint32_t cache_flags
)
1280 return read_write_object(fd
, aio_context
, buf
, oid
, copies
,
1281 datalen
, offset
, false,
1282 false, cache_flags
);
1285 static int write_object(int fd
, AioContext
*aio_context
, char *buf
,
1286 uint64_t oid
, uint8_t copies
,
1287 unsigned int datalen
, uint64_t offset
, bool create
,
1288 uint32_t cache_flags
)
1290 return read_write_object(fd
, aio_context
, buf
, oid
, copies
,
1291 datalen
, offset
, true,
1292 create
, cache_flags
);
1295 /* update inode with the latest state */
1296 static int reload_inode(BDRVSheepdogState
*s
, uint32_t snapid
, const char *tag
)
1298 Error
*local_err
= NULL
;
1299 SheepdogInode
*inode
;
1303 fd
= connect_to_sdog(s
, &local_err
);
1305 error_report_err(local_err
);
1309 inode
= g_malloc(SD_INODE_HEADER_SIZE
);
1311 ret
= find_vdi_name(s
, s
->name
, snapid
, tag
, &vid
, false, &local_err
);
1313 error_report_err(local_err
);
1317 ret
= read_object(fd
, s
->aio_context
, (char *)inode
, vid_to_vdi_oid(vid
),
1318 s
->inode
.nr_copies
, SD_INODE_HEADER_SIZE
, 0,
1324 if (inode
->vdi_id
!= s
->inode
.vdi_id
) {
1325 memcpy(&s
->inode
, inode
, SD_INODE_HEADER_SIZE
);
1335 static void coroutine_fn
resend_aioreq(BDRVSheepdogState
*s
, AIOReq
*aio_req
)
1337 SheepdogAIOCB
*acb
= aio_req
->aiocb
;
1339 aio_req
->create
= false;
1341 /* check whether this request becomes a CoW one */
1342 if (acb
->aiocb_type
== AIOCB_WRITE_UDATA
&& is_data_obj(aio_req
->oid
)) {
1343 int idx
= data_oid_to_idx(aio_req
->oid
);
1345 if (is_data_obj_writable(&s
->inode
, idx
)) {
1349 if (s
->inode
.data_vdi_id
[idx
]) {
1350 aio_req
->base_oid
= vid_to_data_oid(s
->inode
.data_vdi_id
[idx
], idx
);
1351 aio_req
->flags
|= SD_FLAG_CMD_COW
;
1353 aio_req
->create
= true;
1356 if (is_data_obj(aio_req
->oid
)) {
1357 add_aio_request(s
, aio_req
, acb
->qiov
->iov
, acb
->qiov
->niov
,
1361 iov
.iov_base
= &s
->inode
;
1362 iov
.iov_len
= sizeof(s
->inode
);
1363 add_aio_request(s
, aio_req
, &iov
, 1, AIOCB_WRITE_UDATA
);
1367 static void sd_detach_aio_context(BlockDriverState
*bs
)
1369 BDRVSheepdogState
*s
= bs
->opaque
;
1371 aio_set_fd_handler(s
->aio_context
, s
->fd
, NULL
, NULL
, NULL
);
1374 static void sd_attach_aio_context(BlockDriverState
*bs
,
1375 AioContext
*new_context
)
1377 BDRVSheepdogState
*s
= bs
->opaque
;
1379 s
->aio_context
= new_context
;
1380 aio_set_fd_handler(new_context
, s
->fd
, co_read_response
, NULL
, s
);
1383 /* TODO Convert to fine grained options */
1384 static QemuOptsList runtime_opts
= {
1386 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
1390 .type
= QEMU_OPT_STRING
,
1391 .help
= "URL to the sheepdog image",
1393 { /* end of list */ }
1397 static int sd_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1402 BDRVSheepdogState
*s
= bs
->opaque
;
1403 char vdi
[SD_MAX_VDI_LEN
], tag
[SD_MAX_VDI_TAG_LEN
];
1407 Error
*local_err
= NULL
;
1408 const char *filename
;
1411 s
->aio_context
= bdrv_get_aio_context(bs
);
1413 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
1414 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
1416 error_propagate(errp
, local_err
);
1421 filename
= qemu_opt_get(opts
, "filename");
1423 QLIST_INIT(&s
->inflight_aio_head
);
1424 QLIST_INIT(&s
->failed_aio_head
);
1425 QLIST_INIT(&s
->inflight_aiocb_head
);
1428 memset(vdi
, 0, sizeof(vdi
));
1429 memset(tag
, 0, sizeof(tag
));
1431 if (strstr(filename
, "://")) {
1432 ret
= sd_parse_uri(s
, filename
, vdi
, &snapid
, tag
);
1434 ret
= parse_vdiname(s
, filename
, vdi
, &snapid
, tag
);
1437 error_setg(errp
, "Can't parse filename");
1440 s
->fd
= get_sheep_fd(s
, errp
);
1446 ret
= find_vdi_name(s
, vdi
, snapid
, tag
, &vid
, true, errp
);
1452 * QEMU block layer emulates writethrough cache as 'writeback + flush', so
1453 * we always set SD_FLAG_CMD_CACHE (writeback cache) as default.
1455 s
->cache_flags
= SD_FLAG_CMD_CACHE
;
1456 if (flags
& BDRV_O_NOCACHE
) {
1457 s
->cache_flags
= SD_FLAG_CMD_DIRECT
;
1459 s
->discard_supported
= true;
1461 if (snapid
|| tag
[0] != '\0') {
1462 DPRINTF("%" PRIx32
" snapshot inode was open.\n", vid
);
1463 s
->is_snapshot
= true;
1466 fd
= connect_to_sdog(s
, errp
);
1472 buf
= g_malloc(SD_INODE_SIZE
);
1473 ret
= read_object(fd
, s
->aio_context
, buf
, vid_to_vdi_oid(vid
),
1474 0, SD_INODE_SIZE
, 0, s
->cache_flags
);
1479 error_setg(errp
, "Can't read snapshot inode");
1483 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
1485 bs
->total_sectors
= s
->inode
.vdi_size
/ BDRV_SECTOR_SIZE
;
1486 pstrcpy(s
->name
, sizeof(s
->name
), vdi
);
1487 qemu_co_mutex_init(&s
->lock
);
1488 qemu_co_queue_init(&s
->overlapping_queue
);
1489 qemu_opts_del(opts
);
1493 aio_set_fd_handler(bdrv_get_aio_context(bs
), s
->fd
, NULL
, NULL
, NULL
);
1497 qemu_opts_del(opts
);
1502 static int sd_reopen_prepare(BDRVReopenState
*state
, BlockReopenQueue
*queue
,
1505 BDRVSheepdogState
*s
= state
->bs
->opaque
;
1506 BDRVSheepdogReopenState
*re_s
;
1509 re_s
= state
->opaque
= g_new0(BDRVSheepdogReopenState
, 1);
1511 re_s
->cache_flags
= SD_FLAG_CMD_CACHE
;
1512 if (state
->flags
& BDRV_O_NOCACHE
) {
1513 re_s
->cache_flags
= SD_FLAG_CMD_DIRECT
;
1516 re_s
->fd
= get_sheep_fd(s
, errp
);
1525 static void sd_reopen_commit(BDRVReopenState
*state
)
1527 BDRVSheepdogReopenState
*re_s
= state
->opaque
;
1528 BDRVSheepdogState
*s
= state
->bs
->opaque
;
1531 aio_set_fd_handler(s
->aio_context
, s
->fd
, NULL
, NULL
, NULL
);
1536 s
->cache_flags
= re_s
->cache_flags
;
1538 g_free(state
->opaque
);
1539 state
->opaque
= NULL
;
1544 static void sd_reopen_abort(BDRVReopenState
*state
)
1546 BDRVSheepdogReopenState
*re_s
= state
->opaque
;
1547 BDRVSheepdogState
*s
= state
->bs
->opaque
;
1554 aio_set_fd_handler(s
->aio_context
, re_s
->fd
, NULL
, NULL
, NULL
);
1555 closesocket(re_s
->fd
);
1558 g_free(state
->opaque
);
1559 state
->opaque
= NULL
;
1564 static int do_sd_create(BDRVSheepdogState
*s
, uint32_t *vdi_id
, int snapshot
,
1568 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1570 unsigned int wlen
, rlen
= 0;
1571 char buf
[SD_MAX_VDI_LEN
];
1573 fd
= connect_to_sdog(s
, errp
);
1578 /* FIXME: would it be better to fail (e.g., return -EIO) when filename
1579 * does not fit in buf? For now, just truncate and avoid buffer overrun.
1581 memset(buf
, 0, sizeof(buf
));
1582 pstrcpy(buf
, sizeof(buf
), s
->name
);
1584 memset(&hdr
, 0, sizeof(hdr
));
1585 hdr
.opcode
= SD_OP_NEW_VDI
;
1586 hdr
.base_vdi_id
= s
->inode
.vdi_id
;
1588 wlen
= SD_MAX_VDI_LEN
;
1590 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1591 hdr
.snapid
= snapshot
;
1593 hdr
.data_length
= wlen
;
1594 hdr
.vdi_size
= s
->inode
.vdi_size
;
1595 hdr
.copy_policy
= s
->inode
.copy_policy
;
1596 hdr
.copies
= s
->inode
.nr_copies
;
1597 hdr
.block_size_shift
= s
->inode
.block_size_shift
;
1599 ret
= do_req(fd
, s
->aio_context
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1604 error_setg_errno(errp
, -ret
, "create failed");
1608 if (rsp
->result
!= SD_RES_SUCCESS
) {
1609 error_setg(errp
, "%s, %s", sd_strerror(rsp
->result
), s
->inode
.name
);
1614 *vdi_id
= rsp
->vdi_id
;
1620 static int sd_prealloc(const char *filename
, Error
**errp
)
1622 BlockDriverState
*bs
= NULL
;
1623 BDRVSheepdogState
*base
= NULL
;
1624 unsigned long buf_size
;
1625 uint32_t idx
, max_idx
;
1626 uint32_t object_size
;
1631 ret
= bdrv_open(&bs
, filename
, NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
1634 goto out_with_err_set
;
1637 vdi_size
= bdrv_getlength(bs
);
1644 object_size
= (UINT32_C(1) << base
->inode
.block_size_shift
);
1645 buf_size
= MIN(object_size
, SD_DATA_OBJ_SIZE
);
1646 buf
= g_malloc0(buf_size
);
1648 max_idx
= DIV_ROUND_UP(vdi_size
, buf_size
);
1650 for (idx
= 0; idx
< max_idx
; idx
++) {
1652 * The created image can be a cloned image, so we need to read
1653 * a data from the source image.
1655 ret
= bdrv_pread(bs
, idx
* buf_size
, buf
, buf_size
);
1659 ret
= bdrv_pwrite(bs
, idx
* buf_size
, buf
, buf_size
);
1667 error_setg_errno(errp
, -ret
, "Can't pre-allocate");
1679 * Sheepdog support two kinds of redundancy, full replication and erasure
1682 * # create a fully replicated vdi with x copies
1683 * -o redundancy=x (1 <= x <= SD_MAX_COPIES)
1685 * # create a erasure coded vdi with x data strips and y parity strips
1686 * -o redundancy=x:y (x must be one of {2,4,8,16} and 1 <= y < SD_EC_MAX_STRIP)
1688 static int parse_redundancy(BDRVSheepdogState
*s
, const char *opt
)
1690 struct SheepdogInode
*inode
= &s
->inode
;
1691 const char *n1
, *n2
;
1695 pstrcpy(p
, sizeof(p
), opt
);
1696 n1
= strtok(p
, ":");
1697 n2
= strtok(NULL
, ":");
1703 copy
= strtol(n1
, NULL
, 10);
1704 if (copy
> SD_MAX_COPIES
|| copy
< 1) {
1708 inode
->copy_policy
= 0;
1709 inode
->nr_copies
= copy
;
1713 if (copy
!= 2 && copy
!= 4 && copy
!= 8 && copy
!= 16) {
1717 parity
= strtol(n2
, NULL
, 10);
1718 if (parity
>= SD_EC_MAX_STRIP
|| parity
< 1) {
1723 * 4 bits for parity and 4 bits for data.
1724 * We have to compress upper data bits because it can't represent 16
1726 inode
->copy_policy
= ((copy
/ 2) << 4) + parity
;
1727 inode
->nr_copies
= copy
+ parity
;
1732 static int parse_block_size_shift(BDRVSheepdogState
*s
, QemuOpts
*opt
)
1734 struct SheepdogInode
*inode
= &s
->inode
;
1735 uint64_t object_size
;
1738 object_size
= qemu_opt_get_size_del(opt
, BLOCK_OPT_OBJECT_SIZE
, 0);
1740 if ((object_size
- 1) & object_size
) { /* not a power of 2? */
1743 obj_order
= ctz32(object_size
);
1744 if (obj_order
< 20 || obj_order
> 31) {
1747 inode
->block_size_shift
= (uint8_t)obj_order
;
1753 static int sd_create(const char *filename
, QemuOpts
*opts
,
1758 char *backing_file
= NULL
;
1760 BDRVSheepdogState
*s
;
1761 char tag
[SD_MAX_VDI_TAG_LEN
];
1763 uint64_t max_vdi_size
;
1764 bool prealloc
= false;
1766 s
= g_new0(BDRVSheepdogState
, 1);
1768 memset(tag
, 0, sizeof(tag
));
1769 if (strstr(filename
, "://")) {
1770 ret
= sd_parse_uri(s
, filename
, s
->name
, &snapid
, tag
);
1772 ret
= parse_vdiname(s
, filename
, s
->name
, &snapid
, tag
);
1775 error_setg(errp
, "Can't parse filename");
1779 s
->inode
.vdi_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
1781 backing_file
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FILE
);
1782 buf
= qemu_opt_get_del(opts
, BLOCK_OPT_PREALLOC
);
1783 if (!buf
|| !strcmp(buf
, "off")) {
1785 } else if (!strcmp(buf
, "full")) {
1788 error_setg(errp
, "Invalid preallocation mode: '%s'", buf
);
1794 buf
= qemu_opt_get_del(opts
, BLOCK_OPT_REDUNDANCY
);
1796 ret
= parse_redundancy(s
, buf
);
1798 error_setg(errp
, "Invalid redundancy mode: '%s'", buf
);
1802 ret
= parse_block_size_shift(s
, opts
);
1804 error_setg(errp
, "Invalid object_size."
1805 " obect_size needs to be power of 2"
1806 " and be limited from 2^20 to 2^31");
1811 BlockDriverState
*bs
;
1812 BDRVSheepdogState
*base
;
1815 /* Currently, only Sheepdog backing image is supported. */
1816 drv
= bdrv_find_protocol(backing_file
, true, NULL
);
1817 if (!drv
|| strcmp(drv
->protocol_name
, "sheepdog") != 0) {
1818 error_setg(errp
, "backing_file must be a sheepdog image");
1824 ret
= bdrv_open(&bs
, backing_file
, NULL
, NULL
, BDRV_O_PROTOCOL
, errp
);
1831 if (!is_snapshot(&base
->inode
)) {
1832 error_setg(errp
, "cannot clone from a non snapshot vdi");
1837 s
->inode
.vdi_id
= base
->inode
.vdi_id
;
1841 s
->aio_context
= qemu_get_aio_context();
1843 /* if block_size_shift is not specified, get cluster default value */
1844 if (s
->inode
.block_size_shift
== 0) {
1846 SheepdogClusterRsp
*rsp
= (SheepdogClusterRsp
*)&hdr
;
1847 Error
*local_err
= NULL
;
1849 unsigned int wlen
= 0, rlen
= 0;
1851 fd
= connect_to_sdog(s
, &local_err
);
1853 error_report("%s", error_get_pretty(local_err
));
1854 error_free(local_err
);
1859 memset(&hdr
, 0, sizeof(hdr
));
1860 hdr
.opcode
= SD_OP_GET_CLUSTER_DEFAULT
;
1861 hdr
.proto_ver
= SD_PROTO_VER
;
1863 ret
= do_req(fd
, s
->aio_context
, (SheepdogReq
*)&hdr
,
1864 NULL
, &wlen
, &rlen
);
1867 error_setg_errno(errp
, -ret
, "failed to get cluster default");
1870 if (rsp
->result
== SD_RES_SUCCESS
) {
1871 s
->inode
.block_size_shift
= rsp
->block_size_shift
;
1873 s
->inode
.block_size_shift
= SD_DEFAULT_BLOCK_SIZE_SHIFT
;
1877 max_vdi_size
= (UINT64_C(1) << s
->inode
.block_size_shift
) * MAX_DATA_OBJS
;
1879 if (s
->inode
.vdi_size
> max_vdi_size
) {
1880 error_setg(errp
, "An image is too large."
1881 " The maximum image size is %"PRIu64
"GB",
1882 max_vdi_size
/ 1024 / 1024 / 1024);
1887 ret
= do_sd_create(s
, &vid
, 0, errp
);
1893 ret
= sd_prealloc(filename
, errp
);
1896 g_free(backing_file
);
1902 static void sd_close(BlockDriverState
*bs
)
1904 Error
*local_err
= NULL
;
1905 BDRVSheepdogState
*s
= bs
->opaque
;
1907 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1908 unsigned int wlen
, rlen
= 0;
1911 DPRINTF("%s\n", s
->name
);
1913 fd
= connect_to_sdog(s
, &local_err
);
1915 error_report_err(local_err
);
1919 memset(&hdr
, 0, sizeof(hdr
));
1921 hdr
.opcode
= SD_OP_RELEASE_VDI
;
1922 hdr
.type
= LOCK_TYPE_NORMAL
;
1923 hdr
.base_vdi_id
= s
->inode
.vdi_id
;
1924 wlen
= strlen(s
->name
) + 1;
1925 hdr
.data_length
= wlen
;
1926 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1928 ret
= do_req(fd
, s
->aio_context
, (SheepdogReq
*)&hdr
,
1929 s
->name
, &wlen
, &rlen
);
1933 if (!ret
&& rsp
->result
!= SD_RES_SUCCESS
&&
1934 rsp
->result
!= SD_RES_VDI_NOT_LOCKED
) {
1935 error_report("%s, %s", sd_strerror(rsp
->result
), s
->name
);
1938 aio_set_fd_handler(bdrv_get_aio_context(bs
), s
->fd
, NULL
, NULL
, NULL
);
1940 g_free(s
->host_spec
);
1943 static int64_t sd_getlength(BlockDriverState
*bs
)
1945 BDRVSheepdogState
*s
= bs
->opaque
;
1947 return s
->inode
.vdi_size
;
1950 static int sd_truncate(BlockDriverState
*bs
, int64_t offset
)
1952 Error
*local_err
= NULL
;
1953 BDRVSheepdogState
*s
= bs
->opaque
;
1955 unsigned int datalen
;
1956 uint64_t max_vdi_size
;
1958 max_vdi_size
= (UINT64_C(1) << s
->inode
.block_size_shift
) * MAX_DATA_OBJS
;
1959 if (offset
< s
->inode
.vdi_size
) {
1960 error_report("shrinking is not supported");
1962 } else if (offset
> max_vdi_size
) {
1963 error_report("too big image size");
1967 fd
= connect_to_sdog(s
, &local_err
);
1969 error_report_err(local_err
);
1973 /* we don't need to update entire object */
1974 datalen
= SD_INODE_SIZE
- sizeof(s
->inode
.data_vdi_id
);
1975 s
->inode
.vdi_size
= offset
;
1976 ret
= write_object(fd
, s
->aio_context
, (char *)&s
->inode
,
1977 vid_to_vdi_oid(s
->inode
.vdi_id
), s
->inode
.nr_copies
,
1978 datalen
, 0, false, s
->cache_flags
);
1982 error_report("failed to update an inode.");
1989 * This function is called after writing data objects. If we need to
1990 * update metadata, this sends a write request to the vdi object.
1991 * Otherwise, this switches back to sd_co_readv/writev.
1993 static void coroutine_fn
sd_write_done(SheepdogAIOCB
*acb
)
1995 BDRVSheepdogState
*s
= acb
->common
.bs
->opaque
;
1998 uint32_t offset
, data_len
, mn
, mx
;
2000 mn
= acb
->min_dirty_data_idx
;
2001 mx
= acb
->max_dirty_data_idx
;
2003 /* we need to update the vdi object. */
2004 offset
= sizeof(s
->inode
) - sizeof(s
->inode
.data_vdi_id
) +
2005 mn
* sizeof(s
->inode
.data_vdi_id
[0]);
2006 data_len
= (mx
- mn
+ 1) * sizeof(s
->inode
.data_vdi_id
[0]);
2008 acb
->min_dirty_data_idx
= UINT32_MAX
;
2009 acb
->max_dirty_data_idx
= 0;
2011 iov
.iov_base
= &s
->inode
;
2012 iov
.iov_len
= sizeof(s
->inode
);
2013 aio_req
= alloc_aio_req(s
, acb
, vid_to_vdi_oid(s
->inode
.vdi_id
),
2014 data_len
, offset
, 0, false, 0, offset
);
2015 QLIST_INSERT_HEAD(&s
->inflight_aio_head
, aio_req
, aio_siblings
);
2016 add_aio_request(s
, aio_req
, &iov
, 1, AIOCB_WRITE_UDATA
);
2018 acb
->aio_done_func
= sd_finish_aiocb
;
2019 acb
->aiocb_type
= AIOCB_WRITE_UDATA
;
2023 sd_finish_aiocb(acb
);
2026 /* Delete current working VDI on the snapshot chain */
2027 static bool sd_delete(BDRVSheepdogState
*s
)
2029 Error
*local_err
= NULL
;
2030 unsigned int wlen
= SD_MAX_VDI_LEN
, rlen
= 0;
2031 SheepdogVdiReq hdr
= {
2032 .opcode
= SD_OP_DEL_VDI
,
2033 .base_vdi_id
= s
->inode
.vdi_id
,
2034 .data_length
= wlen
,
2035 .flags
= SD_FLAG_CMD_WRITE
,
2037 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
2040 fd
= connect_to_sdog(s
, &local_err
);
2042 error_report_err(local_err
);
2046 ret
= do_req(fd
, s
->aio_context
, (SheepdogReq
*)&hdr
,
2047 s
->name
, &wlen
, &rlen
);
2052 switch (rsp
->result
) {
2054 error_report("%s was already deleted", s
->name
);
2056 case SD_RES_SUCCESS
:
2059 error_report("%s, %s", sd_strerror(rsp
->result
), s
->name
);
2067 * Create a writable VDI from a snapshot
2069 static int sd_create_branch(BDRVSheepdogState
*s
)
2071 Error
*local_err
= NULL
;
2077 DPRINTF("%" PRIx32
" is snapshot.\n", s
->inode
.vdi_id
);
2079 buf
= g_malloc(SD_INODE_SIZE
);
2082 * Even If deletion fails, we will just create extra snapshot based on
2083 * the working VDI which was supposed to be deleted. So no need to
2086 deleted
= sd_delete(s
);
2087 ret
= do_sd_create(s
, &vid
, !deleted
, &local_err
);
2089 error_report_err(local_err
);
2093 DPRINTF("%" PRIx32
" is created.\n", vid
);
2095 fd
= connect_to_sdog(s
, &local_err
);
2097 error_report_err(local_err
);
2102 ret
= read_object(fd
, s
->aio_context
, buf
, vid_to_vdi_oid(vid
),
2103 s
->inode
.nr_copies
, SD_INODE_SIZE
, 0, s
->cache_flags
);
2111 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
2113 s
->is_snapshot
= false;
2115 DPRINTF("%" PRIx32
" was newly created.\n", s
->inode
.vdi_id
);
2124 * Send I/O requests to the server.
2126 * This function sends requests to the server, links the requests to
2127 * the inflight_list in BDRVSheepdogState, and exits without
2128 * waiting the response. The responses are received in the
2129 * `aio_read_response' function which is called from the main loop as
2132 * Returns 1 when we need to wait a response, 0 when there is no sent
2133 * request and -errno in error cases.
2135 static int coroutine_fn
sd_co_rw_vector(void *p
)
2137 SheepdogAIOCB
*acb
= p
;
2139 unsigned long len
, done
= 0, total
= acb
->nb_sectors
* BDRV_SECTOR_SIZE
;
2141 uint32_t object_size
;
2144 BDRVSheepdogState
*s
= acb
->common
.bs
->opaque
;
2145 SheepdogInode
*inode
= &s
->inode
;
2148 if (acb
->aiocb_type
== AIOCB_WRITE_UDATA
&& s
->is_snapshot
) {
2150 * In the case we open the snapshot VDI, Sheepdog creates the
2151 * writable VDI when we do a write operation first.
2153 ret
= sd_create_branch(s
);
2160 object_size
= (UINT32_C(1) << inode
->block_size_shift
);
2161 idx
= acb
->sector_num
* BDRV_SECTOR_SIZE
/ object_size
;
2162 offset
= (acb
->sector_num
* BDRV_SECTOR_SIZE
) % object_size
;
2165 * Make sure we don't free the aiocb before we are done with all requests.
2166 * This additional reference is dropped at the end of this function.
2170 while (done
!= total
) {
2172 uint64_t old_oid
= 0;
2173 bool create
= false;
2175 oid
= vid_to_data_oid(inode
->data_vdi_id
[idx
], idx
);
2177 len
= MIN(total
- done
, object_size
- offset
);
2179 switch (acb
->aiocb_type
) {
2180 case AIOCB_READ_UDATA
:
2181 if (!inode
->data_vdi_id
[idx
]) {
2182 qemu_iovec_memset(acb
->qiov
, done
, 0, len
);
2186 case AIOCB_WRITE_UDATA
:
2187 if (!inode
->data_vdi_id
[idx
]) {
2189 } else if (!is_data_obj_writable(inode
, idx
)) {
2193 flags
= SD_FLAG_CMD_COW
;
2196 case AIOCB_DISCARD_OBJ
:
2198 * We discard the object only when the whole object is
2199 * 1) allocated 2) trimmed. Otherwise, simply skip it.
2201 if (len
!= object_size
|| inode
->data_vdi_id
[idx
] == 0) {
2210 DPRINTF("update ino (%" PRIu32
") %" PRIu64
" %" PRIu64
" %ld\n",
2212 vid_to_data_oid(inode
->data_vdi_id
[idx
], idx
), idx
);
2213 oid
= vid_to_data_oid(inode
->vdi_id
, idx
);
2214 DPRINTF("new oid %" PRIx64
"\n", oid
);
2217 aio_req
= alloc_aio_req(s
, acb
, oid
, len
, offset
, flags
, create
,
2219 acb
->aiocb_type
== AIOCB_DISCARD_OBJ
?
2221 QLIST_INSERT_HEAD(&s
->inflight_aio_head
, aio_req
, aio_siblings
);
2223 add_aio_request(s
, aio_req
, acb
->qiov
->iov
, acb
->qiov
->niov
,
2231 if (!--acb
->nr_pending
) {
2237 static bool check_overlapping_aiocb(BDRVSheepdogState
*s
, SheepdogAIOCB
*aiocb
)
2241 QLIST_FOREACH(cb
, &s
->inflight_aiocb_head
, aiocb_siblings
) {
2242 if (AIOCBOverlapping(aiocb
, cb
)) {
2247 QLIST_INSERT_HEAD(&s
->inflight_aiocb_head
, aiocb
, aiocb_siblings
);
2251 static coroutine_fn
int sd_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
2252 int nb_sectors
, QEMUIOVector
*qiov
)
2256 int64_t offset
= (sector_num
+ nb_sectors
) * BDRV_SECTOR_SIZE
;
2257 BDRVSheepdogState
*s
= bs
->opaque
;
2259 if (offset
> s
->inode
.vdi_size
) {
2260 ret
= sd_truncate(bs
, offset
);
2266 acb
= sd_aio_setup(bs
, qiov
, sector_num
, nb_sectors
);
2267 acb
->aio_done_func
= sd_write_done
;
2268 acb
->aiocb_type
= AIOCB_WRITE_UDATA
;
2271 if (check_overlapping_aiocb(s
, acb
)) {
2272 qemu_co_queue_wait(&s
->overlapping_queue
);
2276 ret
= sd_co_rw_vector(acb
);
2278 QLIST_REMOVE(acb
, aiocb_siblings
);
2279 qemu_co_queue_restart_all(&s
->overlapping_queue
);
2280 qemu_aio_unref(acb
);
2284 qemu_coroutine_yield();
2286 QLIST_REMOVE(acb
, aiocb_siblings
);
2287 qemu_co_queue_restart_all(&s
->overlapping_queue
);
2292 static coroutine_fn
int sd_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
2293 int nb_sectors
, QEMUIOVector
*qiov
)
2297 BDRVSheepdogState
*s
= bs
->opaque
;
2299 acb
= sd_aio_setup(bs
, qiov
, sector_num
, nb_sectors
);
2300 acb
->aiocb_type
= AIOCB_READ_UDATA
;
2301 acb
->aio_done_func
= sd_finish_aiocb
;
2304 if (check_overlapping_aiocb(s
, acb
)) {
2305 qemu_co_queue_wait(&s
->overlapping_queue
);
2309 ret
= sd_co_rw_vector(acb
);
2311 QLIST_REMOVE(acb
, aiocb_siblings
);
2312 qemu_co_queue_restart_all(&s
->overlapping_queue
);
2313 qemu_aio_unref(acb
);
2317 qemu_coroutine_yield();
2319 QLIST_REMOVE(acb
, aiocb_siblings
);
2320 qemu_co_queue_restart_all(&s
->overlapping_queue
);
2324 static int coroutine_fn
sd_co_flush_to_disk(BlockDriverState
*bs
)
2326 BDRVSheepdogState
*s
= bs
->opaque
;
2330 if (s
->cache_flags
!= SD_FLAG_CMD_CACHE
) {
2334 acb
= sd_aio_setup(bs
, NULL
, 0, 0);
2335 acb
->aiocb_type
= AIOCB_FLUSH_CACHE
;
2336 acb
->aio_done_func
= sd_finish_aiocb
;
2338 aio_req
= alloc_aio_req(s
, acb
, vid_to_vdi_oid(s
->inode
.vdi_id
),
2339 0, 0, 0, false, 0, 0);
2340 QLIST_INSERT_HEAD(&s
->inflight_aio_head
, aio_req
, aio_siblings
);
2341 add_aio_request(s
, aio_req
, NULL
, 0, acb
->aiocb_type
);
2343 qemu_coroutine_yield();
2347 static int sd_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
)
2349 Error
*local_err
= NULL
;
2350 BDRVSheepdogState
*s
= bs
->opaque
;
2353 SheepdogInode
*inode
;
2354 unsigned int datalen
;
2356 DPRINTF("sn_info: name %s id_str %s s: name %s vm_state_size %" PRId64
" "
2357 "is_snapshot %d\n", sn_info
->name
, sn_info
->id_str
,
2358 s
->name
, sn_info
->vm_state_size
, s
->is_snapshot
);
2360 if (s
->is_snapshot
) {
2361 error_report("You can't create a snapshot of a snapshot VDI, "
2362 "%s (%" PRIu32
").", s
->name
, s
->inode
.vdi_id
);
2367 DPRINTF("%s %s\n", sn_info
->name
, sn_info
->id_str
);
2369 s
->inode
.vm_state_size
= sn_info
->vm_state_size
;
2370 s
->inode
.vm_clock_nsec
= sn_info
->vm_clock_nsec
;
2371 /* It appears that inode.tag does not require a NUL terminator,
2372 * which means this use of strncpy is ok.
2374 strncpy(s
->inode
.tag
, sn_info
->name
, sizeof(s
->inode
.tag
));
2375 /* we don't need to update entire object */
2376 datalen
= SD_INODE_SIZE
- sizeof(s
->inode
.data_vdi_id
);
2377 inode
= g_malloc(datalen
);
2379 /* refresh inode. */
2380 fd
= connect_to_sdog(s
, &local_err
);
2382 error_report_err(local_err
);
2387 ret
= write_object(fd
, s
->aio_context
, (char *)&s
->inode
,
2388 vid_to_vdi_oid(s
->inode
.vdi_id
), s
->inode
.nr_copies
,
2389 datalen
, 0, false, s
->cache_flags
);
2391 error_report("failed to write snapshot's inode.");
2395 ret
= do_sd_create(s
, &new_vid
, 1, &local_err
);
2397 error_report("failed to create inode for snapshot: %s",
2398 error_get_pretty(local_err
));
2399 error_free(local_err
);
2403 ret
= read_object(fd
, s
->aio_context
, (char *)inode
,
2404 vid_to_vdi_oid(new_vid
), s
->inode
.nr_copies
, datalen
, 0,
2408 error_report("failed to read new inode info. %s", strerror(errno
));
2412 memcpy(&s
->inode
, inode
, datalen
);
2413 DPRINTF("s->inode: name %s snap_id %x oid %x\n",
2414 s
->inode
.name
, s
->inode
.snap_id
, s
->inode
.vdi_id
);
2423 * We implement rollback(loadvm) operation to the specified snapshot by
2424 * 1) switch to the snapshot
2425 * 2) rely on sd_create_branch to delete working VDI and
2426 * 3) create a new working VDI based on the specified snapshot
2428 static int sd_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
)
2430 BDRVSheepdogState
*s
= bs
->opaque
;
2431 BDRVSheepdogState
*old_s
;
2432 char tag
[SD_MAX_VDI_TAG_LEN
];
2433 uint32_t snapid
= 0;
2436 old_s
= g_new(BDRVSheepdogState
, 1);
2438 memcpy(old_s
, s
, sizeof(BDRVSheepdogState
));
2440 snapid
= strtoul(snapshot_id
, NULL
, 10);
2444 pstrcpy(tag
, sizeof(tag
), snapshot_id
);
2447 ret
= reload_inode(s
, snapid
, tag
);
2452 ret
= sd_create_branch(s
);
2461 /* recover bdrv_sd_state */
2462 memcpy(s
, old_s
, sizeof(BDRVSheepdogState
));
2465 error_report("failed to open. recover old bdrv_sd_state.");
2470 static int sd_snapshot_delete(BlockDriverState
*bs
,
2471 const char *snapshot_id
,
2475 /* FIXME: Delete specified snapshot id. */
2479 static int sd_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
)
2481 Error
*local_err
= NULL
;
2482 BDRVSheepdogState
*s
= bs
->opaque
;
2484 int fd
, nr
= 1024, ret
, max
= BITS_TO_LONGS(SD_NR_VDIS
) * sizeof(long);
2485 QEMUSnapshotInfo
*sn_tab
= NULL
;
2486 unsigned wlen
, rlen
;
2488 static SheepdogInode inode
;
2489 unsigned long *vdi_inuse
;
2490 unsigned int start_nr
;
2494 vdi_inuse
= g_malloc(max
);
2496 fd
= connect_to_sdog(s
, &local_err
);
2498 error_report_err(local_err
);
2506 memset(&req
, 0, sizeof(req
));
2508 req
.opcode
= SD_OP_READ_VDIS
;
2509 req
.data_length
= max
;
2511 ret
= do_req(fd
, s
->aio_context
, (SheepdogReq
*)&req
,
2512 vdi_inuse
, &wlen
, &rlen
);
2519 sn_tab
= g_new0(QEMUSnapshotInfo
, nr
);
2521 /* calculate a vdi id with hash function */
2522 hval
= fnv_64a_buf(s
->name
, strlen(s
->name
), FNV1A_64_INIT
);
2523 start_nr
= hval
& (SD_NR_VDIS
- 1);
2525 fd
= connect_to_sdog(s
, &local_err
);
2527 error_report_err(local_err
);
2532 for (vid
= start_nr
; found
< nr
; vid
= (vid
+ 1) % SD_NR_VDIS
) {
2533 if (!test_bit(vid
, vdi_inuse
)) {
2537 /* we don't need to read entire object */
2538 ret
= read_object(fd
, s
->aio_context
, (char *)&inode
,
2539 vid_to_vdi_oid(vid
),
2540 0, SD_INODE_SIZE
- sizeof(inode
.data_vdi_id
), 0,
2547 if (!strcmp(inode
.name
, s
->name
) && is_snapshot(&inode
)) {
2548 sn_tab
[found
].date_sec
= inode
.snap_ctime
>> 32;
2549 sn_tab
[found
].date_nsec
= inode
.snap_ctime
& 0xffffffff;
2550 sn_tab
[found
].vm_state_size
= inode
.vm_state_size
;
2551 sn_tab
[found
].vm_clock_nsec
= inode
.vm_clock_nsec
;
2553 snprintf(sn_tab
[found
].id_str
, sizeof(sn_tab
[found
].id_str
),
2554 "%" PRIu32
, inode
.snap_id
);
2555 pstrcpy(sn_tab
[found
].name
,
2556 MIN(sizeof(sn_tab
[found
].name
), sizeof(inode
.tag
)),
2575 static int do_load_save_vmstate(BDRVSheepdogState
*s
, uint8_t *data
,
2576 int64_t pos
, int size
, int load
)
2578 Error
*local_err
= NULL
;
2580 int fd
, ret
= 0, remaining
= size
;
2581 unsigned int data_len
;
2582 uint64_t vmstate_oid
;
2585 uint32_t vdi_id
= load
? s
->inode
.parent_vdi_id
: s
->inode
.vdi_id
;
2586 uint32_t object_size
= (UINT32_C(1) << s
->inode
.block_size_shift
);
2588 fd
= connect_to_sdog(s
, &local_err
);
2590 error_report_err(local_err
);
2595 vdi_index
= pos
/ object_size
;
2596 offset
= pos
% object_size
;
2598 data_len
= MIN(remaining
, object_size
- offset
);
2600 vmstate_oid
= vid_to_vmstate_oid(vdi_id
, vdi_index
);
2602 create
= (offset
== 0);
2604 ret
= read_object(fd
, s
->aio_context
, (char *)data
, vmstate_oid
,
2605 s
->inode
.nr_copies
, data_len
, offset
,
2608 ret
= write_object(fd
, s
->aio_context
, (char *)data
, vmstate_oid
,
2609 s
->inode
.nr_copies
, data_len
, offset
, create
,
2614 error_report("failed to save vmstate %s", strerror(errno
));
2620 remaining
-= data_len
;
2628 static int sd_save_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
2631 BDRVSheepdogState
*s
= bs
->opaque
;
2635 buf
= qemu_blockalign(bs
, qiov
->size
);
2636 qemu_iovec_to_buf(qiov
, 0, buf
, qiov
->size
);
2637 ret
= do_load_save_vmstate(s
, (uint8_t *) buf
, pos
, qiov
->size
, 0);
2643 static int sd_load_vmstate(BlockDriverState
*bs
, uint8_t *data
,
2644 int64_t pos
, int size
)
2646 BDRVSheepdogState
*s
= bs
->opaque
;
2648 return do_load_save_vmstate(s
, data
, pos
, size
, 1);
2652 static coroutine_fn
int sd_co_discard(BlockDriverState
*bs
, int64_t sector_num
,
2656 BDRVSheepdogState
*s
= bs
->opaque
;
2658 QEMUIOVector discard_iov
;
2662 if (!s
->discard_supported
) {
2666 memset(&discard_iov
, 0, sizeof(discard_iov
));
2667 memset(&iov
, 0, sizeof(iov
));
2668 iov
.iov_base
= &zero
;
2669 iov
.iov_len
= sizeof(zero
);
2670 discard_iov
.iov
= &iov
;
2671 discard_iov
.niov
= 1;
2672 acb
= sd_aio_setup(bs
, &discard_iov
, sector_num
, nb_sectors
);
2673 acb
->aiocb_type
= AIOCB_DISCARD_OBJ
;
2674 acb
->aio_done_func
= sd_finish_aiocb
;
2677 if (check_overlapping_aiocb(s
, acb
)) {
2678 qemu_co_queue_wait(&s
->overlapping_queue
);
2682 ret
= sd_co_rw_vector(acb
);
2684 QLIST_REMOVE(acb
, aiocb_siblings
);
2685 qemu_co_queue_restart_all(&s
->overlapping_queue
);
2686 qemu_aio_unref(acb
);
2690 qemu_coroutine_yield();
2692 QLIST_REMOVE(acb
, aiocb_siblings
);
2693 qemu_co_queue_restart_all(&s
->overlapping_queue
);
2698 static coroutine_fn
int64_t
2699 sd_co_get_block_status(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
2702 BDRVSheepdogState
*s
= bs
->opaque
;
2703 SheepdogInode
*inode
= &s
->inode
;
2704 uint32_t object_size
= (UINT32_C(1) << inode
->block_size_shift
);
2705 uint64_t offset
= sector_num
* BDRV_SECTOR_SIZE
;
2706 unsigned long start
= offset
/ object_size
,
2707 end
= DIV_ROUND_UP((sector_num
+ nb_sectors
) *
2708 BDRV_SECTOR_SIZE
, object_size
);
2710 int64_t ret
= BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
| offset
;
2712 for (idx
= start
; idx
< end
; idx
++) {
2713 if (inode
->data_vdi_id
[idx
] == 0) {
2718 /* Get the longest length of unallocated sectors */
2720 for (idx
= start
+ 1; idx
< end
; idx
++) {
2721 if (inode
->data_vdi_id
[idx
] != 0) {
2727 *pnum
= (idx
- start
) * object_size
/ BDRV_SECTOR_SIZE
;
2728 if (*pnum
> nb_sectors
) {
2734 static int64_t sd_get_allocated_file_size(BlockDriverState
*bs
)
2736 BDRVSheepdogState
*s
= bs
->opaque
;
2737 SheepdogInode
*inode
= &s
->inode
;
2738 uint32_t object_size
= (UINT32_C(1) << inode
->block_size_shift
);
2739 unsigned long i
, last
= DIV_ROUND_UP(inode
->vdi_size
, object_size
);
2742 for (i
= 0; i
< last
; i
++) {
2743 if (inode
->data_vdi_id
[i
] == 0) {
2746 size
+= object_size
;
2751 static QemuOptsList sd_create_opts
= {
2752 .name
= "sheepdog-create-opts",
2753 .head
= QTAILQ_HEAD_INITIALIZER(sd_create_opts
.head
),
2756 .name
= BLOCK_OPT_SIZE
,
2757 .type
= QEMU_OPT_SIZE
,
2758 .help
= "Virtual disk size"
2761 .name
= BLOCK_OPT_BACKING_FILE
,
2762 .type
= QEMU_OPT_STRING
,
2763 .help
= "File name of a base image"
2766 .name
= BLOCK_OPT_PREALLOC
,
2767 .type
= QEMU_OPT_STRING
,
2768 .help
= "Preallocation mode (allowed values: off, full)"
2771 .name
= BLOCK_OPT_REDUNDANCY
,
2772 .type
= QEMU_OPT_STRING
,
2773 .help
= "Redundancy of the image"
2776 .name
= BLOCK_OPT_OBJECT_SIZE
,
2777 .type
= QEMU_OPT_SIZE
,
2778 .help
= "Object size of the image"
2780 { /* end of list */ }
2784 static BlockDriver bdrv_sheepdog
= {
2785 .format_name
= "sheepdog",
2786 .protocol_name
= "sheepdog",
2787 .instance_size
= sizeof(BDRVSheepdogState
),
2788 .bdrv_needs_filename
= true,
2789 .bdrv_file_open
= sd_open
,
2790 .bdrv_reopen_prepare
= sd_reopen_prepare
,
2791 .bdrv_reopen_commit
= sd_reopen_commit
,
2792 .bdrv_reopen_abort
= sd_reopen_abort
,
2793 .bdrv_close
= sd_close
,
2794 .bdrv_create
= sd_create
,
2795 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
2796 .bdrv_getlength
= sd_getlength
,
2797 .bdrv_get_allocated_file_size
= sd_get_allocated_file_size
,
2798 .bdrv_truncate
= sd_truncate
,
2800 .bdrv_co_readv
= sd_co_readv
,
2801 .bdrv_co_writev
= sd_co_writev
,
2802 .bdrv_co_flush_to_disk
= sd_co_flush_to_disk
,
2803 .bdrv_co_discard
= sd_co_discard
,
2804 .bdrv_co_get_block_status
= sd_co_get_block_status
,
2806 .bdrv_snapshot_create
= sd_snapshot_create
,
2807 .bdrv_snapshot_goto
= sd_snapshot_goto
,
2808 .bdrv_snapshot_delete
= sd_snapshot_delete
,
2809 .bdrv_snapshot_list
= sd_snapshot_list
,
2811 .bdrv_save_vmstate
= sd_save_vmstate
,
2812 .bdrv_load_vmstate
= sd_load_vmstate
,
2814 .bdrv_detach_aio_context
= sd_detach_aio_context
,
2815 .bdrv_attach_aio_context
= sd_attach_aio_context
,
2817 .create_opts
= &sd_create_opts
,
2820 static BlockDriver bdrv_sheepdog_tcp
= {
2821 .format_name
= "sheepdog",
2822 .protocol_name
= "sheepdog+tcp",
2823 .instance_size
= sizeof(BDRVSheepdogState
),
2824 .bdrv_needs_filename
= true,
2825 .bdrv_file_open
= sd_open
,
2826 .bdrv_reopen_prepare
= sd_reopen_prepare
,
2827 .bdrv_reopen_commit
= sd_reopen_commit
,
2828 .bdrv_reopen_abort
= sd_reopen_abort
,
2829 .bdrv_close
= sd_close
,
2830 .bdrv_create
= sd_create
,
2831 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
2832 .bdrv_getlength
= sd_getlength
,
2833 .bdrv_get_allocated_file_size
= sd_get_allocated_file_size
,
2834 .bdrv_truncate
= sd_truncate
,
2836 .bdrv_co_readv
= sd_co_readv
,
2837 .bdrv_co_writev
= sd_co_writev
,
2838 .bdrv_co_flush_to_disk
= sd_co_flush_to_disk
,
2839 .bdrv_co_discard
= sd_co_discard
,
2840 .bdrv_co_get_block_status
= sd_co_get_block_status
,
2842 .bdrv_snapshot_create
= sd_snapshot_create
,
2843 .bdrv_snapshot_goto
= sd_snapshot_goto
,
2844 .bdrv_snapshot_delete
= sd_snapshot_delete
,
2845 .bdrv_snapshot_list
= sd_snapshot_list
,
2847 .bdrv_save_vmstate
= sd_save_vmstate
,
2848 .bdrv_load_vmstate
= sd_load_vmstate
,
2850 .bdrv_detach_aio_context
= sd_detach_aio_context
,
2851 .bdrv_attach_aio_context
= sd_attach_aio_context
,
2853 .create_opts
= &sd_create_opts
,
2856 static BlockDriver bdrv_sheepdog_unix
= {
2857 .format_name
= "sheepdog",
2858 .protocol_name
= "sheepdog+unix",
2859 .instance_size
= sizeof(BDRVSheepdogState
),
2860 .bdrv_needs_filename
= true,
2861 .bdrv_file_open
= sd_open
,
2862 .bdrv_reopen_prepare
= sd_reopen_prepare
,
2863 .bdrv_reopen_commit
= sd_reopen_commit
,
2864 .bdrv_reopen_abort
= sd_reopen_abort
,
2865 .bdrv_close
= sd_close
,
2866 .bdrv_create
= sd_create
,
2867 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
2868 .bdrv_getlength
= sd_getlength
,
2869 .bdrv_get_allocated_file_size
= sd_get_allocated_file_size
,
2870 .bdrv_truncate
= sd_truncate
,
2872 .bdrv_co_readv
= sd_co_readv
,
2873 .bdrv_co_writev
= sd_co_writev
,
2874 .bdrv_co_flush_to_disk
= sd_co_flush_to_disk
,
2875 .bdrv_co_discard
= sd_co_discard
,
2876 .bdrv_co_get_block_status
= sd_co_get_block_status
,
2878 .bdrv_snapshot_create
= sd_snapshot_create
,
2879 .bdrv_snapshot_goto
= sd_snapshot_goto
,
2880 .bdrv_snapshot_delete
= sd_snapshot_delete
,
2881 .bdrv_snapshot_list
= sd_snapshot_list
,
2883 .bdrv_save_vmstate
= sd_save_vmstate
,
2884 .bdrv_load_vmstate
= sd_load_vmstate
,
2886 .bdrv_detach_aio_context
= sd_detach_aio_context
,
2887 .bdrv_attach_aio_context
= sd_attach_aio_context
,
2889 .create_opts
= &sd_create_opts
,
2892 static void bdrv_sheepdog_init(void)
2894 bdrv_register(&bdrv_sheepdog
);
2895 bdrv_register(&bdrv_sheepdog_tcp
);
2896 bdrv_register(&bdrv_sheepdog_unix
);
2898 block_init(bdrv_sheepdog_init
);