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/>.
17 #include <netinet/tcp.h>
19 #define closesocket(s) close(s)
22 #include "qemu-common.h"
23 #include "qemu-error.h"
24 #include "qemu_socket.h"
25 #include "block_int.h"
27 #define SD_PROTO_VER 0x01
29 #define SD_DEFAULT_ADDR "localhost"
30 #define SD_DEFAULT_PORT "7000"
32 #define SD_OP_CREATE_AND_WRITE_OBJ 0x01
33 #define SD_OP_READ_OBJ 0x02
34 #define SD_OP_WRITE_OBJ 0x03
36 #define SD_OP_NEW_VDI 0x11
37 #define SD_OP_LOCK_VDI 0x12
38 #define SD_OP_RELEASE_VDI 0x13
39 #define SD_OP_GET_VDI_INFO 0x14
40 #define SD_OP_READ_VDIS 0x15
42 #define SD_FLAG_CMD_WRITE 0x01
43 #define SD_FLAG_CMD_COW 0x02
45 #define SD_RES_SUCCESS 0x00 /* Success */
46 #define SD_RES_UNKNOWN 0x01 /* Unknown error */
47 #define SD_RES_NO_OBJ 0x02 /* No object found */
48 #define SD_RES_EIO 0x03 /* I/O error */
49 #define SD_RES_VDI_EXIST 0x04 /* Vdi exists already */
50 #define SD_RES_INVALID_PARMS 0x05 /* Invalid parameters */
51 #define SD_RES_SYSTEM_ERROR 0x06 /* System error */
52 #define SD_RES_VDI_LOCKED 0x07 /* Vdi is locked */
53 #define SD_RES_NO_VDI 0x08 /* No vdi found */
54 #define SD_RES_NO_BASE_VDI 0x09 /* No base vdi found */
55 #define SD_RES_VDI_READ 0x0A /* Cannot read requested vdi */
56 #define SD_RES_VDI_WRITE 0x0B /* Cannot write requested vdi */
57 #define SD_RES_BASE_VDI_READ 0x0C /* Cannot read base vdi */
58 #define SD_RES_BASE_VDI_WRITE 0x0D /* Cannot write base vdi */
59 #define SD_RES_NO_TAG 0x0E /* Requested tag is not found */
60 #define SD_RES_STARTUP 0x0F /* Sheepdog is on starting up */
61 #define SD_RES_VDI_NOT_LOCKED 0x10 /* Vdi is not locked */
62 #define SD_RES_SHUTDOWN 0x11 /* Sheepdog is shutting down */
63 #define SD_RES_NO_MEM 0x12 /* Cannot allocate memory */
64 #define SD_RES_FULL_VDI 0x13 /* we already have the maximum vdis */
65 #define SD_RES_VER_MISMATCH 0x14 /* Protocol version mismatch */
66 #define SD_RES_NO_SPACE 0x15 /* Server has no room for new objects */
67 #define SD_RES_WAIT_FOR_FORMAT 0x16 /* Waiting for a format operation */
68 #define SD_RES_WAIT_FOR_JOIN 0x17 /* Waiting for other nodes joining */
69 #define SD_RES_JOIN_FAILED 0x18 /* Target node had failed to join sheepdog */
74 * 0 - 19 (20 bits): data object space
75 * 20 - 31 (12 bits): reserved data object space
76 * 32 - 55 (24 bits): vdi object space
77 * 56 - 59 ( 4 bits): reserved vdi object space
78 * 60 - 63 ( 4 bits): object type indentifier space
81 #define VDI_SPACE_SHIFT 32
82 #define VDI_BIT (UINT64_C(1) << 63)
83 #define VMSTATE_BIT (UINT64_C(1) << 62)
84 #define MAX_DATA_OBJS (UINT64_C(1) << 20)
85 #define MAX_CHILDREN 1024
86 #define SD_MAX_VDI_LEN 256
87 #define SD_MAX_VDI_TAG_LEN 256
88 #define SD_NR_VDIS (1U << 24)
89 #define SD_DATA_OBJ_SIZE (UINT64_C(1) << 22)
90 #define SD_MAX_VDI_SIZE (SD_DATA_OBJ_SIZE * MAX_DATA_OBJS)
91 #define SECTOR_SIZE 512
93 #define SD_INODE_SIZE (sizeof(SheepdogInode))
94 #define CURRENT_VDI_ID 0
96 typedef struct SheepdogReq
{
102 uint32_t data_length
;
103 uint32_t opcode_specific
[8];
106 typedef struct SheepdogRsp
{
112 uint32_t data_length
;
114 uint32_t opcode_specific
[7];
117 typedef struct SheepdogObjReq
{
123 uint32_t data_length
;
131 typedef struct SheepdogObjRsp
{
137 uint32_t data_length
;
143 typedef struct SheepdogVdiReq
{
149 uint32_t data_length
;
151 uint32_t base_vdi_id
;
157 typedef struct SheepdogVdiRsp
{
163 uint32_t data_length
;
170 typedef struct SheepdogInode
{
171 char name
[SD_MAX_VDI_LEN
];
172 char tag
[SD_MAX_VDI_TAG_LEN
];
175 uint64_t vm_clock_nsec
;
177 uint64_t vm_state_size
;
178 uint16_t copy_policy
;
180 uint8_t block_size_shift
;
183 uint32_t parent_vdi_id
;
184 uint32_t child_vdi_id
[MAX_CHILDREN
];
185 uint32_t data_vdi_id
[MAX_DATA_OBJS
];
189 * 64 bit FNV-1a non-zero initial basis
191 #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
194 * 64 bit Fowler/Noll/Vo FNV-1a hash code
196 static inline uint64_t fnv_64a_buf(void *buf
, size_t len
, uint64_t hval
)
198 unsigned char *bp
= buf
;
199 unsigned char *be
= bp
+ len
;
201 hval
^= (uint64_t) *bp
++;
202 hval
+= (hval
<< 1) + (hval
<< 4) + (hval
<< 5) +
203 (hval
<< 7) + (hval
<< 8) + (hval
<< 40);
208 static inline int is_data_obj_writeable(SheepdogInode
*inode
, unsigned int idx
)
210 return inode
->vdi_id
== inode
->data_vdi_id
[idx
];
213 static inline int is_data_obj(uint64_t oid
)
215 return !(VDI_BIT
& oid
);
218 static inline uint64_t data_oid_to_idx(uint64_t oid
)
220 return oid
& (MAX_DATA_OBJS
- 1);
223 static inline uint64_t vid_to_vdi_oid(uint32_t vid
)
225 return VDI_BIT
| ((uint64_t)vid
<< VDI_SPACE_SHIFT
);
228 static inline uint64_t vid_to_vmstate_oid(uint32_t vid
, uint32_t idx
)
230 return VMSTATE_BIT
| ((uint64_t)vid
<< VDI_SPACE_SHIFT
) | idx
;
233 static inline uint64_t vid_to_data_oid(uint32_t vid
, uint32_t idx
)
235 return ((uint64_t)vid
<< VDI_SPACE_SHIFT
) | idx
;
238 static inline int is_snapshot(struct SheepdogInode
*inode
)
240 return !!inode
->snap_ctime
;
245 #define dprintf(fmt, args...) \
247 fprintf(stdout, "%s %d: " fmt, __func__, __LINE__, ##args); \
250 #define dprintf(fmt, args...)
253 typedef struct SheepdogAIOCB SheepdogAIOCB
;
255 typedef struct AIOReq
{
256 SheepdogAIOCB
*aiocb
;
257 unsigned int iov_offset
;
262 unsigned int data_len
;
266 QLIST_ENTRY(AIOReq
) outstanding_aio_siblings
;
267 QLIST_ENTRY(AIOReq
) aioreq_siblings
;
275 struct SheepdogAIOCB
{
276 BlockDriverAIOCB common
;
284 enum AIOCBState aiocb_type
;
287 void (*aio_done_func
)(SheepdogAIOCB
*);
291 QLIST_HEAD(aioreq_head
, AIOReq
) aioreq_head
;
294 typedef struct BDRVSheepdogState
{
297 uint32_t min_dirty_data_idx
;
298 uint32_t max_dirty_data_idx
;
300 char name
[SD_MAX_VDI_LEN
];
307 uint32_t aioreq_seq_num
;
308 QLIST_HEAD(outstanding_aio_head
, AIOReq
) outstanding_aio_head
;
311 static const char * sd_strerror(int err
)
315 static const struct {
319 {SD_RES_SUCCESS
, "Success"},
320 {SD_RES_UNKNOWN
, "Unknown error"},
321 {SD_RES_NO_OBJ
, "No object found"},
322 {SD_RES_EIO
, "I/O error"},
323 {SD_RES_VDI_EXIST
, "VDI exists already"},
324 {SD_RES_INVALID_PARMS
, "Invalid parameters"},
325 {SD_RES_SYSTEM_ERROR
, "System error"},
326 {SD_RES_VDI_LOCKED
, "VDI is already locked"},
327 {SD_RES_NO_VDI
, "No vdi found"},
328 {SD_RES_NO_BASE_VDI
, "No base VDI found"},
329 {SD_RES_VDI_READ
, "Failed read the requested VDI"},
330 {SD_RES_VDI_WRITE
, "Failed to write the requested VDI"},
331 {SD_RES_BASE_VDI_READ
, "Failed to read the base VDI"},
332 {SD_RES_BASE_VDI_WRITE
, "Failed to write the base VDI"},
333 {SD_RES_NO_TAG
, "Failed to find the requested tag"},
334 {SD_RES_STARTUP
, "The system is still booting"},
335 {SD_RES_VDI_NOT_LOCKED
, "VDI isn't locked"},
336 {SD_RES_SHUTDOWN
, "The system is shutting down"},
337 {SD_RES_NO_MEM
, "Out of memory on the server"},
338 {SD_RES_FULL_VDI
, "We already have the maximum vdis"},
339 {SD_RES_VER_MISMATCH
, "Protocol version mismatch"},
340 {SD_RES_NO_SPACE
, "Server has no space for new objects"},
341 {SD_RES_WAIT_FOR_FORMAT
, "Sheepdog is waiting for a format operation"},
342 {SD_RES_WAIT_FOR_JOIN
, "Sheepdog is waiting for other nodes joining"},
343 {SD_RES_JOIN_FAILED
, "Target node had failed to join sheepdog"},
346 for (i
= 0; i
< ARRAY_SIZE(errors
); ++i
) {
347 if (errors
[i
].err
== err
) {
348 return errors
[i
].desc
;
352 return "Invalid error code";
356 * Sheepdog I/O handling:
358 * 1. In the sd_aio_readv/writev, read/write requests are added to the
359 * QEMU Bottom Halves.
361 * 2. In sd_readv_writev_bh_cb, the callbacks of BHs, we send the I/O
362 * requests to the server and link the requests to the
363 * outstanding_list in the BDRVSheepdogState. we exits the
364 * function without waiting for receiving the response.
366 * 3. We receive the response in aio_read_response, the fd handler to
367 * the sheepdog connection. If metadata update is needed, we send
368 * the write request to the vdi object in sd_write_done, the write
369 * completion function. The AIOCB callback is not called until all
370 * the requests belonging to the AIOCB are finished.
373 static inline AIOReq
*alloc_aio_req(BDRVSheepdogState
*s
, SheepdogAIOCB
*acb
,
374 uint64_t oid
, unsigned int data_len
,
375 uint64_t offset
, uint8_t flags
,
376 uint64_t base_oid
, unsigned int iov_offset
)
380 aio_req
= qemu_malloc(sizeof(*aio_req
));
381 aio_req
->aiocb
= acb
;
382 aio_req
->iov_offset
= iov_offset
;
384 aio_req
->base_oid
= base_oid
;
385 aio_req
->offset
= offset
;
386 aio_req
->data_len
= data_len
;
387 aio_req
->flags
= flags
;
388 aio_req
->id
= s
->aioreq_seq_num
++;
390 QLIST_INSERT_HEAD(&s
->outstanding_aio_head
, aio_req
,
391 outstanding_aio_siblings
);
392 QLIST_INSERT_HEAD(&acb
->aioreq_head
, aio_req
, aioreq_siblings
);
397 static inline int free_aio_req(BDRVSheepdogState
*s
, AIOReq
*aio_req
)
399 SheepdogAIOCB
*acb
= aio_req
->aiocb
;
400 QLIST_REMOVE(aio_req
, outstanding_aio_siblings
);
401 QLIST_REMOVE(aio_req
, aioreq_siblings
);
404 return !QLIST_EMPTY(&acb
->aioreq_head
);
407 static void sd_finish_aiocb(SheepdogAIOCB
*acb
)
409 if (!acb
->canceled
) {
410 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
412 qemu_aio_release(acb
);
415 static void sd_aio_cancel(BlockDriverAIOCB
*blockacb
)
417 SheepdogAIOCB
*acb
= (SheepdogAIOCB
*)blockacb
;
420 * Sheepdog cannot cancel the requests which are already sent to
421 * the servers, so we just complete the request with -EIO here.
423 acb
->common
.cb(acb
->common
.opaque
, -EIO
);
427 static AIOPool sd_aio_pool
= {
428 .aiocb_size
= sizeof(SheepdogAIOCB
),
429 .cancel
= sd_aio_cancel
,
432 static SheepdogAIOCB
*sd_aio_setup(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
433 int64_t sector_num
, int nb_sectors
,
434 BlockDriverCompletionFunc
*cb
, void *opaque
)
438 acb
= qemu_aio_get(&sd_aio_pool
, bs
, cb
, opaque
);
442 acb
->sector_num
= sector_num
;
443 acb
->nb_sectors
= nb_sectors
;
445 acb
->aio_done_func
= NULL
;
449 QLIST_INIT(&acb
->aioreq_head
);
453 static int sd_schedule_bh(QEMUBHFunc
*cb
, SheepdogAIOCB
*acb
)
456 error_report("bug: %d %d\n", acb
->aiocb_type
, acb
->aiocb_type
);
460 acb
->bh
= qemu_bh_new(cb
, acb
);
462 error_report("oom: %d %d\n", acb
->aiocb_type
, acb
->aiocb_type
);
466 qemu_bh_schedule(acb
->bh
);
474 struct iovec
*msg_iov
;
478 static ssize_t
sendmsg(int s
, const struct msghdr
*msg
, int flags
)
484 /* count the msg size */
485 for (i
= 0; i
< msg
->msg_iovlen
; i
++) {
486 size
+= msg
->msg_iov
[i
].iov_len
;
488 buf
= qemu_malloc(size
);
491 for (i
= 0; i
< msg
->msg_iovlen
; i
++) {
492 memcpy(p
, msg
->msg_iov
[i
].iov_base
, msg
->msg_iov
[i
].iov_len
);
493 p
+= msg
->msg_iov
[i
].iov_len
;
496 ret
= send(s
, buf
, size
, flags
);
502 static ssize_t
recvmsg(int s
, struct msghdr
*msg
, int flags
)
508 /* count the msg size */
509 for (i
= 0; i
< msg
->msg_iovlen
; i
++) {
510 size
+= msg
->msg_iov
[i
].iov_len
;
512 buf
= qemu_malloc(size
);
514 ret
= recv(s
, buf
, size
, flags
);
520 for (i
= 0; i
< msg
->msg_iovlen
; i
++) {
521 memcpy(msg
->msg_iov
[i
].iov_base
, p
, msg
->msg_iov
[i
].iov_len
);
522 p
+= msg
->msg_iov
[i
].iov_len
;
532 * Send/recv data with iovec buffers
534 * This function send/recv data from/to the iovec buffer directly.
535 * The first `offset' bytes in the iovec buffer are skipped and next
536 * `len' bytes are used.
540 * do_send_recv(sockfd, iov, len, offset, 1);
544 * char *buf = malloc(size);
545 * iov_to_buf(iov, iovcnt, buf, offset, size);
546 * send(sockfd, buf, size, 0);
549 static int do_send_recv(int sockfd
, struct iovec
*iov
, int len
, int offset
,
555 memset(&msg
, 0, sizeof(msg
));
561 while (iov
->iov_len
< len
) {
568 diff
= iov
->iov_len
- len
;
569 iov
->iov_len
-= diff
;
571 while (msg
.msg_iov
->iov_len
<= offset
) {
572 offset
-= msg
.msg_iov
->iov_len
;
578 msg
.msg_iov
->iov_base
= (char *) msg
.msg_iov
->iov_base
+ offset
;
579 msg
.msg_iov
->iov_len
-= offset
;
582 ret
= sendmsg(sockfd
, &msg
, 0);
584 ret
= recvmsg(sockfd
, &msg
, 0);
587 msg
.msg_iov
->iov_base
= (char *) msg
.msg_iov
->iov_base
- offset
;
588 msg
.msg_iov
->iov_len
+= offset
;
590 iov
->iov_len
+= diff
;
594 static int connect_to_sdog(const char *addr
, const char *port
)
596 char hbuf
[NI_MAXHOST
], sbuf
[NI_MAXSERV
];
598 struct addrinfo hints
, *res
, *res0
;
601 addr
= SD_DEFAULT_ADDR
;
602 port
= SD_DEFAULT_PORT
;
605 memset(&hints
, 0, sizeof(hints
));
606 hints
.ai_socktype
= SOCK_STREAM
;
608 ret
= getaddrinfo(addr
, port
, &hints
, &res0
);
610 error_report("unable to get address info %s, %s\n",
611 addr
, strerror(errno
));
615 for (res
= res0
; res
; res
= res
->ai_next
) {
616 ret
= getnameinfo(res
->ai_addr
, res
->ai_addrlen
, hbuf
, sizeof(hbuf
),
617 sbuf
, sizeof(sbuf
), NI_NUMERICHOST
| NI_NUMERICSERV
);
622 fd
= socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
628 ret
= connect(fd
, res
->ai_addr
, res
->ai_addrlen
);
630 if (errno
== EINTR
) {
636 dprintf("connected to %s:%s\n", addr
, port
);
640 error_report("failed connect to %s:%s\n", addr
, port
);
646 static int do_readv_writev(int sockfd
, struct iovec
*iov
, int len
,
647 int iov_offset
, int write
)
651 ret
= do_send_recv(sockfd
, iov
, len
, iov_offset
, write
);
653 if (errno
== EINTR
|| errno
== EAGAIN
) {
656 error_report("failed to recv a rsp, %s\n", strerror(errno
));
669 static int do_readv(int sockfd
, struct iovec
*iov
, int len
, int iov_offset
)
671 return do_readv_writev(sockfd
, iov
, len
, iov_offset
, 0);
674 static int do_writev(int sockfd
, struct iovec
*iov
, int len
, int iov_offset
)
676 return do_readv_writev(sockfd
, iov
, len
, iov_offset
, 1);
679 static int do_read_write(int sockfd
, void *buf
, int len
, int write
)
686 return do_readv_writev(sockfd
, &iov
, len
, 0, write
);
689 static int do_read(int sockfd
, void *buf
, int len
)
691 return do_read_write(sockfd
, buf
, len
, 0);
694 static int do_write(int sockfd
, void *buf
, int len
)
696 return do_read_write(sockfd
, buf
, len
, 1);
699 static int send_req(int sockfd
, SheepdogReq
*hdr
, void *data
,
705 iov
[0].iov_base
= hdr
;
706 iov
[0].iov_len
= sizeof(*hdr
);
709 iov
[1].iov_base
= data
;
710 iov
[1].iov_len
= *wlen
;
713 ret
= do_writev(sockfd
, iov
, sizeof(*hdr
) + *wlen
, 0);
715 error_report("failed to send a req, %s\n", strerror(errno
));
722 static int do_req(int sockfd
, SheepdogReq
*hdr
, void *data
,
723 unsigned int *wlen
, unsigned int *rlen
)
727 ret
= send_req(sockfd
, hdr
, data
, wlen
);
733 ret
= do_read(sockfd
, hdr
, sizeof(*hdr
));
735 error_report("failed to get a rsp, %s\n", strerror(errno
));
740 if (*rlen
> hdr
->data_length
) {
741 *rlen
= hdr
->data_length
;
745 ret
= do_read(sockfd
, data
, *rlen
);
747 error_report("failed to get the data, %s\n", strerror(errno
));
757 static int add_aio_request(BDRVSheepdogState
*s
, AIOReq
*aio_req
,
758 struct iovec
*iov
, int niov
, int create
,
759 enum AIOCBState aiocb_type
);
762 * This function searchs pending requests to the object `oid', and
765 static void send_pending_req(BDRVSheepdogState
*s
, uint64_t oid
, uint32_t id
)
767 AIOReq
*aio_req
, *next
;
771 QLIST_FOREACH_SAFE(aio_req
, &s
->outstanding_aio_head
,
772 outstanding_aio_siblings
, next
) {
773 if (id
== aio_req
->id
) {
776 if (aio_req
->oid
!= oid
) {
780 acb
= aio_req
->aiocb
;
781 ret
= add_aio_request(s
, aio_req
, acb
->qiov
->iov
,
782 acb
->qiov
->niov
, 0, acb
->aiocb_type
);
784 error_report("add_aio_request is failed\n");
785 free_aio_req(s
, aio_req
);
786 if (QLIST_EMPTY(&acb
->aioreq_head
)) {
787 sd_finish_aiocb(acb
);
794 * Receive responses of the I/O requests.
796 * This function is registered as a fd handler, and called from the
797 * main loop when s->fd is ready for reading responses.
799 static void aio_read_response(void *opaque
)
802 BDRVSheepdogState
*s
= opaque
;
805 AIOReq
*aio_req
= NULL
;
810 if (QLIST_EMPTY(&s
->outstanding_aio_head
)) {
815 ret
= do_read(fd
, &rsp
, sizeof(rsp
));
817 error_report("failed to get the header, %s\n", strerror(errno
));
821 /* find the right aio_req from the outstanding_aio list */
822 QLIST_FOREACH(aio_req
, &s
->outstanding_aio_head
, outstanding_aio_siblings
) {
823 if (aio_req
->id
== rsp
.id
) {
828 error_report("cannot find aio_req %x\n", rsp
.id
);
832 acb
= aio_req
->aiocb
;
834 switch (acb
->aiocb_type
) {
835 case AIOCB_WRITE_UDATA
:
836 if (!is_data_obj(aio_req
->oid
)) {
839 idx
= data_oid_to_idx(aio_req
->oid
);
841 if (s
->inode
.data_vdi_id
[idx
] != s
->inode
.vdi_id
) {
843 * If the object is newly created one, we need to update
844 * the vdi object (metadata object). min_dirty_data_idx
845 * and max_dirty_data_idx are changed to include updated
846 * index between them.
848 s
->inode
.data_vdi_id
[idx
] = s
->inode
.vdi_id
;
849 s
->max_dirty_data_idx
= MAX(idx
, s
->max_dirty_data_idx
);
850 s
->min_dirty_data_idx
= MIN(idx
, s
->min_dirty_data_idx
);
853 * Some requests may be blocked because simultaneous
854 * create requests are not allowed, so we search the
855 * pending requests here.
857 send_pending_req(s
, vid_to_data_oid(s
->inode
.vdi_id
, idx
), rsp
.id
);
860 case AIOCB_READ_UDATA
:
861 ret
= do_readv(fd
, acb
->qiov
->iov
, rsp
.data_length
,
862 aio_req
->iov_offset
);
864 error_report("failed to get the data, %s\n", strerror(errno
));
870 if (rsp
.result
!= SD_RES_SUCCESS
) {
872 error_report("%s\n", sd_strerror(rsp
.result
));
875 rest
= free_aio_req(s
, aio_req
);
878 * We've finished all requests which belong to the AIOCB, so
879 * we can call the callback now.
881 acb
->aio_done_func(acb
);
885 static int aio_flush_request(void *opaque
)
887 BDRVSheepdogState
*s
= opaque
;
889 return !QLIST_EMPTY(&s
->outstanding_aio_head
);
892 #if !defined(SOL_TCP) || !defined(TCP_CORK)
894 static int set_cork(int fd
, int v
)
901 static int set_cork(int fd
, int v
)
903 return setsockopt(fd
, SOL_TCP
, TCP_CORK
, &v
, sizeof(v
));
908 static int set_nodelay(int fd
)
913 ret
= setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&opt
, sizeof(opt
));
918 * Return a socket discriptor to read/write objects.
920 * We cannot use this discriptor for other operations because
921 * the block driver may be on waiting response from the server.
923 static int get_sheep_fd(BDRVSheepdogState
*s
)
927 fd
= connect_to_sdog(s
->addr
, s
->port
);
929 error_report("%s\n", strerror(errno
));
933 socket_set_nonblock(fd
);
935 ret
= set_nodelay(fd
);
937 error_report("%s\n", strerror(errno
));
942 qemu_aio_set_fd_handler(fd
, aio_read_response
, NULL
, aio_flush_request
,
950 * filename must be one of the following formats:
952 * 2. [vdiname]:[snapid]
954 * 4. [hostname]:[port]:[vdiname]
955 * 5. [hostname]:[port]:[vdiname]:[snapid]
956 * 6. [hostname]:[port]:[vdiname]:[tag]
958 * You can boot from the snapshot images by specifying `snapid` or
961 * You can run VMs outside the Sheepdog cluster by specifying
962 * `hostname' and `port' (experimental).
964 static int parse_vdiname(BDRVSheepdogState
*s
, const char *filename
,
965 char *vdi
, uint32_t *snapid
, char *tag
)
970 p
= q
= qemu_strdup(filename
);
972 /* count the number of separators */
982 /* use the first two tokens as hostname and port number. */
996 strncpy(vdi
, p
, SD_MAX_VDI_LEN
);
998 p
= strchr(vdi
, ':');
1001 *snapid
= strtoul(p
, NULL
, 10);
1003 strncpy(tag
, p
, SD_MAX_VDI_TAG_LEN
);
1006 *snapid
= CURRENT_VDI_ID
; /* search current vdi */
1009 if (s
->addr
== NULL
) {
1016 static int find_vdi_name(BDRVSheepdogState
*s
, char *filename
, uint32_t snapid
,
1017 char *tag
, uint32_t *vid
, int for_snapshot
)
1021 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1022 unsigned int wlen
, rlen
= 0;
1023 char buf
[SD_MAX_VDI_LEN
+ SD_MAX_VDI_TAG_LEN
];
1025 fd
= connect_to_sdog(s
->addr
, s
->port
);
1030 memset(buf
, 0, sizeof(buf
));
1031 strncpy(buf
, filename
, SD_MAX_VDI_LEN
);
1032 strncpy(buf
+ SD_MAX_VDI_LEN
, tag
, SD_MAX_VDI_TAG_LEN
);
1034 memset(&hdr
, 0, sizeof(hdr
));
1036 hdr
.opcode
= SD_OP_GET_VDI_INFO
;
1038 hdr
.opcode
= SD_OP_LOCK_VDI
;
1040 wlen
= SD_MAX_VDI_LEN
+ SD_MAX_VDI_TAG_LEN
;
1041 hdr
.proto_ver
= SD_PROTO_VER
;
1042 hdr
.data_length
= wlen
;
1043 hdr
.snapid
= snapid
;
1044 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1046 ret
= do_req(fd
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1052 if (rsp
->result
!= SD_RES_SUCCESS
) {
1053 error_report("cannot get vdi info, %s, %s %d %s\n",
1054 sd_strerror(rsp
->result
), filename
, snapid
, tag
);
1066 static int add_aio_request(BDRVSheepdogState
*s
, AIOReq
*aio_req
,
1067 struct iovec
*iov
, int niov
, int create
,
1068 enum AIOCBState aiocb_type
)
1070 int nr_copies
= s
->inode
.nr_copies
;
1074 uint64_t oid
= aio_req
->oid
;
1075 unsigned int datalen
= aio_req
->data_len
;
1076 uint64_t offset
= aio_req
->offset
;
1077 uint8_t flags
= aio_req
->flags
;
1078 uint64_t old_oid
= aio_req
->base_oid
;
1081 error_report("bug\n");
1084 memset(&hdr
, 0, sizeof(hdr
));
1086 if (aiocb_type
== AIOCB_READ_UDATA
) {
1088 hdr
.opcode
= SD_OP_READ_OBJ
;
1090 } else if (create
) {
1092 hdr
.opcode
= SD_OP_CREATE_AND_WRITE_OBJ
;
1093 hdr
.flags
= SD_FLAG_CMD_WRITE
| flags
;
1096 hdr
.opcode
= SD_OP_WRITE_OBJ
;
1097 hdr
.flags
= SD_FLAG_CMD_WRITE
| flags
;
1101 hdr
.cow_oid
= old_oid
;
1102 hdr
.copies
= s
->inode
.nr_copies
;
1104 hdr
.data_length
= datalen
;
1105 hdr
.offset
= offset
;
1107 hdr
.id
= aio_req
->id
;
1112 ret
= do_write(s
->fd
, &hdr
, sizeof(hdr
));
1114 error_report("failed to send a req, %s\n", strerror(errno
));
1119 ret
= do_writev(s
->fd
, iov
, wlen
, aio_req
->iov_offset
);
1121 error_report("failed to send a data, %s\n", strerror(errno
));
1131 static int read_write_object(int fd
, char *buf
, uint64_t oid
, int copies
,
1132 unsigned int datalen
, uint64_t offset
,
1133 int write
, int create
)
1136 SheepdogObjRsp
*rsp
= (SheepdogObjRsp
*)&hdr
;
1137 unsigned int wlen
, rlen
;
1140 memset(&hdr
, 0, sizeof(hdr
));
1145 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1147 hdr
.opcode
= SD_OP_CREATE_AND_WRITE_OBJ
;
1149 hdr
.opcode
= SD_OP_WRITE_OBJ
;
1154 hdr
.opcode
= SD_OP_READ_OBJ
;
1157 hdr
.data_length
= datalen
;
1158 hdr
.offset
= offset
;
1159 hdr
.copies
= copies
;
1161 ret
= do_req(fd
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1163 error_report("failed to send a request to the sheep\n");
1167 switch (rsp
->result
) {
1168 case SD_RES_SUCCESS
:
1171 error_report("%s\n", sd_strerror(rsp
->result
));
1176 static int read_object(int fd
, char *buf
, uint64_t oid
, int copies
,
1177 unsigned int datalen
, uint64_t offset
)
1179 return read_write_object(fd
, buf
, oid
, copies
, datalen
, offset
, 0, 0);
1182 static int write_object(int fd
, char *buf
, uint64_t oid
, int copies
,
1183 unsigned int datalen
, uint64_t offset
, int create
)
1185 return read_write_object(fd
, buf
, oid
, copies
, datalen
, offset
, 1, create
);
1188 static int sd_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1192 BDRVSheepdogState
*s
= bs
->opaque
;
1193 char vdi
[SD_MAX_VDI_LEN
], tag
[SD_MAX_VDI_TAG_LEN
];
1197 strstart(filename
, "sheepdog:", (const char **)&filename
);
1199 QLIST_INIT(&s
->outstanding_aio_head
);
1202 memset(vdi
, 0, sizeof(vdi
));
1203 memset(tag
, 0, sizeof(tag
));
1204 if (parse_vdiname(s
, filename
, vdi
, &snapid
, tag
) < 0) {
1207 s
->fd
= get_sheep_fd(s
);
1212 ret
= find_vdi_name(s
, vdi
, snapid
, tag
, &vid
, 0);
1218 dprintf("%" PRIx32
" snapshot inode was open.\n", vid
);
1222 fd
= connect_to_sdog(s
->addr
, s
->port
);
1224 error_report("failed to connect\n");
1228 buf
= qemu_malloc(SD_INODE_SIZE
);
1229 ret
= read_object(fd
, buf
, vid_to_vdi_oid(vid
), 0, SD_INODE_SIZE
, 0);
1237 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
1238 s
->min_dirty_data_idx
= UINT32_MAX
;
1239 s
->max_dirty_data_idx
= 0;
1241 bs
->total_sectors
= s
->inode
.vdi_size
/ SECTOR_SIZE
;
1242 strncpy(s
->name
, vdi
, sizeof(s
->name
));
1246 qemu_aio_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
, NULL
, NULL
);
1254 static int do_sd_create(char *filename
, int64_t vdi_size
,
1255 uint32_t base_vid
, uint32_t *vdi_id
, int snapshot
,
1256 const char *addr
, const char *port
)
1259 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1261 unsigned int wlen
, rlen
= 0;
1262 char buf
[SD_MAX_VDI_LEN
];
1264 fd
= connect_to_sdog(addr
, port
);
1269 memset(buf
, 0, sizeof(buf
));
1270 strncpy(buf
, filename
, SD_MAX_VDI_LEN
);
1272 memset(&hdr
, 0, sizeof(hdr
));
1273 hdr
.opcode
= SD_OP_NEW_VDI
;
1274 hdr
.base_vdi_id
= base_vid
;
1276 wlen
= SD_MAX_VDI_LEN
;
1278 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1279 hdr
.snapid
= snapshot
;
1281 hdr
.data_length
= wlen
;
1282 hdr
.vdi_size
= vdi_size
;
1284 ret
= do_req(fd
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1292 if (rsp
->result
!= SD_RES_SUCCESS
) {
1293 error_report("%s, %s\n", sd_strerror(rsp
->result
), filename
);
1298 *vdi_id
= rsp
->vdi_id
;
1304 static int sd_create(const char *filename
, QEMUOptionParameter
*options
)
1308 int64_t vdi_size
= 0;
1309 char *backing_file
= NULL
;
1311 strstart(filename
, "sheepdog:", (const char **)&filename
);
1313 while (options
&& options
->name
) {
1314 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
1315 vdi_size
= options
->value
.n
;
1316 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
1317 backing_file
= options
->value
.s
;
1322 if (vdi_size
> SD_MAX_VDI_SIZE
) {
1323 error_report("too big image size\n");
1328 BlockDriverState
*bs
;
1329 BDRVSheepdogState
*s
;
1332 /* Currently, only Sheepdog backing image is supported. */
1333 drv
= bdrv_find_protocol(backing_file
);
1334 if (!drv
|| strcmp(drv
->protocol_name
, "sheepdog") != 0) {
1335 error_report("backing_file must be a sheepdog image\n");
1339 ret
= bdrv_file_open(&bs
, backing_file
, 0);
1345 if (!is_snapshot(&s
->inode
)) {
1346 error_report("cannot clone from a non snapshot vdi\n");
1351 vid
= s
->inode
.vdi_id
;
1355 return do_sd_create((char *)filename
, vdi_size
, vid
, NULL
, 0, NULL
, NULL
);
1358 static void sd_close(BlockDriverState
*bs
)
1360 BDRVSheepdogState
*s
= bs
->opaque
;
1362 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1363 unsigned int wlen
, rlen
= 0;
1366 dprintf("%s\n", s
->name
);
1368 fd
= connect_to_sdog(s
->addr
, s
->port
);
1373 memset(&hdr
, 0, sizeof(hdr
));
1375 hdr
.opcode
= SD_OP_RELEASE_VDI
;
1376 wlen
= strlen(s
->name
) + 1;
1377 hdr
.data_length
= wlen
;
1378 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1380 ret
= do_req(fd
, (SheepdogReq
*)&hdr
, s
->name
, &wlen
, &rlen
);
1384 if (!ret
&& rsp
->result
!= SD_RES_SUCCESS
&&
1385 rsp
->result
!= SD_RES_VDI_NOT_LOCKED
) {
1386 error_report("%s, %s\n", sd_strerror(rsp
->result
), s
->name
);
1389 qemu_aio_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
, NULL
, NULL
);
1394 static int64_t sd_getlength(BlockDriverState
*bs
)
1396 BDRVSheepdogState
*s
= bs
->opaque
;
1398 return s
->inode
.vdi_size
;
1401 static int sd_truncate(BlockDriverState
*bs
, int64_t offset
)
1403 BDRVSheepdogState
*s
= bs
->opaque
;
1405 unsigned int datalen
;
1407 if (offset
< s
->inode
.vdi_size
) {
1408 error_report("shrinking is not supported\n");
1410 } else if (offset
> SD_MAX_VDI_SIZE
) {
1411 error_report("too big image size\n");
1415 fd
= connect_to_sdog(s
->addr
, s
->port
);
1420 /* we don't need to update entire object */
1421 datalen
= SD_INODE_SIZE
- sizeof(s
->inode
.data_vdi_id
);
1422 s
->inode
.vdi_size
= offset
;
1423 ret
= write_object(fd
, (char *)&s
->inode
, vid_to_vdi_oid(s
->inode
.vdi_id
),
1424 s
->inode
.nr_copies
, datalen
, 0, 0);
1428 error_report("failed to update an inode.\n");
1436 * This function is called after writing data objects. If we need to
1437 * update metadata, this sends a write request to the vdi object.
1438 * Otherwise, this calls the AIOCB callback.
1440 static void sd_write_done(SheepdogAIOCB
*acb
)
1443 BDRVSheepdogState
*s
= acb
->common
.bs
->opaque
;
1446 uint32_t offset
, data_len
, mn
, mx
;
1448 mn
= s
->min_dirty_data_idx
;
1449 mx
= s
->max_dirty_data_idx
;
1451 /* we need to update the vdi object. */
1452 offset
= sizeof(s
->inode
) - sizeof(s
->inode
.data_vdi_id
) +
1453 mn
* sizeof(s
->inode
.data_vdi_id
[0]);
1454 data_len
= (mx
- mn
+ 1) * sizeof(s
->inode
.data_vdi_id
[0]);
1456 s
->min_dirty_data_idx
= UINT32_MAX
;
1457 s
->max_dirty_data_idx
= 0;
1459 iov
.iov_base
= &s
->inode
;
1460 iov
.iov_len
= sizeof(s
->inode
);
1461 aio_req
= alloc_aio_req(s
, acb
, vid_to_vdi_oid(s
->inode
.vdi_id
),
1462 data_len
, offset
, 0, 0, offset
);
1463 ret
= add_aio_request(s
, aio_req
, &iov
, 1, 0, AIOCB_WRITE_UDATA
);
1465 free_aio_req(s
, aio_req
);
1470 acb
->aio_done_func
= sd_finish_aiocb
;
1471 acb
->aiocb_type
= AIOCB_WRITE_UDATA
;
1475 sd_finish_aiocb(acb
);
1479 * Create a writable VDI from a snapshot
1481 static int sd_create_branch(BDRVSheepdogState
*s
)
1487 dprintf("%" PRIx32
" is snapshot.\n", s
->inode
.vdi_id
);
1489 buf
= qemu_malloc(SD_INODE_SIZE
);
1491 ret
= do_sd_create(s
->name
, s
->inode
.vdi_size
, s
->inode
.vdi_id
, &vid
, 1,
1497 dprintf("%" PRIx32
" is created.\n", vid
);
1499 fd
= connect_to_sdog(s
->addr
, s
->port
);
1501 error_report("failed to connect\n");
1505 ret
= read_object(fd
, buf
, vid_to_vdi_oid(vid
), s
->inode
.nr_copies
,
1514 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
1518 dprintf("%" PRIx32
" was newly created.\n", s
->inode
.vdi_id
);
1527 * Send I/O requests to the server.
1529 * This function sends requests to the server, links the requests to
1530 * the outstanding_list in BDRVSheepdogState, and exits without
1531 * waiting the response. The responses are received in the
1532 * `aio_read_response' function which is called from the main loop as
1535 static void sd_readv_writev_bh_cb(void *p
)
1537 SheepdogAIOCB
*acb
= p
;
1539 unsigned long len
, done
= 0, total
= acb
->nb_sectors
* SECTOR_SIZE
;
1540 unsigned long idx
= acb
->sector_num
* SECTOR_SIZE
/ SD_DATA_OBJ_SIZE
;
1542 uint64_t offset
= (acb
->sector_num
* SECTOR_SIZE
) % SD_DATA_OBJ_SIZE
;
1543 BDRVSheepdogState
*s
= acb
->common
.bs
->opaque
;
1544 SheepdogInode
*inode
= &s
->inode
;
1547 qemu_bh_delete(acb
->bh
);
1550 if (acb
->aiocb_type
== AIOCB_WRITE_UDATA
&& s
->is_snapshot
) {
1552 * In the case we open the snapshot VDI, Sheepdog creates the
1553 * writable VDI when we do a write operation first.
1555 ret
= sd_create_branch(s
);
1562 while (done
!= total
) {
1564 uint64_t old_oid
= 0;
1567 oid
= vid_to_data_oid(inode
->data_vdi_id
[idx
], idx
);
1569 len
= MIN(total
- done
, SD_DATA_OBJ_SIZE
- offset
);
1571 if (!inode
->data_vdi_id
[idx
]) {
1572 if (acb
->aiocb_type
== AIOCB_READ_UDATA
) {
1577 } else if (acb
->aiocb_type
== AIOCB_WRITE_UDATA
1578 && !is_data_obj_writeable(inode
, idx
)) {
1582 flags
= SD_FLAG_CMD_COW
;
1586 dprintf("update ino (%" PRIu32
") %" PRIu64
" %" PRIu64
1587 " %" PRIu64
"\n", inode
->vdi_id
, oid
,
1588 vid_to_data_oid(inode
->data_vdi_id
[idx
], idx
), idx
);
1589 oid
= vid_to_data_oid(inode
->vdi_id
, idx
);
1590 dprintf("new oid %lx\n", oid
);
1593 aio_req
= alloc_aio_req(s
, acb
, oid
, len
, offset
, flags
, old_oid
, done
);
1597 QLIST_FOREACH(areq
, &s
->outstanding_aio_head
,
1598 outstanding_aio_siblings
) {
1599 if (areq
== aio_req
) {
1602 if (areq
->oid
== oid
) {
1604 * Sheepdog cannot handle simultaneous create
1605 * requests to the same object. So we cannot send
1606 * the request until the previous request
1610 aio_req
->base_oid
= 0;
1616 ret
= add_aio_request(s
, aio_req
, acb
->qiov
->iov
, acb
->qiov
->niov
,
1617 create
, acb
->aiocb_type
);
1619 error_report("add_aio_request is failed\n");
1620 free_aio_req(s
, aio_req
);
1630 if (QLIST_EMPTY(&acb
->aioreq_head
)) {
1631 sd_finish_aiocb(acb
);
1635 static BlockDriverAIOCB
*sd_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
1636 QEMUIOVector
*qiov
, int nb_sectors
,
1637 BlockDriverCompletionFunc
*cb
,
1642 if (bs
->growable
&& sector_num
+ nb_sectors
> bs
->total_sectors
) {
1643 /* TODO: shouldn't block here */
1644 if (sd_truncate(bs
, (sector_num
+ nb_sectors
) * SECTOR_SIZE
) < 0) {
1647 bs
->total_sectors
= sector_num
+ nb_sectors
;
1650 acb
= sd_aio_setup(bs
, qiov
, sector_num
, nb_sectors
, cb
, opaque
);
1651 acb
->aio_done_func
= sd_write_done
;
1652 acb
->aiocb_type
= AIOCB_WRITE_UDATA
;
1654 sd_schedule_bh(sd_readv_writev_bh_cb
, acb
);
1655 return &acb
->common
;
1658 static BlockDriverAIOCB
*sd_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
1659 QEMUIOVector
*qiov
, int nb_sectors
,
1660 BlockDriverCompletionFunc
*cb
,
1666 acb
= sd_aio_setup(bs
, qiov
, sector_num
, nb_sectors
, cb
, opaque
);
1667 acb
->aiocb_type
= AIOCB_READ_UDATA
;
1668 acb
->aio_done_func
= sd_finish_aiocb
;
1671 * TODO: we can do better; we don't need to initialize
1674 for (i
= 0; i
< qiov
->niov
; i
++) {
1675 memset(qiov
->iov
[i
].iov_base
, 0, qiov
->iov
[i
].iov_len
);
1678 sd_schedule_bh(sd_readv_writev_bh_cb
, acb
);
1679 return &acb
->common
;
1682 static int sd_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
)
1684 BDRVSheepdogState
*s
= bs
->opaque
;
1687 SheepdogInode
*inode
;
1688 unsigned int datalen
;
1690 dprintf("sn_info: name %s id_str %s s: name %s vm_state_size %d "
1691 "is_snapshot %d\n", sn_info
->name
, sn_info
->id_str
,
1692 s
->name
, sn_info
->vm_state_size
, s
->is_snapshot
);
1694 if (s
->is_snapshot
) {
1695 error_report("You can't create a snapshot of a snapshot VDI, "
1696 "%s (%" PRIu32
").\n", s
->name
, s
->inode
.vdi_id
);
1701 dprintf("%s %s\n", sn_info
->name
, sn_info
->id_str
);
1703 s
->inode
.vm_state_size
= sn_info
->vm_state_size
;
1704 s
->inode
.vm_clock_nsec
= sn_info
->vm_clock_nsec
;
1705 strncpy(s
->inode
.tag
, sn_info
->name
, sizeof(s
->inode
.tag
));
1706 /* we don't need to update entire object */
1707 datalen
= SD_INODE_SIZE
- sizeof(s
->inode
.data_vdi_id
);
1709 /* refresh inode. */
1710 fd
= connect_to_sdog(s
->addr
, s
->port
);
1716 ret
= write_object(fd
, (char *)&s
->inode
, vid_to_vdi_oid(s
->inode
.vdi_id
),
1717 s
->inode
.nr_copies
, datalen
, 0, 0);
1719 error_report("failed to write snapshot's inode.\n");
1724 ret
= do_sd_create(s
->name
, s
->inode
.vdi_size
, s
->inode
.vdi_id
, &new_vid
, 1,
1727 error_report("failed to create inode for snapshot. %s\n",
1733 inode
= (SheepdogInode
*)qemu_malloc(datalen
);
1735 ret
= read_object(fd
, (char *)inode
, vid_to_vdi_oid(new_vid
),
1736 s
->inode
.nr_copies
, datalen
, 0);
1739 error_report("failed to read new inode info. %s\n", strerror(errno
));
1744 memcpy(&s
->inode
, inode
, datalen
);
1745 dprintf("s->inode: name %s snap_id %x oid %x\n",
1746 s
->inode
.name
, s
->inode
.snap_id
, s
->inode
.vdi_id
);
1753 static int sd_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
)
1755 BDRVSheepdogState
*s
= bs
->opaque
;
1756 BDRVSheepdogState
*old_s
;
1757 char vdi
[SD_MAX_VDI_LEN
], tag
[SD_MAX_VDI_TAG_LEN
];
1760 uint32_t snapid
= 0;
1761 int ret
= -ENOENT
, fd
;
1763 old_s
= qemu_malloc(sizeof(BDRVSheepdogState
));
1765 memcpy(old_s
, s
, sizeof(BDRVSheepdogState
));
1767 memset(vdi
, 0, sizeof(vdi
));
1768 strncpy(vdi
, s
->name
, sizeof(vdi
));
1770 memset(tag
, 0, sizeof(tag
));
1771 snapid
= strtoul(snapshot_id
, NULL
, 10);
1773 strncpy(tag
, s
->name
, sizeof(tag
));
1776 ret
= find_vdi_name(s
, vdi
, snapid
, tag
, &vid
, 1);
1778 error_report("Failed to find_vdi_name\n");
1783 fd
= connect_to_sdog(s
->addr
, s
->port
);
1785 error_report("failed to connect\n");
1789 buf
= qemu_malloc(SD_INODE_SIZE
);
1790 ret
= read_object(fd
, buf
, vid_to_vdi_oid(vid
), s
->inode
.nr_copies
,
1800 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
1802 if (!s
->inode
.vm_state_size
) {
1803 error_report("Invalid snapshot\n");
1815 /* recover bdrv_sd_state */
1816 memcpy(s
, old_s
, sizeof(BDRVSheepdogState
));
1820 error_report("failed to open. recover old bdrv_sd_state.\n");
1825 static int sd_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1827 /* FIXME: Delete specified snapshot id. */
1831 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
1832 #define BITS_PER_BYTE 8
1833 #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
1834 #define DECLARE_BITMAP(name,bits) \
1835 unsigned long name[BITS_TO_LONGS(bits)]
1837 #define BITS_PER_LONG (BITS_PER_BYTE * sizeof(long))
1839 static inline int test_bit(unsigned int nr
, const unsigned long *addr
)
1841 return ((1UL << (nr
% BITS_PER_LONG
)) &
1842 (((unsigned long *)addr
)[nr
/ BITS_PER_LONG
])) != 0;
1845 static int sd_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
)
1847 BDRVSheepdogState
*s
= bs
->opaque
;
1849 int fd
, nr
= 1024, ret
, max
= BITS_TO_LONGS(SD_NR_VDIS
) * sizeof(long);
1850 QEMUSnapshotInfo
*sn_tab
= NULL
;
1851 unsigned wlen
, rlen
;
1853 static SheepdogInode inode
;
1854 unsigned long *vdi_inuse
;
1855 unsigned int start_nr
;
1859 vdi_inuse
= qemu_malloc(max
);
1861 fd
= connect_to_sdog(s
->addr
, s
->port
);
1869 memset(&req
, 0, sizeof(req
));
1871 req
.opcode
= SD_OP_READ_VDIS
;
1872 req
.data_length
= max
;
1874 ret
= do_req(fd
, (SheepdogReq
*)&req
, vdi_inuse
, &wlen
, &rlen
);
1881 sn_tab
= qemu_mallocz(nr
* sizeof(*sn_tab
));
1883 /* calculate a vdi id with hash function */
1884 hval
= fnv_64a_buf(s
->name
, strlen(s
->name
), FNV1A_64_INIT
);
1885 start_nr
= hval
& (SD_NR_VDIS
- 1);
1887 fd
= connect_to_sdog(s
->addr
, s
->port
);
1889 error_report("failed to connect\n");
1893 for (vid
= start_nr
; found
< nr
; vid
= (vid
+ 1) % SD_NR_VDIS
) {
1894 if (!test_bit(vid
, vdi_inuse
)) {
1898 /* we don't need to read entire object */
1899 ret
= read_object(fd
, (char *)&inode
, vid_to_vdi_oid(vid
),
1900 0, SD_INODE_SIZE
- sizeof(inode
.data_vdi_id
), 0);
1906 if (!strcmp(inode
.name
, s
->name
) && is_snapshot(&inode
)) {
1907 sn_tab
[found
].date_sec
= inode
.snap_ctime
>> 32;
1908 sn_tab
[found
].date_nsec
= inode
.snap_ctime
& 0xffffffff;
1909 sn_tab
[found
].vm_state_size
= inode
.vm_state_size
;
1910 sn_tab
[found
].vm_clock_nsec
= inode
.vm_clock_nsec
;
1912 snprintf(sn_tab
[found
].id_str
, sizeof(sn_tab
[found
].id_str
), "%u",
1914 strncpy(sn_tab
[found
].name
, inode
.tag
,
1915 MIN(sizeof(sn_tab
[found
].name
), sizeof(inode
.tag
)));
1924 qemu_free(vdi_inuse
);
1929 static int do_load_save_vmstate(BDRVSheepdogState
*s
, uint8_t *data
,
1930 int64_t pos
, int size
, int load
)
1934 unsigned int data_len
;
1935 uint64_t vmstate_oid
;
1939 fd
= connect_to_sdog(s
->addr
, s
->port
);
1946 vdi_index
= pos
/ SD_DATA_OBJ_SIZE
;
1947 offset
= pos
% SD_DATA_OBJ_SIZE
;
1949 data_len
= MIN(size
, SD_DATA_OBJ_SIZE
);
1951 vmstate_oid
= vid_to_vmstate_oid(s
->inode
.vdi_id
, vdi_index
);
1953 create
= (offset
== 0);
1955 ret
= read_object(fd
, (char *)data
, vmstate_oid
,
1956 s
->inode
.nr_copies
, data_len
, offset
);
1958 ret
= write_object(fd
, (char *)data
, vmstate_oid
,
1959 s
->inode
.nr_copies
, data_len
, offset
, create
);
1963 error_report("failed to save vmstate %s\n", strerror(errno
));
1977 static int sd_save_vmstate(BlockDriverState
*bs
, const uint8_t *data
,
1978 int64_t pos
, int size
)
1980 BDRVSheepdogState
*s
= bs
->opaque
;
1982 return do_load_save_vmstate(s
, (uint8_t *)data
, pos
, size
, 0);
1985 static int sd_load_vmstate(BlockDriverState
*bs
, uint8_t *data
,
1986 int64_t pos
, int size
)
1988 BDRVSheepdogState
*s
= bs
->opaque
;
1990 return do_load_save_vmstate(s
, data
, pos
, size
, 1);
1994 static QEMUOptionParameter sd_create_options
[] = {
1996 .name
= BLOCK_OPT_SIZE
,
1998 .help
= "Virtual disk size"
2001 .name
= BLOCK_OPT_BACKING_FILE
,
2003 .help
= "File name of a base image"
2008 BlockDriver bdrv_sheepdog
= {
2009 .format_name
= "sheepdog",
2010 .protocol_name
= "sheepdog",
2011 .instance_size
= sizeof(BDRVSheepdogState
),
2012 .bdrv_file_open
= sd_open
,
2013 .bdrv_close
= sd_close
,
2014 .bdrv_create
= sd_create
,
2015 .bdrv_getlength
= sd_getlength
,
2016 .bdrv_truncate
= sd_truncate
,
2018 .bdrv_aio_readv
= sd_aio_readv
,
2019 .bdrv_aio_writev
= sd_aio_writev
,
2021 .bdrv_snapshot_create
= sd_snapshot_create
,
2022 .bdrv_snapshot_goto
= sd_snapshot_goto
,
2023 .bdrv_snapshot_delete
= sd_snapshot_delete
,
2024 .bdrv_snapshot_list
= sd_snapshot_list
,
2026 .bdrv_save_vmstate
= sd_save_vmstate
,
2027 .bdrv_load_vmstate
= sd_load_vmstate
,
2029 .create_options
= sd_create_options
,
2032 static void bdrv_sheepdog_init(void)
2034 bdrv_register(&bdrv_sheepdog
);
2036 block_init(bdrv_sheepdog_init
);