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
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)
95 * For erasure coding, we use at most SD_EC_MAX_STRIP for data strips and
96 * (SD_EC_MAX_STRIP - 1) for parity strips
98 * SD_MAX_COPIES is sum of number of data strips and parity strips.
100 #define SD_EC_MAX_STRIP 16
101 #define SD_MAX_COPIES (SD_EC_MAX_STRIP * 2 - 1)
103 #define SD_INODE_SIZE (sizeof(SheepdogInode))
104 #define CURRENT_VDI_ID 0
106 typedef struct SheepdogReq
{
112 uint32_t data_length
;
113 uint32_t opcode_specific
[8];
116 typedef struct SheepdogRsp
{
122 uint32_t data_length
;
124 uint32_t opcode_specific
[7];
127 typedef struct SheepdogObjReq
{
133 uint32_t data_length
;
142 typedef struct SheepdogObjRsp
{
148 uint32_t data_length
;
156 typedef struct SheepdogVdiReq
{
162 uint32_t data_length
;
164 uint32_t base_vdi_id
;
172 typedef struct SheepdogVdiRsp
{
178 uint32_t data_length
;
185 typedef struct SheepdogInode
{
186 char name
[SD_MAX_VDI_LEN
];
187 char tag
[SD_MAX_VDI_TAG_LEN
];
190 uint64_t vm_clock_nsec
;
192 uint64_t vm_state_size
;
193 uint16_t copy_policy
;
195 uint8_t block_size_shift
;
198 uint32_t parent_vdi_id
;
199 uint32_t child_vdi_id
[MAX_CHILDREN
];
200 uint32_t data_vdi_id
[MAX_DATA_OBJS
];
203 #define SD_INODE_HEADER_SIZE offsetof(SheepdogInode, data_vdi_id)
206 * 64 bit FNV-1a non-zero initial basis
208 #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
211 * 64 bit Fowler/Noll/Vo FNV-1a hash code
213 static inline uint64_t fnv_64a_buf(void *buf
, size_t len
, uint64_t hval
)
215 unsigned char *bp
= buf
;
216 unsigned char *be
= bp
+ len
;
218 hval
^= (uint64_t) *bp
++;
219 hval
+= (hval
<< 1) + (hval
<< 4) + (hval
<< 5) +
220 (hval
<< 7) + (hval
<< 8) + (hval
<< 40);
225 static inline bool is_data_obj_writable(SheepdogInode
*inode
, unsigned int idx
)
227 return inode
->vdi_id
== inode
->data_vdi_id
[idx
];
230 static inline bool is_data_obj(uint64_t oid
)
232 return !(VDI_BIT
& oid
);
235 static inline uint64_t data_oid_to_idx(uint64_t oid
)
237 return oid
& (MAX_DATA_OBJS
- 1);
240 static inline uint32_t oid_to_vid(uint64_t oid
)
242 return (oid
& ~VDI_BIT
) >> VDI_SPACE_SHIFT
;
245 static inline uint64_t vid_to_vdi_oid(uint32_t vid
)
247 return VDI_BIT
| ((uint64_t)vid
<< VDI_SPACE_SHIFT
);
250 static inline uint64_t vid_to_vmstate_oid(uint32_t vid
, uint32_t idx
)
252 return VMSTATE_BIT
| ((uint64_t)vid
<< VDI_SPACE_SHIFT
) | idx
;
255 static inline uint64_t vid_to_data_oid(uint32_t vid
, uint32_t idx
)
257 return ((uint64_t)vid
<< VDI_SPACE_SHIFT
) | idx
;
260 static inline bool is_snapshot(struct SheepdogInode
*inode
)
262 return !!inode
->snap_ctime
;
267 #define DPRINTF(fmt, args...) \
269 fprintf(stdout, "%s %d: " fmt, __func__, __LINE__, ##args); \
272 #define DPRINTF(fmt, args...)
275 typedef struct SheepdogAIOCB SheepdogAIOCB
;
277 typedef struct AIOReq
{
278 SheepdogAIOCB
*aiocb
;
279 unsigned int iov_offset
;
284 unsigned int data_len
;
289 QLIST_ENTRY(AIOReq
) aio_siblings
;
299 struct SheepdogAIOCB
{
300 BlockDriverAIOCB common
;
308 enum AIOCBState aiocb_type
;
310 Coroutine
*coroutine
;
311 void (*aio_done_func
)(SheepdogAIOCB
*);
318 typedef struct BDRVSheepdogState
{
319 BlockDriverState
*bs
;
320 AioContext
*aio_context
;
324 uint32_t min_dirty_data_idx
;
325 uint32_t max_dirty_data_idx
;
327 char name
[SD_MAX_VDI_LEN
];
329 uint32_t cache_flags
;
330 bool discard_supported
;
340 uint32_t aioreq_seq_num
;
342 /* Every aio request must be linked to either of these queues. */
343 QLIST_HEAD(inflight_aio_head
, AIOReq
) inflight_aio_head
;
344 QLIST_HEAD(pending_aio_head
, AIOReq
) pending_aio_head
;
345 QLIST_HEAD(failed_aio_head
, AIOReq
) failed_aio_head
;
348 static const char * sd_strerror(int err
)
352 static const struct {
356 {SD_RES_SUCCESS
, "Success"},
357 {SD_RES_UNKNOWN
, "Unknown error"},
358 {SD_RES_NO_OBJ
, "No object found"},
359 {SD_RES_EIO
, "I/O error"},
360 {SD_RES_VDI_EXIST
, "VDI exists already"},
361 {SD_RES_INVALID_PARMS
, "Invalid parameters"},
362 {SD_RES_SYSTEM_ERROR
, "System error"},
363 {SD_RES_VDI_LOCKED
, "VDI is already locked"},
364 {SD_RES_NO_VDI
, "No vdi found"},
365 {SD_RES_NO_BASE_VDI
, "No base VDI found"},
366 {SD_RES_VDI_READ
, "Failed read the requested VDI"},
367 {SD_RES_VDI_WRITE
, "Failed to write the requested VDI"},
368 {SD_RES_BASE_VDI_READ
, "Failed to read the base VDI"},
369 {SD_RES_BASE_VDI_WRITE
, "Failed to write the base VDI"},
370 {SD_RES_NO_TAG
, "Failed to find the requested tag"},
371 {SD_RES_STARTUP
, "The system is still booting"},
372 {SD_RES_VDI_NOT_LOCKED
, "VDI isn't locked"},
373 {SD_RES_SHUTDOWN
, "The system is shutting down"},
374 {SD_RES_NO_MEM
, "Out of memory on the server"},
375 {SD_RES_FULL_VDI
, "We already have the maximum vdis"},
376 {SD_RES_VER_MISMATCH
, "Protocol version mismatch"},
377 {SD_RES_NO_SPACE
, "Server has no space for new objects"},
378 {SD_RES_WAIT_FOR_FORMAT
, "Sheepdog is waiting for a format operation"},
379 {SD_RES_WAIT_FOR_JOIN
, "Sheepdog is waiting for other nodes joining"},
380 {SD_RES_JOIN_FAILED
, "Target node had failed to join sheepdog"},
381 {SD_RES_HALT
, "Sheepdog is stopped serving IO request"},
382 {SD_RES_READONLY
, "Object is read-only"},
385 for (i
= 0; i
< ARRAY_SIZE(errors
); ++i
) {
386 if (errors
[i
].err
== err
) {
387 return errors
[i
].desc
;
391 return "Invalid error code";
395 * Sheepdog I/O handling:
397 * 1. In sd_co_rw_vector, we send the I/O requests to the server and
398 * link the requests to the inflight_list in the
399 * BDRVSheepdogState. The function exits without waiting for
400 * receiving the response.
402 * 2. We receive the response in aio_read_response, the fd handler to
403 * the sheepdog connection. If metadata update is needed, we send
404 * the write request to the vdi object in sd_write_done, the write
405 * completion function. We switch back to sd_co_readv/writev after
406 * all the requests belonging to the AIOCB are finished.
409 static inline AIOReq
*alloc_aio_req(BDRVSheepdogState
*s
, SheepdogAIOCB
*acb
,
410 uint64_t oid
, unsigned int data_len
,
411 uint64_t offset
, uint8_t flags
, bool create
,
412 uint64_t base_oid
, unsigned int iov_offset
)
416 aio_req
= g_malloc(sizeof(*aio_req
));
417 aio_req
->aiocb
= acb
;
418 aio_req
->iov_offset
= iov_offset
;
420 aio_req
->base_oid
= base_oid
;
421 aio_req
->offset
= offset
;
422 aio_req
->data_len
= data_len
;
423 aio_req
->flags
= flags
;
424 aio_req
->id
= s
->aioreq_seq_num
++;
425 aio_req
->create
= create
;
431 static inline void free_aio_req(BDRVSheepdogState
*s
, AIOReq
*aio_req
)
433 SheepdogAIOCB
*acb
= aio_req
->aiocb
;
435 acb
->cancelable
= false;
436 QLIST_REMOVE(aio_req
, aio_siblings
);
442 static void coroutine_fn
sd_finish_aiocb(SheepdogAIOCB
*acb
)
444 qemu_coroutine_enter(acb
->coroutine
, NULL
);
446 *acb
->finished
= true;
448 qemu_aio_release(acb
);
452 * Check whether the specified acb can be canceled
454 * We can cancel aio when any request belonging to the acb is:
455 * - Not processed by the sheepdog server.
456 * - Not linked to the inflight queue.
458 static bool sd_acb_cancelable(const SheepdogAIOCB
*acb
)
460 BDRVSheepdogState
*s
= acb
->common
.bs
->opaque
;
463 if (!acb
->cancelable
) {
467 QLIST_FOREACH(aioreq
, &s
->inflight_aio_head
, aio_siblings
) {
468 if (aioreq
->aiocb
== acb
) {
476 static void sd_aio_cancel(BlockDriverAIOCB
*blockacb
)
478 SheepdogAIOCB
*acb
= (SheepdogAIOCB
*)blockacb
;
479 BDRVSheepdogState
*s
= acb
->common
.bs
->opaque
;
480 AIOReq
*aioreq
, *next
;
481 bool finished
= false;
483 acb
->finished
= &finished
;
485 if (sd_acb_cancelable(acb
)) {
486 /* Remove outstanding requests from pending and failed queues. */
487 QLIST_FOREACH_SAFE(aioreq
, &s
->pending_aio_head
, aio_siblings
,
489 if (aioreq
->aiocb
== acb
) {
490 free_aio_req(s
, aioreq
);
493 QLIST_FOREACH_SAFE(aioreq
, &s
->failed_aio_head
, aio_siblings
,
495 if (aioreq
->aiocb
== acb
) {
496 free_aio_req(s
, aioreq
);
500 assert(acb
->nr_pending
== 0);
501 sd_finish_aiocb(acb
);
504 aio_poll(s
->aio_context
, true);
508 static const AIOCBInfo sd_aiocb_info
= {
509 .aiocb_size
= sizeof(SheepdogAIOCB
),
510 .cancel
= sd_aio_cancel
,
513 static SheepdogAIOCB
*sd_aio_setup(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
514 int64_t sector_num
, int nb_sectors
)
518 acb
= qemu_aio_get(&sd_aiocb_info
, bs
, NULL
, NULL
);
522 acb
->sector_num
= sector_num
;
523 acb
->nb_sectors
= nb_sectors
;
525 acb
->aio_done_func
= NULL
;
526 acb
->cancelable
= true;
527 acb
->finished
= NULL
;
528 acb
->coroutine
= qemu_coroutine_self();
534 static int connect_to_sdog(BDRVSheepdogState
*s
, Error
**errp
)
539 fd
= unix_connect(s
->host_spec
, errp
);
541 fd
= inet_connect(s
->host_spec
, errp
);
544 int ret
= socket_set_nodelay(fd
);
546 error_report("%s", strerror(errno
));
552 qemu_set_nonblock(fd
);
558 static coroutine_fn
int send_co_req(int sockfd
, SheepdogReq
*hdr
, void *data
,
563 ret
= qemu_co_send(sockfd
, hdr
, sizeof(*hdr
));
564 if (ret
!= sizeof(*hdr
)) {
565 error_report("failed to send a req, %s", strerror(errno
));
569 ret
= qemu_co_send(sockfd
, data
, *wlen
);
571 error_report("failed to send a req, %s", strerror(errno
));
577 static void restart_co_req(void *opaque
)
579 Coroutine
*co
= opaque
;
581 qemu_coroutine_enter(co
, NULL
);
584 typedef struct SheepdogReqCo
{
586 AioContext
*aio_context
;
595 static coroutine_fn
void do_co_req(void *opaque
)
599 SheepdogReqCo
*srco
= opaque
;
600 int sockfd
= srco
->sockfd
;
601 SheepdogReq
*hdr
= srco
->hdr
;
602 void *data
= srco
->data
;
603 unsigned int *wlen
= srco
->wlen
;
604 unsigned int *rlen
= srco
->rlen
;
606 co
= qemu_coroutine_self();
607 aio_set_fd_handler(srco
->aio_context
, sockfd
, NULL
, restart_co_req
, co
);
609 ret
= send_co_req(sockfd
, hdr
, data
, wlen
);
614 aio_set_fd_handler(srco
->aio_context
, sockfd
, restart_co_req
, NULL
, co
);
616 ret
= qemu_co_recv(sockfd
, hdr
, sizeof(*hdr
));
617 if (ret
!= sizeof(*hdr
)) {
618 error_report("failed to get a rsp, %s", strerror(errno
));
623 if (*rlen
> hdr
->data_length
) {
624 *rlen
= hdr
->data_length
;
628 ret
= qemu_co_recv(sockfd
, data
, *rlen
);
630 error_report("failed to get the data, %s", strerror(errno
));
637 /* there is at most one request for this sockfd, so it is safe to
638 * set each handler to NULL. */
639 aio_set_fd_handler(srco
->aio_context
, sockfd
, NULL
, NULL
, NULL
);
642 srco
->finished
= true;
645 static int do_req(int sockfd
, AioContext
*aio_context
, SheepdogReq
*hdr
,
646 void *data
, unsigned int *wlen
, unsigned int *rlen
)
649 SheepdogReqCo srco
= {
651 .aio_context
= aio_context
,
660 if (qemu_in_coroutine()) {
663 co
= qemu_coroutine_create(do_co_req
);
664 qemu_coroutine_enter(co
, &srco
);
665 while (!srco
.finished
) {
666 aio_poll(aio_context
, true);
673 static void coroutine_fn
add_aio_request(BDRVSheepdogState
*s
, AIOReq
*aio_req
,
674 struct iovec
*iov
, int niov
,
675 enum AIOCBState aiocb_type
);
676 static void coroutine_fn
resend_aioreq(BDRVSheepdogState
*s
, AIOReq
*aio_req
);
677 static int reload_inode(BDRVSheepdogState
*s
, uint32_t snapid
, const char *tag
);
678 static int get_sheep_fd(BDRVSheepdogState
*s
, Error
**errp
);
679 static void co_write_request(void *opaque
);
681 static AIOReq
*find_pending_req(BDRVSheepdogState
*s
, uint64_t oid
)
685 QLIST_FOREACH(aio_req
, &s
->pending_aio_head
, aio_siblings
) {
686 if (aio_req
->oid
== oid
) {
695 * This function searchs pending requests to the object `oid', and
698 static void coroutine_fn
send_pending_req(BDRVSheepdogState
*s
, uint64_t oid
)
703 while ((aio_req
= find_pending_req(s
, oid
)) != NULL
) {
704 acb
= aio_req
->aiocb
;
705 /* move aio_req from pending list to inflight one */
706 QLIST_REMOVE(aio_req
, aio_siblings
);
707 QLIST_INSERT_HEAD(&s
->inflight_aio_head
, aio_req
, aio_siblings
);
708 add_aio_request(s
, aio_req
, acb
->qiov
->iov
, acb
->qiov
->niov
,
713 static coroutine_fn
void reconnect_to_sdog(void *opaque
)
715 Error
*local_err
= NULL
;
716 BDRVSheepdogState
*s
= opaque
;
717 AIOReq
*aio_req
, *next
;
719 aio_set_fd_handler(s
->aio_context
, s
->fd
, NULL
, NULL
, NULL
);
723 /* Wait for outstanding write requests to be completed. */
724 while (s
->co_send
!= NULL
) {
725 co_write_request(opaque
);
728 /* Try to reconnect the sheepdog server every one second. */
730 s
->fd
= get_sheep_fd(s
, &local_err
);
732 DPRINTF("Wait for connection to be established\n");
733 error_report("%s", error_get_pretty(local_err
));
734 error_free(local_err
);
735 co_aio_sleep_ns(bdrv_get_aio_context(s
->bs
), QEMU_CLOCK_REALTIME
,
741 * Now we have to resend all the request in the inflight queue. However,
742 * resend_aioreq() can yield and newly created requests can be added to the
743 * inflight queue before the coroutine is resumed. To avoid mixing them, we
744 * have to move all the inflight requests to the failed queue before
745 * resend_aioreq() is called.
747 QLIST_FOREACH_SAFE(aio_req
, &s
->inflight_aio_head
, aio_siblings
, next
) {
748 QLIST_REMOVE(aio_req
, aio_siblings
);
749 QLIST_INSERT_HEAD(&s
->failed_aio_head
, aio_req
, aio_siblings
);
752 /* Resend all the failed aio requests. */
753 while (!QLIST_EMPTY(&s
->failed_aio_head
)) {
754 aio_req
= QLIST_FIRST(&s
->failed_aio_head
);
755 QLIST_REMOVE(aio_req
, aio_siblings
);
756 QLIST_INSERT_HEAD(&s
->inflight_aio_head
, aio_req
, aio_siblings
);
757 resend_aioreq(s
, aio_req
);
762 * Receive responses of the I/O requests.
764 * This function is registered as a fd handler, and called from the
765 * main loop when s->fd is ready for reading responses.
767 static void coroutine_fn
aio_read_response(void *opaque
)
770 BDRVSheepdogState
*s
= opaque
;
773 AIOReq
*aio_req
= NULL
;
778 ret
= qemu_co_recv(fd
, &rsp
, sizeof(rsp
));
779 if (ret
!= sizeof(rsp
)) {
780 error_report("failed to get the header, %s", strerror(errno
));
784 /* find the right aio_req from the inflight aio list */
785 QLIST_FOREACH(aio_req
, &s
->inflight_aio_head
, aio_siblings
) {
786 if (aio_req
->id
== rsp
.id
) {
791 error_report("cannot find aio_req %x", rsp
.id
);
795 acb
= aio_req
->aiocb
;
797 switch (acb
->aiocb_type
) {
798 case AIOCB_WRITE_UDATA
:
799 /* this coroutine context is no longer suitable for co_recv
800 * because we may send data to update vdi objects */
802 if (!is_data_obj(aio_req
->oid
)) {
805 idx
= data_oid_to_idx(aio_req
->oid
);
807 if (aio_req
->create
) {
809 * If the object is newly created one, we need to update
810 * the vdi object (metadata object). min_dirty_data_idx
811 * and max_dirty_data_idx are changed to include updated
812 * index between them.
814 if (rsp
.result
== SD_RES_SUCCESS
) {
815 s
->inode
.data_vdi_id
[idx
] = s
->inode
.vdi_id
;
816 s
->max_dirty_data_idx
= MAX(idx
, s
->max_dirty_data_idx
);
817 s
->min_dirty_data_idx
= MIN(idx
, s
->min_dirty_data_idx
);
820 * Some requests may be blocked because simultaneous
821 * create requests are not allowed, so we search the
822 * pending requests here.
824 send_pending_req(s
, aio_req
->oid
);
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
;
1094 hdr
.opcode
= SD_OP_GET_VDI_INFO
;
1096 wlen
= SD_MAX_VDI_LEN
+ SD_MAX_VDI_TAG_LEN
;
1097 hdr
.proto_ver
= SD_PROTO_VER
;
1098 hdr
.data_length
= wlen
;
1099 hdr
.snapid
= snapid
;
1100 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1102 ret
= do_req(fd
, s
->aio_context
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1104 error_setg_errno(errp
, -ret
, "cannot get vdi info");
1108 if (rsp
->result
!= SD_RES_SUCCESS
) {
1109 error_setg(errp
, "cannot get vdi info, %s, %s %" PRIu32
" %s",
1110 sd_strerror(rsp
->result
), filename
, snapid
, tag
);
1111 if (rsp
->result
== SD_RES_NO_VDI
) {
1126 static void coroutine_fn
add_aio_request(BDRVSheepdogState
*s
, AIOReq
*aio_req
,
1127 struct iovec
*iov
, int niov
,
1128 enum AIOCBState aiocb_type
)
1130 int nr_copies
= s
->inode
.nr_copies
;
1132 unsigned int wlen
= 0;
1134 uint64_t oid
= aio_req
->oid
;
1135 unsigned int datalen
= aio_req
->data_len
;
1136 uint64_t offset
= aio_req
->offset
;
1137 uint8_t flags
= aio_req
->flags
;
1138 uint64_t old_oid
= aio_req
->base_oid
;
1139 bool create
= aio_req
->create
;
1142 error_report("bug");
1145 memset(&hdr
, 0, sizeof(hdr
));
1147 switch (aiocb_type
) {
1148 case AIOCB_FLUSH_CACHE
:
1149 hdr
.opcode
= SD_OP_FLUSH_VDI
;
1151 case AIOCB_READ_UDATA
:
1152 hdr
.opcode
= SD_OP_READ_OBJ
;
1155 case AIOCB_WRITE_UDATA
:
1157 hdr
.opcode
= SD_OP_CREATE_AND_WRITE_OBJ
;
1159 hdr
.opcode
= SD_OP_WRITE_OBJ
;
1162 hdr
.flags
= SD_FLAG_CMD_WRITE
| flags
;
1164 case AIOCB_DISCARD_OBJ
:
1165 hdr
.opcode
= SD_OP_DISCARD_OBJ
;
1169 if (s
->cache_flags
) {
1170 hdr
.flags
|= s
->cache_flags
;
1174 hdr
.cow_oid
= old_oid
;
1175 hdr
.copies
= s
->inode
.nr_copies
;
1177 hdr
.data_length
= datalen
;
1178 hdr
.offset
= offset
;
1180 hdr
.id
= aio_req
->id
;
1182 qemu_co_mutex_lock(&s
->lock
);
1183 s
->co_send
= qemu_coroutine_self();
1184 aio_set_fd_handler(s
->aio_context
, s
->fd
,
1185 co_read_response
, co_write_request
, s
);
1186 socket_set_cork(s
->fd
, 1);
1189 ret
= qemu_co_send(s
->fd
, &hdr
, sizeof(hdr
));
1190 if (ret
!= sizeof(hdr
)) {
1191 error_report("failed to send a req, %s", strerror(errno
));
1196 ret
= qemu_co_sendv(s
->fd
, iov
, niov
, aio_req
->iov_offset
, wlen
);
1198 error_report("failed to send a data, %s", strerror(errno
));
1202 socket_set_cork(s
->fd
, 0);
1203 aio_set_fd_handler(s
->aio_context
, s
->fd
, co_read_response
, NULL
, s
);
1205 qemu_co_mutex_unlock(&s
->lock
);
1208 static int read_write_object(int fd
, AioContext
*aio_context
, char *buf
,
1209 uint64_t oid
, uint8_t copies
,
1210 unsigned int datalen
, uint64_t offset
,
1211 bool write
, bool create
, uint32_t cache_flags
)
1214 SheepdogObjRsp
*rsp
= (SheepdogObjRsp
*)&hdr
;
1215 unsigned int wlen
, rlen
;
1218 memset(&hdr
, 0, sizeof(hdr
));
1223 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1225 hdr
.opcode
= SD_OP_CREATE_AND_WRITE_OBJ
;
1227 hdr
.opcode
= SD_OP_WRITE_OBJ
;
1232 hdr
.opcode
= SD_OP_READ_OBJ
;
1235 hdr
.flags
|= cache_flags
;
1238 hdr
.data_length
= datalen
;
1239 hdr
.offset
= offset
;
1240 hdr
.copies
= copies
;
1242 ret
= do_req(fd
, aio_context
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1244 error_report("failed to send a request to the sheep");
1248 switch (rsp
->result
) {
1249 case SD_RES_SUCCESS
:
1252 error_report("%s", sd_strerror(rsp
->result
));
1257 static int read_object(int fd
, AioContext
*aio_context
, char *buf
,
1258 uint64_t oid
, uint8_t copies
,
1259 unsigned int datalen
, uint64_t offset
,
1260 uint32_t cache_flags
)
1262 return read_write_object(fd
, aio_context
, buf
, oid
, copies
,
1263 datalen
, offset
, false,
1264 false, cache_flags
);
1267 static int write_object(int fd
, AioContext
*aio_context
, char *buf
,
1268 uint64_t oid
, uint8_t copies
,
1269 unsigned int datalen
, uint64_t offset
, bool create
,
1270 uint32_t cache_flags
)
1272 return read_write_object(fd
, aio_context
, buf
, oid
, copies
,
1273 datalen
, offset
, true,
1274 create
, cache_flags
);
1277 /* update inode with the latest state */
1278 static int reload_inode(BDRVSheepdogState
*s
, uint32_t snapid
, const char *tag
)
1280 Error
*local_err
= NULL
;
1281 SheepdogInode
*inode
;
1285 fd
= connect_to_sdog(s
, &local_err
);
1287 error_report("%s", error_get_pretty(local_err
));;
1288 error_free(local_err
);
1292 inode
= g_malloc(SD_INODE_HEADER_SIZE
);
1294 ret
= find_vdi_name(s
, s
->name
, snapid
, tag
, &vid
, false, &local_err
);
1296 error_report("%s", error_get_pretty(local_err
));;
1297 error_free(local_err
);
1301 ret
= read_object(fd
, s
->aio_context
, (char *)inode
, vid_to_vdi_oid(vid
),
1302 s
->inode
.nr_copies
, SD_INODE_HEADER_SIZE
, 0,
1308 if (inode
->vdi_id
!= s
->inode
.vdi_id
) {
1309 memcpy(&s
->inode
, inode
, SD_INODE_HEADER_SIZE
);
1319 /* Return true if the specified request is linked to the pending list. */
1320 static bool check_simultaneous_create(BDRVSheepdogState
*s
, AIOReq
*aio_req
)
1323 QLIST_FOREACH(areq
, &s
->inflight_aio_head
, aio_siblings
) {
1324 if (areq
!= aio_req
&& areq
->oid
== aio_req
->oid
) {
1326 * Sheepdog cannot handle simultaneous create requests to the same
1327 * object, so we cannot send the request until the previous request
1330 DPRINTF("simultaneous create to %" PRIx64
"\n", aio_req
->oid
);
1332 aio_req
->base_oid
= 0;
1333 aio_req
->create
= false;
1334 QLIST_REMOVE(aio_req
, aio_siblings
);
1335 QLIST_INSERT_HEAD(&s
->pending_aio_head
, aio_req
, aio_siblings
);
1343 static void coroutine_fn
resend_aioreq(BDRVSheepdogState
*s
, AIOReq
*aio_req
)
1345 SheepdogAIOCB
*acb
= aio_req
->aiocb
;
1347 aio_req
->create
= false;
1349 /* check whether this request becomes a CoW one */
1350 if (acb
->aiocb_type
== AIOCB_WRITE_UDATA
&& is_data_obj(aio_req
->oid
)) {
1351 int idx
= data_oid_to_idx(aio_req
->oid
);
1353 if (is_data_obj_writable(&s
->inode
, idx
)) {
1357 if (check_simultaneous_create(s
, aio_req
)) {
1361 if (s
->inode
.data_vdi_id
[idx
]) {
1362 aio_req
->base_oid
= vid_to_data_oid(s
->inode
.data_vdi_id
[idx
], idx
);
1363 aio_req
->flags
|= SD_FLAG_CMD_COW
;
1365 aio_req
->create
= true;
1368 if (is_data_obj(aio_req
->oid
)) {
1369 add_aio_request(s
, aio_req
, acb
->qiov
->iov
, acb
->qiov
->niov
,
1373 iov
.iov_base
= &s
->inode
;
1374 iov
.iov_len
= sizeof(s
->inode
);
1375 add_aio_request(s
, aio_req
, &iov
, 1, AIOCB_WRITE_UDATA
);
1379 static void sd_detach_aio_context(BlockDriverState
*bs
)
1381 BDRVSheepdogState
*s
= bs
->opaque
;
1383 aio_set_fd_handler(s
->aio_context
, s
->fd
, NULL
, NULL
, NULL
);
1386 static void sd_attach_aio_context(BlockDriverState
*bs
,
1387 AioContext
*new_context
)
1389 BDRVSheepdogState
*s
= bs
->opaque
;
1391 s
->aio_context
= new_context
;
1392 aio_set_fd_handler(new_context
, s
->fd
, co_read_response
, NULL
, s
);
1395 /* TODO Convert to fine grained options */
1396 static QemuOptsList runtime_opts
= {
1398 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
1402 .type
= QEMU_OPT_STRING
,
1403 .help
= "URL to the sheepdog image",
1405 { /* end of list */ }
1409 static int sd_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1414 BDRVSheepdogState
*s
= bs
->opaque
;
1415 char vdi
[SD_MAX_VDI_LEN
], tag
[SD_MAX_VDI_TAG_LEN
];
1419 Error
*local_err
= NULL
;
1420 const char *filename
;
1423 s
->aio_context
= bdrv_get_aio_context(bs
);
1425 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
1426 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
1428 error_propagate(errp
, local_err
);
1433 filename
= qemu_opt_get(opts
, "filename");
1435 QLIST_INIT(&s
->inflight_aio_head
);
1436 QLIST_INIT(&s
->pending_aio_head
);
1437 QLIST_INIT(&s
->failed_aio_head
);
1440 memset(vdi
, 0, sizeof(vdi
));
1441 memset(tag
, 0, sizeof(tag
));
1443 if (strstr(filename
, "://")) {
1444 ret
= sd_parse_uri(s
, filename
, vdi
, &snapid
, tag
);
1446 ret
= parse_vdiname(s
, filename
, vdi
, &snapid
, tag
);
1449 error_setg(errp
, "Can't parse filename");
1452 s
->fd
= get_sheep_fd(s
, errp
);
1458 ret
= find_vdi_name(s
, vdi
, snapid
, tag
, &vid
, true, errp
);
1464 * QEMU block layer emulates writethrough cache as 'writeback + flush', so
1465 * we always set SD_FLAG_CMD_CACHE (writeback cache) as default.
1467 s
->cache_flags
= SD_FLAG_CMD_CACHE
;
1468 if (flags
& BDRV_O_NOCACHE
) {
1469 s
->cache_flags
= SD_FLAG_CMD_DIRECT
;
1471 s
->discard_supported
= true;
1473 if (snapid
|| tag
[0] != '\0') {
1474 DPRINTF("%" PRIx32
" snapshot inode was open.\n", vid
);
1475 s
->is_snapshot
= true;
1478 fd
= connect_to_sdog(s
, errp
);
1484 buf
= g_malloc(SD_INODE_SIZE
);
1485 ret
= read_object(fd
, s
->aio_context
, buf
, vid_to_vdi_oid(vid
),
1486 0, SD_INODE_SIZE
, 0, s
->cache_flags
);
1491 error_setg(errp
, "Can't read snapshot inode");
1495 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
1496 s
->min_dirty_data_idx
= UINT32_MAX
;
1497 s
->max_dirty_data_idx
= 0;
1499 bs
->total_sectors
= s
->inode
.vdi_size
/ BDRV_SECTOR_SIZE
;
1500 pstrcpy(s
->name
, sizeof(s
->name
), vdi
);
1501 qemu_co_mutex_init(&s
->lock
);
1502 qemu_opts_del(opts
);
1506 aio_set_fd_handler(bdrv_get_aio_context(bs
), s
->fd
, NULL
, NULL
, NULL
);
1510 qemu_opts_del(opts
);
1515 static int do_sd_create(BDRVSheepdogState
*s
, uint32_t *vdi_id
, int snapshot
,
1519 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1521 unsigned int wlen
, rlen
= 0;
1522 char buf
[SD_MAX_VDI_LEN
];
1524 fd
= connect_to_sdog(s
, errp
);
1529 /* FIXME: would it be better to fail (e.g., return -EIO) when filename
1530 * does not fit in buf? For now, just truncate and avoid buffer overrun.
1532 memset(buf
, 0, sizeof(buf
));
1533 pstrcpy(buf
, sizeof(buf
), s
->name
);
1535 memset(&hdr
, 0, sizeof(hdr
));
1536 hdr
.opcode
= SD_OP_NEW_VDI
;
1537 hdr
.base_vdi_id
= s
->inode
.vdi_id
;
1539 wlen
= SD_MAX_VDI_LEN
;
1541 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1542 hdr
.snapid
= snapshot
;
1544 hdr
.data_length
= wlen
;
1545 hdr
.vdi_size
= s
->inode
.vdi_size
;
1546 hdr
.copy_policy
= s
->inode
.copy_policy
;
1547 hdr
.copies
= s
->inode
.nr_copies
;
1549 ret
= do_req(fd
, s
->aio_context
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1554 error_setg_errno(errp
, -ret
, "create failed");
1558 if (rsp
->result
!= SD_RES_SUCCESS
) {
1559 error_setg(errp
, "%s, %s", sd_strerror(rsp
->result
), s
->inode
.name
);
1564 *vdi_id
= rsp
->vdi_id
;
1570 static int sd_prealloc(const char *filename
, Error
**errp
)
1572 BlockDriverState
*bs
= NULL
;
1573 uint32_t idx
, max_idx
;
1575 void *buf
= g_malloc0(SD_DATA_OBJ_SIZE
);
1578 ret
= bdrv_open(&bs
, filename
, NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
1581 goto out_with_err_set
;
1584 vdi_size
= bdrv_getlength(bs
);
1589 max_idx
= DIV_ROUND_UP(vdi_size
, SD_DATA_OBJ_SIZE
);
1591 for (idx
= 0; idx
< max_idx
; idx
++) {
1593 * The created image can be a cloned image, so we need to read
1594 * a data from the source image.
1596 ret
= bdrv_pread(bs
, idx
* SD_DATA_OBJ_SIZE
, buf
, SD_DATA_OBJ_SIZE
);
1600 ret
= bdrv_pwrite(bs
, idx
* SD_DATA_OBJ_SIZE
, buf
, SD_DATA_OBJ_SIZE
);
1608 error_setg_errno(errp
, -ret
, "Can't pre-allocate");
1620 * Sheepdog support two kinds of redundancy, full replication and erasure
1623 * # create a fully replicated vdi with x copies
1624 * -o redundancy=x (1 <= x <= SD_MAX_COPIES)
1626 * # create a erasure coded vdi with x data strips and y parity strips
1627 * -o redundancy=x:y (x must be one of {2,4,8,16} and 1 <= y < SD_EC_MAX_STRIP)
1629 static int parse_redundancy(BDRVSheepdogState
*s
, const char *opt
)
1631 struct SheepdogInode
*inode
= &s
->inode
;
1632 const char *n1
, *n2
;
1636 pstrcpy(p
, sizeof(p
), opt
);
1637 n1
= strtok(p
, ":");
1638 n2
= strtok(NULL
, ":");
1644 copy
= strtol(n1
, NULL
, 10);
1645 if (copy
> SD_MAX_COPIES
|| copy
< 1) {
1649 inode
->copy_policy
= 0;
1650 inode
->nr_copies
= copy
;
1654 if (copy
!= 2 && copy
!= 4 && copy
!= 8 && copy
!= 16) {
1658 parity
= strtol(n2
, NULL
, 10);
1659 if (parity
>= SD_EC_MAX_STRIP
|| parity
< 1) {
1664 * 4 bits for parity and 4 bits for data.
1665 * We have to compress upper data bits because it can't represent 16
1667 inode
->copy_policy
= ((copy
/ 2) << 4) + parity
;
1668 inode
->nr_copies
= copy
+ parity
;
1673 static int sd_create(const char *filename
, QemuOpts
*opts
,
1678 char *backing_file
= NULL
;
1680 BDRVSheepdogState
*s
;
1681 char tag
[SD_MAX_VDI_TAG_LEN
];
1683 bool prealloc
= false;
1685 s
= g_new0(BDRVSheepdogState
, 1);
1687 memset(tag
, 0, sizeof(tag
));
1688 if (strstr(filename
, "://")) {
1689 ret
= sd_parse_uri(s
, filename
, s
->name
, &snapid
, tag
);
1691 ret
= parse_vdiname(s
, filename
, s
->name
, &snapid
, tag
);
1694 error_setg(errp
, "Can't parse filename");
1698 s
->inode
.vdi_size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0);
1699 backing_file
= qemu_opt_get_del(opts
, BLOCK_OPT_BACKING_FILE
);
1700 buf
= qemu_opt_get_del(opts
, BLOCK_OPT_PREALLOC
);
1701 if (!buf
|| !strcmp(buf
, "off")) {
1703 } else if (!strcmp(buf
, "full")) {
1706 error_setg(errp
, "Invalid preallocation mode: '%s'", buf
);
1712 buf
= qemu_opt_get_del(opts
, BLOCK_OPT_REDUNDANCY
);
1714 ret
= parse_redundancy(s
, buf
);
1716 error_setg(errp
, "Invalid redundancy mode: '%s'", buf
);
1721 if (s
->inode
.vdi_size
> SD_MAX_VDI_SIZE
) {
1722 error_setg(errp
, "too big image size");
1728 BlockDriverState
*bs
;
1729 BDRVSheepdogState
*base
;
1732 /* Currently, only Sheepdog backing image is supported. */
1733 drv
= bdrv_find_protocol(backing_file
, true);
1734 if (!drv
|| strcmp(drv
->protocol_name
, "sheepdog") != 0) {
1735 error_setg(errp
, "backing_file must be a sheepdog image");
1741 ret
= bdrv_open(&bs
, backing_file
, NULL
, NULL
, BDRV_O_PROTOCOL
, NULL
,
1749 if (!is_snapshot(&base
->inode
)) {
1750 error_setg(errp
, "cannot clone from a non snapshot vdi");
1755 s
->inode
.vdi_id
= base
->inode
.vdi_id
;
1759 s
->aio_context
= qemu_get_aio_context();
1760 ret
= do_sd_create(s
, &vid
, 0, errp
);
1766 ret
= sd_prealloc(filename
, errp
);
1769 g_free(backing_file
);
1775 static void sd_close(BlockDriverState
*bs
)
1777 Error
*local_err
= NULL
;
1778 BDRVSheepdogState
*s
= bs
->opaque
;
1780 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1781 unsigned int wlen
, rlen
= 0;
1784 DPRINTF("%s\n", s
->name
);
1786 fd
= connect_to_sdog(s
, &local_err
);
1788 error_report("%s", error_get_pretty(local_err
));;
1789 error_free(local_err
);
1793 memset(&hdr
, 0, sizeof(hdr
));
1795 hdr
.opcode
= SD_OP_RELEASE_VDI
;
1796 hdr
.base_vdi_id
= s
->inode
.vdi_id
;
1797 wlen
= strlen(s
->name
) + 1;
1798 hdr
.data_length
= wlen
;
1799 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1801 ret
= do_req(fd
, s
->aio_context
, (SheepdogReq
*)&hdr
,
1802 s
->name
, &wlen
, &rlen
);
1806 if (!ret
&& rsp
->result
!= SD_RES_SUCCESS
&&
1807 rsp
->result
!= SD_RES_VDI_NOT_LOCKED
) {
1808 error_report("%s, %s", sd_strerror(rsp
->result
), s
->name
);
1811 aio_set_fd_handler(bdrv_get_aio_context(bs
), s
->fd
, NULL
, NULL
, NULL
);
1813 g_free(s
->host_spec
);
1816 static int64_t sd_getlength(BlockDriverState
*bs
)
1818 BDRVSheepdogState
*s
= bs
->opaque
;
1820 return s
->inode
.vdi_size
;
1823 static int sd_truncate(BlockDriverState
*bs
, int64_t offset
)
1825 Error
*local_err
= NULL
;
1826 BDRVSheepdogState
*s
= bs
->opaque
;
1828 unsigned int datalen
;
1830 if (offset
< s
->inode
.vdi_size
) {
1831 error_report("shrinking is not supported");
1833 } else if (offset
> SD_MAX_VDI_SIZE
) {
1834 error_report("too big image size");
1838 fd
= connect_to_sdog(s
, &local_err
);
1840 error_report("%s", error_get_pretty(local_err
));;
1841 error_free(local_err
);
1845 /* we don't need to update entire object */
1846 datalen
= SD_INODE_SIZE
- sizeof(s
->inode
.data_vdi_id
);
1847 s
->inode
.vdi_size
= offset
;
1848 ret
= write_object(fd
, s
->aio_context
, (char *)&s
->inode
,
1849 vid_to_vdi_oid(s
->inode
.vdi_id
), s
->inode
.nr_copies
,
1850 datalen
, 0, false, s
->cache_flags
);
1854 error_report("failed to update an inode.");
1861 * This function is called after writing data objects. If we need to
1862 * update metadata, this sends a write request to the vdi object.
1863 * Otherwise, this switches back to sd_co_readv/writev.
1865 static void coroutine_fn
sd_write_done(SheepdogAIOCB
*acb
)
1867 BDRVSheepdogState
*s
= acb
->common
.bs
->opaque
;
1870 uint32_t offset
, data_len
, mn
, mx
;
1872 mn
= s
->min_dirty_data_idx
;
1873 mx
= s
->max_dirty_data_idx
;
1875 /* we need to update the vdi object. */
1876 offset
= sizeof(s
->inode
) - sizeof(s
->inode
.data_vdi_id
) +
1877 mn
* sizeof(s
->inode
.data_vdi_id
[0]);
1878 data_len
= (mx
- mn
+ 1) * sizeof(s
->inode
.data_vdi_id
[0]);
1880 s
->min_dirty_data_idx
= UINT32_MAX
;
1881 s
->max_dirty_data_idx
= 0;
1883 iov
.iov_base
= &s
->inode
;
1884 iov
.iov_len
= sizeof(s
->inode
);
1885 aio_req
= alloc_aio_req(s
, acb
, vid_to_vdi_oid(s
->inode
.vdi_id
),
1886 data_len
, offset
, 0, false, 0, offset
);
1887 QLIST_INSERT_HEAD(&s
->inflight_aio_head
, aio_req
, aio_siblings
);
1888 add_aio_request(s
, aio_req
, &iov
, 1, AIOCB_WRITE_UDATA
);
1890 acb
->aio_done_func
= sd_finish_aiocb
;
1891 acb
->aiocb_type
= AIOCB_WRITE_UDATA
;
1895 sd_finish_aiocb(acb
);
1898 /* Delete current working VDI on the snapshot chain */
1899 static bool sd_delete(BDRVSheepdogState
*s
)
1901 Error
*local_err
= NULL
;
1902 unsigned int wlen
= SD_MAX_VDI_LEN
, rlen
= 0;
1903 SheepdogVdiReq hdr
= {
1904 .opcode
= SD_OP_DEL_VDI
,
1905 .base_vdi_id
= s
->inode
.vdi_id
,
1906 .data_length
= wlen
,
1907 .flags
= SD_FLAG_CMD_WRITE
,
1909 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1912 fd
= connect_to_sdog(s
, &local_err
);
1914 error_report("%s", error_get_pretty(local_err
));;
1915 error_free(local_err
);
1919 ret
= do_req(fd
, s
->aio_context
, (SheepdogReq
*)&hdr
,
1920 s
->name
, &wlen
, &rlen
);
1925 switch (rsp
->result
) {
1927 error_report("%s was already deleted", s
->name
);
1929 case SD_RES_SUCCESS
:
1932 error_report("%s, %s", sd_strerror(rsp
->result
), s
->name
);
1940 * Create a writable VDI from a snapshot
1942 static int sd_create_branch(BDRVSheepdogState
*s
)
1944 Error
*local_err
= NULL
;
1950 DPRINTF("%" PRIx32
" is snapshot.\n", s
->inode
.vdi_id
);
1952 buf
= g_malloc(SD_INODE_SIZE
);
1955 * Even If deletion fails, we will just create extra snapshot based on
1956 * the working VDI which was supposed to be deleted. So no need to
1959 deleted
= sd_delete(s
);
1960 ret
= do_sd_create(s
, &vid
, !deleted
, &local_err
);
1962 error_report("%s", error_get_pretty(local_err
));;
1963 error_free(local_err
);
1967 DPRINTF("%" PRIx32
" is created.\n", vid
);
1969 fd
= connect_to_sdog(s
, &local_err
);
1971 error_report("%s", error_get_pretty(local_err
));;
1972 error_free(local_err
);
1977 ret
= read_object(fd
, s
->aio_context
, buf
, vid_to_vdi_oid(vid
),
1978 s
->inode
.nr_copies
, SD_INODE_SIZE
, 0, s
->cache_flags
);
1986 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
1988 s
->is_snapshot
= false;
1990 DPRINTF("%" PRIx32
" was newly created.\n", s
->inode
.vdi_id
);
1999 * Send I/O requests to the server.
2001 * This function sends requests to the server, links the requests to
2002 * the inflight_list in BDRVSheepdogState, and exits without
2003 * waiting the response. The responses are received in the
2004 * `aio_read_response' function which is called from the main loop as
2007 * Returns 1 when we need to wait a response, 0 when there is no sent
2008 * request and -errno in error cases.
2010 static int coroutine_fn
sd_co_rw_vector(void *p
)
2012 SheepdogAIOCB
*acb
= p
;
2014 unsigned long len
, done
= 0, total
= acb
->nb_sectors
* BDRV_SECTOR_SIZE
;
2015 unsigned long idx
= acb
->sector_num
* BDRV_SECTOR_SIZE
/ SD_DATA_OBJ_SIZE
;
2017 uint64_t offset
= (acb
->sector_num
* BDRV_SECTOR_SIZE
) % SD_DATA_OBJ_SIZE
;
2018 BDRVSheepdogState
*s
= acb
->common
.bs
->opaque
;
2019 SheepdogInode
*inode
= &s
->inode
;
2022 if (acb
->aiocb_type
== AIOCB_WRITE_UDATA
&& s
->is_snapshot
) {
2024 * In the case we open the snapshot VDI, Sheepdog creates the
2025 * writable VDI when we do a write operation first.
2027 ret
= sd_create_branch(s
);
2035 * Make sure we don't free the aiocb before we are done with all requests.
2036 * This additional reference is dropped at the end of this function.
2040 while (done
!= total
) {
2042 uint64_t old_oid
= 0;
2043 bool create
= false;
2045 oid
= vid_to_data_oid(inode
->data_vdi_id
[idx
], idx
);
2047 len
= MIN(total
- done
, SD_DATA_OBJ_SIZE
- offset
);
2049 switch (acb
->aiocb_type
) {
2050 case AIOCB_READ_UDATA
:
2051 if (!inode
->data_vdi_id
[idx
]) {
2052 qemu_iovec_memset(acb
->qiov
, done
, 0, len
);
2056 case AIOCB_WRITE_UDATA
:
2057 if (!inode
->data_vdi_id
[idx
]) {
2059 } else if (!is_data_obj_writable(inode
, idx
)) {
2063 flags
= SD_FLAG_CMD_COW
;
2066 case AIOCB_DISCARD_OBJ
:
2068 * We discard the object only when the whole object is
2069 * 1) allocated 2) trimmed. Otherwise, simply skip it.
2071 if (len
!= SD_DATA_OBJ_SIZE
|| inode
->data_vdi_id
[idx
] == 0) {
2080 DPRINTF("update ino (%" PRIu32
") %" PRIu64
" %" PRIu64
" %ld\n",
2082 vid_to_data_oid(inode
->data_vdi_id
[idx
], idx
), idx
);
2083 oid
= vid_to_data_oid(inode
->vdi_id
, idx
);
2084 DPRINTF("new oid %" PRIx64
"\n", oid
);
2087 aio_req
= alloc_aio_req(s
, acb
, oid
, len
, offset
, flags
, create
,
2089 QLIST_INSERT_HEAD(&s
->inflight_aio_head
, aio_req
, aio_siblings
);
2092 if (check_simultaneous_create(s
, aio_req
)) {
2097 add_aio_request(s
, aio_req
, acb
->qiov
->iov
, acb
->qiov
->niov
,
2105 if (!--acb
->nr_pending
) {
2111 static coroutine_fn
int sd_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
2112 int nb_sectors
, QEMUIOVector
*qiov
)
2116 int64_t offset
= (sector_num
+ nb_sectors
) * BDRV_SECTOR_SIZE
;
2117 BDRVSheepdogState
*s
= bs
->opaque
;
2119 if (bs
->growable
&& offset
> s
->inode
.vdi_size
) {
2120 ret
= sd_truncate(bs
, offset
);
2126 acb
= sd_aio_setup(bs
, qiov
, sector_num
, nb_sectors
);
2127 acb
->aio_done_func
= sd_write_done
;
2128 acb
->aiocb_type
= AIOCB_WRITE_UDATA
;
2130 ret
= sd_co_rw_vector(acb
);
2132 qemu_aio_release(acb
);
2136 qemu_coroutine_yield();
2141 static coroutine_fn
int sd_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
2142 int nb_sectors
, QEMUIOVector
*qiov
)
2147 acb
= sd_aio_setup(bs
, qiov
, sector_num
, nb_sectors
);
2148 acb
->aiocb_type
= AIOCB_READ_UDATA
;
2149 acb
->aio_done_func
= sd_finish_aiocb
;
2151 ret
= sd_co_rw_vector(acb
);
2153 qemu_aio_release(acb
);
2157 qemu_coroutine_yield();
2162 static int coroutine_fn
sd_co_flush_to_disk(BlockDriverState
*bs
)
2164 BDRVSheepdogState
*s
= bs
->opaque
;
2168 if (s
->cache_flags
!= SD_FLAG_CMD_CACHE
) {
2172 acb
= sd_aio_setup(bs
, NULL
, 0, 0);
2173 acb
->aiocb_type
= AIOCB_FLUSH_CACHE
;
2174 acb
->aio_done_func
= sd_finish_aiocb
;
2176 aio_req
= alloc_aio_req(s
, acb
, vid_to_vdi_oid(s
->inode
.vdi_id
),
2177 0, 0, 0, false, 0, 0);
2178 QLIST_INSERT_HEAD(&s
->inflight_aio_head
, aio_req
, aio_siblings
);
2179 add_aio_request(s
, aio_req
, NULL
, 0, acb
->aiocb_type
);
2181 qemu_coroutine_yield();
2185 static int sd_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
)
2187 Error
*local_err
= NULL
;
2188 BDRVSheepdogState
*s
= bs
->opaque
;
2191 SheepdogInode
*inode
;
2192 unsigned int datalen
;
2194 DPRINTF("sn_info: name %s id_str %s s: name %s vm_state_size %" PRId64
" "
2195 "is_snapshot %d\n", sn_info
->name
, sn_info
->id_str
,
2196 s
->name
, sn_info
->vm_state_size
, s
->is_snapshot
);
2198 if (s
->is_snapshot
) {
2199 error_report("You can't create a snapshot of a snapshot VDI, "
2200 "%s (%" PRIu32
").", s
->name
, s
->inode
.vdi_id
);
2205 DPRINTF("%s %s\n", sn_info
->name
, sn_info
->id_str
);
2207 s
->inode
.vm_state_size
= sn_info
->vm_state_size
;
2208 s
->inode
.vm_clock_nsec
= sn_info
->vm_clock_nsec
;
2209 /* It appears that inode.tag does not require a NUL terminator,
2210 * which means this use of strncpy is ok.
2212 strncpy(s
->inode
.tag
, sn_info
->name
, sizeof(s
->inode
.tag
));
2213 /* we don't need to update entire object */
2214 datalen
= SD_INODE_SIZE
- sizeof(s
->inode
.data_vdi_id
);
2215 inode
= g_malloc(datalen
);
2217 /* refresh inode. */
2218 fd
= connect_to_sdog(s
, &local_err
);
2220 error_report("%s", error_get_pretty(local_err
));;
2221 error_free(local_err
);
2226 ret
= write_object(fd
, s
->aio_context
, (char *)&s
->inode
,
2227 vid_to_vdi_oid(s
->inode
.vdi_id
), s
->inode
.nr_copies
,
2228 datalen
, 0, false, s
->cache_flags
);
2230 error_report("failed to write snapshot's inode.");
2234 ret
= do_sd_create(s
, &new_vid
, 1, &local_err
);
2236 error_report("%s", error_get_pretty(local_err
));;
2237 error_free(local_err
);
2238 error_report("failed to create inode for snapshot. %s",
2243 ret
= read_object(fd
, s
->aio_context
, (char *)inode
,
2244 vid_to_vdi_oid(new_vid
), s
->inode
.nr_copies
, datalen
, 0,
2248 error_report("failed to read new inode info. %s", strerror(errno
));
2252 memcpy(&s
->inode
, inode
, datalen
);
2253 DPRINTF("s->inode: name %s snap_id %x oid %x\n",
2254 s
->inode
.name
, s
->inode
.snap_id
, s
->inode
.vdi_id
);
2263 * We implement rollback(loadvm) operation to the specified snapshot by
2264 * 1) switch to the snapshot
2265 * 2) rely on sd_create_branch to delete working VDI and
2266 * 3) create a new working VDI based on the specified snapshot
2268 static int sd_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
)
2270 BDRVSheepdogState
*s
= bs
->opaque
;
2271 BDRVSheepdogState
*old_s
;
2272 char tag
[SD_MAX_VDI_TAG_LEN
];
2273 uint32_t snapid
= 0;
2276 old_s
= g_new(BDRVSheepdogState
, 1);
2278 memcpy(old_s
, s
, sizeof(BDRVSheepdogState
));
2280 snapid
= strtoul(snapshot_id
, NULL
, 10);
2284 pstrcpy(tag
, sizeof(tag
), snapshot_id
);
2287 ret
= reload_inode(s
, snapid
, tag
);
2292 ret
= sd_create_branch(s
);
2301 /* recover bdrv_sd_state */
2302 memcpy(s
, old_s
, sizeof(BDRVSheepdogState
));
2305 error_report("failed to open. recover old bdrv_sd_state.");
2310 static int sd_snapshot_delete(BlockDriverState
*bs
,
2311 const char *snapshot_id
,
2315 /* FIXME: Delete specified snapshot id. */
2319 static int sd_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
)
2321 Error
*local_err
= NULL
;
2322 BDRVSheepdogState
*s
= bs
->opaque
;
2324 int fd
, nr
= 1024, ret
, max
= BITS_TO_LONGS(SD_NR_VDIS
) * sizeof(long);
2325 QEMUSnapshotInfo
*sn_tab
= NULL
;
2326 unsigned wlen
, rlen
;
2328 static SheepdogInode inode
;
2329 unsigned long *vdi_inuse
;
2330 unsigned int start_nr
;
2334 vdi_inuse
= g_malloc(max
);
2336 fd
= connect_to_sdog(s
, &local_err
);
2338 error_report("%s", error_get_pretty(local_err
));;
2339 error_free(local_err
);
2347 memset(&req
, 0, sizeof(req
));
2349 req
.opcode
= SD_OP_READ_VDIS
;
2350 req
.data_length
= max
;
2352 ret
= do_req(fd
, s
->aio_context
, (SheepdogReq
*)&req
,
2353 vdi_inuse
, &wlen
, &rlen
);
2360 sn_tab
= g_new0(QEMUSnapshotInfo
, nr
);
2362 /* calculate a vdi id with hash function */
2363 hval
= fnv_64a_buf(s
->name
, strlen(s
->name
), FNV1A_64_INIT
);
2364 start_nr
= hval
& (SD_NR_VDIS
- 1);
2366 fd
= connect_to_sdog(s
, &local_err
);
2368 error_report("%s", error_get_pretty(local_err
));;
2369 error_free(local_err
);
2374 for (vid
= start_nr
; found
< nr
; vid
= (vid
+ 1) % SD_NR_VDIS
) {
2375 if (!test_bit(vid
, vdi_inuse
)) {
2379 /* we don't need to read entire object */
2380 ret
= read_object(fd
, s
->aio_context
, (char *)&inode
,
2381 vid_to_vdi_oid(vid
),
2382 0, SD_INODE_SIZE
- sizeof(inode
.data_vdi_id
), 0,
2389 if (!strcmp(inode
.name
, s
->name
) && is_snapshot(&inode
)) {
2390 sn_tab
[found
].date_sec
= inode
.snap_ctime
>> 32;
2391 sn_tab
[found
].date_nsec
= inode
.snap_ctime
& 0xffffffff;
2392 sn_tab
[found
].vm_state_size
= inode
.vm_state_size
;
2393 sn_tab
[found
].vm_clock_nsec
= inode
.vm_clock_nsec
;
2395 snprintf(sn_tab
[found
].id_str
, sizeof(sn_tab
[found
].id_str
),
2396 "%" PRIu32
, inode
.snap_id
);
2397 pstrcpy(sn_tab
[found
].name
,
2398 MIN(sizeof(sn_tab
[found
].name
), sizeof(inode
.tag
)),
2417 static int do_load_save_vmstate(BDRVSheepdogState
*s
, uint8_t *data
,
2418 int64_t pos
, int size
, int load
)
2420 Error
*local_err
= NULL
;
2422 int fd
, ret
= 0, remaining
= size
;
2423 unsigned int data_len
;
2424 uint64_t vmstate_oid
;
2427 uint32_t vdi_id
= load
? s
->inode
.parent_vdi_id
: s
->inode
.vdi_id
;
2429 fd
= connect_to_sdog(s
, &local_err
);
2431 error_report("%s", error_get_pretty(local_err
));;
2432 error_free(local_err
);
2437 vdi_index
= pos
/ SD_DATA_OBJ_SIZE
;
2438 offset
= pos
% SD_DATA_OBJ_SIZE
;
2440 data_len
= MIN(remaining
, SD_DATA_OBJ_SIZE
- offset
);
2442 vmstate_oid
= vid_to_vmstate_oid(vdi_id
, vdi_index
);
2444 create
= (offset
== 0);
2446 ret
= read_object(fd
, s
->aio_context
, (char *)data
, vmstate_oid
,
2447 s
->inode
.nr_copies
, data_len
, offset
,
2450 ret
= write_object(fd
, s
->aio_context
, (char *)data
, vmstate_oid
,
2451 s
->inode
.nr_copies
, data_len
, offset
, create
,
2456 error_report("failed to save vmstate %s", strerror(errno
));
2462 remaining
-= data_len
;
2470 static int sd_save_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
2473 BDRVSheepdogState
*s
= bs
->opaque
;
2477 buf
= qemu_blockalign(bs
, qiov
->size
);
2478 qemu_iovec_to_buf(qiov
, 0, buf
, qiov
->size
);
2479 ret
= do_load_save_vmstate(s
, (uint8_t *) buf
, pos
, qiov
->size
, 0);
2485 static int sd_load_vmstate(BlockDriverState
*bs
, uint8_t *data
,
2486 int64_t pos
, int size
)
2488 BDRVSheepdogState
*s
= bs
->opaque
;
2490 return do_load_save_vmstate(s
, data
, pos
, size
, 1);
2494 static coroutine_fn
int sd_co_discard(BlockDriverState
*bs
, int64_t sector_num
,
2499 BDRVSheepdogState
*s
= bs
->opaque
;
2502 if (!s
->discard_supported
) {
2506 acb
= sd_aio_setup(bs
, &dummy
, sector_num
, nb_sectors
);
2507 acb
->aiocb_type
= AIOCB_DISCARD_OBJ
;
2508 acb
->aio_done_func
= sd_finish_aiocb
;
2510 ret
= sd_co_rw_vector(acb
);
2512 qemu_aio_release(acb
);
2516 qemu_coroutine_yield();
2521 static coroutine_fn
int64_t
2522 sd_co_get_block_status(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
2525 BDRVSheepdogState
*s
= bs
->opaque
;
2526 SheepdogInode
*inode
= &s
->inode
;
2527 uint64_t offset
= sector_num
* BDRV_SECTOR_SIZE
;
2528 unsigned long start
= offset
/ SD_DATA_OBJ_SIZE
,
2529 end
= DIV_ROUND_UP((sector_num
+ nb_sectors
) *
2530 BDRV_SECTOR_SIZE
, SD_DATA_OBJ_SIZE
);
2532 int64_t ret
= BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
| offset
;
2534 for (idx
= start
; idx
< end
; idx
++) {
2535 if (inode
->data_vdi_id
[idx
] == 0) {
2540 /* Get the longest length of unallocated sectors */
2542 for (idx
= start
+ 1; idx
< end
; idx
++) {
2543 if (inode
->data_vdi_id
[idx
] != 0) {
2549 *pnum
= (idx
- start
) * SD_DATA_OBJ_SIZE
/ BDRV_SECTOR_SIZE
;
2550 if (*pnum
> nb_sectors
) {
2556 static int64_t sd_get_allocated_file_size(BlockDriverState
*bs
)
2558 BDRVSheepdogState
*s
= bs
->opaque
;
2559 SheepdogInode
*inode
= &s
->inode
;
2560 unsigned long i
, last
= DIV_ROUND_UP(inode
->vdi_size
, SD_DATA_OBJ_SIZE
);
2563 for (i
= 0; i
< last
; i
++) {
2564 if (inode
->data_vdi_id
[i
] == 0) {
2567 size
+= SD_DATA_OBJ_SIZE
;
2572 static QemuOptsList sd_create_opts
= {
2573 .name
= "sheepdog-create-opts",
2574 .head
= QTAILQ_HEAD_INITIALIZER(sd_create_opts
.head
),
2577 .name
= BLOCK_OPT_SIZE
,
2578 .type
= QEMU_OPT_SIZE
,
2579 .help
= "Virtual disk size"
2582 .name
= BLOCK_OPT_BACKING_FILE
,
2583 .type
= QEMU_OPT_STRING
,
2584 .help
= "File name of a base image"
2587 .name
= BLOCK_OPT_PREALLOC
,
2588 .type
= QEMU_OPT_STRING
,
2589 .help
= "Preallocation mode (allowed values: off, full)"
2592 .name
= BLOCK_OPT_REDUNDANCY
,
2593 .type
= QEMU_OPT_STRING
,
2594 .help
= "Redundancy of the image"
2596 { /* end of list */ }
2600 static BlockDriver bdrv_sheepdog
= {
2601 .format_name
= "sheepdog",
2602 .protocol_name
= "sheepdog",
2603 .instance_size
= sizeof(BDRVSheepdogState
),
2604 .bdrv_needs_filename
= true,
2605 .bdrv_file_open
= sd_open
,
2606 .bdrv_close
= sd_close
,
2607 .bdrv_create
= sd_create
,
2608 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
2609 .bdrv_getlength
= sd_getlength
,
2610 .bdrv_get_allocated_file_size
= sd_get_allocated_file_size
,
2611 .bdrv_truncate
= sd_truncate
,
2613 .bdrv_co_readv
= sd_co_readv
,
2614 .bdrv_co_writev
= sd_co_writev
,
2615 .bdrv_co_flush_to_disk
= sd_co_flush_to_disk
,
2616 .bdrv_co_discard
= sd_co_discard
,
2617 .bdrv_co_get_block_status
= sd_co_get_block_status
,
2619 .bdrv_snapshot_create
= sd_snapshot_create
,
2620 .bdrv_snapshot_goto
= sd_snapshot_goto
,
2621 .bdrv_snapshot_delete
= sd_snapshot_delete
,
2622 .bdrv_snapshot_list
= sd_snapshot_list
,
2624 .bdrv_save_vmstate
= sd_save_vmstate
,
2625 .bdrv_load_vmstate
= sd_load_vmstate
,
2627 .bdrv_detach_aio_context
= sd_detach_aio_context
,
2628 .bdrv_attach_aio_context
= sd_attach_aio_context
,
2630 .create_opts
= &sd_create_opts
,
2633 static BlockDriver bdrv_sheepdog_tcp
= {
2634 .format_name
= "sheepdog",
2635 .protocol_name
= "sheepdog+tcp",
2636 .instance_size
= sizeof(BDRVSheepdogState
),
2637 .bdrv_needs_filename
= true,
2638 .bdrv_file_open
= sd_open
,
2639 .bdrv_close
= sd_close
,
2640 .bdrv_create
= sd_create
,
2641 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
2642 .bdrv_getlength
= sd_getlength
,
2643 .bdrv_get_allocated_file_size
= sd_get_allocated_file_size
,
2644 .bdrv_truncate
= sd_truncate
,
2646 .bdrv_co_readv
= sd_co_readv
,
2647 .bdrv_co_writev
= sd_co_writev
,
2648 .bdrv_co_flush_to_disk
= sd_co_flush_to_disk
,
2649 .bdrv_co_discard
= sd_co_discard
,
2650 .bdrv_co_get_block_status
= sd_co_get_block_status
,
2652 .bdrv_snapshot_create
= sd_snapshot_create
,
2653 .bdrv_snapshot_goto
= sd_snapshot_goto
,
2654 .bdrv_snapshot_delete
= sd_snapshot_delete
,
2655 .bdrv_snapshot_list
= sd_snapshot_list
,
2657 .bdrv_save_vmstate
= sd_save_vmstate
,
2658 .bdrv_load_vmstate
= sd_load_vmstate
,
2660 .bdrv_detach_aio_context
= sd_detach_aio_context
,
2661 .bdrv_attach_aio_context
= sd_attach_aio_context
,
2663 .create_opts
= &sd_create_opts
,
2666 static BlockDriver bdrv_sheepdog_unix
= {
2667 .format_name
= "sheepdog",
2668 .protocol_name
= "sheepdog+unix",
2669 .instance_size
= sizeof(BDRVSheepdogState
),
2670 .bdrv_needs_filename
= true,
2671 .bdrv_file_open
= sd_open
,
2672 .bdrv_close
= sd_close
,
2673 .bdrv_create
= sd_create
,
2674 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
2675 .bdrv_getlength
= sd_getlength
,
2676 .bdrv_get_allocated_file_size
= sd_get_allocated_file_size
,
2677 .bdrv_truncate
= sd_truncate
,
2679 .bdrv_co_readv
= sd_co_readv
,
2680 .bdrv_co_writev
= sd_co_writev
,
2681 .bdrv_co_flush_to_disk
= sd_co_flush_to_disk
,
2682 .bdrv_co_discard
= sd_co_discard
,
2683 .bdrv_co_get_block_status
= sd_co_get_block_status
,
2685 .bdrv_snapshot_create
= sd_snapshot_create
,
2686 .bdrv_snapshot_goto
= sd_snapshot_goto
,
2687 .bdrv_snapshot_delete
= sd_snapshot_delete
,
2688 .bdrv_snapshot_list
= sd_snapshot_list
,
2690 .bdrv_save_vmstate
= sd_save_vmstate
,
2691 .bdrv_load_vmstate
= sd_load_vmstate
,
2693 .bdrv_detach_aio_context
= sd_detach_aio_context
,
2694 .bdrv_attach_aio_context
= sd_attach_aio_context
,
2696 .create_opts
= &sd_create_opts
,
2699 static void bdrv_sheepdog_init(void)
2701 bdrv_register(&bdrv_sheepdog
);
2702 bdrv_register(&bdrv_sheepdog_tcp
);
2703 bdrv_register(&bdrv_sheepdog_unix
);
2705 block_init(bdrv_sheepdog_init
);