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"
16 #include "qemu-error.h"
17 #include "qemu_socket.h"
18 #include "block_int.h"
21 #define SD_PROTO_VER 0x01
23 #define SD_DEFAULT_ADDR "localhost"
24 #define SD_DEFAULT_PORT "7000"
26 #define SD_OP_CREATE_AND_WRITE_OBJ 0x01
27 #define SD_OP_READ_OBJ 0x02
28 #define SD_OP_WRITE_OBJ 0x03
30 #define SD_OP_NEW_VDI 0x11
31 #define SD_OP_LOCK_VDI 0x12
32 #define SD_OP_RELEASE_VDI 0x13
33 #define SD_OP_GET_VDI_INFO 0x14
34 #define SD_OP_READ_VDIS 0x15
36 #define SD_FLAG_CMD_WRITE 0x01
37 #define SD_FLAG_CMD_COW 0x02
39 #define SD_RES_SUCCESS 0x00 /* Success */
40 #define SD_RES_UNKNOWN 0x01 /* Unknown error */
41 #define SD_RES_NO_OBJ 0x02 /* No object found */
42 #define SD_RES_EIO 0x03 /* I/O error */
43 #define SD_RES_VDI_EXIST 0x04 /* Vdi exists already */
44 #define SD_RES_INVALID_PARMS 0x05 /* Invalid parameters */
45 #define SD_RES_SYSTEM_ERROR 0x06 /* System error */
46 #define SD_RES_VDI_LOCKED 0x07 /* Vdi is locked */
47 #define SD_RES_NO_VDI 0x08 /* No vdi found */
48 #define SD_RES_NO_BASE_VDI 0x09 /* No base vdi found */
49 #define SD_RES_VDI_READ 0x0A /* Cannot read requested vdi */
50 #define SD_RES_VDI_WRITE 0x0B /* Cannot write requested vdi */
51 #define SD_RES_BASE_VDI_READ 0x0C /* Cannot read base vdi */
52 #define SD_RES_BASE_VDI_WRITE 0x0D /* Cannot write base vdi */
53 #define SD_RES_NO_TAG 0x0E /* Requested tag is not found */
54 #define SD_RES_STARTUP 0x0F /* Sheepdog is on starting up */
55 #define SD_RES_VDI_NOT_LOCKED 0x10 /* Vdi is not locked */
56 #define SD_RES_SHUTDOWN 0x11 /* Sheepdog is shutting down */
57 #define SD_RES_NO_MEM 0x12 /* Cannot allocate memory */
58 #define SD_RES_FULL_VDI 0x13 /* we already have the maximum vdis */
59 #define SD_RES_VER_MISMATCH 0x14 /* Protocol version mismatch */
60 #define SD_RES_NO_SPACE 0x15 /* Server has no room for new objects */
61 #define SD_RES_WAIT_FOR_FORMAT 0x16 /* Waiting for a format operation */
62 #define SD_RES_WAIT_FOR_JOIN 0x17 /* Waiting for other nodes joining */
63 #define SD_RES_JOIN_FAILED 0x18 /* Target node had failed to join sheepdog */
68 * 0 - 19 (20 bits): data object space
69 * 20 - 31 (12 bits): reserved data object space
70 * 32 - 55 (24 bits): vdi object space
71 * 56 - 59 ( 4 bits): reserved vdi object space
72 * 60 - 63 ( 4 bits): object type identifier space
75 #define VDI_SPACE_SHIFT 32
76 #define VDI_BIT (UINT64_C(1) << 63)
77 #define VMSTATE_BIT (UINT64_C(1) << 62)
78 #define MAX_DATA_OBJS (UINT64_C(1) << 20)
79 #define MAX_CHILDREN 1024
80 #define SD_MAX_VDI_LEN 256
81 #define SD_MAX_VDI_TAG_LEN 256
82 #define SD_NR_VDIS (1U << 24)
83 #define SD_DATA_OBJ_SIZE (UINT64_C(1) << 22)
84 #define SD_MAX_VDI_SIZE (SD_DATA_OBJ_SIZE * MAX_DATA_OBJS)
85 #define SECTOR_SIZE 512
87 #define SD_INODE_SIZE (sizeof(SheepdogInode))
88 #define CURRENT_VDI_ID 0
90 typedef struct SheepdogReq
{
97 uint32_t opcode_specific
[8];
100 typedef struct SheepdogRsp
{
106 uint32_t data_length
;
108 uint32_t opcode_specific
[7];
111 typedef struct SheepdogObjReq
{
117 uint32_t data_length
;
125 typedef struct SheepdogObjRsp
{
131 uint32_t data_length
;
137 typedef struct SheepdogVdiReq
{
143 uint32_t data_length
;
145 uint32_t base_vdi_id
;
151 typedef struct SheepdogVdiRsp
{
157 uint32_t data_length
;
164 typedef struct SheepdogInode
{
165 char name
[SD_MAX_VDI_LEN
];
166 char tag
[SD_MAX_VDI_TAG_LEN
];
169 uint64_t vm_clock_nsec
;
171 uint64_t vm_state_size
;
172 uint16_t copy_policy
;
174 uint8_t block_size_shift
;
177 uint32_t parent_vdi_id
;
178 uint32_t child_vdi_id
[MAX_CHILDREN
];
179 uint32_t data_vdi_id
[MAX_DATA_OBJS
];
183 * 64 bit FNV-1a non-zero initial basis
185 #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
188 * 64 bit Fowler/Noll/Vo FNV-1a hash code
190 static inline uint64_t fnv_64a_buf(void *buf
, size_t len
, uint64_t hval
)
192 unsigned char *bp
= buf
;
193 unsigned char *be
= bp
+ len
;
195 hval
^= (uint64_t) *bp
++;
196 hval
+= (hval
<< 1) + (hval
<< 4) + (hval
<< 5) +
197 (hval
<< 7) + (hval
<< 8) + (hval
<< 40);
202 static inline int is_data_obj_writable(SheepdogInode
*inode
, unsigned int idx
)
204 return inode
->vdi_id
== inode
->data_vdi_id
[idx
];
207 static inline int is_data_obj(uint64_t oid
)
209 return !(VDI_BIT
& oid
);
212 static inline uint64_t data_oid_to_idx(uint64_t oid
)
214 return oid
& (MAX_DATA_OBJS
- 1);
217 static inline uint64_t vid_to_vdi_oid(uint32_t vid
)
219 return VDI_BIT
| ((uint64_t)vid
<< VDI_SPACE_SHIFT
);
222 static inline uint64_t vid_to_vmstate_oid(uint32_t vid
, uint32_t idx
)
224 return VMSTATE_BIT
| ((uint64_t)vid
<< VDI_SPACE_SHIFT
) | idx
;
227 static inline uint64_t vid_to_data_oid(uint32_t vid
, uint32_t idx
)
229 return ((uint64_t)vid
<< VDI_SPACE_SHIFT
) | idx
;
232 static inline int is_snapshot(struct SheepdogInode
*inode
)
234 return !!inode
->snap_ctime
;
239 #define dprintf(fmt, args...) \
241 fprintf(stdout, "%s %d: " fmt, __func__, __LINE__, ##args); \
244 #define dprintf(fmt, args...)
247 typedef struct SheepdogAIOCB SheepdogAIOCB
;
249 typedef struct AIOReq
{
250 SheepdogAIOCB
*aiocb
;
251 unsigned int iov_offset
;
256 unsigned int data_len
;
260 QLIST_ENTRY(AIOReq
) outstanding_aio_siblings
;
261 QLIST_ENTRY(AIOReq
) aioreq_siblings
;
269 struct SheepdogAIOCB
{
270 BlockDriverAIOCB common
;
278 enum AIOCBState aiocb_type
;
280 Coroutine
*coroutine
;
281 void (*aio_done_func
)(SheepdogAIOCB
*);
285 QLIST_HEAD(aioreq_head
, AIOReq
) aioreq_head
;
288 typedef struct BDRVSheepdogState
{
291 uint32_t min_dirty_data_idx
;
292 uint32_t max_dirty_data_idx
;
294 char name
[SD_MAX_VDI_LEN
];
305 uint32_t aioreq_seq_num
;
306 QLIST_HEAD(outstanding_aio_head
, AIOReq
) outstanding_aio_head
;
309 static const char * sd_strerror(int err
)
313 static const struct {
317 {SD_RES_SUCCESS
, "Success"},
318 {SD_RES_UNKNOWN
, "Unknown error"},
319 {SD_RES_NO_OBJ
, "No object found"},
320 {SD_RES_EIO
, "I/O error"},
321 {SD_RES_VDI_EXIST
, "VDI exists already"},
322 {SD_RES_INVALID_PARMS
, "Invalid parameters"},
323 {SD_RES_SYSTEM_ERROR
, "System error"},
324 {SD_RES_VDI_LOCKED
, "VDI is already locked"},
325 {SD_RES_NO_VDI
, "No vdi found"},
326 {SD_RES_NO_BASE_VDI
, "No base VDI found"},
327 {SD_RES_VDI_READ
, "Failed read the requested VDI"},
328 {SD_RES_VDI_WRITE
, "Failed to write the requested VDI"},
329 {SD_RES_BASE_VDI_READ
, "Failed to read the base VDI"},
330 {SD_RES_BASE_VDI_WRITE
, "Failed to write the base VDI"},
331 {SD_RES_NO_TAG
, "Failed to find the requested tag"},
332 {SD_RES_STARTUP
, "The system is still booting"},
333 {SD_RES_VDI_NOT_LOCKED
, "VDI isn't locked"},
334 {SD_RES_SHUTDOWN
, "The system is shutting down"},
335 {SD_RES_NO_MEM
, "Out of memory on the server"},
336 {SD_RES_FULL_VDI
, "We already have the maximum vdis"},
337 {SD_RES_VER_MISMATCH
, "Protocol version mismatch"},
338 {SD_RES_NO_SPACE
, "Server has no space for new objects"},
339 {SD_RES_WAIT_FOR_FORMAT
, "Sheepdog is waiting for a format operation"},
340 {SD_RES_WAIT_FOR_JOIN
, "Sheepdog is waiting for other nodes joining"},
341 {SD_RES_JOIN_FAILED
, "Target node had failed to join sheepdog"},
344 for (i
= 0; i
< ARRAY_SIZE(errors
); ++i
) {
345 if (errors
[i
].err
== err
) {
346 return errors
[i
].desc
;
350 return "Invalid error code";
354 * Sheepdog I/O handling:
356 * 1. In sd_co_rw_vector, we send the I/O requests to the server and
357 * link the requests to the outstanding_list in the
358 * BDRVSheepdogState. The function exits without waiting for
359 * receiving the response.
361 * 2. We receive the response in aio_read_response, the fd handler to
362 * the sheepdog connection. If metadata update is needed, we send
363 * the write request to the vdi object in sd_write_done, the write
364 * completion function. We switch back to sd_co_readv/writev after
365 * all the requests belonging to the AIOCB are finished.
368 static inline AIOReq
*alloc_aio_req(BDRVSheepdogState
*s
, SheepdogAIOCB
*acb
,
369 uint64_t oid
, unsigned int data_len
,
370 uint64_t offset
, uint8_t flags
,
371 uint64_t base_oid
, unsigned int iov_offset
)
375 aio_req
= g_malloc(sizeof(*aio_req
));
376 aio_req
->aiocb
= acb
;
377 aio_req
->iov_offset
= iov_offset
;
379 aio_req
->base_oid
= base_oid
;
380 aio_req
->offset
= offset
;
381 aio_req
->data_len
= data_len
;
382 aio_req
->flags
= flags
;
383 aio_req
->id
= s
->aioreq_seq_num
++;
385 QLIST_INSERT_HEAD(&s
->outstanding_aio_head
, aio_req
,
386 outstanding_aio_siblings
);
387 QLIST_INSERT_HEAD(&acb
->aioreq_head
, aio_req
, aioreq_siblings
);
392 static inline int free_aio_req(BDRVSheepdogState
*s
, AIOReq
*aio_req
)
394 SheepdogAIOCB
*acb
= aio_req
->aiocb
;
395 QLIST_REMOVE(aio_req
, outstanding_aio_siblings
);
396 QLIST_REMOVE(aio_req
, aioreq_siblings
);
399 return !QLIST_EMPTY(&acb
->aioreq_head
);
402 static void coroutine_fn
sd_finish_aiocb(SheepdogAIOCB
*acb
)
404 if (!acb
->canceled
) {
405 qemu_coroutine_enter(acb
->coroutine
, NULL
);
407 qemu_aio_release(acb
);
410 static void sd_aio_cancel(BlockDriverAIOCB
*blockacb
)
412 SheepdogAIOCB
*acb
= (SheepdogAIOCB
*)blockacb
;
415 * Sheepdog cannot cancel the requests which are already sent to
416 * the servers, so we just complete the request with -EIO here.
419 qemu_coroutine_enter(acb
->coroutine
, NULL
);
423 static AIOPool sd_aio_pool
= {
424 .aiocb_size
= sizeof(SheepdogAIOCB
),
425 .cancel
= sd_aio_cancel
,
428 static SheepdogAIOCB
*sd_aio_setup(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
429 int64_t sector_num
, int nb_sectors
,
430 BlockDriverCompletionFunc
*cb
, void *opaque
)
434 acb
= qemu_aio_get(&sd_aio_pool
, bs
, cb
, opaque
);
438 acb
->sector_num
= sector_num
;
439 acb
->nb_sectors
= nb_sectors
;
441 acb
->aio_done_func
= NULL
;
443 acb
->coroutine
= qemu_coroutine_self();
445 QLIST_INIT(&acb
->aioreq_head
);
449 static int connect_to_sdog(const char *addr
, const char *port
)
451 char hbuf
[NI_MAXHOST
], sbuf
[NI_MAXSERV
];
453 struct addrinfo hints
, *res
, *res0
;
456 addr
= SD_DEFAULT_ADDR
;
457 port
= SD_DEFAULT_PORT
;
460 memset(&hints
, 0, sizeof(hints
));
461 hints
.ai_socktype
= SOCK_STREAM
;
463 ret
= getaddrinfo(addr
, port
, &hints
, &res0
);
465 error_report("unable to get address info %s, %s",
466 addr
, strerror(errno
));
470 for (res
= res0
; res
; res
= res
->ai_next
) {
471 ret
= getnameinfo(res
->ai_addr
, res
->ai_addrlen
, hbuf
, sizeof(hbuf
),
472 sbuf
, sizeof(sbuf
), NI_NUMERICHOST
| NI_NUMERICSERV
);
477 fd
= socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
483 ret
= connect(fd
, res
->ai_addr
, res
->ai_addrlen
);
485 if (errno
== EINTR
) {
491 dprintf("connected to %s:%s\n", addr
, port
);
495 error_report("failed connect to %s:%s", addr
, port
);
501 static int send_req(int sockfd
, SheepdogReq
*hdr
, void *data
,
506 ret
= qemu_send_full(sockfd
, hdr
, sizeof(*hdr
), 0);
507 if (ret
< sizeof(*hdr
)) {
508 error_report("failed to send a req, %s", strerror(errno
));
511 ret
= qemu_send_full(sockfd
, data
, *wlen
, 0);
513 error_report("failed to send a req, %s", strerror(errno
));
519 static int do_req(int sockfd
, SheepdogReq
*hdr
, void *data
,
520 unsigned int *wlen
, unsigned int *rlen
)
524 socket_set_block(sockfd
);
525 ret
= send_req(sockfd
, hdr
, data
, wlen
);
530 ret
= qemu_recv_full(sockfd
, hdr
, sizeof(*hdr
), 0);
531 if (ret
< sizeof(*hdr
)) {
532 error_report("failed to get a rsp, %s", strerror(errno
));
536 if (*rlen
> hdr
->data_length
) {
537 *rlen
= hdr
->data_length
;
541 ret
= qemu_recv_full(sockfd
, data
, *rlen
, 0);
543 error_report("failed to get the data, %s", strerror(errno
));
549 socket_set_nonblock(sockfd
);
553 static int coroutine_fn
add_aio_request(BDRVSheepdogState
*s
, AIOReq
*aio_req
,
554 struct iovec
*iov
, int niov
, int create
,
555 enum AIOCBState aiocb_type
);
558 * This function searchs pending requests to the object `oid', and
561 static void coroutine_fn
send_pending_req(BDRVSheepdogState
*s
, uint64_t oid
, uint32_t id
)
563 AIOReq
*aio_req
, *next
;
567 QLIST_FOREACH_SAFE(aio_req
, &s
->outstanding_aio_head
,
568 outstanding_aio_siblings
, next
) {
569 if (id
== aio_req
->id
) {
572 if (aio_req
->oid
!= oid
) {
576 acb
= aio_req
->aiocb
;
577 ret
= add_aio_request(s
, aio_req
, acb
->qiov
->iov
,
578 acb
->qiov
->niov
, 0, acb
->aiocb_type
);
580 error_report("add_aio_request is failed");
581 free_aio_req(s
, aio_req
);
582 if (QLIST_EMPTY(&acb
->aioreq_head
)) {
583 sd_finish_aiocb(acb
);
590 * Receive responses of the I/O requests.
592 * This function is registered as a fd handler, and called from the
593 * main loop when s->fd is ready for reading responses.
595 static void coroutine_fn
aio_read_response(void *opaque
)
598 BDRVSheepdogState
*s
= opaque
;
601 AIOReq
*aio_req
= NULL
;
606 if (QLIST_EMPTY(&s
->outstanding_aio_head
)) {
611 ret
= qemu_co_recv(fd
, &rsp
, sizeof(rsp
));
613 error_report("failed to get the header, %s", strerror(errno
));
617 /* find the right aio_req from the outstanding_aio list */
618 QLIST_FOREACH(aio_req
, &s
->outstanding_aio_head
, outstanding_aio_siblings
) {
619 if (aio_req
->id
== rsp
.id
) {
624 error_report("cannot find aio_req %x", rsp
.id
);
628 acb
= aio_req
->aiocb
;
630 switch (acb
->aiocb_type
) {
631 case AIOCB_WRITE_UDATA
:
632 if (!is_data_obj(aio_req
->oid
)) {
635 idx
= data_oid_to_idx(aio_req
->oid
);
637 if (s
->inode
.data_vdi_id
[idx
] != s
->inode
.vdi_id
) {
639 * If the object is newly created one, we need to update
640 * the vdi object (metadata object). min_dirty_data_idx
641 * and max_dirty_data_idx are changed to include updated
642 * index between them.
644 s
->inode
.data_vdi_id
[idx
] = s
->inode
.vdi_id
;
645 s
->max_dirty_data_idx
= MAX(idx
, s
->max_dirty_data_idx
);
646 s
->min_dirty_data_idx
= MIN(idx
, s
->min_dirty_data_idx
);
649 * Some requests may be blocked because simultaneous
650 * create requests are not allowed, so we search the
651 * pending requests here.
653 send_pending_req(s
, vid_to_data_oid(s
->inode
.vdi_id
, idx
), rsp
.id
);
656 case AIOCB_READ_UDATA
:
657 ret
= qemu_co_recvv(fd
, acb
->qiov
->iov
, rsp
.data_length
,
658 aio_req
->iov_offset
);
660 error_report("failed to get the data, %s", strerror(errno
));
666 if (rsp
.result
!= SD_RES_SUCCESS
) {
668 error_report("%s", sd_strerror(rsp
.result
));
671 rest
= free_aio_req(s
, aio_req
);
674 * We've finished all requests which belong to the AIOCB, so
675 * we can switch back to sd_co_readv/writev now.
677 acb
->aio_done_func(acb
);
683 static void co_read_response(void *opaque
)
685 BDRVSheepdogState
*s
= opaque
;
688 s
->co_recv
= qemu_coroutine_create(aio_read_response
);
691 qemu_coroutine_enter(s
->co_recv
, opaque
);
694 static void co_write_request(void *opaque
)
696 BDRVSheepdogState
*s
= opaque
;
698 qemu_coroutine_enter(s
->co_send
, NULL
);
701 static int aio_flush_request(void *opaque
)
703 BDRVSheepdogState
*s
= opaque
;
705 return !QLIST_EMPTY(&s
->outstanding_aio_head
);
708 static int set_nodelay(int fd
)
713 ret
= setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&opt
, sizeof(opt
));
718 * Return a socket discriptor to read/write objects.
720 * We cannot use this discriptor for other operations because
721 * the block driver may be on waiting response from the server.
723 static int get_sheep_fd(BDRVSheepdogState
*s
)
727 fd
= connect_to_sdog(s
->addr
, s
->port
);
729 error_report("%s", strerror(errno
));
733 socket_set_nonblock(fd
);
735 ret
= set_nodelay(fd
);
737 error_report("%s", strerror(errno
));
742 qemu_aio_set_fd_handler(fd
, co_read_response
, NULL
, aio_flush_request
,
750 * filename must be one of the following formats:
752 * 2. [vdiname]:[snapid]
754 * 4. [hostname]:[port]:[vdiname]
755 * 5. [hostname]:[port]:[vdiname]:[snapid]
756 * 6. [hostname]:[port]:[vdiname]:[tag]
758 * You can boot from the snapshot images by specifying `snapid` or
761 * You can run VMs outside the Sheepdog cluster by specifying
762 * `hostname' and `port' (experimental).
764 static int parse_vdiname(BDRVSheepdogState
*s
, const char *filename
,
765 char *vdi
, uint32_t *snapid
, char *tag
)
770 p
= q
= g_strdup(filename
);
772 /* count the number of separators */
782 /* use the first two tokens as hostname and port number. */
796 strncpy(vdi
, p
, SD_MAX_VDI_LEN
);
798 p
= strchr(vdi
, ':');
801 *snapid
= strtoul(p
, NULL
, 10);
803 strncpy(tag
, p
, SD_MAX_VDI_TAG_LEN
);
806 *snapid
= CURRENT_VDI_ID
; /* search current vdi */
809 if (s
->addr
== NULL
) {
816 static int find_vdi_name(BDRVSheepdogState
*s
, char *filename
, uint32_t snapid
,
817 char *tag
, uint32_t *vid
, int for_snapshot
)
821 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
822 unsigned int wlen
, rlen
= 0;
823 char buf
[SD_MAX_VDI_LEN
+ SD_MAX_VDI_TAG_LEN
];
825 fd
= connect_to_sdog(s
->addr
, s
->port
);
830 memset(buf
, 0, sizeof(buf
));
831 strncpy(buf
, filename
, SD_MAX_VDI_LEN
);
832 strncpy(buf
+ SD_MAX_VDI_LEN
, tag
, SD_MAX_VDI_TAG_LEN
);
834 memset(&hdr
, 0, sizeof(hdr
));
836 hdr
.opcode
= SD_OP_GET_VDI_INFO
;
838 hdr
.opcode
= SD_OP_LOCK_VDI
;
840 wlen
= SD_MAX_VDI_LEN
+ SD_MAX_VDI_TAG_LEN
;
841 hdr
.proto_ver
= SD_PROTO_VER
;
842 hdr
.data_length
= wlen
;
844 hdr
.flags
= SD_FLAG_CMD_WRITE
;
846 ret
= do_req(fd
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
852 if (rsp
->result
!= SD_RES_SUCCESS
) {
853 error_report("cannot get vdi info, %s, %s %d %s",
854 sd_strerror(rsp
->result
), filename
, snapid
, tag
);
866 static int coroutine_fn
add_aio_request(BDRVSheepdogState
*s
, AIOReq
*aio_req
,
867 struct iovec
*iov
, int niov
, int create
,
868 enum AIOCBState aiocb_type
)
870 int nr_copies
= s
->inode
.nr_copies
;
874 uint64_t oid
= aio_req
->oid
;
875 unsigned int datalen
= aio_req
->data_len
;
876 uint64_t offset
= aio_req
->offset
;
877 uint8_t flags
= aio_req
->flags
;
878 uint64_t old_oid
= aio_req
->base_oid
;
884 memset(&hdr
, 0, sizeof(hdr
));
886 if (aiocb_type
== AIOCB_READ_UDATA
) {
888 hdr
.opcode
= SD_OP_READ_OBJ
;
892 hdr
.opcode
= SD_OP_CREATE_AND_WRITE_OBJ
;
893 hdr
.flags
= SD_FLAG_CMD_WRITE
| flags
;
896 hdr
.opcode
= SD_OP_WRITE_OBJ
;
897 hdr
.flags
= SD_FLAG_CMD_WRITE
| flags
;
901 hdr
.cow_oid
= old_oid
;
902 hdr
.copies
= s
->inode
.nr_copies
;
904 hdr
.data_length
= datalen
;
907 hdr
.id
= aio_req
->id
;
909 qemu_co_mutex_lock(&s
->lock
);
910 s
->co_send
= qemu_coroutine_self();
911 qemu_aio_set_fd_handler(s
->fd
, co_read_response
, co_write_request
,
912 aio_flush_request
, NULL
, s
);
913 socket_set_cork(s
->fd
, 1);
916 ret
= qemu_co_send(s
->fd
, &hdr
, sizeof(hdr
));
918 qemu_co_mutex_unlock(&s
->lock
);
919 error_report("failed to send a req, %s", strerror(errno
));
924 ret
= qemu_co_sendv(s
->fd
, iov
, wlen
, aio_req
->iov_offset
);
926 qemu_co_mutex_unlock(&s
->lock
);
927 error_report("failed to send a data, %s", strerror(errno
));
932 socket_set_cork(s
->fd
, 0);
933 qemu_aio_set_fd_handler(s
->fd
, co_read_response
, NULL
,
934 aio_flush_request
, NULL
, s
);
935 qemu_co_mutex_unlock(&s
->lock
);
940 static int read_write_object(int fd
, char *buf
, uint64_t oid
, int copies
,
941 unsigned int datalen
, uint64_t offset
,
942 int write
, int create
)
945 SheepdogObjRsp
*rsp
= (SheepdogObjRsp
*)&hdr
;
946 unsigned int wlen
, rlen
;
949 memset(&hdr
, 0, sizeof(hdr
));
954 hdr
.flags
= SD_FLAG_CMD_WRITE
;
956 hdr
.opcode
= SD_OP_CREATE_AND_WRITE_OBJ
;
958 hdr
.opcode
= SD_OP_WRITE_OBJ
;
963 hdr
.opcode
= SD_OP_READ_OBJ
;
966 hdr
.data_length
= datalen
;
970 ret
= do_req(fd
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
972 error_report("failed to send a request to the sheep");
976 switch (rsp
->result
) {
980 error_report("%s", sd_strerror(rsp
->result
));
985 static int read_object(int fd
, char *buf
, uint64_t oid
, int copies
,
986 unsigned int datalen
, uint64_t offset
)
988 return read_write_object(fd
, buf
, oid
, copies
, datalen
, offset
, 0, 0);
991 static int write_object(int fd
, char *buf
, uint64_t oid
, int copies
,
992 unsigned int datalen
, uint64_t offset
, int create
)
994 return read_write_object(fd
, buf
, oid
, copies
, datalen
, offset
, 1, create
);
997 static int sd_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1001 BDRVSheepdogState
*s
= bs
->opaque
;
1002 char vdi
[SD_MAX_VDI_LEN
], tag
[SD_MAX_VDI_TAG_LEN
];
1006 strstart(filename
, "sheepdog:", (const char **)&filename
);
1008 QLIST_INIT(&s
->outstanding_aio_head
);
1011 memset(vdi
, 0, sizeof(vdi
));
1012 memset(tag
, 0, sizeof(tag
));
1013 if (parse_vdiname(s
, filename
, vdi
, &snapid
, tag
) < 0) {
1016 s
->fd
= get_sheep_fd(s
);
1021 ret
= find_vdi_name(s
, vdi
, snapid
, tag
, &vid
, 0);
1027 dprintf("%" PRIx32
" snapshot inode was open.\n", vid
);
1031 fd
= connect_to_sdog(s
->addr
, s
->port
);
1033 error_report("failed to connect");
1037 buf
= g_malloc(SD_INODE_SIZE
);
1038 ret
= read_object(fd
, buf
, vid_to_vdi_oid(vid
), 0, SD_INODE_SIZE
, 0);
1046 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
1047 s
->min_dirty_data_idx
= UINT32_MAX
;
1048 s
->max_dirty_data_idx
= 0;
1050 bs
->total_sectors
= s
->inode
.vdi_size
/ SECTOR_SIZE
;
1051 strncpy(s
->name
, vdi
, sizeof(s
->name
));
1052 qemu_co_mutex_init(&s
->lock
);
1056 qemu_aio_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
, NULL
, NULL
);
1064 static int do_sd_create(char *filename
, int64_t vdi_size
,
1065 uint32_t base_vid
, uint32_t *vdi_id
, int snapshot
,
1066 const char *addr
, const char *port
)
1069 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1071 unsigned int wlen
, rlen
= 0;
1072 char buf
[SD_MAX_VDI_LEN
];
1074 fd
= connect_to_sdog(addr
, port
);
1079 memset(buf
, 0, sizeof(buf
));
1080 strncpy(buf
, filename
, SD_MAX_VDI_LEN
);
1082 memset(&hdr
, 0, sizeof(hdr
));
1083 hdr
.opcode
= SD_OP_NEW_VDI
;
1084 hdr
.base_vdi_id
= base_vid
;
1086 wlen
= SD_MAX_VDI_LEN
;
1088 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1089 hdr
.snapid
= snapshot
;
1091 hdr
.data_length
= wlen
;
1092 hdr
.vdi_size
= vdi_size
;
1094 ret
= do_req(fd
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1102 if (rsp
->result
!= SD_RES_SUCCESS
) {
1103 error_report("%s, %s", sd_strerror(rsp
->result
), filename
);
1108 *vdi_id
= rsp
->vdi_id
;
1114 static int sd_prealloc(const char *filename
)
1116 BlockDriverState
*bs
= NULL
;
1117 uint32_t idx
, max_idx
;
1119 void *buf
= g_malloc0(SD_DATA_OBJ_SIZE
);
1122 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDWR
);
1127 vdi_size
= bdrv_getlength(bs
);
1132 max_idx
= DIV_ROUND_UP(vdi_size
, SD_DATA_OBJ_SIZE
);
1134 for (idx
= 0; idx
< max_idx
; idx
++) {
1136 * The created image can be a cloned image, so we need to read
1137 * a data from the source image.
1139 ret
= bdrv_pread(bs
, idx
* SD_DATA_OBJ_SIZE
, buf
, SD_DATA_OBJ_SIZE
);
1143 ret
= bdrv_pwrite(bs
, idx
* SD_DATA_OBJ_SIZE
, buf
, SD_DATA_OBJ_SIZE
);
1157 static int sd_create(const char *filename
, QEMUOptionParameter
*options
)
1160 uint32_t vid
= 0, base_vid
= 0;
1161 int64_t vdi_size
= 0;
1162 char *backing_file
= NULL
;
1163 BDRVSheepdogState s
;
1164 char vdi
[SD_MAX_VDI_LEN
], tag
[SD_MAX_VDI_TAG_LEN
];
1167 const char *vdiname
;
1169 strstart(filename
, "sheepdog:", &vdiname
);
1171 memset(&s
, 0, sizeof(s
));
1172 memset(vdi
, 0, sizeof(vdi
));
1173 memset(tag
, 0, sizeof(tag
));
1174 if (parse_vdiname(&s
, vdiname
, vdi
, &snapid
, tag
) < 0) {
1175 error_report("invalid filename");
1179 while (options
&& options
->name
) {
1180 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
1181 vdi_size
= options
->value
.n
;
1182 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
1183 backing_file
= options
->value
.s
;
1184 } else if (!strcmp(options
->name
, BLOCK_OPT_PREALLOC
)) {
1185 if (!options
->value
.s
|| !strcmp(options
->value
.s
, "off")) {
1187 } else if (!strcmp(options
->value
.s
, "full")) {
1190 error_report("Invalid preallocation mode: '%s'",
1198 if (vdi_size
> SD_MAX_VDI_SIZE
) {
1199 error_report("too big image size");
1204 BlockDriverState
*bs
;
1205 BDRVSheepdogState
*s
;
1208 /* Currently, only Sheepdog backing image is supported. */
1209 drv
= bdrv_find_protocol(backing_file
);
1210 if (!drv
|| strcmp(drv
->protocol_name
, "sheepdog") != 0) {
1211 error_report("backing_file must be a sheepdog image");
1215 ret
= bdrv_file_open(&bs
, backing_file
, 0);
1221 if (!is_snapshot(&s
->inode
)) {
1222 error_report("cannot clone from a non snapshot vdi");
1227 base_vid
= s
->inode
.vdi_id
;
1231 ret
= do_sd_create(vdi
, vdi_size
, base_vid
, &vid
, 0, s
.addr
, s
.port
);
1232 if (!prealloc
|| ret
) {
1236 return sd_prealloc(filename
);
1239 static void sd_close(BlockDriverState
*bs
)
1241 BDRVSheepdogState
*s
= bs
->opaque
;
1243 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1244 unsigned int wlen
, rlen
= 0;
1247 dprintf("%s\n", s
->name
);
1249 fd
= connect_to_sdog(s
->addr
, s
->port
);
1254 memset(&hdr
, 0, sizeof(hdr
));
1256 hdr
.opcode
= SD_OP_RELEASE_VDI
;
1257 wlen
= strlen(s
->name
) + 1;
1258 hdr
.data_length
= wlen
;
1259 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1261 ret
= do_req(fd
, (SheepdogReq
*)&hdr
, s
->name
, &wlen
, &rlen
);
1265 if (!ret
&& rsp
->result
!= SD_RES_SUCCESS
&&
1266 rsp
->result
!= SD_RES_VDI_NOT_LOCKED
) {
1267 error_report("%s, %s", sd_strerror(rsp
->result
), s
->name
);
1270 qemu_aio_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
, NULL
, NULL
);
1275 static int64_t sd_getlength(BlockDriverState
*bs
)
1277 BDRVSheepdogState
*s
= bs
->opaque
;
1279 return s
->inode
.vdi_size
;
1282 static int sd_truncate(BlockDriverState
*bs
, int64_t offset
)
1284 BDRVSheepdogState
*s
= bs
->opaque
;
1286 unsigned int datalen
;
1288 if (offset
< s
->inode
.vdi_size
) {
1289 error_report("shrinking is not supported");
1291 } else if (offset
> SD_MAX_VDI_SIZE
) {
1292 error_report("too big image size");
1296 fd
= connect_to_sdog(s
->addr
, s
->port
);
1301 /* we don't need to update entire object */
1302 datalen
= SD_INODE_SIZE
- sizeof(s
->inode
.data_vdi_id
);
1303 s
->inode
.vdi_size
= offset
;
1304 ret
= write_object(fd
, (char *)&s
->inode
, vid_to_vdi_oid(s
->inode
.vdi_id
),
1305 s
->inode
.nr_copies
, datalen
, 0, 0);
1309 error_report("failed to update an inode.");
1317 * This function is called after writing data objects. If we need to
1318 * update metadata, this sends a write request to the vdi object.
1319 * Otherwise, this switches back to sd_co_readv/writev.
1321 static void coroutine_fn
sd_write_done(SheepdogAIOCB
*acb
)
1324 BDRVSheepdogState
*s
= acb
->common
.bs
->opaque
;
1327 uint32_t offset
, data_len
, mn
, mx
;
1329 mn
= s
->min_dirty_data_idx
;
1330 mx
= s
->max_dirty_data_idx
;
1332 /* we need to update the vdi object. */
1333 offset
= sizeof(s
->inode
) - sizeof(s
->inode
.data_vdi_id
) +
1334 mn
* sizeof(s
->inode
.data_vdi_id
[0]);
1335 data_len
= (mx
- mn
+ 1) * sizeof(s
->inode
.data_vdi_id
[0]);
1337 s
->min_dirty_data_idx
= UINT32_MAX
;
1338 s
->max_dirty_data_idx
= 0;
1340 iov
.iov_base
= &s
->inode
;
1341 iov
.iov_len
= sizeof(s
->inode
);
1342 aio_req
= alloc_aio_req(s
, acb
, vid_to_vdi_oid(s
->inode
.vdi_id
),
1343 data_len
, offset
, 0, 0, offset
);
1344 ret
= add_aio_request(s
, aio_req
, &iov
, 1, 0, AIOCB_WRITE_UDATA
);
1346 free_aio_req(s
, aio_req
);
1351 acb
->aio_done_func
= sd_finish_aiocb
;
1352 acb
->aiocb_type
= AIOCB_WRITE_UDATA
;
1356 sd_finish_aiocb(acb
);
1360 * Create a writable VDI from a snapshot
1362 static int sd_create_branch(BDRVSheepdogState
*s
)
1368 dprintf("%" PRIx32
" is snapshot.\n", s
->inode
.vdi_id
);
1370 buf
= g_malloc(SD_INODE_SIZE
);
1372 ret
= do_sd_create(s
->name
, s
->inode
.vdi_size
, s
->inode
.vdi_id
, &vid
, 1,
1378 dprintf("%" PRIx32
" is created.\n", vid
);
1380 fd
= connect_to_sdog(s
->addr
, s
->port
);
1382 error_report("failed to connect");
1386 ret
= read_object(fd
, buf
, vid_to_vdi_oid(vid
), s
->inode
.nr_copies
,
1395 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
1399 dprintf("%" PRIx32
" was newly created.\n", s
->inode
.vdi_id
);
1408 * Send I/O requests to the server.
1410 * This function sends requests to the server, links the requests to
1411 * the outstanding_list in BDRVSheepdogState, and exits without
1412 * waiting the response. The responses are received in the
1413 * `aio_read_response' function which is called from the main loop as
1416 * Returns 1 when we need to wait a response, 0 when there is no sent
1417 * request and -errno in error cases.
1419 static int coroutine_fn
sd_co_rw_vector(void *p
)
1421 SheepdogAIOCB
*acb
= p
;
1423 unsigned long len
, done
= 0, total
= acb
->nb_sectors
* SECTOR_SIZE
;
1424 unsigned long idx
= acb
->sector_num
* SECTOR_SIZE
/ SD_DATA_OBJ_SIZE
;
1426 uint64_t offset
= (acb
->sector_num
* SECTOR_SIZE
) % SD_DATA_OBJ_SIZE
;
1427 BDRVSheepdogState
*s
= acb
->common
.bs
->opaque
;
1428 SheepdogInode
*inode
= &s
->inode
;
1431 if (acb
->aiocb_type
== AIOCB_WRITE_UDATA
&& s
->is_snapshot
) {
1433 * In the case we open the snapshot VDI, Sheepdog creates the
1434 * writable VDI when we do a write operation first.
1436 ret
= sd_create_branch(s
);
1443 while (done
!= total
) {
1445 uint64_t old_oid
= 0;
1448 oid
= vid_to_data_oid(inode
->data_vdi_id
[idx
], idx
);
1450 len
= MIN(total
- done
, SD_DATA_OBJ_SIZE
- offset
);
1452 if (!inode
->data_vdi_id
[idx
]) {
1453 if (acb
->aiocb_type
== AIOCB_READ_UDATA
) {
1458 } else if (acb
->aiocb_type
== AIOCB_WRITE_UDATA
1459 && !is_data_obj_writable(inode
, idx
)) {
1463 flags
= SD_FLAG_CMD_COW
;
1467 dprintf("update ino (%" PRIu32
") %" PRIu64
" %" PRIu64
1468 " %" PRIu64
"\n", inode
->vdi_id
, oid
,
1469 vid_to_data_oid(inode
->data_vdi_id
[idx
], idx
), idx
);
1470 oid
= vid_to_data_oid(inode
->vdi_id
, idx
);
1471 dprintf("new oid %lx\n", oid
);
1474 aio_req
= alloc_aio_req(s
, acb
, oid
, len
, offset
, flags
, old_oid
, done
);
1478 QLIST_FOREACH(areq
, &s
->outstanding_aio_head
,
1479 outstanding_aio_siblings
) {
1480 if (areq
== aio_req
) {
1483 if (areq
->oid
== oid
) {
1485 * Sheepdog cannot handle simultaneous create
1486 * requests to the same object. So we cannot send
1487 * the request until the previous request
1491 aio_req
->base_oid
= 0;
1497 ret
= add_aio_request(s
, aio_req
, acb
->qiov
->iov
, acb
->qiov
->niov
,
1498 create
, acb
->aiocb_type
);
1500 error_report("add_aio_request is failed");
1501 free_aio_req(s
, aio_req
);
1511 if (QLIST_EMPTY(&acb
->aioreq_head
)) {
1517 static coroutine_fn
int sd_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
1518 int nb_sectors
, QEMUIOVector
*qiov
)
1523 if (bs
->growable
&& sector_num
+ nb_sectors
> bs
->total_sectors
) {
1524 /* TODO: shouldn't block here */
1525 if (sd_truncate(bs
, (sector_num
+ nb_sectors
) * SECTOR_SIZE
) < 0) {
1528 bs
->total_sectors
= sector_num
+ nb_sectors
;
1531 acb
= sd_aio_setup(bs
, qiov
, sector_num
, nb_sectors
, NULL
, NULL
);
1532 acb
->aio_done_func
= sd_write_done
;
1533 acb
->aiocb_type
= AIOCB_WRITE_UDATA
;
1535 ret
= sd_co_rw_vector(acb
);
1537 qemu_aio_release(acb
);
1541 qemu_coroutine_yield();
1546 static coroutine_fn
int sd_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
1547 int nb_sectors
, QEMUIOVector
*qiov
)
1552 acb
= sd_aio_setup(bs
, qiov
, sector_num
, nb_sectors
, NULL
, NULL
);
1553 acb
->aiocb_type
= AIOCB_READ_UDATA
;
1554 acb
->aio_done_func
= sd_finish_aiocb
;
1557 * TODO: we can do better; we don't need to initialize
1560 for (i
= 0; i
< qiov
->niov
; i
++) {
1561 memset(qiov
->iov
[i
].iov_base
, 0, qiov
->iov
[i
].iov_len
);
1564 ret
= sd_co_rw_vector(acb
);
1566 qemu_aio_release(acb
);
1570 qemu_coroutine_yield();
1575 static int sd_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
)
1577 BDRVSheepdogState
*s
= bs
->opaque
;
1580 SheepdogInode
*inode
;
1581 unsigned int datalen
;
1583 dprintf("sn_info: name %s id_str %s s: name %s vm_state_size %d "
1584 "is_snapshot %d\n", sn_info
->name
, sn_info
->id_str
,
1585 s
->name
, sn_info
->vm_state_size
, s
->is_snapshot
);
1587 if (s
->is_snapshot
) {
1588 error_report("You can't create a snapshot of a snapshot VDI, "
1589 "%s (%" PRIu32
").", s
->name
, s
->inode
.vdi_id
);
1594 dprintf("%s %s\n", sn_info
->name
, sn_info
->id_str
);
1596 s
->inode
.vm_state_size
= sn_info
->vm_state_size
;
1597 s
->inode
.vm_clock_nsec
= sn_info
->vm_clock_nsec
;
1598 strncpy(s
->inode
.tag
, sn_info
->name
, sizeof(s
->inode
.tag
));
1599 /* we don't need to update entire object */
1600 datalen
= SD_INODE_SIZE
- sizeof(s
->inode
.data_vdi_id
);
1602 /* refresh inode. */
1603 fd
= connect_to_sdog(s
->addr
, s
->port
);
1609 ret
= write_object(fd
, (char *)&s
->inode
, vid_to_vdi_oid(s
->inode
.vdi_id
),
1610 s
->inode
.nr_copies
, datalen
, 0, 0);
1612 error_report("failed to write snapshot's inode.");
1617 ret
= do_sd_create(s
->name
, s
->inode
.vdi_size
, s
->inode
.vdi_id
, &new_vid
, 1,
1620 error_report("failed to create inode for snapshot. %s",
1626 inode
= (SheepdogInode
*)g_malloc(datalen
);
1628 ret
= read_object(fd
, (char *)inode
, vid_to_vdi_oid(new_vid
),
1629 s
->inode
.nr_copies
, datalen
, 0);
1632 error_report("failed to read new inode info. %s", strerror(errno
));
1637 memcpy(&s
->inode
, inode
, datalen
);
1638 dprintf("s->inode: name %s snap_id %x oid %x\n",
1639 s
->inode
.name
, s
->inode
.snap_id
, s
->inode
.vdi_id
);
1646 static int sd_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
)
1648 BDRVSheepdogState
*s
= bs
->opaque
;
1649 BDRVSheepdogState
*old_s
;
1650 char vdi
[SD_MAX_VDI_LEN
], tag
[SD_MAX_VDI_TAG_LEN
];
1653 uint32_t snapid
= 0;
1654 int ret
= -ENOENT
, fd
;
1656 old_s
= g_malloc(sizeof(BDRVSheepdogState
));
1658 memcpy(old_s
, s
, sizeof(BDRVSheepdogState
));
1660 memset(vdi
, 0, sizeof(vdi
));
1661 strncpy(vdi
, s
->name
, sizeof(vdi
));
1663 memset(tag
, 0, sizeof(tag
));
1664 snapid
= strtoul(snapshot_id
, NULL
, 10);
1666 strncpy(tag
, s
->name
, sizeof(tag
));
1669 ret
= find_vdi_name(s
, vdi
, snapid
, tag
, &vid
, 1);
1671 error_report("Failed to find_vdi_name");
1676 fd
= connect_to_sdog(s
->addr
, s
->port
);
1678 error_report("failed to connect");
1682 buf
= g_malloc(SD_INODE_SIZE
);
1683 ret
= read_object(fd
, buf
, vid_to_vdi_oid(vid
), s
->inode
.nr_copies
,
1693 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
1695 if (!s
->inode
.vm_state_size
) {
1696 error_report("Invalid snapshot");
1708 /* recover bdrv_sd_state */
1709 memcpy(s
, old_s
, sizeof(BDRVSheepdogState
));
1713 error_report("failed to open. recover old bdrv_sd_state.");
1718 static int sd_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1720 /* FIXME: Delete specified snapshot id. */
1724 static int sd_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
)
1726 BDRVSheepdogState
*s
= bs
->opaque
;
1728 int fd
, nr
= 1024, ret
, max
= BITS_TO_LONGS(SD_NR_VDIS
) * sizeof(long);
1729 QEMUSnapshotInfo
*sn_tab
= NULL
;
1730 unsigned wlen
, rlen
;
1732 static SheepdogInode inode
;
1733 unsigned long *vdi_inuse
;
1734 unsigned int start_nr
;
1738 vdi_inuse
= g_malloc(max
);
1740 fd
= connect_to_sdog(s
->addr
, s
->port
);
1748 memset(&req
, 0, sizeof(req
));
1750 req
.opcode
= SD_OP_READ_VDIS
;
1751 req
.data_length
= max
;
1753 ret
= do_req(fd
, (SheepdogReq
*)&req
, vdi_inuse
, &wlen
, &rlen
);
1760 sn_tab
= g_malloc0(nr
* sizeof(*sn_tab
));
1762 /* calculate a vdi id with hash function */
1763 hval
= fnv_64a_buf(s
->name
, strlen(s
->name
), FNV1A_64_INIT
);
1764 start_nr
= hval
& (SD_NR_VDIS
- 1);
1766 fd
= connect_to_sdog(s
->addr
, s
->port
);
1768 error_report("failed to connect");
1772 for (vid
= start_nr
; found
< nr
; vid
= (vid
+ 1) % SD_NR_VDIS
) {
1773 if (!test_bit(vid
, vdi_inuse
)) {
1777 /* we don't need to read entire object */
1778 ret
= read_object(fd
, (char *)&inode
, vid_to_vdi_oid(vid
),
1779 0, SD_INODE_SIZE
- sizeof(inode
.data_vdi_id
), 0);
1785 if (!strcmp(inode
.name
, s
->name
) && is_snapshot(&inode
)) {
1786 sn_tab
[found
].date_sec
= inode
.snap_ctime
>> 32;
1787 sn_tab
[found
].date_nsec
= inode
.snap_ctime
& 0xffffffff;
1788 sn_tab
[found
].vm_state_size
= inode
.vm_state_size
;
1789 sn_tab
[found
].vm_clock_nsec
= inode
.vm_clock_nsec
;
1791 snprintf(sn_tab
[found
].id_str
, sizeof(sn_tab
[found
].id_str
), "%u",
1793 strncpy(sn_tab
[found
].name
, inode
.tag
,
1794 MIN(sizeof(sn_tab
[found
].name
), sizeof(inode
.tag
)));
1808 static int do_load_save_vmstate(BDRVSheepdogState
*s
, uint8_t *data
,
1809 int64_t pos
, int size
, int load
)
1813 unsigned int data_len
;
1814 uint64_t vmstate_oid
;
1818 fd
= connect_to_sdog(s
->addr
, s
->port
);
1825 vdi_index
= pos
/ SD_DATA_OBJ_SIZE
;
1826 offset
= pos
% SD_DATA_OBJ_SIZE
;
1828 data_len
= MIN(size
, SD_DATA_OBJ_SIZE
);
1830 vmstate_oid
= vid_to_vmstate_oid(s
->inode
.vdi_id
, vdi_index
);
1832 create
= (offset
== 0);
1834 ret
= read_object(fd
, (char *)data
, vmstate_oid
,
1835 s
->inode
.nr_copies
, data_len
, offset
);
1837 ret
= write_object(fd
, (char *)data
, vmstate_oid
,
1838 s
->inode
.nr_copies
, data_len
, offset
, create
);
1842 error_report("failed to save vmstate %s", strerror(errno
));
1856 static int sd_save_vmstate(BlockDriverState
*bs
, const uint8_t *data
,
1857 int64_t pos
, int size
)
1859 BDRVSheepdogState
*s
= bs
->opaque
;
1861 return do_load_save_vmstate(s
, (uint8_t *)data
, pos
, size
, 0);
1864 static int sd_load_vmstate(BlockDriverState
*bs
, uint8_t *data
,
1865 int64_t pos
, int size
)
1867 BDRVSheepdogState
*s
= bs
->opaque
;
1869 return do_load_save_vmstate(s
, data
, pos
, size
, 1);
1873 static QEMUOptionParameter sd_create_options
[] = {
1875 .name
= BLOCK_OPT_SIZE
,
1877 .help
= "Virtual disk size"
1880 .name
= BLOCK_OPT_BACKING_FILE
,
1882 .help
= "File name of a base image"
1885 .name
= BLOCK_OPT_PREALLOC
,
1887 .help
= "Preallocation mode (allowed values: off, full)"
1892 BlockDriver bdrv_sheepdog
= {
1893 .format_name
= "sheepdog",
1894 .protocol_name
= "sheepdog",
1895 .instance_size
= sizeof(BDRVSheepdogState
),
1896 .bdrv_file_open
= sd_open
,
1897 .bdrv_close
= sd_close
,
1898 .bdrv_create
= sd_create
,
1899 .bdrv_getlength
= sd_getlength
,
1900 .bdrv_truncate
= sd_truncate
,
1902 .bdrv_co_readv
= sd_co_readv
,
1903 .bdrv_co_writev
= sd_co_writev
,
1905 .bdrv_snapshot_create
= sd_snapshot_create
,
1906 .bdrv_snapshot_goto
= sd_snapshot_goto
,
1907 .bdrv_snapshot_delete
= sd_snapshot_delete
,
1908 .bdrv_snapshot_list
= sd_snapshot_list
,
1910 .bdrv_save_vmstate
= sd_save_vmstate
,
1911 .bdrv_load_vmstate
= sd_load_vmstate
,
1913 .create_options
= sd_create_options
,
1916 static void bdrv_sheepdog_init(void)
1918 bdrv_register(&bdrv_sheepdog
);
1920 block_init(bdrv_sheepdog_init
);