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 #define LOCK_TYPE_NORMAL 0
107 #define LOCK_TYPE_SHARED 1 /* for iSCSI multipath */
109 typedef struct SheepdogReq
{
115 uint32_t data_length
;
116 uint32_t opcode_specific
[8];
119 typedef struct SheepdogRsp
{
125 uint32_t data_length
;
127 uint32_t opcode_specific
[7];
130 typedef struct SheepdogObjReq
{
136 uint32_t data_length
;
145 typedef struct SheepdogObjRsp
{
151 uint32_t data_length
;
159 typedef struct SheepdogVdiReq
{
165 uint32_t data_length
;
167 uint32_t base_vdi_id
;
176 typedef struct SheepdogVdiRsp
{
182 uint32_t data_length
;
189 typedef struct SheepdogInode
{
190 char name
[SD_MAX_VDI_LEN
];
191 char tag
[SD_MAX_VDI_TAG_LEN
];
194 uint64_t vm_clock_nsec
;
196 uint64_t vm_state_size
;
197 uint16_t copy_policy
;
199 uint8_t block_size_shift
;
202 uint32_t parent_vdi_id
;
203 uint32_t child_vdi_id
[MAX_CHILDREN
];
204 uint32_t data_vdi_id
[MAX_DATA_OBJS
];
207 #define SD_INODE_HEADER_SIZE offsetof(SheepdogInode, data_vdi_id)
210 * 64 bit FNV-1a non-zero initial basis
212 #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
215 * 64 bit Fowler/Noll/Vo FNV-1a hash code
217 static inline uint64_t fnv_64a_buf(void *buf
, size_t len
, uint64_t hval
)
219 unsigned char *bp
= buf
;
220 unsigned char *be
= bp
+ len
;
222 hval
^= (uint64_t) *bp
++;
223 hval
+= (hval
<< 1) + (hval
<< 4) + (hval
<< 5) +
224 (hval
<< 7) + (hval
<< 8) + (hval
<< 40);
229 static inline bool is_data_obj_writable(SheepdogInode
*inode
, unsigned int idx
)
231 return inode
->vdi_id
== inode
->data_vdi_id
[idx
];
234 static inline bool is_data_obj(uint64_t oid
)
236 return !(VDI_BIT
& oid
);
239 static inline uint64_t data_oid_to_idx(uint64_t oid
)
241 return oid
& (MAX_DATA_OBJS
- 1);
244 static inline uint32_t oid_to_vid(uint64_t oid
)
246 return (oid
& ~VDI_BIT
) >> VDI_SPACE_SHIFT
;
249 static inline uint64_t vid_to_vdi_oid(uint32_t vid
)
251 return VDI_BIT
| ((uint64_t)vid
<< VDI_SPACE_SHIFT
);
254 static inline uint64_t vid_to_vmstate_oid(uint32_t vid
, uint32_t idx
)
256 return VMSTATE_BIT
| ((uint64_t)vid
<< VDI_SPACE_SHIFT
) | idx
;
259 static inline uint64_t vid_to_data_oid(uint32_t vid
, uint32_t idx
)
261 return ((uint64_t)vid
<< VDI_SPACE_SHIFT
) | idx
;
264 static inline bool is_snapshot(struct SheepdogInode
*inode
)
266 return !!inode
->snap_ctime
;
271 #define DPRINTF(fmt, args...) \
273 fprintf(stdout, "%s %d: " fmt, __func__, __LINE__, ##args); \
276 #define DPRINTF(fmt, args...)
279 typedef struct SheepdogAIOCB SheepdogAIOCB
;
281 typedef struct AIOReq
{
282 SheepdogAIOCB
*aiocb
;
283 unsigned int iov_offset
;
288 unsigned int data_len
;
293 QLIST_ENTRY(AIOReq
) aio_siblings
;
303 struct SheepdogAIOCB
{
312 enum AIOCBState aiocb_type
;
314 Coroutine
*coroutine
;
315 void (*aio_done_func
)(SheepdogAIOCB
*);
321 typedef struct BDRVSheepdogState
{
322 BlockDriverState
*bs
;
323 AioContext
*aio_context
;
327 uint32_t min_dirty_data_idx
;
328 uint32_t max_dirty_data_idx
;
330 char name
[SD_MAX_VDI_LEN
];
332 uint32_t cache_flags
;
333 bool discard_supported
;
343 uint32_t aioreq_seq_num
;
345 /* Every aio request must be linked to either of these queues. */
346 QLIST_HEAD(inflight_aio_head
, AIOReq
) inflight_aio_head
;
347 QLIST_HEAD(pending_aio_head
, AIOReq
) pending_aio_head
;
348 QLIST_HEAD(failed_aio_head
, AIOReq
) failed_aio_head
;
351 static const char * sd_strerror(int err
)
355 static const struct {
359 {SD_RES_SUCCESS
, "Success"},
360 {SD_RES_UNKNOWN
, "Unknown error"},
361 {SD_RES_NO_OBJ
, "No object found"},
362 {SD_RES_EIO
, "I/O error"},
363 {SD_RES_VDI_EXIST
, "VDI exists already"},
364 {SD_RES_INVALID_PARMS
, "Invalid parameters"},
365 {SD_RES_SYSTEM_ERROR
, "System error"},
366 {SD_RES_VDI_LOCKED
, "VDI is already locked"},
367 {SD_RES_NO_VDI
, "No vdi found"},
368 {SD_RES_NO_BASE_VDI
, "No base VDI found"},
369 {SD_RES_VDI_READ
, "Failed read the requested VDI"},
370 {SD_RES_VDI_WRITE
, "Failed to write the requested VDI"},
371 {SD_RES_BASE_VDI_READ
, "Failed to read the base VDI"},
372 {SD_RES_BASE_VDI_WRITE
, "Failed to write the base VDI"},
373 {SD_RES_NO_TAG
, "Failed to find the requested tag"},
374 {SD_RES_STARTUP
, "The system is still booting"},
375 {SD_RES_VDI_NOT_LOCKED
, "VDI isn't locked"},
376 {SD_RES_SHUTDOWN
, "The system is shutting down"},
377 {SD_RES_NO_MEM
, "Out of memory on the server"},
378 {SD_RES_FULL_VDI
, "We already have the maximum vdis"},
379 {SD_RES_VER_MISMATCH
, "Protocol version mismatch"},
380 {SD_RES_NO_SPACE
, "Server has no space for new objects"},
381 {SD_RES_WAIT_FOR_FORMAT
, "Sheepdog is waiting for a format operation"},
382 {SD_RES_WAIT_FOR_JOIN
, "Sheepdog is waiting for other nodes joining"},
383 {SD_RES_JOIN_FAILED
, "Target node had failed to join sheepdog"},
384 {SD_RES_HALT
, "Sheepdog is stopped serving IO request"},
385 {SD_RES_READONLY
, "Object is read-only"},
388 for (i
= 0; i
< ARRAY_SIZE(errors
); ++i
) {
389 if (errors
[i
].err
== err
) {
390 return errors
[i
].desc
;
394 return "Invalid error code";
398 * Sheepdog I/O handling:
400 * 1. In sd_co_rw_vector, we send the I/O requests to the server and
401 * link the requests to the inflight_list in the
402 * BDRVSheepdogState. The function exits without waiting for
403 * receiving the response.
405 * 2. We receive the response in aio_read_response, the fd handler to
406 * the sheepdog connection. If metadata update is needed, we send
407 * the write request to the vdi object in sd_write_done, the write
408 * completion function. We switch back to sd_co_readv/writev after
409 * all the requests belonging to the AIOCB are finished.
412 static inline AIOReq
*alloc_aio_req(BDRVSheepdogState
*s
, SheepdogAIOCB
*acb
,
413 uint64_t oid
, unsigned int data_len
,
414 uint64_t offset
, uint8_t flags
, bool create
,
415 uint64_t base_oid
, unsigned int iov_offset
)
419 aio_req
= g_malloc(sizeof(*aio_req
));
420 aio_req
->aiocb
= acb
;
421 aio_req
->iov_offset
= iov_offset
;
423 aio_req
->base_oid
= base_oid
;
424 aio_req
->offset
= offset
;
425 aio_req
->data_len
= data_len
;
426 aio_req
->flags
= flags
;
427 aio_req
->id
= s
->aioreq_seq_num
++;
428 aio_req
->create
= create
;
434 static inline void free_aio_req(BDRVSheepdogState
*s
, AIOReq
*aio_req
)
436 SheepdogAIOCB
*acb
= aio_req
->aiocb
;
438 acb
->cancelable
= false;
439 QLIST_REMOVE(aio_req
, aio_siblings
);
445 static void coroutine_fn
sd_finish_aiocb(SheepdogAIOCB
*acb
)
447 qemu_coroutine_enter(acb
->coroutine
, NULL
);
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(BlockAIOCB
*blockacb
)
478 SheepdogAIOCB
*acb
= (SheepdogAIOCB
*)blockacb
;
479 BDRVSheepdogState
*s
= acb
->common
.bs
->opaque
;
480 AIOReq
*aioreq
, *next
;
482 if (sd_acb_cancelable(acb
)) {
483 /* Remove outstanding requests from pending and failed queues. */
484 QLIST_FOREACH_SAFE(aioreq
, &s
->pending_aio_head
, aio_siblings
,
486 if (aioreq
->aiocb
== acb
) {
487 free_aio_req(s
, aioreq
);
490 QLIST_FOREACH_SAFE(aioreq
, &s
->failed_aio_head
, aio_siblings
,
492 if (aioreq
->aiocb
== acb
) {
493 free_aio_req(s
, aioreq
);
497 assert(acb
->nr_pending
== 0);
498 if (acb
->common
.cb
) {
499 acb
->common
.cb(acb
->common
.opaque
, -ECANCELED
);
501 sd_finish_aiocb(acb
);
505 static const AIOCBInfo sd_aiocb_info
= {
506 .aiocb_size
= sizeof(SheepdogAIOCB
),
507 .cancel_async
= sd_aio_cancel
,
510 static SheepdogAIOCB
*sd_aio_setup(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
511 int64_t sector_num
, int nb_sectors
)
515 acb
= qemu_aio_get(&sd_aiocb_info
, bs
, NULL
, NULL
);
519 acb
->sector_num
= sector_num
;
520 acb
->nb_sectors
= nb_sectors
;
522 acb
->aio_done_func
= NULL
;
523 acb
->cancelable
= true;
524 acb
->coroutine
= qemu_coroutine_self();
530 static int connect_to_sdog(BDRVSheepdogState
*s
, Error
**errp
)
535 fd
= unix_connect(s
->host_spec
, errp
);
537 fd
= inet_connect(s
->host_spec
, errp
);
540 int ret
= socket_set_nodelay(fd
);
542 error_report("%s", strerror(errno
));
548 qemu_set_nonblock(fd
);
554 static coroutine_fn
int send_co_req(int sockfd
, SheepdogReq
*hdr
, void *data
,
559 ret
= qemu_co_send(sockfd
, hdr
, sizeof(*hdr
));
560 if (ret
!= sizeof(*hdr
)) {
561 error_report("failed to send a req, %s", strerror(errno
));
565 ret
= qemu_co_send(sockfd
, data
, *wlen
);
567 error_report("failed to send a req, %s", strerror(errno
));
573 static void restart_co_req(void *opaque
)
575 Coroutine
*co
= opaque
;
577 qemu_coroutine_enter(co
, NULL
);
580 typedef struct SheepdogReqCo
{
582 AioContext
*aio_context
;
591 static coroutine_fn
void do_co_req(void *opaque
)
595 SheepdogReqCo
*srco
= opaque
;
596 int sockfd
= srco
->sockfd
;
597 SheepdogReq
*hdr
= srco
->hdr
;
598 void *data
= srco
->data
;
599 unsigned int *wlen
= srco
->wlen
;
600 unsigned int *rlen
= srco
->rlen
;
602 co
= qemu_coroutine_self();
603 aio_set_fd_handler(srco
->aio_context
, sockfd
, NULL
, restart_co_req
, co
);
605 ret
= send_co_req(sockfd
, hdr
, data
, wlen
);
610 aio_set_fd_handler(srco
->aio_context
, sockfd
, restart_co_req
, NULL
, co
);
612 ret
= qemu_co_recv(sockfd
, hdr
, sizeof(*hdr
));
613 if (ret
!= sizeof(*hdr
)) {
614 error_report("failed to get a rsp, %s", strerror(errno
));
619 if (*rlen
> hdr
->data_length
) {
620 *rlen
= hdr
->data_length
;
624 ret
= qemu_co_recv(sockfd
, data
, *rlen
);
626 error_report("failed to get the data, %s", strerror(errno
));
633 /* there is at most one request for this sockfd, so it is safe to
634 * set each handler to NULL. */
635 aio_set_fd_handler(srco
->aio_context
, sockfd
, NULL
, NULL
, NULL
);
638 srco
->finished
= true;
641 static int do_req(int sockfd
, AioContext
*aio_context
, SheepdogReq
*hdr
,
642 void *data
, unsigned int *wlen
, unsigned int *rlen
)
645 SheepdogReqCo srco
= {
647 .aio_context
= aio_context
,
656 if (qemu_in_coroutine()) {
659 co
= qemu_coroutine_create(do_co_req
);
660 qemu_coroutine_enter(co
, &srco
);
661 while (!srco
.finished
) {
662 aio_poll(aio_context
, true);
669 static void coroutine_fn
add_aio_request(BDRVSheepdogState
*s
, AIOReq
*aio_req
,
670 struct iovec
*iov
, int niov
,
671 enum AIOCBState aiocb_type
);
672 static void coroutine_fn
resend_aioreq(BDRVSheepdogState
*s
, AIOReq
*aio_req
);
673 static int reload_inode(BDRVSheepdogState
*s
, uint32_t snapid
, const char *tag
);
674 static int get_sheep_fd(BDRVSheepdogState
*s
, Error
**errp
);
675 static void co_write_request(void *opaque
);
677 static AIOReq
*find_pending_req(BDRVSheepdogState
*s
, uint64_t oid
)
681 QLIST_FOREACH(aio_req
, &s
->pending_aio_head
, aio_siblings
) {
682 if (aio_req
->oid
== oid
) {
691 * This function searchs pending requests to the object `oid', and
694 static void coroutine_fn
send_pending_req(BDRVSheepdogState
*s
, uint64_t oid
)
699 while ((aio_req
= find_pending_req(s
, oid
)) != NULL
) {
700 acb
= aio_req
->aiocb
;
701 /* move aio_req from pending list to inflight one */
702 QLIST_REMOVE(aio_req
, aio_siblings
);
703 QLIST_INSERT_HEAD(&s
->inflight_aio_head
, aio_req
, aio_siblings
);
704 add_aio_request(s
, aio_req
, acb
->qiov
->iov
, acb
->qiov
->niov
,
709 static coroutine_fn
void reconnect_to_sdog(void *opaque
)
711 BDRVSheepdogState
*s
= opaque
;
712 AIOReq
*aio_req
, *next
;
714 aio_set_fd_handler(s
->aio_context
, s
->fd
, NULL
, NULL
, NULL
);
718 /* Wait for outstanding write requests to be completed. */
719 while (s
->co_send
!= NULL
) {
720 co_write_request(opaque
);
723 /* Try to reconnect the sheepdog server every one second. */
725 Error
*local_err
= NULL
;
726 s
->fd
= get_sheep_fd(s
, &local_err
);
728 DPRINTF("Wait for connection to be established\n");
729 error_report("%s", error_get_pretty(local_err
));
730 error_free(local_err
);
731 co_aio_sleep_ns(bdrv_get_aio_context(s
->bs
), QEMU_CLOCK_REALTIME
,
737 * Now we have to resend all the request in the inflight queue. However,
738 * resend_aioreq() can yield and newly created requests can be added to the
739 * inflight queue before the coroutine is resumed. To avoid mixing them, we
740 * have to move all the inflight requests to the failed queue before
741 * resend_aioreq() is called.
743 QLIST_FOREACH_SAFE(aio_req
, &s
->inflight_aio_head
, aio_siblings
, next
) {
744 QLIST_REMOVE(aio_req
, aio_siblings
);
745 QLIST_INSERT_HEAD(&s
->failed_aio_head
, aio_req
, aio_siblings
);
748 /* Resend all the failed aio requests. */
749 while (!QLIST_EMPTY(&s
->failed_aio_head
)) {
750 aio_req
= QLIST_FIRST(&s
->failed_aio_head
);
751 QLIST_REMOVE(aio_req
, aio_siblings
);
752 QLIST_INSERT_HEAD(&s
->inflight_aio_head
, aio_req
, aio_siblings
);
753 resend_aioreq(s
, aio_req
);
758 * Receive responses of the I/O requests.
760 * This function is registered as a fd handler, and called from the
761 * main loop when s->fd is ready for reading responses.
763 static void coroutine_fn
aio_read_response(void *opaque
)
766 BDRVSheepdogState
*s
= opaque
;
769 AIOReq
*aio_req
= NULL
;
774 ret
= qemu_co_recv(fd
, &rsp
, sizeof(rsp
));
775 if (ret
!= sizeof(rsp
)) {
776 error_report("failed to get the header, %s", strerror(errno
));
780 /* find the right aio_req from the inflight aio list */
781 QLIST_FOREACH(aio_req
, &s
->inflight_aio_head
, aio_siblings
) {
782 if (aio_req
->id
== rsp
.id
) {
787 error_report("cannot find aio_req %x", rsp
.id
);
791 acb
= aio_req
->aiocb
;
793 switch (acb
->aiocb_type
) {
794 case AIOCB_WRITE_UDATA
:
795 /* this coroutine context is no longer suitable for co_recv
796 * because we may send data to update vdi objects */
798 if (!is_data_obj(aio_req
->oid
)) {
801 idx
= data_oid_to_idx(aio_req
->oid
);
803 if (aio_req
->create
) {
805 * If the object is newly created one, we need to update
806 * the vdi object (metadata object). min_dirty_data_idx
807 * and max_dirty_data_idx are changed to include updated
808 * index between them.
810 if (rsp
.result
== SD_RES_SUCCESS
) {
811 s
->inode
.data_vdi_id
[idx
] = s
->inode
.vdi_id
;
812 s
->max_dirty_data_idx
= MAX(idx
, s
->max_dirty_data_idx
);
813 s
->min_dirty_data_idx
= MIN(idx
, s
->min_dirty_data_idx
);
816 * Some requests may be blocked because simultaneous
817 * create requests are not allowed, so we search the
818 * pending requests here.
820 send_pending_req(s
, aio_req
->oid
);
823 case AIOCB_READ_UDATA
:
824 ret
= qemu_co_recvv(fd
, acb
->qiov
->iov
, acb
->qiov
->niov
,
825 aio_req
->iov_offset
, rsp
.data_length
);
826 if (ret
!= rsp
.data_length
) {
827 error_report("failed to get the data, %s", strerror(errno
));
831 case AIOCB_FLUSH_CACHE
:
832 if (rsp
.result
== SD_RES_INVALID_PARMS
) {
833 DPRINTF("disable cache since the server doesn't support it\n");
834 s
->cache_flags
= SD_FLAG_CMD_DIRECT
;
835 rsp
.result
= SD_RES_SUCCESS
;
838 case AIOCB_DISCARD_OBJ
:
839 switch (rsp
.result
) {
840 case SD_RES_INVALID_PARMS
:
841 error_report("sheep(%s) doesn't support discard command",
843 rsp
.result
= SD_RES_SUCCESS
;
844 s
->discard_supported
= false;
847 idx
= data_oid_to_idx(aio_req
->oid
);
848 s
->inode
.data_vdi_id
[idx
] = 0;
855 switch (rsp
.result
) {
858 case SD_RES_READONLY
:
859 if (s
->inode
.vdi_id
== oid_to_vid(aio_req
->oid
)) {
860 ret
= reload_inode(s
, 0, "");
865 if (is_data_obj(aio_req
->oid
)) {
866 aio_req
->oid
= vid_to_data_oid(s
->inode
.vdi_id
,
867 data_oid_to_idx(aio_req
->oid
));
869 aio_req
->oid
= vid_to_vdi_oid(s
->inode
.vdi_id
);
871 resend_aioreq(s
, aio_req
);
875 error_report("%s", sd_strerror(rsp
.result
));
879 free_aio_req(s
, aio_req
);
880 if (!acb
->nr_pending
) {
882 * We've finished all requests which belong to the AIOCB, so
883 * we can switch back to sd_co_readv/writev now.
885 acb
->aio_done_func(acb
);
892 reconnect_to_sdog(opaque
);
895 static void co_read_response(void *opaque
)
897 BDRVSheepdogState
*s
= opaque
;
900 s
->co_recv
= qemu_coroutine_create(aio_read_response
);
903 qemu_coroutine_enter(s
->co_recv
, opaque
);
906 static void co_write_request(void *opaque
)
908 BDRVSheepdogState
*s
= opaque
;
910 qemu_coroutine_enter(s
->co_send
, NULL
);
914 * Return a socket descriptor to read/write objects.
916 * We cannot use this descriptor for other operations because
917 * the block driver may be on waiting response from the server.
919 static int get_sheep_fd(BDRVSheepdogState
*s
, Error
**errp
)
923 fd
= connect_to_sdog(s
, errp
);
928 aio_set_fd_handler(s
->aio_context
, fd
, co_read_response
, NULL
, s
);
932 static int sd_parse_uri(BDRVSheepdogState
*s
, const char *filename
,
933 char *vdi
, uint32_t *snapid
, char *tag
)
936 QueryParams
*qp
= NULL
;
939 uri
= uri_parse(filename
);
945 if (!strcmp(uri
->scheme
, "sheepdog")) {
947 } else if (!strcmp(uri
->scheme
, "sheepdog+tcp")) {
949 } else if (!strcmp(uri
->scheme
, "sheepdog+unix")) {
956 if (uri
->path
== NULL
|| !strcmp(uri
->path
, "/")) {
960 pstrcpy(vdi
, SD_MAX_VDI_LEN
, uri
->path
+ 1);
962 qp
= query_params_parse(uri
->query
);
963 if (qp
->n
> 1 || (s
->is_unix
&& !qp
->n
) || (!s
->is_unix
&& qp
->n
)) {
969 /* sheepdog+unix:///vdiname?socket=path */
970 if (uri
->server
|| uri
->port
|| strcmp(qp
->p
[0].name
, "socket")) {
974 s
->host_spec
= g_strdup(qp
->p
[0].value
);
976 /* sheepdog[+tcp]://[host:port]/vdiname */
977 s
->host_spec
= g_strdup_printf("%s:%d", uri
->server
?: SD_DEFAULT_ADDR
,
978 uri
->port
?: SD_DEFAULT_PORT
);
983 *snapid
= strtoul(uri
->fragment
, NULL
, 10);
985 pstrcpy(tag
, SD_MAX_VDI_TAG_LEN
, uri
->fragment
);
988 *snapid
= CURRENT_VDI_ID
; /* search current vdi */
993 query_params_free(qp
);
1000 * Parse a filename (old syntax)
1002 * filename must be one of the following formats:
1004 * 2. [vdiname]:[snapid]
1005 * 3. [vdiname]:[tag]
1006 * 4. [hostname]:[port]:[vdiname]
1007 * 5. [hostname]:[port]:[vdiname]:[snapid]
1008 * 6. [hostname]:[port]:[vdiname]:[tag]
1010 * You can boot from the snapshot images by specifying `snapid` or
1013 * You can run VMs outside the Sheepdog cluster by specifying
1014 * `hostname' and `port' (experimental).
1016 static int parse_vdiname(BDRVSheepdogState
*s
, const char *filename
,
1017 char *vdi
, uint32_t *snapid
, char *tag
)
1020 const char *host_spec
, *vdi_spec
;
1023 strstart(filename
, "sheepdog:", (const char **)&filename
);
1024 p
= q
= g_strdup(filename
);
1026 /* count the number of separators */
1036 /* use the first two tokens as host_spec. */
1049 p
= strchr(vdi_spec
, ':');
1054 uri
= g_strdup_printf("sheepdog://%s/%s", host_spec
, vdi_spec
);
1056 ret
= sd_parse_uri(s
, uri
, vdi
, snapid
, tag
);
1064 static int find_vdi_name(BDRVSheepdogState
*s
, const char *filename
,
1065 uint32_t snapid
, const char *tag
, uint32_t *vid
,
1066 bool lock
, Error
**errp
)
1070 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1071 unsigned int wlen
, rlen
= 0;
1072 char buf
[SD_MAX_VDI_LEN
+ SD_MAX_VDI_TAG_LEN
];
1074 fd
= connect_to_sdog(s
, errp
);
1079 /* This pair of strncpy calls ensures that the buffer is zero-filled,
1080 * which is desirable since we'll soon be sending those bytes, and
1081 * don't want the send_req to read uninitialized data.
1083 strncpy(buf
, filename
, SD_MAX_VDI_LEN
);
1084 strncpy(buf
+ SD_MAX_VDI_LEN
, tag
, SD_MAX_VDI_TAG_LEN
);
1086 memset(&hdr
, 0, sizeof(hdr
));
1088 hdr
.opcode
= SD_OP_LOCK_VDI
;
1089 hdr
.type
= LOCK_TYPE_NORMAL
;
1091 hdr
.opcode
= SD_OP_GET_VDI_INFO
;
1093 wlen
= SD_MAX_VDI_LEN
+ SD_MAX_VDI_TAG_LEN
;
1094 hdr
.proto_ver
= SD_PROTO_VER
;
1095 hdr
.data_length
= wlen
;
1096 hdr
.snapid
= snapid
;
1097 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1099 ret
= do_req(fd
, s
->aio_context
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1101 error_setg_errno(errp
, -ret
, "cannot get vdi info");
1105 if (rsp
->result
!= SD_RES_SUCCESS
) {
1106 error_setg(errp
, "cannot get vdi info, %s, %s %" PRIu32
" %s",
1107 sd_strerror(rsp
->result
), filename
, snapid
, tag
);
1108 if (rsp
->result
== SD_RES_NO_VDI
) {
1110 } else if (rsp
->result
== SD_RES_VDI_LOCKED
) {
1125 static void coroutine_fn
add_aio_request(BDRVSheepdogState
*s
, AIOReq
*aio_req
,
1126 struct iovec
*iov
, int niov
,
1127 enum AIOCBState aiocb_type
)
1129 int nr_copies
= s
->inode
.nr_copies
;
1131 unsigned int wlen
= 0;
1133 uint64_t oid
= aio_req
->oid
;
1134 unsigned int datalen
= aio_req
->data_len
;
1135 uint64_t offset
= aio_req
->offset
;
1136 uint8_t flags
= aio_req
->flags
;
1137 uint64_t old_oid
= aio_req
->base_oid
;
1138 bool create
= aio_req
->create
;
1141 error_report("bug");
1144 memset(&hdr
, 0, sizeof(hdr
));
1146 switch (aiocb_type
) {
1147 case AIOCB_FLUSH_CACHE
:
1148 hdr
.opcode
= SD_OP_FLUSH_VDI
;
1150 case AIOCB_READ_UDATA
:
1151 hdr
.opcode
= SD_OP_READ_OBJ
;
1154 case AIOCB_WRITE_UDATA
:
1156 hdr
.opcode
= SD_OP_CREATE_AND_WRITE_OBJ
;
1158 hdr
.opcode
= SD_OP_WRITE_OBJ
;
1161 hdr
.flags
= SD_FLAG_CMD_WRITE
| flags
;
1163 case AIOCB_DISCARD_OBJ
:
1164 hdr
.opcode
= SD_OP_DISCARD_OBJ
;
1168 if (s
->cache_flags
) {
1169 hdr
.flags
|= s
->cache_flags
;
1173 hdr
.cow_oid
= old_oid
;
1174 hdr
.copies
= s
->inode
.nr_copies
;
1176 hdr
.data_length
= datalen
;
1177 hdr
.offset
= offset
;
1179 hdr
.id
= aio_req
->id
;
1181 qemu_co_mutex_lock(&s
->lock
);
1182 s
->co_send
= qemu_coroutine_self();
1183 aio_set_fd_handler(s
->aio_context
, s
->fd
,
1184 co_read_response
, co_write_request
, s
);
1185 socket_set_cork(s
->fd
, 1);
1188 ret
= qemu_co_send(s
->fd
, &hdr
, sizeof(hdr
));
1189 if (ret
!= sizeof(hdr
)) {
1190 error_report("failed to send a req, %s", strerror(errno
));
1195 ret
= qemu_co_sendv(s
->fd
, iov
, niov
, aio_req
->iov_offset
, wlen
);
1197 error_report("failed to send a data, %s", strerror(errno
));
1201 socket_set_cork(s
->fd
, 0);
1202 aio_set_fd_handler(s
->aio_context
, s
->fd
, co_read_response
, NULL
, s
);
1204 qemu_co_mutex_unlock(&s
->lock
);
1207 static int read_write_object(int fd
, AioContext
*aio_context
, char *buf
,
1208 uint64_t oid
, uint8_t copies
,
1209 unsigned int datalen
, uint64_t offset
,
1210 bool write
, bool create
, uint32_t cache_flags
)
1213 SheepdogObjRsp
*rsp
= (SheepdogObjRsp
*)&hdr
;
1214 unsigned int wlen
, rlen
;
1217 memset(&hdr
, 0, sizeof(hdr
));
1222 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1224 hdr
.opcode
= SD_OP_CREATE_AND_WRITE_OBJ
;
1226 hdr
.opcode
= SD_OP_WRITE_OBJ
;
1231 hdr
.opcode
= SD_OP_READ_OBJ
;
1234 hdr
.flags
|= cache_flags
;
1237 hdr
.data_length
= datalen
;
1238 hdr
.offset
= offset
;
1239 hdr
.copies
= copies
;
1241 ret
= do_req(fd
, aio_context
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1243 error_report("failed to send a request to the sheep");
1247 switch (rsp
->result
) {
1248 case SD_RES_SUCCESS
:
1251 error_report("%s", sd_strerror(rsp
->result
));
1256 static int read_object(int fd
, AioContext
*aio_context
, char *buf
,
1257 uint64_t oid
, uint8_t copies
,
1258 unsigned int datalen
, uint64_t offset
,
1259 uint32_t cache_flags
)
1261 return read_write_object(fd
, aio_context
, buf
, oid
, copies
,
1262 datalen
, offset
, false,
1263 false, cache_flags
);
1266 static int write_object(int fd
, AioContext
*aio_context
, char *buf
,
1267 uint64_t oid
, uint8_t copies
,
1268 unsigned int datalen
, uint64_t offset
, bool create
,
1269 uint32_t cache_flags
)
1271 return read_write_object(fd
, aio_context
, buf
, oid
, copies
,
1272 datalen
, offset
, true,
1273 create
, cache_flags
);
1276 /* update inode with the latest state */
1277 static int reload_inode(BDRVSheepdogState
*s
, uint32_t snapid
, const char *tag
)
1279 Error
*local_err
= NULL
;
1280 SheepdogInode
*inode
;
1284 fd
= connect_to_sdog(s
, &local_err
);
1286 error_report("%s", error_get_pretty(local_err
));;
1287 error_free(local_err
);
1291 inode
= g_malloc(SD_INODE_HEADER_SIZE
);
1293 ret
= find_vdi_name(s
, s
->name
, snapid
, tag
, &vid
, false, &local_err
);
1295 error_report("%s", error_get_pretty(local_err
));;
1296 error_free(local_err
);
1300 ret
= read_object(fd
, s
->aio_context
, (char *)inode
, vid_to_vdi_oid(vid
),
1301 s
->inode
.nr_copies
, SD_INODE_HEADER_SIZE
, 0,
1307 if (inode
->vdi_id
!= s
->inode
.vdi_id
) {
1308 memcpy(&s
->inode
, inode
, SD_INODE_HEADER_SIZE
);
1318 /* Return true if the specified request is linked to the pending list. */
1319 static bool check_simultaneous_create(BDRVSheepdogState
*s
, AIOReq
*aio_req
)
1322 QLIST_FOREACH(areq
, &s
->inflight_aio_head
, aio_siblings
) {
1323 if (areq
!= aio_req
&& areq
->oid
== aio_req
->oid
) {
1325 * Sheepdog cannot handle simultaneous create requests to the same
1326 * object, so we cannot send the request until the previous request
1329 DPRINTF("simultaneous create to %" PRIx64
"\n", aio_req
->oid
);
1331 aio_req
->base_oid
= 0;
1332 aio_req
->create
= false;
1333 QLIST_REMOVE(aio_req
, aio_siblings
);
1334 QLIST_INSERT_HEAD(&s
->pending_aio_head
, aio_req
, aio_siblings
);
1342 static void coroutine_fn
resend_aioreq(BDRVSheepdogState
*s
, AIOReq
*aio_req
)
1344 SheepdogAIOCB
*acb
= aio_req
->aiocb
;
1346 aio_req
->create
= false;
1348 /* check whether this request becomes a CoW one */
1349 if (acb
->aiocb_type
== AIOCB_WRITE_UDATA
&& is_data_obj(aio_req
->oid
)) {
1350 int idx
= data_oid_to_idx(aio_req
->oid
);
1352 if (is_data_obj_writable(&s
->inode
, idx
)) {
1356 if (check_simultaneous_create(s
, aio_req
)) {
1360 if (s
->inode
.data_vdi_id
[idx
]) {
1361 aio_req
->base_oid
= vid_to_data_oid(s
->inode
.data_vdi_id
[idx
], idx
);
1362 aio_req
->flags
|= SD_FLAG_CMD_COW
;
1364 aio_req
->create
= true;
1367 if (is_data_obj(aio_req
->oid
)) {
1368 add_aio_request(s
, aio_req
, acb
->qiov
->iov
, acb
->qiov
->niov
,
1372 iov
.iov_base
= &s
->inode
;
1373 iov
.iov_len
= sizeof(s
->inode
);
1374 add_aio_request(s
, aio_req
, &iov
, 1, AIOCB_WRITE_UDATA
);
1378 static void sd_detach_aio_context(BlockDriverState
*bs
)
1380 BDRVSheepdogState
*s
= bs
->opaque
;
1382 aio_set_fd_handler(s
->aio_context
, s
->fd
, NULL
, NULL
, NULL
);
1385 static void sd_attach_aio_context(BlockDriverState
*bs
,
1386 AioContext
*new_context
)
1388 BDRVSheepdogState
*s
= bs
->opaque
;
1390 s
->aio_context
= new_context
;
1391 aio_set_fd_handler(new_context
, s
->fd
, co_read_response
, NULL
, s
);
1394 /* TODO Convert to fine grained options */
1395 static QemuOptsList runtime_opts
= {
1397 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
1401 .type
= QEMU_OPT_STRING
,
1402 .help
= "URL to the sheepdog image",
1404 { /* end of list */ }
1408 static int sd_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1413 BDRVSheepdogState
*s
= bs
->opaque
;
1414 char vdi
[SD_MAX_VDI_LEN
], tag
[SD_MAX_VDI_TAG_LEN
];
1418 Error
*local_err
= NULL
;
1419 const char *filename
;
1422 s
->aio_context
= bdrv_get_aio_context(bs
);
1424 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
1425 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
1427 error_propagate(errp
, local_err
);
1432 filename
= qemu_opt_get(opts
, "filename");
1434 QLIST_INIT(&s
->inflight_aio_head
);
1435 QLIST_INIT(&s
->pending_aio_head
);
1436 QLIST_INIT(&s
->failed_aio_head
);
1439 memset(vdi
, 0, sizeof(vdi
));
1440 memset(tag
, 0, sizeof(tag
));
1442 if (strstr(filename
, "://")) {
1443 ret
= sd_parse_uri(s
, filename
, vdi
, &snapid
, tag
);
1445 ret
= parse_vdiname(s
, filename
, vdi
, &snapid
, tag
);
1448 error_setg(errp
, "Can't parse filename");
1451 s
->fd
= get_sheep_fd(s
, errp
);
1457 ret
= find_vdi_name(s
, vdi
, snapid
, tag
, &vid
, true, errp
);
1463 * QEMU block layer emulates writethrough cache as 'writeback + flush', so
1464 * we always set SD_FLAG_CMD_CACHE (writeback cache) as default.
1466 s
->cache_flags
= SD_FLAG_CMD_CACHE
;
1467 if (flags
& BDRV_O_NOCACHE
) {
1468 s
->cache_flags
= SD_FLAG_CMD_DIRECT
;
1470 s
->discard_supported
= true;
1472 if (snapid
|| tag
[0] != '\0') {
1473 DPRINTF("%" PRIx32
" snapshot inode was open.\n", vid
);
1474 s
->is_snapshot
= true;
1477 fd
= connect_to_sdog(s
, errp
);
1483 buf
= g_malloc(SD_INODE_SIZE
);
1484 ret
= read_object(fd
, s
->aio_context
, buf
, vid_to_vdi_oid(vid
),
1485 0, SD_INODE_SIZE
, 0, s
->cache_flags
);
1490 error_setg(errp
, "Can't read snapshot inode");
1494 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
1495 s
->min_dirty_data_idx
= UINT32_MAX
;
1496 s
->max_dirty_data_idx
= 0;
1498 bs
->total_sectors
= s
->inode
.vdi_size
/ BDRV_SECTOR_SIZE
;
1499 pstrcpy(s
->name
, sizeof(s
->name
), vdi
);
1500 qemu_co_mutex_init(&s
->lock
);
1501 qemu_opts_del(opts
);
1505 aio_set_fd_handler(bdrv_get_aio_context(bs
), s
->fd
, NULL
, NULL
, NULL
);
1509 qemu_opts_del(opts
);
1514 static int do_sd_create(BDRVSheepdogState
*s
, uint32_t *vdi_id
, int snapshot
,
1518 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1520 unsigned int wlen
, rlen
= 0;
1521 char buf
[SD_MAX_VDI_LEN
];
1523 fd
= connect_to_sdog(s
, errp
);
1528 /* FIXME: would it be better to fail (e.g., return -EIO) when filename
1529 * does not fit in buf? For now, just truncate and avoid buffer overrun.
1531 memset(buf
, 0, sizeof(buf
));
1532 pstrcpy(buf
, sizeof(buf
), s
->name
);
1534 memset(&hdr
, 0, sizeof(hdr
));
1535 hdr
.opcode
= SD_OP_NEW_VDI
;
1536 hdr
.base_vdi_id
= s
->inode
.vdi_id
;
1538 wlen
= SD_MAX_VDI_LEN
;
1540 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1541 hdr
.snapid
= snapshot
;
1543 hdr
.data_length
= wlen
;
1544 hdr
.vdi_size
= s
->inode
.vdi_size
;
1545 hdr
.copy_policy
= s
->inode
.copy_policy
;
1546 hdr
.copies
= s
->inode
.nr_copies
;
1548 ret
= do_req(fd
, s
->aio_context
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1553 error_setg_errno(errp
, -ret
, "create failed");
1557 if (rsp
->result
!= SD_RES_SUCCESS
) {
1558 error_setg(errp
, "%s, %s", sd_strerror(rsp
->result
), s
->inode
.name
);
1563 *vdi_id
= rsp
->vdi_id
;
1569 static int sd_prealloc(const char *filename
, Error
**errp
)
1571 BlockDriverState
*bs
= NULL
;
1572 uint32_t idx
, max_idx
;
1574 void *buf
= g_malloc0(SD_DATA_OBJ_SIZE
);
1577 ret
= bdrv_open(&bs
, filename
, NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
1580 goto out_with_err_set
;
1583 vdi_size
= bdrv_getlength(bs
);
1588 max_idx
= DIV_ROUND_UP(vdi_size
, SD_DATA_OBJ_SIZE
);
1590 for (idx
= 0; idx
< max_idx
; idx
++) {
1592 * The created image can be a cloned image, so we need to read
1593 * a data from the source image.
1595 ret
= bdrv_pread(bs
, idx
* SD_DATA_OBJ_SIZE
, buf
, SD_DATA_OBJ_SIZE
);
1599 ret
= bdrv_pwrite(bs
, idx
* SD_DATA_OBJ_SIZE
, buf
, SD_DATA_OBJ_SIZE
);
1607 error_setg_errno(errp
, -ret
, "Can't pre-allocate");
1619 * Sheepdog support two kinds of redundancy, full replication and erasure
1622 * # create a fully replicated vdi with x copies
1623 * -o redundancy=x (1 <= x <= SD_MAX_COPIES)
1625 * # create a erasure coded vdi with x data strips and y parity strips
1626 * -o redundancy=x:y (x must be one of {2,4,8,16} and 1 <= y < SD_EC_MAX_STRIP)
1628 static int parse_redundancy(BDRVSheepdogState
*s
, const char *opt
)
1630 struct SheepdogInode
*inode
= &s
->inode
;
1631 const char *n1
, *n2
;
1635 pstrcpy(p
, sizeof(p
), opt
);
1636 n1
= strtok(p
, ":");
1637 n2
= strtok(NULL
, ":");
1643 copy
= strtol(n1
, NULL
, 10);
1644 if (copy
> SD_MAX_COPIES
|| copy
< 1) {
1648 inode
->copy_policy
= 0;
1649 inode
->nr_copies
= copy
;
1653 if (copy
!= 2 && copy
!= 4 && copy
!= 8 && copy
!= 16) {
1657 parity
= strtol(n2
, NULL
, 10);
1658 if (parity
>= SD_EC_MAX_STRIP
|| parity
< 1) {
1663 * 4 bits for parity and 4 bits for data.
1664 * We have to compress upper data bits because it can't represent 16
1666 inode
->copy_policy
= ((copy
/ 2) << 4) + parity
;
1667 inode
->nr_copies
= copy
+ parity
;
1672 static int sd_create(const char *filename
, QemuOpts
*opts
,
1677 char *backing_file
= NULL
;
1679 BDRVSheepdogState
*s
;
1680 char tag
[SD_MAX_VDI_TAG_LEN
];
1682 bool prealloc
= false;
1684 s
= g_new0(BDRVSheepdogState
, 1);
1686 memset(tag
, 0, sizeof(tag
));
1687 if (strstr(filename
, "://")) {
1688 ret
= sd_parse_uri(s
, filename
, s
->name
, &snapid
, tag
);
1690 ret
= parse_vdiname(s
, filename
, s
->name
, &snapid
, tag
);
1693 error_setg(errp
, "Can't parse filename");
1697 s
->inode
.vdi_size
= ROUND_UP(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
.type
= LOCK_TYPE_NORMAL
;
1797 hdr
.base_vdi_id
= s
->inode
.vdi_id
;
1798 wlen
= strlen(s
->name
) + 1;
1799 hdr
.data_length
= wlen
;
1800 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1802 ret
= do_req(fd
, s
->aio_context
, (SheepdogReq
*)&hdr
,
1803 s
->name
, &wlen
, &rlen
);
1807 if (!ret
&& rsp
->result
!= SD_RES_SUCCESS
&&
1808 rsp
->result
!= SD_RES_VDI_NOT_LOCKED
) {
1809 error_report("%s, %s", sd_strerror(rsp
->result
), s
->name
);
1812 aio_set_fd_handler(bdrv_get_aio_context(bs
), s
->fd
, NULL
, NULL
, NULL
);
1814 g_free(s
->host_spec
);
1817 static int64_t sd_getlength(BlockDriverState
*bs
)
1819 BDRVSheepdogState
*s
= bs
->opaque
;
1821 return s
->inode
.vdi_size
;
1824 static int sd_truncate(BlockDriverState
*bs
, int64_t offset
)
1826 Error
*local_err
= NULL
;
1827 BDRVSheepdogState
*s
= bs
->opaque
;
1829 unsigned int datalen
;
1831 if (offset
< s
->inode
.vdi_size
) {
1832 error_report("shrinking is not supported");
1834 } else if (offset
> SD_MAX_VDI_SIZE
) {
1835 error_report("too big image size");
1839 fd
= connect_to_sdog(s
, &local_err
);
1841 error_report("%s", error_get_pretty(local_err
));;
1842 error_free(local_err
);
1846 /* we don't need to update entire object */
1847 datalen
= SD_INODE_SIZE
- sizeof(s
->inode
.data_vdi_id
);
1848 s
->inode
.vdi_size
= offset
;
1849 ret
= write_object(fd
, s
->aio_context
, (char *)&s
->inode
,
1850 vid_to_vdi_oid(s
->inode
.vdi_id
), s
->inode
.nr_copies
,
1851 datalen
, 0, false, s
->cache_flags
);
1855 error_report("failed to update an inode.");
1862 * This function is called after writing data objects. If we need to
1863 * update metadata, this sends a write request to the vdi object.
1864 * Otherwise, this switches back to sd_co_readv/writev.
1866 static void coroutine_fn
sd_write_done(SheepdogAIOCB
*acb
)
1868 BDRVSheepdogState
*s
= acb
->common
.bs
->opaque
;
1871 uint32_t offset
, data_len
, mn
, mx
;
1873 mn
= s
->min_dirty_data_idx
;
1874 mx
= s
->max_dirty_data_idx
;
1876 /* we need to update the vdi object. */
1877 offset
= sizeof(s
->inode
) - sizeof(s
->inode
.data_vdi_id
) +
1878 mn
* sizeof(s
->inode
.data_vdi_id
[0]);
1879 data_len
= (mx
- mn
+ 1) * sizeof(s
->inode
.data_vdi_id
[0]);
1881 s
->min_dirty_data_idx
= UINT32_MAX
;
1882 s
->max_dirty_data_idx
= 0;
1884 iov
.iov_base
= &s
->inode
;
1885 iov
.iov_len
= sizeof(s
->inode
);
1886 aio_req
= alloc_aio_req(s
, acb
, vid_to_vdi_oid(s
->inode
.vdi_id
),
1887 data_len
, offset
, 0, false, 0, offset
);
1888 QLIST_INSERT_HEAD(&s
->inflight_aio_head
, aio_req
, aio_siblings
);
1889 add_aio_request(s
, aio_req
, &iov
, 1, AIOCB_WRITE_UDATA
);
1891 acb
->aio_done_func
= sd_finish_aiocb
;
1892 acb
->aiocb_type
= AIOCB_WRITE_UDATA
;
1896 sd_finish_aiocb(acb
);
1899 /* Delete current working VDI on the snapshot chain */
1900 static bool sd_delete(BDRVSheepdogState
*s
)
1902 Error
*local_err
= NULL
;
1903 unsigned int wlen
= SD_MAX_VDI_LEN
, rlen
= 0;
1904 SheepdogVdiReq hdr
= {
1905 .opcode
= SD_OP_DEL_VDI
,
1906 .base_vdi_id
= s
->inode
.vdi_id
,
1907 .data_length
= wlen
,
1908 .flags
= SD_FLAG_CMD_WRITE
,
1910 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1913 fd
= connect_to_sdog(s
, &local_err
);
1915 error_report("%s", error_get_pretty(local_err
));;
1916 error_free(local_err
);
1920 ret
= do_req(fd
, s
->aio_context
, (SheepdogReq
*)&hdr
,
1921 s
->name
, &wlen
, &rlen
);
1926 switch (rsp
->result
) {
1928 error_report("%s was already deleted", s
->name
);
1930 case SD_RES_SUCCESS
:
1933 error_report("%s, %s", sd_strerror(rsp
->result
), s
->name
);
1941 * Create a writable VDI from a snapshot
1943 static int sd_create_branch(BDRVSheepdogState
*s
)
1945 Error
*local_err
= NULL
;
1951 DPRINTF("%" PRIx32
" is snapshot.\n", s
->inode
.vdi_id
);
1953 buf
= g_malloc(SD_INODE_SIZE
);
1956 * Even If deletion fails, we will just create extra snapshot based on
1957 * the working VDI which was supposed to be deleted. So no need to
1960 deleted
= sd_delete(s
);
1961 ret
= do_sd_create(s
, &vid
, !deleted
, &local_err
);
1963 error_report("%s", error_get_pretty(local_err
));;
1964 error_free(local_err
);
1968 DPRINTF("%" PRIx32
" is created.\n", vid
);
1970 fd
= connect_to_sdog(s
, &local_err
);
1972 error_report("%s", error_get_pretty(local_err
));;
1973 error_free(local_err
);
1978 ret
= read_object(fd
, s
->aio_context
, buf
, vid_to_vdi_oid(vid
),
1979 s
->inode
.nr_copies
, SD_INODE_SIZE
, 0, s
->cache_flags
);
1987 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
1989 s
->is_snapshot
= false;
1991 DPRINTF("%" PRIx32
" was newly created.\n", s
->inode
.vdi_id
);
2000 * Send I/O requests to the server.
2002 * This function sends requests to the server, links the requests to
2003 * the inflight_list in BDRVSheepdogState, and exits without
2004 * waiting the response. The responses are received in the
2005 * `aio_read_response' function which is called from the main loop as
2008 * Returns 1 when we need to wait a response, 0 when there is no sent
2009 * request and -errno in error cases.
2011 static int coroutine_fn
sd_co_rw_vector(void *p
)
2013 SheepdogAIOCB
*acb
= p
;
2015 unsigned long len
, done
= 0, total
= acb
->nb_sectors
* BDRV_SECTOR_SIZE
;
2016 unsigned long idx
= acb
->sector_num
* BDRV_SECTOR_SIZE
/ SD_DATA_OBJ_SIZE
;
2018 uint64_t offset
= (acb
->sector_num
* BDRV_SECTOR_SIZE
) % SD_DATA_OBJ_SIZE
;
2019 BDRVSheepdogState
*s
= acb
->common
.bs
->opaque
;
2020 SheepdogInode
*inode
= &s
->inode
;
2023 if (acb
->aiocb_type
== AIOCB_WRITE_UDATA
&& s
->is_snapshot
) {
2025 * In the case we open the snapshot VDI, Sheepdog creates the
2026 * writable VDI when we do a write operation first.
2028 ret
= sd_create_branch(s
);
2036 * Make sure we don't free the aiocb before we are done with all requests.
2037 * This additional reference is dropped at the end of this function.
2041 while (done
!= total
) {
2043 uint64_t old_oid
= 0;
2044 bool create
= false;
2046 oid
= vid_to_data_oid(inode
->data_vdi_id
[idx
], idx
);
2048 len
= MIN(total
- done
, SD_DATA_OBJ_SIZE
- offset
);
2050 switch (acb
->aiocb_type
) {
2051 case AIOCB_READ_UDATA
:
2052 if (!inode
->data_vdi_id
[idx
]) {
2053 qemu_iovec_memset(acb
->qiov
, done
, 0, len
);
2057 case AIOCB_WRITE_UDATA
:
2058 if (!inode
->data_vdi_id
[idx
]) {
2060 } else if (!is_data_obj_writable(inode
, idx
)) {
2064 flags
= SD_FLAG_CMD_COW
;
2067 case AIOCB_DISCARD_OBJ
:
2069 * We discard the object only when the whole object is
2070 * 1) allocated 2) trimmed. Otherwise, simply skip it.
2072 if (len
!= SD_DATA_OBJ_SIZE
|| inode
->data_vdi_id
[idx
] == 0) {
2081 DPRINTF("update ino (%" PRIu32
") %" PRIu64
" %" PRIu64
" %ld\n",
2083 vid_to_data_oid(inode
->data_vdi_id
[idx
], idx
), idx
);
2084 oid
= vid_to_data_oid(inode
->vdi_id
, idx
);
2085 DPRINTF("new oid %" PRIx64
"\n", oid
);
2088 aio_req
= alloc_aio_req(s
, acb
, oid
, len
, offset
, flags
, create
,
2090 QLIST_INSERT_HEAD(&s
->inflight_aio_head
, aio_req
, aio_siblings
);
2093 if (check_simultaneous_create(s
, aio_req
)) {
2098 add_aio_request(s
, aio_req
, acb
->qiov
->iov
, acb
->qiov
->niov
,
2106 if (!--acb
->nr_pending
) {
2112 static coroutine_fn
int sd_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
2113 int nb_sectors
, QEMUIOVector
*qiov
)
2117 int64_t offset
= (sector_num
+ nb_sectors
) * BDRV_SECTOR_SIZE
;
2118 BDRVSheepdogState
*s
= bs
->opaque
;
2120 if (bs
->growable
&& offset
> s
->inode
.vdi_size
) {
2121 ret
= sd_truncate(bs
, offset
);
2127 acb
= sd_aio_setup(bs
, qiov
, sector_num
, nb_sectors
);
2128 acb
->aio_done_func
= sd_write_done
;
2129 acb
->aiocb_type
= AIOCB_WRITE_UDATA
;
2131 ret
= sd_co_rw_vector(acb
);
2133 qemu_aio_unref(acb
);
2137 qemu_coroutine_yield();
2142 static coroutine_fn
int sd_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
2143 int nb_sectors
, QEMUIOVector
*qiov
)
2148 acb
= sd_aio_setup(bs
, qiov
, sector_num
, nb_sectors
);
2149 acb
->aiocb_type
= AIOCB_READ_UDATA
;
2150 acb
->aio_done_func
= sd_finish_aiocb
;
2152 ret
= sd_co_rw_vector(acb
);
2154 qemu_aio_unref(acb
);
2158 qemu_coroutine_yield();
2163 static int coroutine_fn
sd_co_flush_to_disk(BlockDriverState
*bs
)
2165 BDRVSheepdogState
*s
= bs
->opaque
;
2169 if (s
->cache_flags
!= SD_FLAG_CMD_CACHE
) {
2173 acb
= sd_aio_setup(bs
, NULL
, 0, 0);
2174 acb
->aiocb_type
= AIOCB_FLUSH_CACHE
;
2175 acb
->aio_done_func
= sd_finish_aiocb
;
2177 aio_req
= alloc_aio_req(s
, acb
, vid_to_vdi_oid(s
->inode
.vdi_id
),
2178 0, 0, 0, false, 0, 0);
2179 QLIST_INSERT_HEAD(&s
->inflight_aio_head
, aio_req
, aio_siblings
);
2180 add_aio_request(s
, aio_req
, NULL
, 0, acb
->aiocb_type
);
2182 qemu_coroutine_yield();
2186 static int sd_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
)
2188 Error
*local_err
= NULL
;
2189 BDRVSheepdogState
*s
= bs
->opaque
;
2192 SheepdogInode
*inode
;
2193 unsigned int datalen
;
2195 DPRINTF("sn_info: name %s id_str %s s: name %s vm_state_size %" PRId64
" "
2196 "is_snapshot %d\n", sn_info
->name
, sn_info
->id_str
,
2197 s
->name
, sn_info
->vm_state_size
, s
->is_snapshot
);
2199 if (s
->is_snapshot
) {
2200 error_report("You can't create a snapshot of a snapshot VDI, "
2201 "%s (%" PRIu32
").", s
->name
, s
->inode
.vdi_id
);
2206 DPRINTF("%s %s\n", sn_info
->name
, sn_info
->id_str
);
2208 s
->inode
.vm_state_size
= sn_info
->vm_state_size
;
2209 s
->inode
.vm_clock_nsec
= sn_info
->vm_clock_nsec
;
2210 /* It appears that inode.tag does not require a NUL terminator,
2211 * which means this use of strncpy is ok.
2213 strncpy(s
->inode
.tag
, sn_info
->name
, sizeof(s
->inode
.tag
));
2214 /* we don't need to update entire object */
2215 datalen
= SD_INODE_SIZE
- sizeof(s
->inode
.data_vdi_id
);
2216 inode
= g_malloc(datalen
);
2218 /* refresh inode. */
2219 fd
= connect_to_sdog(s
, &local_err
);
2221 error_report("%s", error_get_pretty(local_err
));;
2222 error_free(local_err
);
2227 ret
= write_object(fd
, s
->aio_context
, (char *)&s
->inode
,
2228 vid_to_vdi_oid(s
->inode
.vdi_id
), s
->inode
.nr_copies
,
2229 datalen
, 0, false, s
->cache_flags
);
2231 error_report("failed to write snapshot's inode.");
2235 ret
= do_sd_create(s
, &new_vid
, 1, &local_err
);
2237 error_report("%s", error_get_pretty(local_err
));;
2238 error_free(local_err
);
2239 error_report("failed to create inode for snapshot. %s",
2244 ret
= read_object(fd
, s
->aio_context
, (char *)inode
,
2245 vid_to_vdi_oid(new_vid
), s
->inode
.nr_copies
, datalen
, 0,
2249 error_report("failed to read new inode info. %s", strerror(errno
));
2253 memcpy(&s
->inode
, inode
, datalen
);
2254 DPRINTF("s->inode: name %s snap_id %x oid %x\n",
2255 s
->inode
.name
, s
->inode
.snap_id
, s
->inode
.vdi_id
);
2264 * We implement rollback(loadvm) operation to the specified snapshot by
2265 * 1) switch to the snapshot
2266 * 2) rely on sd_create_branch to delete working VDI and
2267 * 3) create a new working VDI based on the specified snapshot
2269 static int sd_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
)
2271 BDRVSheepdogState
*s
= bs
->opaque
;
2272 BDRVSheepdogState
*old_s
;
2273 char tag
[SD_MAX_VDI_TAG_LEN
];
2274 uint32_t snapid
= 0;
2277 old_s
= g_new(BDRVSheepdogState
, 1);
2279 memcpy(old_s
, s
, sizeof(BDRVSheepdogState
));
2281 snapid
= strtoul(snapshot_id
, NULL
, 10);
2285 pstrcpy(tag
, sizeof(tag
), snapshot_id
);
2288 ret
= reload_inode(s
, snapid
, tag
);
2293 ret
= sd_create_branch(s
);
2302 /* recover bdrv_sd_state */
2303 memcpy(s
, old_s
, sizeof(BDRVSheepdogState
));
2306 error_report("failed to open. recover old bdrv_sd_state.");
2311 static int sd_snapshot_delete(BlockDriverState
*bs
,
2312 const char *snapshot_id
,
2316 /* FIXME: Delete specified snapshot id. */
2320 static int sd_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
)
2322 Error
*local_err
= NULL
;
2323 BDRVSheepdogState
*s
= bs
->opaque
;
2325 int fd
, nr
= 1024, ret
, max
= BITS_TO_LONGS(SD_NR_VDIS
) * sizeof(long);
2326 QEMUSnapshotInfo
*sn_tab
= NULL
;
2327 unsigned wlen
, rlen
;
2329 static SheepdogInode inode
;
2330 unsigned long *vdi_inuse
;
2331 unsigned int start_nr
;
2335 vdi_inuse
= g_malloc(max
);
2337 fd
= connect_to_sdog(s
, &local_err
);
2339 error_report("%s", error_get_pretty(local_err
));;
2340 error_free(local_err
);
2348 memset(&req
, 0, sizeof(req
));
2350 req
.opcode
= SD_OP_READ_VDIS
;
2351 req
.data_length
= max
;
2353 ret
= do_req(fd
, s
->aio_context
, (SheepdogReq
*)&req
,
2354 vdi_inuse
, &wlen
, &rlen
);
2361 sn_tab
= g_new0(QEMUSnapshotInfo
, nr
);
2363 /* calculate a vdi id with hash function */
2364 hval
= fnv_64a_buf(s
->name
, strlen(s
->name
), FNV1A_64_INIT
);
2365 start_nr
= hval
& (SD_NR_VDIS
- 1);
2367 fd
= connect_to_sdog(s
, &local_err
);
2369 error_report("%s", error_get_pretty(local_err
));;
2370 error_free(local_err
);
2375 for (vid
= start_nr
; found
< nr
; vid
= (vid
+ 1) % SD_NR_VDIS
) {
2376 if (!test_bit(vid
, vdi_inuse
)) {
2380 /* we don't need to read entire object */
2381 ret
= read_object(fd
, s
->aio_context
, (char *)&inode
,
2382 vid_to_vdi_oid(vid
),
2383 0, SD_INODE_SIZE
- sizeof(inode
.data_vdi_id
), 0,
2390 if (!strcmp(inode
.name
, s
->name
) && is_snapshot(&inode
)) {
2391 sn_tab
[found
].date_sec
= inode
.snap_ctime
>> 32;
2392 sn_tab
[found
].date_nsec
= inode
.snap_ctime
& 0xffffffff;
2393 sn_tab
[found
].vm_state_size
= inode
.vm_state_size
;
2394 sn_tab
[found
].vm_clock_nsec
= inode
.vm_clock_nsec
;
2396 snprintf(sn_tab
[found
].id_str
, sizeof(sn_tab
[found
].id_str
),
2397 "%" PRIu32
, inode
.snap_id
);
2398 pstrcpy(sn_tab
[found
].name
,
2399 MIN(sizeof(sn_tab
[found
].name
), sizeof(inode
.tag
)),
2418 static int do_load_save_vmstate(BDRVSheepdogState
*s
, uint8_t *data
,
2419 int64_t pos
, int size
, int load
)
2421 Error
*local_err
= NULL
;
2423 int fd
, ret
= 0, remaining
= size
;
2424 unsigned int data_len
;
2425 uint64_t vmstate_oid
;
2428 uint32_t vdi_id
= load
? s
->inode
.parent_vdi_id
: s
->inode
.vdi_id
;
2430 fd
= connect_to_sdog(s
, &local_err
);
2432 error_report("%s", error_get_pretty(local_err
));;
2433 error_free(local_err
);
2438 vdi_index
= pos
/ SD_DATA_OBJ_SIZE
;
2439 offset
= pos
% SD_DATA_OBJ_SIZE
;
2441 data_len
= MIN(remaining
, SD_DATA_OBJ_SIZE
- offset
);
2443 vmstate_oid
= vid_to_vmstate_oid(vdi_id
, vdi_index
);
2445 create
= (offset
== 0);
2447 ret
= read_object(fd
, s
->aio_context
, (char *)data
, vmstate_oid
,
2448 s
->inode
.nr_copies
, data_len
, offset
,
2451 ret
= write_object(fd
, s
->aio_context
, (char *)data
, vmstate_oid
,
2452 s
->inode
.nr_copies
, data_len
, offset
, create
,
2457 error_report("failed to save vmstate %s", strerror(errno
));
2463 remaining
-= data_len
;
2471 static int sd_save_vmstate(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
2474 BDRVSheepdogState
*s
= bs
->opaque
;
2478 buf
= qemu_blockalign(bs
, qiov
->size
);
2479 qemu_iovec_to_buf(qiov
, 0, buf
, qiov
->size
);
2480 ret
= do_load_save_vmstate(s
, (uint8_t *) buf
, pos
, qiov
->size
, 0);
2486 static int sd_load_vmstate(BlockDriverState
*bs
, uint8_t *data
,
2487 int64_t pos
, int size
)
2489 BDRVSheepdogState
*s
= bs
->opaque
;
2491 return do_load_save_vmstate(s
, data
, pos
, size
, 1);
2495 static coroutine_fn
int sd_co_discard(BlockDriverState
*bs
, int64_t sector_num
,
2500 BDRVSheepdogState
*s
= bs
->opaque
;
2503 if (!s
->discard_supported
) {
2507 acb
= sd_aio_setup(bs
, &dummy
, sector_num
, nb_sectors
);
2508 acb
->aiocb_type
= AIOCB_DISCARD_OBJ
;
2509 acb
->aio_done_func
= sd_finish_aiocb
;
2511 ret
= sd_co_rw_vector(acb
);
2513 qemu_aio_unref(acb
);
2517 qemu_coroutine_yield();
2522 static coroutine_fn
int64_t
2523 sd_co_get_block_status(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
2526 BDRVSheepdogState
*s
= bs
->opaque
;
2527 SheepdogInode
*inode
= &s
->inode
;
2528 uint64_t offset
= sector_num
* BDRV_SECTOR_SIZE
;
2529 unsigned long start
= offset
/ SD_DATA_OBJ_SIZE
,
2530 end
= DIV_ROUND_UP((sector_num
+ nb_sectors
) *
2531 BDRV_SECTOR_SIZE
, SD_DATA_OBJ_SIZE
);
2533 int64_t ret
= BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
| offset
;
2535 for (idx
= start
; idx
< end
; idx
++) {
2536 if (inode
->data_vdi_id
[idx
] == 0) {
2541 /* Get the longest length of unallocated sectors */
2543 for (idx
= start
+ 1; idx
< end
; idx
++) {
2544 if (inode
->data_vdi_id
[idx
] != 0) {
2550 *pnum
= (idx
- start
) * SD_DATA_OBJ_SIZE
/ BDRV_SECTOR_SIZE
;
2551 if (*pnum
> nb_sectors
) {
2557 static int64_t sd_get_allocated_file_size(BlockDriverState
*bs
)
2559 BDRVSheepdogState
*s
= bs
->opaque
;
2560 SheepdogInode
*inode
= &s
->inode
;
2561 unsigned long i
, last
= DIV_ROUND_UP(inode
->vdi_size
, SD_DATA_OBJ_SIZE
);
2564 for (i
= 0; i
< last
; i
++) {
2565 if (inode
->data_vdi_id
[i
] == 0) {
2568 size
+= SD_DATA_OBJ_SIZE
;
2573 static QemuOptsList sd_create_opts
= {
2574 .name
= "sheepdog-create-opts",
2575 .head
= QTAILQ_HEAD_INITIALIZER(sd_create_opts
.head
),
2578 .name
= BLOCK_OPT_SIZE
,
2579 .type
= QEMU_OPT_SIZE
,
2580 .help
= "Virtual disk size"
2583 .name
= BLOCK_OPT_BACKING_FILE
,
2584 .type
= QEMU_OPT_STRING
,
2585 .help
= "File name of a base image"
2588 .name
= BLOCK_OPT_PREALLOC
,
2589 .type
= QEMU_OPT_STRING
,
2590 .help
= "Preallocation mode (allowed values: off, full)"
2593 .name
= BLOCK_OPT_REDUNDANCY
,
2594 .type
= QEMU_OPT_STRING
,
2595 .help
= "Redundancy of the image"
2597 { /* end of list */ }
2601 static BlockDriver bdrv_sheepdog
= {
2602 .format_name
= "sheepdog",
2603 .protocol_name
= "sheepdog",
2604 .instance_size
= sizeof(BDRVSheepdogState
),
2605 .bdrv_needs_filename
= true,
2606 .bdrv_file_open
= sd_open
,
2607 .bdrv_close
= sd_close
,
2608 .bdrv_create
= sd_create
,
2609 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
2610 .bdrv_getlength
= sd_getlength
,
2611 .bdrv_get_allocated_file_size
= sd_get_allocated_file_size
,
2612 .bdrv_truncate
= sd_truncate
,
2614 .bdrv_co_readv
= sd_co_readv
,
2615 .bdrv_co_writev
= sd_co_writev
,
2616 .bdrv_co_flush_to_disk
= sd_co_flush_to_disk
,
2617 .bdrv_co_discard
= sd_co_discard
,
2618 .bdrv_co_get_block_status
= sd_co_get_block_status
,
2620 .bdrv_snapshot_create
= sd_snapshot_create
,
2621 .bdrv_snapshot_goto
= sd_snapshot_goto
,
2622 .bdrv_snapshot_delete
= sd_snapshot_delete
,
2623 .bdrv_snapshot_list
= sd_snapshot_list
,
2625 .bdrv_save_vmstate
= sd_save_vmstate
,
2626 .bdrv_load_vmstate
= sd_load_vmstate
,
2628 .bdrv_detach_aio_context
= sd_detach_aio_context
,
2629 .bdrv_attach_aio_context
= sd_attach_aio_context
,
2631 .create_opts
= &sd_create_opts
,
2634 static BlockDriver bdrv_sheepdog_tcp
= {
2635 .format_name
= "sheepdog",
2636 .protocol_name
= "sheepdog+tcp",
2637 .instance_size
= sizeof(BDRVSheepdogState
),
2638 .bdrv_needs_filename
= true,
2639 .bdrv_file_open
= sd_open
,
2640 .bdrv_close
= sd_close
,
2641 .bdrv_create
= sd_create
,
2642 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
2643 .bdrv_getlength
= sd_getlength
,
2644 .bdrv_get_allocated_file_size
= sd_get_allocated_file_size
,
2645 .bdrv_truncate
= sd_truncate
,
2647 .bdrv_co_readv
= sd_co_readv
,
2648 .bdrv_co_writev
= sd_co_writev
,
2649 .bdrv_co_flush_to_disk
= sd_co_flush_to_disk
,
2650 .bdrv_co_discard
= sd_co_discard
,
2651 .bdrv_co_get_block_status
= sd_co_get_block_status
,
2653 .bdrv_snapshot_create
= sd_snapshot_create
,
2654 .bdrv_snapshot_goto
= sd_snapshot_goto
,
2655 .bdrv_snapshot_delete
= sd_snapshot_delete
,
2656 .bdrv_snapshot_list
= sd_snapshot_list
,
2658 .bdrv_save_vmstate
= sd_save_vmstate
,
2659 .bdrv_load_vmstate
= sd_load_vmstate
,
2661 .bdrv_detach_aio_context
= sd_detach_aio_context
,
2662 .bdrv_attach_aio_context
= sd_attach_aio_context
,
2664 .create_opts
= &sd_create_opts
,
2667 static BlockDriver bdrv_sheepdog_unix
= {
2668 .format_name
= "sheepdog",
2669 .protocol_name
= "sheepdog+unix",
2670 .instance_size
= sizeof(BDRVSheepdogState
),
2671 .bdrv_needs_filename
= true,
2672 .bdrv_file_open
= sd_open
,
2673 .bdrv_close
= sd_close
,
2674 .bdrv_create
= sd_create
,
2675 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
2676 .bdrv_getlength
= sd_getlength
,
2677 .bdrv_get_allocated_file_size
= sd_get_allocated_file_size
,
2678 .bdrv_truncate
= sd_truncate
,
2680 .bdrv_co_readv
= sd_co_readv
,
2681 .bdrv_co_writev
= sd_co_writev
,
2682 .bdrv_co_flush_to_disk
= sd_co_flush_to_disk
,
2683 .bdrv_co_discard
= sd_co_discard
,
2684 .bdrv_co_get_block_status
= sd_co_get_block_status
,
2686 .bdrv_snapshot_create
= sd_snapshot_create
,
2687 .bdrv_snapshot_goto
= sd_snapshot_goto
,
2688 .bdrv_snapshot_delete
= sd_snapshot_delete
,
2689 .bdrv_snapshot_list
= sd_snapshot_list
,
2691 .bdrv_save_vmstate
= sd_save_vmstate
,
2692 .bdrv_load_vmstate
= sd_load_vmstate
,
2694 .bdrv_detach_aio_context
= sd_detach_aio_context
,
2695 .bdrv_attach_aio_context
= sd_attach_aio_context
,
2697 .create_opts
= &sd_create_opts
,
2700 static void bdrv_sheepdog_init(void)
2702 bdrv_register(&bdrv_sheepdog
);
2703 bdrv_register(&bdrv_sheepdog_tcp
);
2704 bdrv_register(&bdrv_sheepdog_unix
);
2706 block_init(bdrv_sheepdog_init
);