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/>.
12 #include "qemu-common.h"
13 #include "qemu-error.h"
14 #include "qemu_socket.h"
15 #include "block_int.h"
18 #define SD_PROTO_VER 0x01
20 #define SD_DEFAULT_ADDR "localhost"
21 #define SD_DEFAULT_PORT "7000"
23 #define SD_OP_CREATE_AND_WRITE_OBJ 0x01
24 #define SD_OP_READ_OBJ 0x02
25 #define SD_OP_WRITE_OBJ 0x03
27 #define SD_OP_NEW_VDI 0x11
28 #define SD_OP_LOCK_VDI 0x12
29 #define SD_OP_RELEASE_VDI 0x13
30 #define SD_OP_GET_VDI_INFO 0x14
31 #define SD_OP_READ_VDIS 0x15
33 #define SD_FLAG_CMD_WRITE 0x01
34 #define SD_FLAG_CMD_COW 0x02
36 #define SD_RES_SUCCESS 0x00 /* Success */
37 #define SD_RES_UNKNOWN 0x01 /* Unknown error */
38 #define SD_RES_NO_OBJ 0x02 /* No object found */
39 #define SD_RES_EIO 0x03 /* I/O error */
40 #define SD_RES_VDI_EXIST 0x04 /* Vdi exists already */
41 #define SD_RES_INVALID_PARMS 0x05 /* Invalid parameters */
42 #define SD_RES_SYSTEM_ERROR 0x06 /* System error */
43 #define SD_RES_VDI_LOCKED 0x07 /* Vdi is locked */
44 #define SD_RES_NO_VDI 0x08 /* No vdi found */
45 #define SD_RES_NO_BASE_VDI 0x09 /* No base vdi found */
46 #define SD_RES_VDI_READ 0x0A /* Cannot read requested vdi */
47 #define SD_RES_VDI_WRITE 0x0B /* Cannot write requested vdi */
48 #define SD_RES_BASE_VDI_READ 0x0C /* Cannot read base vdi */
49 #define SD_RES_BASE_VDI_WRITE 0x0D /* Cannot write base vdi */
50 #define SD_RES_NO_TAG 0x0E /* Requested tag is not found */
51 #define SD_RES_STARTUP 0x0F /* Sheepdog is on starting up */
52 #define SD_RES_VDI_NOT_LOCKED 0x10 /* Vdi is not locked */
53 #define SD_RES_SHUTDOWN 0x11 /* Sheepdog is shutting down */
54 #define SD_RES_NO_MEM 0x12 /* Cannot allocate memory */
55 #define SD_RES_FULL_VDI 0x13 /* we already have the maximum vdis */
56 #define SD_RES_VER_MISMATCH 0x14 /* Protocol version mismatch */
57 #define SD_RES_NO_SPACE 0x15 /* Server has no room for new objects */
58 #define SD_RES_WAIT_FOR_FORMAT 0x16 /* Waiting for a format operation */
59 #define SD_RES_WAIT_FOR_JOIN 0x17 /* Waiting for other nodes joining */
60 #define SD_RES_JOIN_FAILED 0x18 /* Target node had failed to join sheepdog */
65 * 0 - 19 (20 bits): data object space
66 * 20 - 31 (12 bits): reserved data object space
67 * 32 - 55 (24 bits): vdi object space
68 * 56 - 59 ( 4 bits): reserved vdi object space
69 * 60 - 63 ( 4 bits): object type identifier space
72 #define VDI_SPACE_SHIFT 32
73 #define VDI_BIT (UINT64_C(1) << 63)
74 #define VMSTATE_BIT (UINT64_C(1) << 62)
75 #define MAX_DATA_OBJS (UINT64_C(1) << 20)
76 #define MAX_CHILDREN 1024
77 #define SD_MAX_VDI_LEN 256
78 #define SD_MAX_VDI_TAG_LEN 256
79 #define SD_NR_VDIS (1U << 24)
80 #define SD_DATA_OBJ_SIZE (UINT64_C(1) << 22)
81 #define SD_MAX_VDI_SIZE (SD_DATA_OBJ_SIZE * MAX_DATA_OBJS)
82 #define SECTOR_SIZE 512
84 #define SD_INODE_SIZE (sizeof(SheepdogInode))
85 #define CURRENT_VDI_ID 0
87 typedef struct SheepdogReq
{
94 uint32_t opcode_specific
[8];
97 typedef struct SheepdogRsp
{
103 uint32_t data_length
;
105 uint32_t opcode_specific
[7];
108 typedef struct SheepdogObjReq
{
114 uint32_t data_length
;
122 typedef struct SheepdogObjRsp
{
128 uint32_t data_length
;
134 typedef struct SheepdogVdiReq
{
140 uint32_t data_length
;
142 uint32_t base_vdi_id
;
148 typedef struct SheepdogVdiRsp
{
154 uint32_t data_length
;
161 typedef struct SheepdogInode
{
162 char name
[SD_MAX_VDI_LEN
];
163 char tag
[SD_MAX_VDI_TAG_LEN
];
166 uint64_t vm_clock_nsec
;
168 uint64_t vm_state_size
;
169 uint16_t copy_policy
;
171 uint8_t block_size_shift
;
174 uint32_t parent_vdi_id
;
175 uint32_t child_vdi_id
[MAX_CHILDREN
];
176 uint32_t data_vdi_id
[MAX_DATA_OBJS
];
180 * 64 bit FNV-1a non-zero initial basis
182 #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
185 * 64 bit Fowler/Noll/Vo FNV-1a hash code
187 static inline uint64_t fnv_64a_buf(void *buf
, size_t len
, uint64_t hval
)
189 unsigned char *bp
= buf
;
190 unsigned char *be
= bp
+ len
;
192 hval
^= (uint64_t) *bp
++;
193 hval
+= (hval
<< 1) + (hval
<< 4) + (hval
<< 5) +
194 (hval
<< 7) + (hval
<< 8) + (hval
<< 40);
199 static inline int is_data_obj_writable(SheepdogInode
*inode
, unsigned int idx
)
201 return inode
->vdi_id
== inode
->data_vdi_id
[idx
];
204 static inline int is_data_obj(uint64_t oid
)
206 return !(VDI_BIT
& oid
);
209 static inline uint64_t data_oid_to_idx(uint64_t oid
)
211 return oid
& (MAX_DATA_OBJS
- 1);
214 static inline uint64_t vid_to_vdi_oid(uint32_t vid
)
216 return VDI_BIT
| ((uint64_t)vid
<< VDI_SPACE_SHIFT
);
219 static inline uint64_t vid_to_vmstate_oid(uint32_t vid
, uint32_t idx
)
221 return VMSTATE_BIT
| ((uint64_t)vid
<< VDI_SPACE_SHIFT
) | idx
;
224 static inline uint64_t vid_to_data_oid(uint32_t vid
, uint32_t idx
)
226 return ((uint64_t)vid
<< VDI_SPACE_SHIFT
) | idx
;
229 static inline int is_snapshot(struct SheepdogInode
*inode
)
231 return !!inode
->snap_ctime
;
236 #define dprintf(fmt, args...) \
238 fprintf(stdout, "%s %d: " fmt, __func__, __LINE__, ##args); \
241 #define dprintf(fmt, args...)
244 typedef struct SheepdogAIOCB SheepdogAIOCB
;
246 typedef struct AIOReq
{
247 SheepdogAIOCB
*aiocb
;
248 unsigned int iov_offset
;
253 unsigned int data_len
;
257 QLIST_ENTRY(AIOReq
) outstanding_aio_siblings
;
258 QLIST_ENTRY(AIOReq
) aioreq_siblings
;
266 struct SheepdogAIOCB
{
267 BlockDriverAIOCB common
;
275 enum AIOCBState aiocb_type
;
277 Coroutine
*coroutine
;
278 void (*aio_done_func
)(SheepdogAIOCB
*);
282 QLIST_HEAD(aioreq_head
, AIOReq
) aioreq_head
;
285 typedef struct BDRVSheepdogState
{
288 uint32_t min_dirty_data_idx
;
289 uint32_t max_dirty_data_idx
;
291 char name
[SD_MAX_VDI_LEN
];
302 uint32_t aioreq_seq_num
;
303 QLIST_HEAD(outstanding_aio_head
, AIOReq
) outstanding_aio_head
;
306 static const char * sd_strerror(int err
)
310 static const struct {
314 {SD_RES_SUCCESS
, "Success"},
315 {SD_RES_UNKNOWN
, "Unknown error"},
316 {SD_RES_NO_OBJ
, "No object found"},
317 {SD_RES_EIO
, "I/O error"},
318 {SD_RES_VDI_EXIST
, "VDI exists already"},
319 {SD_RES_INVALID_PARMS
, "Invalid parameters"},
320 {SD_RES_SYSTEM_ERROR
, "System error"},
321 {SD_RES_VDI_LOCKED
, "VDI is already locked"},
322 {SD_RES_NO_VDI
, "No vdi found"},
323 {SD_RES_NO_BASE_VDI
, "No base VDI found"},
324 {SD_RES_VDI_READ
, "Failed read the requested VDI"},
325 {SD_RES_VDI_WRITE
, "Failed to write the requested VDI"},
326 {SD_RES_BASE_VDI_READ
, "Failed to read the base VDI"},
327 {SD_RES_BASE_VDI_WRITE
, "Failed to write the base VDI"},
328 {SD_RES_NO_TAG
, "Failed to find the requested tag"},
329 {SD_RES_STARTUP
, "The system is still booting"},
330 {SD_RES_VDI_NOT_LOCKED
, "VDI isn't locked"},
331 {SD_RES_SHUTDOWN
, "The system is shutting down"},
332 {SD_RES_NO_MEM
, "Out of memory on the server"},
333 {SD_RES_FULL_VDI
, "We already have the maximum vdis"},
334 {SD_RES_VER_MISMATCH
, "Protocol version mismatch"},
335 {SD_RES_NO_SPACE
, "Server has no space for new objects"},
336 {SD_RES_WAIT_FOR_FORMAT
, "Sheepdog is waiting for a format operation"},
337 {SD_RES_WAIT_FOR_JOIN
, "Sheepdog is waiting for other nodes joining"},
338 {SD_RES_JOIN_FAILED
, "Target node had failed to join sheepdog"},
341 for (i
= 0; i
< ARRAY_SIZE(errors
); ++i
) {
342 if (errors
[i
].err
== err
) {
343 return errors
[i
].desc
;
347 return "Invalid error code";
351 * Sheepdog I/O handling:
353 * 1. In sd_co_rw_vector, we send the I/O requests to the server and
354 * link the requests to the outstanding_list in the
355 * BDRVSheepdogState. The function exits without waiting for
356 * receiving the response.
358 * 2. We receive the response in aio_read_response, the fd handler to
359 * the sheepdog connection. If metadata update is needed, we send
360 * the write request to the vdi object in sd_write_done, the write
361 * completion function. We switch back to sd_co_readv/writev after
362 * all the requests belonging to the AIOCB are finished.
365 static inline AIOReq
*alloc_aio_req(BDRVSheepdogState
*s
, SheepdogAIOCB
*acb
,
366 uint64_t oid
, unsigned int data_len
,
367 uint64_t offset
, uint8_t flags
,
368 uint64_t base_oid
, unsigned int iov_offset
)
372 aio_req
= g_malloc(sizeof(*aio_req
));
373 aio_req
->aiocb
= acb
;
374 aio_req
->iov_offset
= iov_offset
;
376 aio_req
->base_oid
= base_oid
;
377 aio_req
->offset
= offset
;
378 aio_req
->data_len
= data_len
;
379 aio_req
->flags
= flags
;
380 aio_req
->id
= s
->aioreq_seq_num
++;
382 QLIST_INSERT_HEAD(&s
->outstanding_aio_head
, aio_req
,
383 outstanding_aio_siblings
);
384 QLIST_INSERT_HEAD(&acb
->aioreq_head
, aio_req
, aioreq_siblings
);
389 static inline int free_aio_req(BDRVSheepdogState
*s
, AIOReq
*aio_req
)
391 SheepdogAIOCB
*acb
= aio_req
->aiocb
;
392 QLIST_REMOVE(aio_req
, outstanding_aio_siblings
);
393 QLIST_REMOVE(aio_req
, aioreq_siblings
);
396 return !QLIST_EMPTY(&acb
->aioreq_head
);
399 static void coroutine_fn
sd_finish_aiocb(SheepdogAIOCB
*acb
)
401 if (!acb
->canceled
) {
402 qemu_coroutine_enter(acb
->coroutine
, NULL
);
404 qemu_aio_release(acb
);
407 static void sd_aio_cancel(BlockDriverAIOCB
*blockacb
)
409 SheepdogAIOCB
*acb
= (SheepdogAIOCB
*)blockacb
;
412 * Sheepdog cannot cancel the requests which are already sent to
413 * the servers, so we just complete the request with -EIO here.
416 qemu_coroutine_enter(acb
->coroutine
, NULL
);
420 static AIOPool sd_aio_pool
= {
421 .aiocb_size
= sizeof(SheepdogAIOCB
),
422 .cancel
= sd_aio_cancel
,
425 static SheepdogAIOCB
*sd_aio_setup(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
426 int64_t sector_num
, int nb_sectors
,
427 BlockDriverCompletionFunc
*cb
, void *opaque
)
431 acb
= qemu_aio_get(&sd_aio_pool
, bs
, cb
, opaque
);
435 acb
->sector_num
= sector_num
;
436 acb
->nb_sectors
= nb_sectors
;
438 acb
->aio_done_func
= NULL
;
440 acb
->coroutine
= qemu_coroutine_self();
442 QLIST_INIT(&acb
->aioreq_head
);
449 struct iovec
*msg_iov
;
453 static ssize_t
sendmsg(int s
, const struct msghdr
*msg
, int flags
)
459 /* count the msg size */
460 for (i
= 0; i
< msg
->msg_iovlen
; i
++) {
461 size
+= msg
->msg_iov
[i
].iov_len
;
463 buf
= g_malloc(size
);
466 for (i
= 0; i
< msg
->msg_iovlen
; i
++) {
467 memcpy(p
, msg
->msg_iov
[i
].iov_base
, msg
->msg_iov
[i
].iov_len
);
468 p
+= msg
->msg_iov
[i
].iov_len
;
471 ret
= send(s
, buf
, size
, flags
);
477 static ssize_t
recvmsg(int s
, struct msghdr
*msg
, int flags
)
483 /* count the msg size */
484 for (i
= 0; i
< msg
->msg_iovlen
; i
++) {
485 size
+= msg
->msg_iov
[i
].iov_len
;
487 buf
= g_malloc(size
);
489 ret
= qemu_recv(s
, buf
, size
, flags
);
495 for (i
= 0; i
< msg
->msg_iovlen
; i
++) {
496 memcpy(msg
->msg_iov
[i
].iov_base
, p
, msg
->msg_iov
[i
].iov_len
);
497 p
+= msg
->msg_iov
[i
].iov_len
;
507 * Send/recv data with iovec buffers
509 * This function send/recv data from/to the iovec buffer directly.
510 * The first `offset' bytes in the iovec buffer are skipped and next
511 * `len' bytes are used.
515 * do_send_recv(sockfd, iov, len, offset, 1);
519 * char *buf = malloc(size);
520 * iov_to_buf(iov, iovcnt, buf, offset, size);
521 * send(sockfd, buf, size, 0);
524 static int do_send_recv(int sockfd
, struct iovec
*iov
, int len
, int offset
,
530 memset(&msg
, 0, sizeof(msg
));
536 while (iov
->iov_len
< len
) {
543 diff
= iov
->iov_len
- len
;
544 iov
->iov_len
-= diff
;
546 while (msg
.msg_iov
->iov_len
<= offset
) {
547 offset
-= msg
.msg_iov
->iov_len
;
553 msg
.msg_iov
->iov_base
= (char *) msg
.msg_iov
->iov_base
+ offset
;
554 msg
.msg_iov
->iov_len
-= offset
;
557 ret
= sendmsg(sockfd
, &msg
, 0);
559 ret
= recvmsg(sockfd
, &msg
, 0);
562 msg
.msg_iov
->iov_base
= (char *) msg
.msg_iov
->iov_base
- offset
;
563 msg
.msg_iov
->iov_len
+= offset
;
565 iov
->iov_len
+= diff
;
569 static int connect_to_sdog(const char *addr
, const char *port
)
571 char hbuf
[NI_MAXHOST
], sbuf
[NI_MAXSERV
];
573 struct addrinfo hints
, *res
, *res0
;
576 addr
= SD_DEFAULT_ADDR
;
577 port
= SD_DEFAULT_PORT
;
580 memset(&hints
, 0, sizeof(hints
));
581 hints
.ai_socktype
= SOCK_STREAM
;
583 ret
= getaddrinfo(addr
, port
, &hints
, &res0
);
585 error_report("unable to get address info %s, %s",
586 addr
, strerror(errno
));
590 for (res
= res0
; res
; res
= res
->ai_next
) {
591 ret
= getnameinfo(res
->ai_addr
, res
->ai_addrlen
, hbuf
, sizeof(hbuf
),
592 sbuf
, sizeof(sbuf
), NI_NUMERICHOST
| NI_NUMERICSERV
);
597 fd
= socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
603 ret
= connect(fd
, res
->ai_addr
, res
->ai_addrlen
);
605 if (errno
== EINTR
) {
611 dprintf("connected to %s:%s\n", addr
, port
);
615 error_report("failed connect to %s:%s", addr
, port
);
621 static int do_readv_writev(int sockfd
, struct iovec
*iov
, int len
,
622 int iov_offset
, int write
)
626 ret
= do_send_recv(sockfd
, iov
, len
, iov_offset
, write
);
628 if (errno
== EINTR
) {
631 if (errno
== EAGAIN
) {
632 if (qemu_in_coroutine()) {
633 qemu_coroutine_yield();
637 error_report("failed to recv a rsp, %s", strerror(errno
));
650 static int do_readv(int sockfd
, struct iovec
*iov
, int len
, int iov_offset
)
652 return do_readv_writev(sockfd
, iov
, len
, iov_offset
, 0);
655 static int do_writev(int sockfd
, struct iovec
*iov
, int len
, int iov_offset
)
657 return do_readv_writev(sockfd
, iov
, len
, iov_offset
, 1);
660 static int do_read_write(int sockfd
, void *buf
, int len
, int write
)
667 return do_readv_writev(sockfd
, &iov
, len
, 0, write
);
670 static int do_read(int sockfd
, void *buf
, int len
)
672 return do_read_write(sockfd
, buf
, len
, 0);
675 static int do_write(int sockfd
, void *buf
, int len
)
677 return do_read_write(sockfd
, buf
, len
, 1);
680 static int send_req(int sockfd
, SheepdogReq
*hdr
, void *data
,
686 iov
[0].iov_base
= hdr
;
687 iov
[0].iov_len
= sizeof(*hdr
);
690 iov
[1].iov_base
= data
;
691 iov
[1].iov_len
= *wlen
;
694 ret
= do_writev(sockfd
, iov
, sizeof(*hdr
) + *wlen
, 0);
696 error_report("failed to send a req, %s", strerror(errno
));
703 static int do_req(int sockfd
, SheepdogReq
*hdr
, void *data
,
704 unsigned int *wlen
, unsigned int *rlen
)
708 ret
= send_req(sockfd
, hdr
, data
, wlen
);
714 ret
= do_read(sockfd
, hdr
, sizeof(*hdr
));
716 error_report("failed to get a rsp, %s", strerror(errno
));
721 if (*rlen
> hdr
->data_length
) {
722 *rlen
= hdr
->data_length
;
726 ret
= do_read(sockfd
, data
, *rlen
);
728 error_report("failed to get the data, %s", strerror(errno
));
738 static int coroutine_fn
add_aio_request(BDRVSheepdogState
*s
, AIOReq
*aio_req
,
739 struct iovec
*iov
, int niov
, int create
,
740 enum AIOCBState aiocb_type
);
743 * This function searchs pending requests to the object `oid', and
746 static void coroutine_fn
send_pending_req(BDRVSheepdogState
*s
, uint64_t oid
, uint32_t id
)
748 AIOReq
*aio_req
, *next
;
752 QLIST_FOREACH_SAFE(aio_req
, &s
->outstanding_aio_head
,
753 outstanding_aio_siblings
, next
) {
754 if (id
== aio_req
->id
) {
757 if (aio_req
->oid
!= oid
) {
761 acb
= aio_req
->aiocb
;
762 ret
= add_aio_request(s
, aio_req
, acb
->qiov
->iov
,
763 acb
->qiov
->niov
, 0, acb
->aiocb_type
);
765 error_report("add_aio_request is failed");
766 free_aio_req(s
, aio_req
);
767 if (QLIST_EMPTY(&acb
->aioreq_head
)) {
768 sd_finish_aiocb(acb
);
775 * Receive responses of the I/O requests.
777 * This function is registered as a fd handler, and called from the
778 * main loop when s->fd is ready for reading responses.
780 static void coroutine_fn
aio_read_response(void *opaque
)
783 BDRVSheepdogState
*s
= opaque
;
786 AIOReq
*aio_req
= NULL
;
791 if (QLIST_EMPTY(&s
->outstanding_aio_head
)) {
796 ret
= do_read(fd
, &rsp
, sizeof(rsp
));
798 error_report("failed to get the header, %s", strerror(errno
));
802 /* find the right aio_req from the outstanding_aio list */
803 QLIST_FOREACH(aio_req
, &s
->outstanding_aio_head
, outstanding_aio_siblings
) {
804 if (aio_req
->id
== rsp
.id
) {
809 error_report("cannot find aio_req %x", rsp
.id
);
813 acb
= aio_req
->aiocb
;
815 switch (acb
->aiocb_type
) {
816 case AIOCB_WRITE_UDATA
:
817 if (!is_data_obj(aio_req
->oid
)) {
820 idx
= data_oid_to_idx(aio_req
->oid
);
822 if (s
->inode
.data_vdi_id
[idx
] != s
->inode
.vdi_id
) {
824 * If the object is newly created one, we need to update
825 * the vdi object (metadata object). min_dirty_data_idx
826 * and max_dirty_data_idx are changed to include updated
827 * index between them.
829 s
->inode
.data_vdi_id
[idx
] = s
->inode
.vdi_id
;
830 s
->max_dirty_data_idx
= MAX(idx
, s
->max_dirty_data_idx
);
831 s
->min_dirty_data_idx
= MIN(idx
, s
->min_dirty_data_idx
);
834 * Some requests may be blocked because simultaneous
835 * create requests are not allowed, so we search the
836 * pending requests here.
838 send_pending_req(s
, vid_to_data_oid(s
->inode
.vdi_id
, idx
), rsp
.id
);
841 case AIOCB_READ_UDATA
:
842 ret
= do_readv(fd
, acb
->qiov
->iov
, rsp
.data_length
,
843 aio_req
->iov_offset
);
845 error_report("failed to get the data, %s", strerror(errno
));
851 if (rsp
.result
!= SD_RES_SUCCESS
) {
853 error_report("%s", sd_strerror(rsp
.result
));
856 rest
= free_aio_req(s
, aio_req
);
859 * We've finished all requests which belong to the AIOCB, so
860 * we can switch back to sd_co_readv/writev now.
862 acb
->aio_done_func(acb
);
868 static void co_read_response(void *opaque
)
870 BDRVSheepdogState
*s
= opaque
;
873 s
->co_recv
= qemu_coroutine_create(aio_read_response
);
876 qemu_coroutine_enter(s
->co_recv
, opaque
);
879 static void co_write_request(void *opaque
)
881 BDRVSheepdogState
*s
= opaque
;
883 qemu_coroutine_enter(s
->co_send
, NULL
);
886 static int aio_flush_request(void *opaque
)
888 BDRVSheepdogState
*s
= opaque
;
890 return !QLIST_EMPTY(&s
->outstanding_aio_head
);
893 #if !defined(SOL_TCP) || !defined(TCP_CORK)
895 static int set_cork(int fd
, int v
)
902 static int set_cork(int fd
, int v
)
904 return setsockopt(fd
, SOL_TCP
, TCP_CORK
, &v
, sizeof(v
));
909 static int set_nodelay(int fd
)
914 ret
= setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&opt
, sizeof(opt
));
919 * Return a socket discriptor to read/write objects.
921 * We cannot use this discriptor for other operations because
922 * the block driver may be on waiting response from the server.
924 static int get_sheep_fd(BDRVSheepdogState
*s
)
928 fd
= connect_to_sdog(s
->addr
, s
->port
);
930 error_report("%s", strerror(errno
));
934 socket_set_nonblock(fd
);
936 ret
= set_nodelay(fd
);
938 error_report("%s", strerror(errno
));
943 qemu_aio_set_fd_handler(fd
, co_read_response
, NULL
, aio_flush_request
,
951 * filename must be one of the following formats:
953 * 2. [vdiname]:[snapid]
955 * 4. [hostname]:[port]:[vdiname]
956 * 5. [hostname]:[port]:[vdiname]:[snapid]
957 * 6. [hostname]:[port]:[vdiname]:[tag]
959 * You can boot from the snapshot images by specifying `snapid` or
962 * You can run VMs outside the Sheepdog cluster by specifying
963 * `hostname' and `port' (experimental).
965 static int parse_vdiname(BDRVSheepdogState
*s
, const char *filename
,
966 char *vdi
, uint32_t *snapid
, char *tag
)
971 p
= q
= g_strdup(filename
);
973 /* count the number of separators */
983 /* use the first two tokens as hostname and port number. */
997 strncpy(vdi
, p
, SD_MAX_VDI_LEN
);
999 p
= strchr(vdi
, ':');
1002 *snapid
= strtoul(p
, NULL
, 10);
1004 strncpy(tag
, p
, SD_MAX_VDI_TAG_LEN
);
1007 *snapid
= CURRENT_VDI_ID
; /* search current vdi */
1010 if (s
->addr
== NULL
) {
1017 static int find_vdi_name(BDRVSheepdogState
*s
, char *filename
, uint32_t snapid
,
1018 char *tag
, uint32_t *vid
, int for_snapshot
)
1022 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1023 unsigned int wlen
, rlen
= 0;
1024 char buf
[SD_MAX_VDI_LEN
+ SD_MAX_VDI_TAG_LEN
];
1026 fd
= connect_to_sdog(s
->addr
, s
->port
);
1031 memset(buf
, 0, sizeof(buf
));
1032 strncpy(buf
, filename
, SD_MAX_VDI_LEN
);
1033 strncpy(buf
+ SD_MAX_VDI_LEN
, tag
, SD_MAX_VDI_TAG_LEN
);
1035 memset(&hdr
, 0, sizeof(hdr
));
1037 hdr
.opcode
= SD_OP_GET_VDI_INFO
;
1039 hdr
.opcode
= SD_OP_LOCK_VDI
;
1041 wlen
= SD_MAX_VDI_LEN
+ SD_MAX_VDI_TAG_LEN
;
1042 hdr
.proto_ver
= SD_PROTO_VER
;
1043 hdr
.data_length
= wlen
;
1044 hdr
.snapid
= snapid
;
1045 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1047 ret
= do_req(fd
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1053 if (rsp
->result
!= SD_RES_SUCCESS
) {
1054 error_report("cannot get vdi info, %s, %s %d %s",
1055 sd_strerror(rsp
->result
), filename
, snapid
, tag
);
1067 static int coroutine_fn
add_aio_request(BDRVSheepdogState
*s
, AIOReq
*aio_req
,
1068 struct iovec
*iov
, int niov
, int create
,
1069 enum AIOCBState aiocb_type
)
1071 int nr_copies
= s
->inode
.nr_copies
;
1075 uint64_t oid
= aio_req
->oid
;
1076 unsigned int datalen
= aio_req
->data_len
;
1077 uint64_t offset
= aio_req
->offset
;
1078 uint8_t flags
= aio_req
->flags
;
1079 uint64_t old_oid
= aio_req
->base_oid
;
1082 error_report("bug");
1085 memset(&hdr
, 0, sizeof(hdr
));
1087 if (aiocb_type
== AIOCB_READ_UDATA
) {
1089 hdr
.opcode
= SD_OP_READ_OBJ
;
1091 } else if (create
) {
1093 hdr
.opcode
= SD_OP_CREATE_AND_WRITE_OBJ
;
1094 hdr
.flags
= SD_FLAG_CMD_WRITE
| flags
;
1097 hdr
.opcode
= SD_OP_WRITE_OBJ
;
1098 hdr
.flags
= SD_FLAG_CMD_WRITE
| flags
;
1102 hdr
.cow_oid
= old_oid
;
1103 hdr
.copies
= s
->inode
.nr_copies
;
1105 hdr
.data_length
= datalen
;
1106 hdr
.offset
= offset
;
1108 hdr
.id
= aio_req
->id
;
1110 qemu_co_mutex_lock(&s
->lock
);
1111 s
->co_send
= qemu_coroutine_self();
1112 qemu_aio_set_fd_handler(s
->fd
, co_read_response
, co_write_request
,
1113 aio_flush_request
, NULL
, s
);
1117 ret
= do_write(s
->fd
, &hdr
, sizeof(hdr
));
1119 qemu_co_mutex_unlock(&s
->lock
);
1120 error_report("failed to send a req, %s", strerror(errno
));
1125 ret
= do_writev(s
->fd
, iov
, wlen
, aio_req
->iov_offset
);
1127 qemu_co_mutex_unlock(&s
->lock
);
1128 error_report("failed to send a data, %s", strerror(errno
));
1134 qemu_aio_set_fd_handler(s
->fd
, co_read_response
, NULL
,
1135 aio_flush_request
, NULL
, s
);
1136 qemu_co_mutex_unlock(&s
->lock
);
1141 static int read_write_object(int fd
, char *buf
, uint64_t oid
, int copies
,
1142 unsigned int datalen
, uint64_t offset
,
1143 int write
, int create
)
1146 SheepdogObjRsp
*rsp
= (SheepdogObjRsp
*)&hdr
;
1147 unsigned int wlen
, rlen
;
1150 memset(&hdr
, 0, sizeof(hdr
));
1155 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1157 hdr
.opcode
= SD_OP_CREATE_AND_WRITE_OBJ
;
1159 hdr
.opcode
= SD_OP_WRITE_OBJ
;
1164 hdr
.opcode
= SD_OP_READ_OBJ
;
1167 hdr
.data_length
= datalen
;
1168 hdr
.offset
= offset
;
1169 hdr
.copies
= copies
;
1171 ret
= do_req(fd
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1173 error_report("failed to send a request to the sheep");
1177 switch (rsp
->result
) {
1178 case SD_RES_SUCCESS
:
1181 error_report("%s", sd_strerror(rsp
->result
));
1186 static int read_object(int fd
, char *buf
, uint64_t oid
, int copies
,
1187 unsigned int datalen
, uint64_t offset
)
1189 return read_write_object(fd
, buf
, oid
, copies
, datalen
, offset
, 0, 0);
1192 static int write_object(int fd
, char *buf
, uint64_t oid
, int copies
,
1193 unsigned int datalen
, uint64_t offset
, int create
)
1195 return read_write_object(fd
, buf
, oid
, copies
, datalen
, offset
, 1, create
);
1198 static int sd_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1202 BDRVSheepdogState
*s
= bs
->opaque
;
1203 char vdi
[SD_MAX_VDI_LEN
], tag
[SD_MAX_VDI_TAG_LEN
];
1207 strstart(filename
, "sheepdog:", (const char **)&filename
);
1209 QLIST_INIT(&s
->outstanding_aio_head
);
1212 memset(vdi
, 0, sizeof(vdi
));
1213 memset(tag
, 0, sizeof(tag
));
1214 if (parse_vdiname(s
, filename
, vdi
, &snapid
, tag
) < 0) {
1217 s
->fd
= get_sheep_fd(s
);
1222 ret
= find_vdi_name(s
, vdi
, snapid
, tag
, &vid
, 0);
1228 dprintf("%" PRIx32
" snapshot inode was open.\n", vid
);
1232 fd
= connect_to_sdog(s
->addr
, s
->port
);
1234 error_report("failed to connect");
1238 buf
= g_malloc(SD_INODE_SIZE
);
1239 ret
= read_object(fd
, buf
, vid_to_vdi_oid(vid
), 0, SD_INODE_SIZE
, 0);
1247 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
1248 s
->min_dirty_data_idx
= UINT32_MAX
;
1249 s
->max_dirty_data_idx
= 0;
1251 bs
->total_sectors
= s
->inode
.vdi_size
/ SECTOR_SIZE
;
1252 strncpy(s
->name
, vdi
, sizeof(s
->name
));
1253 qemu_co_mutex_init(&s
->lock
);
1257 qemu_aio_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
, NULL
, NULL
);
1265 static int do_sd_create(char *filename
, int64_t vdi_size
,
1266 uint32_t base_vid
, uint32_t *vdi_id
, int snapshot
,
1267 const char *addr
, const char *port
)
1270 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1272 unsigned int wlen
, rlen
= 0;
1273 char buf
[SD_MAX_VDI_LEN
];
1275 fd
= connect_to_sdog(addr
, port
);
1280 memset(buf
, 0, sizeof(buf
));
1281 strncpy(buf
, filename
, SD_MAX_VDI_LEN
);
1283 memset(&hdr
, 0, sizeof(hdr
));
1284 hdr
.opcode
= SD_OP_NEW_VDI
;
1285 hdr
.base_vdi_id
= base_vid
;
1287 wlen
= SD_MAX_VDI_LEN
;
1289 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1290 hdr
.snapid
= snapshot
;
1292 hdr
.data_length
= wlen
;
1293 hdr
.vdi_size
= vdi_size
;
1295 ret
= do_req(fd
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1303 if (rsp
->result
!= SD_RES_SUCCESS
) {
1304 error_report("%s, %s", sd_strerror(rsp
->result
), filename
);
1309 *vdi_id
= rsp
->vdi_id
;
1315 static int sd_prealloc(const char *filename
)
1317 BlockDriverState
*bs
= NULL
;
1318 uint32_t idx
, max_idx
;
1320 void *buf
= g_malloc0(SD_DATA_OBJ_SIZE
);
1323 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDWR
);
1328 vdi_size
= bdrv_getlength(bs
);
1333 max_idx
= DIV_ROUND_UP(vdi_size
, SD_DATA_OBJ_SIZE
);
1335 for (idx
= 0; idx
< max_idx
; idx
++) {
1337 * The created image can be a cloned image, so we need to read
1338 * a data from the source image.
1340 ret
= bdrv_pread(bs
, idx
* SD_DATA_OBJ_SIZE
, buf
, SD_DATA_OBJ_SIZE
);
1344 ret
= bdrv_pwrite(bs
, idx
* SD_DATA_OBJ_SIZE
, buf
, SD_DATA_OBJ_SIZE
);
1358 static int sd_create(const char *filename
, QEMUOptionParameter
*options
)
1361 uint32_t vid
= 0, base_vid
= 0;
1362 int64_t vdi_size
= 0;
1363 char *backing_file
= NULL
;
1364 BDRVSheepdogState s
;
1365 char vdi
[SD_MAX_VDI_LEN
], tag
[SD_MAX_VDI_TAG_LEN
];
1368 const char *vdiname
;
1370 strstart(filename
, "sheepdog:", &vdiname
);
1372 memset(&s
, 0, sizeof(s
));
1373 memset(vdi
, 0, sizeof(vdi
));
1374 memset(tag
, 0, sizeof(tag
));
1375 if (parse_vdiname(&s
, vdiname
, vdi
, &snapid
, tag
) < 0) {
1376 error_report("invalid filename");
1380 while (options
&& options
->name
) {
1381 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
1382 vdi_size
= options
->value
.n
;
1383 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
1384 backing_file
= options
->value
.s
;
1385 } else if (!strcmp(options
->name
, BLOCK_OPT_PREALLOC
)) {
1386 if (!options
->value
.s
|| !strcmp(options
->value
.s
, "off")) {
1388 } else if (!strcmp(options
->value
.s
, "full")) {
1391 error_report("Invalid preallocation mode: '%s'",
1399 if (vdi_size
> SD_MAX_VDI_SIZE
) {
1400 error_report("too big image size");
1405 BlockDriverState
*bs
;
1406 BDRVSheepdogState
*s
;
1409 /* Currently, only Sheepdog backing image is supported. */
1410 drv
= bdrv_find_protocol(backing_file
);
1411 if (!drv
|| strcmp(drv
->protocol_name
, "sheepdog") != 0) {
1412 error_report("backing_file must be a sheepdog image");
1416 ret
= bdrv_file_open(&bs
, backing_file
, 0);
1422 if (!is_snapshot(&s
->inode
)) {
1423 error_report("cannot clone from a non snapshot vdi");
1428 base_vid
= s
->inode
.vdi_id
;
1432 ret
= do_sd_create(vdi
, vdi_size
, base_vid
, &vid
, 0, s
.addr
, s
.port
);
1433 if (!prealloc
|| ret
) {
1437 return sd_prealloc(filename
);
1440 static void sd_close(BlockDriverState
*bs
)
1442 BDRVSheepdogState
*s
= bs
->opaque
;
1444 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1445 unsigned int wlen
, rlen
= 0;
1448 dprintf("%s\n", s
->name
);
1450 fd
= connect_to_sdog(s
->addr
, s
->port
);
1455 memset(&hdr
, 0, sizeof(hdr
));
1457 hdr
.opcode
= SD_OP_RELEASE_VDI
;
1458 wlen
= strlen(s
->name
) + 1;
1459 hdr
.data_length
= wlen
;
1460 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1462 ret
= do_req(fd
, (SheepdogReq
*)&hdr
, s
->name
, &wlen
, &rlen
);
1466 if (!ret
&& rsp
->result
!= SD_RES_SUCCESS
&&
1467 rsp
->result
!= SD_RES_VDI_NOT_LOCKED
) {
1468 error_report("%s, %s", sd_strerror(rsp
->result
), s
->name
);
1471 qemu_aio_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
, NULL
, NULL
);
1476 static int64_t sd_getlength(BlockDriverState
*bs
)
1478 BDRVSheepdogState
*s
= bs
->opaque
;
1480 return s
->inode
.vdi_size
;
1483 static int sd_truncate(BlockDriverState
*bs
, int64_t offset
)
1485 BDRVSheepdogState
*s
= bs
->opaque
;
1487 unsigned int datalen
;
1489 if (offset
< s
->inode
.vdi_size
) {
1490 error_report("shrinking is not supported");
1492 } else if (offset
> SD_MAX_VDI_SIZE
) {
1493 error_report("too big image size");
1497 fd
= connect_to_sdog(s
->addr
, s
->port
);
1502 /* we don't need to update entire object */
1503 datalen
= SD_INODE_SIZE
- sizeof(s
->inode
.data_vdi_id
);
1504 s
->inode
.vdi_size
= offset
;
1505 ret
= write_object(fd
, (char *)&s
->inode
, vid_to_vdi_oid(s
->inode
.vdi_id
),
1506 s
->inode
.nr_copies
, datalen
, 0, 0);
1510 error_report("failed to update an inode.");
1518 * This function is called after writing data objects. If we need to
1519 * update metadata, this sends a write request to the vdi object.
1520 * Otherwise, this switches back to sd_co_readv/writev.
1522 static void coroutine_fn
sd_write_done(SheepdogAIOCB
*acb
)
1525 BDRVSheepdogState
*s
= acb
->common
.bs
->opaque
;
1528 uint32_t offset
, data_len
, mn
, mx
;
1530 mn
= s
->min_dirty_data_idx
;
1531 mx
= s
->max_dirty_data_idx
;
1533 /* we need to update the vdi object. */
1534 offset
= sizeof(s
->inode
) - sizeof(s
->inode
.data_vdi_id
) +
1535 mn
* sizeof(s
->inode
.data_vdi_id
[0]);
1536 data_len
= (mx
- mn
+ 1) * sizeof(s
->inode
.data_vdi_id
[0]);
1538 s
->min_dirty_data_idx
= UINT32_MAX
;
1539 s
->max_dirty_data_idx
= 0;
1541 iov
.iov_base
= &s
->inode
;
1542 iov
.iov_len
= sizeof(s
->inode
);
1543 aio_req
= alloc_aio_req(s
, acb
, vid_to_vdi_oid(s
->inode
.vdi_id
),
1544 data_len
, offset
, 0, 0, offset
);
1545 ret
= add_aio_request(s
, aio_req
, &iov
, 1, 0, AIOCB_WRITE_UDATA
);
1547 free_aio_req(s
, aio_req
);
1552 acb
->aio_done_func
= sd_finish_aiocb
;
1553 acb
->aiocb_type
= AIOCB_WRITE_UDATA
;
1557 sd_finish_aiocb(acb
);
1561 * Create a writable VDI from a snapshot
1563 static int sd_create_branch(BDRVSheepdogState
*s
)
1569 dprintf("%" PRIx32
" is snapshot.\n", s
->inode
.vdi_id
);
1571 buf
= g_malloc(SD_INODE_SIZE
);
1573 ret
= do_sd_create(s
->name
, s
->inode
.vdi_size
, s
->inode
.vdi_id
, &vid
, 1,
1579 dprintf("%" PRIx32
" is created.\n", vid
);
1581 fd
= connect_to_sdog(s
->addr
, s
->port
);
1583 error_report("failed to connect");
1587 ret
= read_object(fd
, buf
, vid_to_vdi_oid(vid
), s
->inode
.nr_copies
,
1596 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
1600 dprintf("%" PRIx32
" was newly created.\n", s
->inode
.vdi_id
);
1609 * Send I/O requests to the server.
1611 * This function sends requests to the server, links the requests to
1612 * the outstanding_list in BDRVSheepdogState, and exits without
1613 * waiting the response. The responses are received in the
1614 * `aio_read_response' function which is called from the main loop as
1617 * Returns 1 when we need to wait a response, 0 when there is no sent
1618 * request and -errno in error cases.
1620 static int coroutine_fn
sd_co_rw_vector(void *p
)
1622 SheepdogAIOCB
*acb
= p
;
1624 unsigned long len
, done
= 0, total
= acb
->nb_sectors
* SECTOR_SIZE
;
1625 unsigned long idx
= acb
->sector_num
* SECTOR_SIZE
/ SD_DATA_OBJ_SIZE
;
1627 uint64_t offset
= (acb
->sector_num
* SECTOR_SIZE
) % SD_DATA_OBJ_SIZE
;
1628 BDRVSheepdogState
*s
= acb
->common
.bs
->opaque
;
1629 SheepdogInode
*inode
= &s
->inode
;
1632 if (acb
->aiocb_type
== AIOCB_WRITE_UDATA
&& s
->is_snapshot
) {
1634 * In the case we open the snapshot VDI, Sheepdog creates the
1635 * writable VDI when we do a write operation first.
1637 ret
= sd_create_branch(s
);
1644 while (done
!= total
) {
1646 uint64_t old_oid
= 0;
1649 oid
= vid_to_data_oid(inode
->data_vdi_id
[idx
], idx
);
1651 len
= MIN(total
- done
, SD_DATA_OBJ_SIZE
- offset
);
1653 if (!inode
->data_vdi_id
[idx
]) {
1654 if (acb
->aiocb_type
== AIOCB_READ_UDATA
) {
1659 } else if (acb
->aiocb_type
== AIOCB_WRITE_UDATA
1660 && !is_data_obj_writable(inode
, idx
)) {
1664 flags
= SD_FLAG_CMD_COW
;
1668 dprintf("update ino (%" PRIu32
") %" PRIu64
" %" PRIu64
1669 " %" PRIu64
"\n", inode
->vdi_id
, oid
,
1670 vid_to_data_oid(inode
->data_vdi_id
[idx
], idx
), idx
);
1671 oid
= vid_to_data_oid(inode
->vdi_id
, idx
);
1672 dprintf("new oid %lx\n", oid
);
1675 aio_req
= alloc_aio_req(s
, acb
, oid
, len
, offset
, flags
, old_oid
, done
);
1679 QLIST_FOREACH(areq
, &s
->outstanding_aio_head
,
1680 outstanding_aio_siblings
) {
1681 if (areq
== aio_req
) {
1684 if (areq
->oid
== oid
) {
1686 * Sheepdog cannot handle simultaneous create
1687 * requests to the same object. So we cannot send
1688 * the request until the previous request
1692 aio_req
->base_oid
= 0;
1698 ret
= add_aio_request(s
, aio_req
, acb
->qiov
->iov
, acb
->qiov
->niov
,
1699 create
, acb
->aiocb_type
);
1701 error_report("add_aio_request is failed");
1702 free_aio_req(s
, aio_req
);
1712 if (QLIST_EMPTY(&acb
->aioreq_head
)) {
1718 static int sd_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
1719 int nb_sectors
, QEMUIOVector
*qiov
)
1724 if (bs
->growable
&& sector_num
+ nb_sectors
> bs
->total_sectors
) {
1725 /* TODO: shouldn't block here */
1726 if (sd_truncate(bs
, (sector_num
+ nb_sectors
) * SECTOR_SIZE
) < 0) {
1729 bs
->total_sectors
= sector_num
+ nb_sectors
;
1732 acb
= sd_aio_setup(bs
, qiov
, sector_num
, nb_sectors
, NULL
, NULL
);
1733 acb
->aio_done_func
= sd_write_done
;
1734 acb
->aiocb_type
= AIOCB_WRITE_UDATA
;
1736 ret
= sd_co_rw_vector(acb
);
1738 qemu_aio_release(acb
);
1742 qemu_coroutine_yield();
1747 static int sd_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
1748 int nb_sectors
, QEMUIOVector
*qiov
)
1753 acb
= sd_aio_setup(bs
, qiov
, sector_num
, nb_sectors
, NULL
, NULL
);
1754 acb
->aiocb_type
= AIOCB_READ_UDATA
;
1755 acb
->aio_done_func
= sd_finish_aiocb
;
1758 * TODO: we can do better; we don't need to initialize
1761 for (i
= 0; i
< qiov
->niov
; i
++) {
1762 memset(qiov
->iov
[i
].iov_base
, 0, qiov
->iov
[i
].iov_len
);
1765 ret
= sd_co_rw_vector(acb
);
1767 qemu_aio_release(acb
);
1771 qemu_coroutine_yield();
1776 static int sd_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
)
1778 BDRVSheepdogState
*s
= bs
->opaque
;
1781 SheepdogInode
*inode
;
1782 unsigned int datalen
;
1784 dprintf("sn_info: name %s id_str %s s: name %s vm_state_size %d "
1785 "is_snapshot %d\n", sn_info
->name
, sn_info
->id_str
,
1786 s
->name
, sn_info
->vm_state_size
, s
->is_snapshot
);
1788 if (s
->is_snapshot
) {
1789 error_report("You can't create a snapshot of a snapshot VDI, "
1790 "%s (%" PRIu32
").", s
->name
, s
->inode
.vdi_id
);
1795 dprintf("%s %s\n", sn_info
->name
, sn_info
->id_str
);
1797 s
->inode
.vm_state_size
= sn_info
->vm_state_size
;
1798 s
->inode
.vm_clock_nsec
= sn_info
->vm_clock_nsec
;
1799 strncpy(s
->inode
.tag
, sn_info
->name
, sizeof(s
->inode
.tag
));
1800 /* we don't need to update entire object */
1801 datalen
= SD_INODE_SIZE
- sizeof(s
->inode
.data_vdi_id
);
1803 /* refresh inode. */
1804 fd
= connect_to_sdog(s
->addr
, s
->port
);
1810 ret
= write_object(fd
, (char *)&s
->inode
, vid_to_vdi_oid(s
->inode
.vdi_id
),
1811 s
->inode
.nr_copies
, datalen
, 0, 0);
1813 error_report("failed to write snapshot's inode.");
1818 ret
= do_sd_create(s
->name
, s
->inode
.vdi_size
, s
->inode
.vdi_id
, &new_vid
, 1,
1821 error_report("failed to create inode for snapshot. %s",
1827 inode
= (SheepdogInode
*)g_malloc(datalen
);
1829 ret
= read_object(fd
, (char *)inode
, vid_to_vdi_oid(new_vid
),
1830 s
->inode
.nr_copies
, datalen
, 0);
1833 error_report("failed to read new inode info. %s", strerror(errno
));
1838 memcpy(&s
->inode
, inode
, datalen
);
1839 dprintf("s->inode: name %s snap_id %x oid %x\n",
1840 s
->inode
.name
, s
->inode
.snap_id
, s
->inode
.vdi_id
);
1847 static int sd_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
)
1849 BDRVSheepdogState
*s
= bs
->opaque
;
1850 BDRVSheepdogState
*old_s
;
1851 char vdi
[SD_MAX_VDI_LEN
], tag
[SD_MAX_VDI_TAG_LEN
];
1854 uint32_t snapid
= 0;
1855 int ret
= -ENOENT
, fd
;
1857 old_s
= g_malloc(sizeof(BDRVSheepdogState
));
1859 memcpy(old_s
, s
, sizeof(BDRVSheepdogState
));
1861 memset(vdi
, 0, sizeof(vdi
));
1862 strncpy(vdi
, s
->name
, sizeof(vdi
));
1864 memset(tag
, 0, sizeof(tag
));
1865 snapid
= strtoul(snapshot_id
, NULL
, 10);
1867 strncpy(tag
, s
->name
, sizeof(tag
));
1870 ret
= find_vdi_name(s
, vdi
, snapid
, tag
, &vid
, 1);
1872 error_report("Failed to find_vdi_name");
1877 fd
= connect_to_sdog(s
->addr
, s
->port
);
1879 error_report("failed to connect");
1883 buf
= g_malloc(SD_INODE_SIZE
);
1884 ret
= read_object(fd
, buf
, vid_to_vdi_oid(vid
), s
->inode
.nr_copies
,
1894 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
1896 if (!s
->inode
.vm_state_size
) {
1897 error_report("Invalid snapshot");
1909 /* recover bdrv_sd_state */
1910 memcpy(s
, old_s
, sizeof(BDRVSheepdogState
));
1914 error_report("failed to open. recover old bdrv_sd_state.");
1919 static int sd_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1921 /* FIXME: Delete specified snapshot id. */
1925 static int sd_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
)
1927 BDRVSheepdogState
*s
= bs
->opaque
;
1929 int fd
, nr
= 1024, ret
, max
= BITS_TO_LONGS(SD_NR_VDIS
) * sizeof(long);
1930 QEMUSnapshotInfo
*sn_tab
= NULL
;
1931 unsigned wlen
, rlen
;
1933 static SheepdogInode inode
;
1934 unsigned long *vdi_inuse
;
1935 unsigned int start_nr
;
1939 vdi_inuse
= g_malloc(max
);
1941 fd
= connect_to_sdog(s
->addr
, s
->port
);
1949 memset(&req
, 0, sizeof(req
));
1951 req
.opcode
= SD_OP_READ_VDIS
;
1952 req
.data_length
= max
;
1954 ret
= do_req(fd
, (SheepdogReq
*)&req
, vdi_inuse
, &wlen
, &rlen
);
1961 sn_tab
= g_malloc0(nr
* sizeof(*sn_tab
));
1963 /* calculate a vdi id with hash function */
1964 hval
= fnv_64a_buf(s
->name
, strlen(s
->name
), FNV1A_64_INIT
);
1965 start_nr
= hval
& (SD_NR_VDIS
- 1);
1967 fd
= connect_to_sdog(s
->addr
, s
->port
);
1969 error_report("failed to connect");
1973 for (vid
= start_nr
; found
< nr
; vid
= (vid
+ 1) % SD_NR_VDIS
) {
1974 if (!test_bit(vid
, vdi_inuse
)) {
1978 /* we don't need to read entire object */
1979 ret
= read_object(fd
, (char *)&inode
, vid_to_vdi_oid(vid
),
1980 0, SD_INODE_SIZE
- sizeof(inode
.data_vdi_id
), 0);
1986 if (!strcmp(inode
.name
, s
->name
) && is_snapshot(&inode
)) {
1987 sn_tab
[found
].date_sec
= inode
.snap_ctime
>> 32;
1988 sn_tab
[found
].date_nsec
= inode
.snap_ctime
& 0xffffffff;
1989 sn_tab
[found
].vm_state_size
= inode
.vm_state_size
;
1990 sn_tab
[found
].vm_clock_nsec
= inode
.vm_clock_nsec
;
1992 snprintf(sn_tab
[found
].id_str
, sizeof(sn_tab
[found
].id_str
), "%u",
1994 strncpy(sn_tab
[found
].name
, inode
.tag
,
1995 MIN(sizeof(sn_tab
[found
].name
), sizeof(inode
.tag
)));
2009 static int do_load_save_vmstate(BDRVSheepdogState
*s
, uint8_t *data
,
2010 int64_t pos
, int size
, int load
)
2014 unsigned int data_len
;
2015 uint64_t vmstate_oid
;
2019 fd
= connect_to_sdog(s
->addr
, s
->port
);
2026 vdi_index
= pos
/ SD_DATA_OBJ_SIZE
;
2027 offset
= pos
% SD_DATA_OBJ_SIZE
;
2029 data_len
= MIN(size
, SD_DATA_OBJ_SIZE
);
2031 vmstate_oid
= vid_to_vmstate_oid(s
->inode
.vdi_id
, vdi_index
);
2033 create
= (offset
== 0);
2035 ret
= read_object(fd
, (char *)data
, vmstate_oid
,
2036 s
->inode
.nr_copies
, data_len
, offset
);
2038 ret
= write_object(fd
, (char *)data
, vmstate_oid
,
2039 s
->inode
.nr_copies
, data_len
, offset
, create
);
2043 error_report("failed to save vmstate %s", strerror(errno
));
2057 static int sd_save_vmstate(BlockDriverState
*bs
, const uint8_t *data
,
2058 int64_t pos
, int size
)
2060 BDRVSheepdogState
*s
= bs
->opaque
;
2062 return do_load_save_vmstate(s
, (uint8_t *)data
, pos
, size
, 0);
2065 static int sd_load_vmstate(BlockDriverState
*bs
, uint8_t *data
,
2066 int64_t pos
, int size
)
2068 BDRVSheepdogState
*s
= bs
->opaque
;
2070 return do_load_save_vmstate(s
, data
, pos
, size
, 1);
2074 static QEMUOptionParameter sd_create_options
[] = {
2076 .name
= BLOCK_OPT_SIZE
,
2078 .help
= "Virtual disk size"
2081 .name
= BLOCK_OPT_BACKING_FILE
,
2083 .help
= "File name of a base image"
2086 .name
= BLOCK_OPT_PREALLOC
,
2088 .help
= "Preallocation mode (allowed values: off, full)"
2093 BlockDriver bdrv_sheepdog
= {
2094 .format_name
= "sheepdog",
2095 .protocol_name
= "sheepdog",
2096 .instance_size
= sizeof(BDRVSheepdogState
),
2097 .bdrv_file_open
= sd_open
,
2098 .bdrv_close
= sd_close
,
2099 .bdrv_create
= sd_create
,
2100 .bdrv_getlength
= sd_getlength
,
2101 .bdrv_truncate
= sd_truncate
,
2103 .bdrv_co_readv
= sd_co_readv
,
2104 .bdrv_co_writev
= sd_co_writev
,
2106 .bdrv_snapshot_create
= sd_snapshot_create
,
2107 .bdrv_snapshot_goto
= sd_snapshot_goto
,
2108 .bdrv_snapshot_delete
= sd_snapshot_delete
,
2109 .bdrv_snapshot_list
= sd_snapshot_list
,
2111 .bdrv_save_vmstate
= sd_save_vmstate
,
2112 .bdrv_load_vmstate
= sd_load_vmstate
,
2114 .create_options
= sd_create_options
,
2117 static void bdrv_sheepdog_init(void)
2119 bdrv_register(&bdrv_sheepdog
);
2121 block_init(bdrv_sheepdog_init
);