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-report.h"
17 #include "qemu/sockets.h"
18 #include "block/block_int.h"
19 #include "qemu/bitops.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
35 #define SD_OP_FLUSH_VDI 0x16
37 #define SD_FLAG_CMD_WRITE 0x01
38 #define SD_FLAG_CMD_COW 0x02
39 #define SD_FLAG_CMD_CACHE 0x04 /* Writeback mode for cache */
40 #define SD_FLAG_CMD_DIRECT 0x08 /* Don't use cache */
42 #define SD_RES_SUCCESS 0x00 /* Success */
43 #define SD_RES_UNKNOWN 0x01 /* Unknown error */
44 #define SD_RES_NO_OBJ 0x02 /* No object found */
45 #define SD_RES_EIO 0x03 /* I/O error */
46 #define SD_RES_VDI_EXIST 0x04 /* Vdi exists already */
47 #define SD_RES_INVALID_PARMS 0x05 /* Invalid parameters */
48 #define SD_RES_SYSTEM_ERROR 0x06 /* System error */
49 #define SD_RES_VDI_LOCKED 0x07 /* Vdi is locked */
50 #define SD_RES_NO_VDI 0x08 /* No vdi found */
51 #define SD_RES_NO_BASE_VDI 0x09 /* No base vdi found */
52 #define SD_RES_VDI_READ 0x0A /* Cannot read requested vdi */
53 #define SD_RES_VDI_WRITE 0x0B /* Cannot write requested vdi */
54 #define SD_RES_BASE_VDI_READ 0x0C /* Cannot read base vdi */
55 #define SD_RES_BASE_VDI_WRITE 0x0D /* Cannot write base vdi */
56 #define SD_RES_NO_TAG 0x0E /* Requested tag is not found */
57 #define SD_RES_STARTUP 0x0F /* Sheepdog is on starting up */
58 #define SD_RES_VDI_NOT_LOCKED 0x10 /* Vdi is not locked */
59 #define SD_RES_SHUTDOWN 0x11 /* Sheepdog is shutting down */
60 #define SD_RES_NO_MEM 0x12 /* Cannot allocate memory */
61 #define SD_RES_FULL_VDI 0x13 /* we already have the maximum vdis */
62 #define SD_RES_VER_MISMATCH 0x14 /* Protocol version mismatch */
63 #define SD_RES_NO_SPACE 0x15 /* Server has no room for new objects */
64 #define SD_RES_WAIT_FOR_FORMAT 0x16 /* Waiting for a format operation */
65 #define SD_RES_WAIT_FOR_JOIN 0x17 /* Waiting for other nodes joining */
66 #define SD_RES_JOIN_FAILED 0x18 /* Target node had failed to join sheepdog */
71 * 0 - 19 (20 bits): data object space
72 * 20 - 31 (12 bits): reserved data object space
73 * 32 - 55 (24 bits): vdi object space
74 * 56 - 59 ( 4 bits): reserved vdi object space
75 * 60 - 63 ( 4 bits): object type identifier space
78 #define VDI_SPACE_SHIFT 32
79 #define VDI_BIT (UINT64_C(1) << 63)
80 #define VMSTATE_BIT (UINT64_C(1) << 62)
81 #define MAX_DATA_OBJS (UINT64_C(1) << 20)
82 #define MAX_CHILDREN 1024
83 #define SD_MAX_VDI_LEN 256
84 #define SD_MAX_VDI_TAG_LEN 256
85 #define SD_NR_VDIS (1U << 24)
86 #define SD_DATA_OBJ_SIZE (UINT64_C(1) << 22)
87 #define SD_MAX_VDI_SIZE (SD_DATA_OBJ_SIZE * MAX_DATA_OBJS)
88 #define SECTOR_SIZE 512
90 #define SD_INODE_SIZE (sizeof(SheepdogInode))
91 #define CURRENT_VDI_ID 0
93 typedef struct SheepdogReq
{
100 uint32_t opcode_specific
[8];
103 typedef struct SheepdogRsp
{
109 uint32_t data_length
;
111 uint32_t opcode_specific
[7];
114 typedef struct SheepdogObjReq
{
120 uint32_t data_length
;
128 typedef struct SheepdogObjRsp
{
134 uint32_t data_length
;
140 typedef struct SheepdogVdiReq
{
146 uint32_t data_length
;
154 typedef struct SheepdogVdiRsp
{
160 uint32_t data_length
;
167 typedef struct SheepdogInode
{
168 char name
[SD_MAX_VDI_LEN
];
169 char tag
[SD_MAX_VDI_TAG_LEN
];
172 uint64_t vm_clock_nsec
;
174 uint64_t vm_state_size
;
175 uint16_t copy_policy
;
177 uint8_t block_size_shift
;
180 uint32_t parent_vdi_id
;
181 uint32_t child_vdi_id
[MAX_CHILDREN
];
182 uint32_t data_vdi_id
[MAX_DATA_OBJS
];
186 * 64 bit FNV-1a non-zero initial basis
188 #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
191 * 64 bit Fowler/Noll/Vo FNV-1a hash code
193 static inline uint64_t fnv_64a_buf(void *buf
, size_t len
, uint64_t hval
)
195 unsigned char *bp
= buf
;
196 unsigned char *be
= bp
+ len
;
198 hval
^= (uint64_t) *bp
++;
199 hval
+= (hval
<< 1) + (hval
<< 4) + (hval
<< 5) +
200 (hval
<< 7) + (hval
<< 8) + (hval
<< 40);
205 static inline bool is_data_obj_writable(SheepdogInode
*inode
, unsigned int idx
)
207 return inode
->vdi_id
== inode
->data_vdi_id
[idx
];
210 static inline bool is_data_obj(uint64_t oid
)
212 return !(VDI_BIT
& oid
);
215 static inline uint64_t data_oid_to_idx(uint64_t oid
)
217 return oid
& (MAX_DATA_OBJS
- 1);
220 static inline uint64_t vid_to_vdi_oid(uint32_t vid
)
222 return VDI_BIT
| ((uint64_t)vid
<< VDI_SPACE_SHIFT
);
225 static inline uint64_t vid_to_vmstate_oid(uint32_t vid
, uint32_t idx
)
227 return VMSTATE_BIT
| ((uint64_t)vid
<< VDI_SPACE_SHIFT
) | idx
;
230 static inline uint64_t vid_to_data_oid(uint32_t vid
, uint32_t idx
)
232 return ((uint64_t)vid
<< VDI_SPACE_SHIFT
) | idx
;
235 static inline bool is_snapshot(struct SheepdogInode
*inode
)
237 return !!inode
->snap_ctime
;
242 #define dprintf(fmt, args...) \
244 fprintf(stdout, "%s %d: " fmt, __func__, __LINE__, ##args); \
247 #define dprintf(fmt, args...)
250 typedef struct SheepdogAIOCB SheepdogAIOCB
;
252 typedef struct AIOReq
{
253 SheepdogAIOCB
*aiocb
;
254 unsigned int iov_offset
;
259 unsigned int data_len
;
263 QLIST_ENTRY(AIOReq
) aio_siblings
;
272 struct SheepdogAIOCB
{
273 BlockDriverAIOCB common
;
281 enum AIOCBState aiocb_type
;
283 Coroutine
*coroutine
;
284 void (*aio_done_func
)(SheepdogAIOCB
*);
290 typedef struct BDRVSheepdogState
{
293 uint32_t min_dirty_data_idx
;
294 uint32_t max_dirty_data_idx
;
296 char name
[SD_MAX_VDI_LEN
];
298 uint32_t cache_flags
;
308 uint32_t aioreq_seq_num
;
309 QLIST_HEAD(inflight_aio_head
, AIOReq
) inflight_aio_head
;
310 QLIST_HEAD(pending_aio_head
, AIOReq
) pending_aio_head
;
313 static const char * sd_strerror(int err
)
317 static const struct {
321 {SD_RES_SUCCESS
, "Success"},
322 {SD_RES_UNKNOWN
, "Unknown error"},
323 {SD_RES_NO_OBJ
, "No object found"},
324 {SD_RES_EIO
, "I/O error"},
325 {SD_RES_VDI_EXIST
, "VDI exists already"},
326 {SD_RES_INVALID_PARMS
, "Invalid parameters"},
327 {SD_RES_SYSTEM_ERROR
, "System error"},
328 {SD_RES_VDI_LOCKED
, "VDI is already locked"},
329 {SD_RES_NO_VDI
, "No vdi found"},
330 {SD_RES_NO_BASE_VDI
, "No base VDI found"},
331 {SD_RES_VDI_READ
, "Failed read the requested VDI"},
332 {SD_RES_VDI_WRITE
, "Failed to write the requested VDI"},
333 {SD_RES_BASE_VDI_READ
, "Failed to read the base VDI"},
334 {SD_RES_BASE_VDI_WRITE
, "Failed to write the base VDI"},
335 {SD_RES_NO_TAG
, "Failed to find the requested tag"},
336 {SD_RES_STARTUP
, "The system is still booting"},
337 {SD_RES_VDI_NOT_LOCKED
, "VDI isn't locked"},
338 {SD_RES_SHUTDOWN
, "The system is shutting down"},
339 {SD_RES_NO_MEM
, "Out of memory on the server"},
340 {SD_RES_FULL_VDI
, "We already have the maximum vdis"},
341 {SD_RES_VER_MISMATCH
, "Protocol version mismatch"},
342 {SD_RES_NO_SPACE
, "Server has no space for new objects"},
343 {SD_RES_WAIT_FOR_FORMAT
, "Sheepdog is waiting for a format operation"},
344 {SD_RES_WAIT_FOR_JOIN
, "Sheepdog is waiting for other nodes joining"},
345 {SD_RES_JOIN_FAILED
, "Target node had failed to join sheepdog"},
348 for (i
= 0; i
< ARRAY_SIZE(errors
); ++i
) {
349 if (errors
[i
].err
== err
) {
350 return errors
[i
].desc
;
354 return "Invalid error code";
358 * Sheepdog I/O handling:
360 * 1. In sd_co_rw_vector, we send the I/O requests to the server and
361 * link the requests to the inflight_list in the
362 * BDRVSheepdogState. The function exits without waiting for
363 * receiving the response.
365 * 2. We receive the response in aio_read_response, the fd handler to
366 * the sheepdog connection. If metadata update is needed, we send
367 * the write request to the vdi object in sd_write_done, the write
368 * completion function. We switch back to sd_co_readv/writev after
369 * all the requests belonging to the AIOCB are finished.
372 static inline AIOReq
*alloc_aio_req(BDRVSheepdogState
*s
, SheepdogAIOCB
*acb
,
373 uint64_t oid
, unsigned int data_len
,
374 uint64_t offset
, uint8_t flags
,
375 uint64_t base_oid
, unsigned int iov_offset
)
379 aio_req
= g_malloc(sizeof(*aio_req
));
380 aio_req
->aiocb
= acb
;
381 aio_req
->iov_offset
= iov_offset
;
383 aio_req
->base_oid
= base_oid
;
384 aio_req
->offset
= offset
;
385 aio_req
->data_len
= data_len
;
386 aio_req
->flags
= flags
;
387 aio_req
->id
= s
->aioreq_seq_num
++;
393 static inline void free_aio_req(BDRVSheepdogState
*s
, AIOReq
*aio_req
)
395 SheepdogAIOCB
*acb
= aio_req
->aiocb
;
397 QLIST_REMOVE(aio_req
, aio_siblings
);
403 static void coroutine_fn
sd_finish_aiocb(SheepdogAIOCB
*acb
)
405 if (!acb
->canceled
) {
406 qemu_coroutine_enter(acb
->coroutine
, NULL
);
408 qemu_aio_release(acb
);
411 static void sd_aio_cancel(BlockDriverAIOCB
*blockacb
)
413 SheepdogAIOCB
*acb
= (SheepdogAIOCB
*)blockacb
;
416 * Sheepdog cannot cancel the requests which are already sent to
417 * the servers, so we just complete the request with -EIO here.
420 qemu_coroutine_enter(acb
->coroutine
, NULL
);
421 acb
->canceled
= true;
424 static const AIOCBInfo sd_aiocb_info
= {
425 .aiocb_size
= sizeof(SheepdogAIOCB
),
426 .cancel
= sd_aio_cancel
,
429 static SheepdogAIOCB
*sd_aio_setup(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
430 int64_t sector_num
, int nb_sectors
)
434 acb
= qemu_aio_get(&sd_aiocb_info
, bs
, NULL
, NULL
);
438 acb
->sector_num
= sector_num
;
439 acb
->nb_sectors
= nb_sectors
;
441 acb
->aio_done_func
= NULL
;
442 acb
->canceled
= false;
443 acb
->coroutine
= qemu_coroutine_self();
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
) {
492 dprintf("connected to %s:%s\n", addr
, port
);
496 error_report("failed connect to %s:%s", addr
, port
);
502 static coroutine_fn
int send_co_req(int sockfd
, SheepdogReq
*hdr
, void *data
,
507 ret
= qemu_co_send(sockfd
, hdr
, sizeof(*hdr
));
508 if (ret
< sizeof(*hdr
)) {
509 error_report("failed to send a req, %s", strerror(errno
));
513 ret
= qemu_co_send(sockfd
, data
, *wlen
);
515 error_report("failed to send a req, %s", strerror(errno
));
521 static void restart_co_req(void *opaque
)
523 Coroutine
*co
= opaque
;
525 qemu_coroutine_enter(co
, NULL
);
528 typedef struct SheepdogReqCo
{
538 static coroutine_fn
void do_co_req(void *opaque
)
542 SheepdogReqCo
*srco
= opaque
;
543 int sockfd
= srco
->sockfd
;
544 SheepdogReq
*hdr
= srco
->hdr
;
545 void *data
= srco
->data
;
546 unsigned int *wlen
= srco
->wlen
;
547 unsigned int *rlen
= srco
->rlen
;
549 co
= qemu_coroutine_self();
550 qemu_aio_set_fd_handler(sockfd
, NULL
, restart_co_req
, NULL
, co
);
552 socket_set_block(sockfd
);
553 ret
= send_co_req(sockfd
, hdr
, data
, wlen
);
558 qemu_aio_set_fd_handler(sockfd
, restart_co_req
, NULL
, NULL
, co
);
560 ret
= qemu_co_recv(sockfd
, hdr
, sizeof(*hdr
));
561 if (ret
< sizeof(*hdr
)) {
562 error_report("failed to get a rsp, %s", strerror(errno
));
567 if (*rlen
> hdr
->data_length
) {
568 *rlen
= hdr
->data_length
;
572 ret
= qemu_co_recv(sockfd
, data
, *rlen
);
574 error_report("failed to get the data, %s", strerror(errno
));
581 qemu_aio_set_fd_handler(sockfd
, NULL
, NULL
, NULL
, NULL
);
582 socket_set_nonblock(sockfd
);
585 srco
->finished
= true;
588 static int do_req(int sockfd
, SheepdogReq
*hdr
, void *data
,
589 unsigned int *wlen
, unsigned int *rlen
)
592 SheepdogReqCo srco
= {
602 if (qemu_in_coroutine()) {
605 co
= qemu_coroutine_create(do_co_req
);
606 qemu_coroutine_enter(co
, &srco
);
607 while (!srco
.finished
) {
615 static int coroutine_fn
add_aio_request(BDRVSheepdogState
*s
, AIOReq
*aio_req
,
616 struct iovec
*iov
, int niov
, bool create
,
617 enum AIOCBState aiocb_type
);
620 static AIOReq
*find_pending_req(BDRVSheepdogState
*s
, uint64_t oid
)
624 QLIST_FOREACH(aio_req
, &s
->pending_aio_head
, aio_siblings
) {
625 if (aio_req
->oid
== oid
) {
634 * This function searchs pending requests to the object `oid', and
637 static void coroutine_fn
send_pending_req(BDRVSheepdogState
*s
, uint64_t oid
)
643 while ((aio_req
= find_pending_req(s
, oid
)) != NULL
) {
644 acb
= aio_req
->aiocb
;
645 /* move aio_req from pending list to inflight one */
646 QLIST_REMOVE(aio_req
, aio_siblings
);
647 QLIST_INSERT_HEAD(&s
->inflight_aio_head
, aio_req
, aio_siblings
);
648 ret
= add_aio_request(s
, aio_req
, acb
->qiov
->iov
,
649 acb
->qiov
->niov
, false, acb
->aiocb_type
);
651 error_report("add_aio_request is failed");
652 free_aio_req(s
, aio_req
);
653 if (!acb
->nr_pending
) {
654 sd_finish_aiocb(acb
);
661 * Receive responses of the I/O requests.
663 * This function is registered as a fd handler, and called from the
664 * main loop when s->fd is ready for reading responses.
666 static void coroutine_fn
aio_read_response(void *opaque
)
669 BDRVSheepdogState
*s
= opaque
;
672 AIOReq
*aio_req
= NULL
;
676 if (QLIST_EMPTY(&s
->inflight_aio_head
)) {
681 ret
= qemu_co_recv(fd
, &rsp
, sizeof(rsp
));
683 error_report("failed to get the header, %s", strerror(errno
));
687 /* find the right aio_req from the inflight aio list */
688 QLIST_FOREACH(aio_req
, &s
->inflight_aio_head
, aio_siblings
) {
689 if (aio_req
->id
== rsp
.id
) {
694 error_report("cannot find aio_req %x", rsp
.id
);
698 acb
= aio_req
->aiocb
;
700 switch (acb
->aiocb_type
) {
701 case AIOCB_WRITE_UDATA
:
702 /* this coroutine context is no longer suitable for co_recv
703 * because we may send data to update vdi objects */
705 if (!is_data_obj(aio_req
->oid
)) {
708 idx
= data_oid_to_idx(aio_req
->oid
);
710 if (s
->inode
.data_vdi_id
[idx
] != s
->inode
.vdi_id
) {
712 * If the object is newly created one, we need to update
713 * the vdi object (metadata object). min_dirty_data_idx
714 * and max_dirty_data_idx are changed to include updated
715 * index between them.
717 if (rsp
.result
== SD_RES_SUCCESS
) {
718 s
->inode
.data_vdi_id
[idx
] = s
->inode
.vdi_id
;
719 s
->max_dirty_data_idx
= MAX(idx
, s
->max_dirty_data_idx
);
720 s
->min_dirty_data_idx
= MIN(idx
, s
->min_dirty_data_idx
);
723 * Some requests may be blocked because simultaneous
724 * create requests are not allowed, so we search the
725 * pending requests here.
727 send_pending_req(s
, aio_req
->oid
);
730 case AIOCB_READ_UDATA
:
731 ret
= qemu_co_recvv(fd
, acb
->qiov
->iov
, acb
->qiov
->niov
,
732 aio_req
->iov_offset
, rsp
.data_length
);
734 error_report("failed to get the data, %s", strerror(errno
));
738 case AIOCB_FLUSH_CACHE
:
739 if (rsp
.result
== SD_RES_INVALID_PARMS
) {
740 dprintf("disable cache since the server doesn't support it\n");
741 s
->cache_flags
= SD_FLAG_CMD_DIRECT
;
742 rsp
.result
= SD_RES_SUCCESS
;
747 if (rsp
.result
!= SD_RES_SUCCESS
) {
749 error_report("%s", sd_strerror(rsp
.result
));
752 free_aio_req(s
, aio_req
);
753 if (!acb
->nr_pending
) {
755 * We've finished all requests which belong to the AIOCB, so
756 * we can switch back to sd_co_readv/writev now.
758 acb
->aio_done_func(acb
);
764 static void co_read_response(void *opaque
)
766 BDRVSheepdogState
*s
= opaque
;
769 s
->co_recv
= qemu_coroutine_create(aio_read_response
);
772 qemu_coroutine_enter(s
->co_recv
, opaque
);
775 static void co_write_request(void *opaque
)
777 BDRVSheepdogState
*s
= opaque
;
779 qemu_coroutine_enter(s
->co_send
, NULL
);
782 static int aio_flush_request(void *opaque
)
784 BDRVSheepdogState
*s
= opaque
;
786 return !QLIST_EMPTY(&s
->inflight_aio_head
) ||
787 !QLIST_EMPTY(&s
->pending_aio_head
);
790 static int set_nodelay(int fd
)
795 ret
= setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&opt
, sizeof(opt
));
800 * Return a socket discriptor to read/write objects.
802 * We cannot use this discriptor for other operations because
803 * the block driver may be on waiting response from the server.
805 static int get_sheep_fd(BDRVSheepdogState
*s
)
809 fd
= connect_to_sdog(s
->addr
, s
->port
);
811 error_report("%s", strerror(errno
));
815 socket_set_nonblock(fd
);
817 ret
= set_nodelay(fd
);
819 error_report("%s", strerror(errno
));
824 qemu_aio_set_fd_handler(fd
, co_read_response
, NULL
, aio_flush_request
, s
);
831 * filename must be one of the following formats:
833 * 2. [vdiname]:[snapid]
835 * 4. [hostname]:[port]:[vdiname]
836 * 5. [hostname]:[port]:[vdiname]:[snapid]
837 * 6. [hostname]:[port]:[vdiname]:[tag]
839 * You can boot from the snapshot images by specifying `snapid` or
842 * You can run VMs outside the Sheepdog cluster by specifying
843 * `hostname' and `port' (experimental).
845 static int parse_vdiname(BDRVSheepdogState
*s
, const char *filename
,
846 char *vdi
, uint32_t *snapid
, char *tag
)
851 p
= q
= g_strdup(filename
);
853 /* count the number of separators */
863 /* use the first two tokens as hostname and port number. */
877 pstrcpy(vdi
, SD_MAX_VDI_LEN
, p
);
879 p
= strchr(vdi
, ':');
882 *snapid
= strtoul(p
, NULL
, 10);
884 pstrcpy(tag
, SD_MAX_VDI_TAG_LEN
, p
);
887 *snapid
= CURRENT_VDI_ID
; /* search current vdi */
890 if (s
->addr
== NULL
) {
897 static int find_vdi_name(BDRVSheepdogState
*s
, char *filename
, uint32_t snapid
,
898 char *tag
, uint32_t *vid
, int for_snapshot
)
902 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
903 unsigned int wlen
, rlen
= 0;
904 char buf
[SD_MAX_VDI_LEN
+ SD_MAX_VDI_TAG_LEN
];
906 fd
= connect_to_sdog(s
->addr
, s
->port
);
911 /* This pair of strncpy calls ensures that the buffer is zero-filled,
912 * which is desirable since we'll soon be sending those bytes, and
913 * don't want the send_req to read uninitialized data.
915 strncpy(buf
, filename
, SD_MAX_VDI_LEN
);
916 strncpy(buf
+ SD_MAX_VDI_LEN
, tag
, SD_MAX_VDI_TAG_LEN
);
918 memset(&hdr
, 0, sizeof(hdr
));
920 hdr
.opcode
= SD_OP_GET_VDI_INFO
;
922 hdr
.opcode
= SD_OP_LOCK_VDI
;
924 wlen
= SD_MAX_VDI_LEN
+ SD_MAX_VDI_TAG_LEN
;
925 hdr
.proto_ver
= SD_PROTO_VER
;
926 hdr
.data_length
= wlen
;
928 hdr
.flags
= SD_FLAG_CMD_WRITE
;
930 ret
= do_req(fd
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
935 if (rsp
->result
!= SD_RES_SUCCESS
) {
936 error_report("cannot get vdi info, %s, %s %d %s",
937 sd_strerror(rsp
->result
), filename
, snapid
, tag
);
938 if (rsp
->result
== SD_RES_NO_VDI
) {
953 static int coroutine_fn
add_aio_request(BDRVSheepdogState
*s
, AIOReq
*aio_req
,
954 struct iovec
*iov
, int niov
, bool create
,
955 enum AIOCBState aiocb_type
)
957 int nr_copies
= s
->inode
.nr_copies
;
959 unsigned int wlen
= 0;
961 uint64_t oid
= aio_req
->oid
;
962 unsigned int datalen
= aio_req
->data_len
;
963 uint64_t offset
= aio_req
->offset
;
964 uint8_t flags
= aio_req
->flags
;
965 uint64_t old_oid
= aio_req
->base_oid
;
971 memset(&hdr
, 0, sizeof(hdr
));
973 switch (aiocb_type
) {
974 case AIOCB_FLUSH_CACHE
:
975 hdr
.opcode
= SD_OP_FLUSH_VDI
;
977 case AIOCB_READ_UDATA
:
978 hdr
.opcode
= SD_OP_READ_OBJ
;
981 case AIOCB_WRITE_UDATA
:
983 hdr
.opcode
= SD_OP_CREATE_AND_WRITE_OBJ
;
985 hdr
.opcode
= SD_OP_WRITE_OBJ
;
988 hdr
.flags
= SD_FLAG_CMD_WRITE
| flags
;
992 if (s
->cache_flags
) {
993 hdr
.flags
|= s
->cache_flags
;
997 hdr
.cow_oid
= old_oid
;
998 hdr
.copies
= s
->inode
.nr_copies
;
1000 hdr
.data_length
= datalen
;
1001 hdr
.offset
= offset
;
1003 hdr
.id
= aio_req
->id
;
1005 qemu_co_mutex_lock(&s
->lock
);
1006 s
->co_send
= qemu_coroutine_self();
1007 qemu_aio_set_fd_handler(s
->fd
, co_read_response
, co_write_request
,
1008 aio_flush_request
, s
);
1009 socket_set_cork(s
->fd
, 1);
1012 ret
= qemu_co_send(s
->fd
, &hdr
, sizeof(hdr
));
1014 qemu_co_mutex_unlock(&s
->lock
);
1015 error_report("failed to send a req, %s", strerror(errno
));
1020 ret
= qemu_co_sendv(s
->fd
, iov
, niov
, aio_req
->iov_offset
, wlen
);
1022 qemu_co_mutex_unlock(&s
->lock
);
1023 error_report("failed to send a data, %s", strerror(errno
));
1028 socket_set_cork(s
->fd
, 0);
1029 qemu_aio_set_fd_handler(s
->fd
, co_read_response
, NULL
,
1030 aio_flush_request
, s
);
1031 qemu_co_mutex_unlock(&s
->lock
);
1036 static int read_write_object(int fd
, char *buf
, uint64_t oid
, int copies
,
1037 unsigned int datalen
, uint64_t offset
,
1038 bool write
, bool create
, uint32_t cache_flags
)
1041 SheepdogObjRsp
*rsp
= (SheepdogObjRsp
*)&hdr
;
1042 unsigned int wlen
, rlen
;
1045 memset(&hdr
, 0, sizeof(hdr
));
1050 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1052 hdr
.opcode
= SD_OP_CREATE_AND_WRITE_OBJ
;
1054 hdr
.opcode
= SD_OP_WRITE_OBJ
;
1059 hdr
.opcode
= SD_OP_READ_OBJ
;
1062 hdr
.flags
|= cache_flags
;
1065 hdr
.data_length
= datalen
;
1066 hdr
.offset
= offset
;
1067 hdr
.copies
= copies
;
1069 ret
= do_req(fd
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1071 error_report("failed to send a request to the sheep");
1075 switch (rsp
->result
) {
1076 case SD_RES_SUCCESS
:
1079 error_report("%s", sd_strerror(rsp
->result
));
1084 static int read_object(int fd
, char *buf
, uint64_t oid
, int copies
,
1085 unsigned int datalen
, uint64_t offset
,
1086 uint32_t cache_flags
)
1088 return read_write_object(fd
, buf
, oid
, copies
, datalen
, offset
, false,
1089 false, cache_flags
);
1092 static int write_object(int fd
, char *buf
, uint64_t oid
, int copies
,
1093 unsigned int datalen
, uint64_t offset
, bool create
,
1094 uint32_t cache_flags
)
1096 return read_write_object(fd
, buf
, oid
, copies
, datalen
, offset
, true,
1097 create
, cache_flags
);
1100 static int sd_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1104 BDRVSheepdogState
*s
= bs
->opaque
;
1105 char vdi
[SD_MAX_VDI_LEN
], tag
[SD_MAX_VDI_TAG_LEN
];
1109 strstart(filename
, "sheepdog:", (const char **)&filename
);
1111 QLIST_INIT(&s
->inflight_aio_head
);
1112 QLIST_INIT(&s
->pending_aio_head
);
1115 memset(vdi
, 0, sizeof(vdi
));
1116 memset(tag
, 0, sizeof(tag
));
1117 if (parse_vdiname(s
, filename
, vdi
, &snapid
, tag
) < 0) {
1121 s
->fd
= get_sheep_fd(s
);
1127 ret
= find_vdi_name(s
, vdi
, snapid
, tag
, &vid
, 0);
1133 * QEMU block layer emulates writethrough cache as 'writeback + flush', so
1134 * we always set SD_FLAG_CMD_CACHE (writeback cache) as default.
1136 s
->cache_flags
= SD_FLAG_CMD_CACHE
;
1137 if (flags
& BDRV_O_NOCACHE
) {
1138 s
->cache_flags
= SD_FLAG_CMD_DIRECT
;
1141 if (snapid
|| tag
[0] != '\0') {
1142 dprintf("%" PRIx32
" snapshot inode was open.\n", vid
);
1143 s
->is_snapshot
= true;
1146 fd
= connect_to_sdog(s
->addr
, s
->port
);
1148 error_report("failed to connect");
1153 buf
= g_malloc(SD_INODE_SIZE
);
1154 ret
= read_object(fd
, buf
, vid_to_vdi_oid(vid
), 0, SD_INODE_SIZE
, 0,
1163 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
1164 s
->min_dirty_data_idx
= UINT32_MAX
;
1165 s
->max_dirty_data_idx
= 0;
1167 bs
->total_sectors
= s
->inode
.vdi_size
/ SECTOR_SIZE
;
1168 pstrcpy(s
->name
, sizeof(s
->name
), vdi
);
1169 qemu_co_mutex_init(&s
->lock
);
1173 qemu_aio_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
, NULL
);
1181 static int do_sd_create(char *filename
, int64_t vdi_size
,
1182 uint32_t base_vid
, uint32_t *vdi_id
, int snapshot
,
1183 const char *addr
, const char *port
)
1186 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1188 unsigned int wlen
, rlen
= 0;
1189 char buf
[SD_MAX_VDI_LEN
];
1191 fd
= connect_to_sdog(addr
, port
);
1196 /* FIXME: would it be better to fail (e.g., return -EIO) when filename
1197 * does not fit in buf? For now, just truncate and avoid buffer overrun.
1199 memset(buf
, 0, sizeof(buf
));
1200 pstrcpy(buf
, sizeof(buf
), filename
);
1202 memset(&hdr
, 0, sizeof(hdr
));
1203 hdr
.opcode
= SD_OP_NEW_VDI
;
1204 hdr
.vdi_id
= base_vid
;
1206 wlen
= SD_MAX_VDI_LEN
;
1208 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1209 hdr
.snapid
= snapshot
;
1211 hdr
.data_length
= wlen
;
1212 hdr
.vdi_size
= vdi_size
;
1214 ret
= do_req(fd
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1222 if (rsp
->result
!= SD_RES_SUCCESS
) {
1223 error_report("%s, %s", sd_strerror(rsp
->result
), filename
);
1228 *vdi_id
= rsp
->vdi_id
;
1234 static int sd_prealloc(const char *filename
)
1236 BlockDriverState
*bs
= NULL
;
1237 uint32_t idx
, max_idx
;
1239 void *buf
= g_malloc0(SD_DATA_OBJ_SIZE
);
1242 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDWR
);
1247 vdi_size
= bdrv_getlength(bs
);
1252 max_idx
= DIV_ROUND_UP(vdi_size
, SD_DATA_OBJ_SIZE
);
1254 for (idx
= 0; idx
< max_idx
; idx
++) {
1256 * The created image can be a cloned image, so we need to read
1257 * a data from the source image.
1259 ret
= bdrv_pread(bs
, idx
* SD_DATA_OBJ_SIZE
, buf
, SD_DATA_OBJ_SIZE
);
1263 ret
= bdrv_pwrite(bs
, idx
* SD_DATA_OBJ_SIZE
, buf
, SD_DATA_OBJ_SIZE
);
1277 static int sd_create(const char *filename
, QEMUOptionParameter
*options
)
1280 uint32_t vid
= 0, base_vid
= 0;
1281 int64_t vdi_size
= 0;
1282 char *backing_file
= NULL
;
1283 BDRVSheepdogState
*s
;
1284 char vdi
[SD_MAX_VDI_LEN
], tag
[SD_MAX_VDI_TAG_LEN
];
1286 bool prealloc
= false;
1287 const char *vdiname
;
1289 s
= g_malloc0(sizeof(BDRVSheepdogState
));
1291 strstart(filename
, "sheepdog:", &vdiname
);
1293 memset(vdi
, 0, sizeof(vdi
));
1294 memset(tag
, 0, sizeof(tag
));
1295 if (parse_vdiname(s
, vdiname
, vdi
, &snapid
, tag
) < 0) {
1296 error_report("invalid filename");
1301 while (options
&& options
->name
) {
1302 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
1303 vdi_size
= options
->value
.n
;
1304 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
1305 backing_file
= options
->value
.s
;
1306 } else if (!strcmp(options
->name
, BLOCK_OPT_PREALLOC
)) {
1307 if (!options
->value
.s
|| !strcmp(options
->value
.s
, "off")) {
1309 } else if (!strcmp(options
->value
.s
, "full")) {
1312 error_report("Invalid preallocation mode: '%s'",
1321 if (vdi_size
> SD_MAX_VDI_SIZE
) {
1322 error_report("too big image size");
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");
1340 ret
= bdrv_file_open(&bs
, backing_file
, 0);
1347 if (!is_snapshot(&s
->inode
)) {
1348 error_report("cannot clone from a non snapshot vdi");
1354 base_vid
= s
->inode
.vdi_id
;
1358 ret
= do_sd_create(vdi
, vdi_size
, base_vid
, &vid
, 0, s
->addr
, s
->port
);
1359 if (!prealloc
|| ret
) {
1363 ret
= sd_prealloc(filename
);
1369 static void sd_close(BlockDriverState
*bs
)
1371 BDRVSheepdogState
*s
= bs
->opaque
;
1373 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1374 unsigned int wlen
, rlen
= 0;
1377 dprintf("%s\n", s
->name
);
1379 fd
= connect_to_sdog(s
->addr
, s
->port
);
1384 memset(&hdr
, 0, sizeof(hdr
));
1386 hdr
.opcode
= SD_OP_RELEASE_VDI
;
1387 hdr
.vdi_id
= s
->inode
.vdi_id
;
1388 wlen
= strlen(s
->name
) + 1;
1389 hdr
.data_length
= wlen
;
1390 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1392 ret
= do_req(fd
, (SheepdogReq
*)&hdr
, s
->name
, &wlen
, &rlen
);
1396 if (!ret
&& rsp
->result
!= SD_RES_SUCCESS
&&
1397 rsp
->result
!= SD_RES_VDI_NOT_LOCKED
) {
1398 error_report("%s, %s", sd_strerror(rsp
->result
), s
->name
);
1401 qemu_aio_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
, NULL
);
1406 static int64_t sd_getlength(BlockDriverState
*bs
)
1408 BDRVSheepdogState
*s
= bs
->opaque
;
1410 return s
->inode
.vdi_size
;
1413 static int sd_truncate(BlockDriverState
*bs
, int64_t offset
)
1415 BDRVSheepdogState
*s
= bs
->opaque
;
1417 unsigned int datalen
;
1419 if (offset
< s
->inode
.vdi_size
) {
1420 error_report("shrinking is not supported");
1422 } else if (offset
> SD_MAX_VDI_SIZE
) {
1423 error_report("too big image size");
1427 fd
= connect_to_sdog(s
->addr
, s
->port
);
1432 /* we don't need to update entire object */
1433 datalen
= SD_INODE_SIZE
- sizeof(s
->inode
.data_vdi_id
);
1434 s
->inode
.vdi_size
= offset
;
1435 ret
= write_object(fd
, (char *)&s
->inode
, vid_to_vdi_oid(s
->inode
.vdi_id
),
1436 s
->inode
.nr_copies
, datalen
, 0, false, s
->cache_flags
);
1440 error_report("failed to update an inode.");
1447 * This function is called after writing data objects. If we need to
1448 * update metadata, this sends a write request to the vdi object.
1449 * Otherwise, this switches back to sd_co_readv/writev.
1451 static void coroutine_fn
sd_write_done(SheepdogAIOCB
*acb
)
1454 BDRVSheepdogState
*s
= acb
->common
.bs
->opaque
;
1457 uint32_t offset
, data_len
, mn
, mx
;
1459 mn
= s
->min_dirty_data_idx
;
1460 mx
= s
->max_dirty_data_idx
;
1462 /* we need to update the vdi object. */
1463 offset
= sizeof(s
->inode
) - sizeof(s
->inode
.data_vdi_id
) +
1464 mn
* sizeof(s
->inode
.data_vdi_id
[0]);
1465 data_len
= (mx
- mn
+ 1) * sizeof(s
->inode
.data_vdi_id
[0]);
1467 s
->min_dirty_data_idx
= UINT32_MAX
;
1468 s
->max_dirty_data_idx
= 0;
1470 iov
.iov_base
= &s
->inode
;
1471 iov
.iov_len
= sizeof(s
->inode
);
1472 aio_req
= alloc_aio_req(s
, acb
, vid_to_vdi_oid(s
->inode
.vdi_id
),
1473 data_len
, offset
, 0, 0, offset
);
1474 QLIST_INSERT_HEAD(&s
->inflight_aio_head
, aio_req
, aio_siblings
);
1475 ret
= add_aio_request(s
, aio_req
, &iov
, 1, false, AIOCB_WRITE_UDATA
);
1477 free_aio_req(s
, aio_req
);
1482 acb
->aio_done_func
= sd_finish_aiocb
;
1483 acb
->aiocb_type
= AIOCB_WRITE_UDATA
;
1487 sd_finish_aiocb(acb
);
1491 * Create a writable VDI from a snapshot
1493 static int sd_create_branch(BDRVSheepdogState
*s
)
1499 dprintf("%" PRIx32
" is snapshot.\n", s
->inode
.vdi_id
);
1501 buf
= g_malloc(SD_INODE_SIZE
);
1503 ret
= do_sd_create(s
->name
, s
->inode
.vdi_size
, s
->inode
.vdi_id
, &vid
, 1,
1509 dprintf("%" PRIx32
" is created.\n", vid
);
1511 fd
= connect_to_sdog(s
->addr
, s
->port
);
1513 error_report("failed to connect");
1518 ret
= read_object(fd
, buf
, vid_to_vdi_oid(vid
), s
->inode
.nr_copies
,
1519 SD_INODE_SIZE
, 0, s
->cache_flags
);
1527 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
1529 s
->is_snapshot
= false;
1531 dprintf("%" PRIx32
" was newly created.\n", s
->inode
.vdi_id
);
1540 * Send I/O requests to the server.
1542 * This function sends requests to the server, links the requests to
1543 * the inflight_list in BDRVSheepdogState, and exits without
1544 * waiting the response. The responses are received in the
1545 * `aio_read_response' function which is called from the main loop as
1548 * Returns 1 when we need to wait a response, 0 when there is no sent
1549 * request and -errno in error cases.
1551 static int coroutine_fn
sd_co_rw_vector(void *p
)
1553 SheepdogAIOCB
*acb
= p
;
1555 unsigned long len
, done
= 0, total
= acb
->nb_sectors
* SECTOR_SIZE
;
1556 unsigned long idx
= acb
->sector_num
* SECTOR_SIZE
/ SD_DATA_OBJ_SIZE
;
1558 uint64_t offset
= (acb
->sector_num
* SECTOR_SIZE
) % SD_DATA_OBJ_SIZE
;
1559 BDRVSheepdogState
*s
= acb
->common
.bs
->opaque
;
1560 SheepdogInode
*inode
= &s
->inode
;
1563 if (acb
->aiocb_type
== AIOCB_WRITE_UDATA
&& s
->is_snapshot
) {
1565 * In the case we open the snapshot VDI, Sheepdog creates the
1566 * writable VDI when we do a write operation first.
1568 ret
= sd_create_branch(s
);
1576 * Make sure we don't free the aiocb before we are done with all requests.
1577 * This additional reference is dropped at the end of this function.
1581 while (done
!= total
) {
1583 uint64_t old_oid
= 0;
1584 bool create
= false;
1586 oid
= vid_to_data_oid(inode
->data_vdi_id
[idx
], idx
);
1588 len
= MIN(total
- done
, SD_DATA_OBJ_SIZE
- offset
);
1590 switch (acb
->aiocb_type
) {
1591 case AIOCB_READ_UDATA
:
1592 if (!inode
->data_vdi_id
[idx
]) {
1593 qemu_iovec_memset(acb
->qiov
, done
, 0, len
);
1597 case AIOCB_WRITE_UDATA
:
1598 if (!inode
->data_vdi_id
[idx
]) {
1600 } else if (!is_data_obj_writable(inode
, idx
)) {
1604 flags
= SD_FLAG_CMD_COW
;
1612 dprintf("update ino (%" PRIu32
") %" PRIu64
" %" PRIu64
" %ld\n",
1614 vid_to_data_oid(inode
->data_vdi_id
[idx
], idx
), idx
);
1615 oid
= vid_to_data_oid(inode
->vdi_id
, idx
);
1616 dprintf("new oid %" PRIx64
"\n", oid
);
1619 aio_req
= alloc_aio_req(s
, acb
, oid
, len
, offset
, flags
, old_oid
, done
);
1623 QLIST_FOREACH(areq
, &s
->inflight_aio_head
, aio_siblings
) {
1624 if (areq
->oid
== oid
) {
1626 * Sheepdog cannot handle simultaneous create
1627 * requests to the same object. So we cannot send
1628 * the request until the previous request
1632 aio_req
->base_oid
= 0;
1633 QLIST_INSERT_HEAD(&s
->pending_aio_head
, aio_req
,
1640 QLIST_INSERT_HEAD(&s
->inflight_aio_head
, aio_req
, aio_siblings
);
1641 ret
= add_aio_request(s
, aio_req
, acb
->qiov
->iov
, acb
->qiov
->niov
,
1642 create
, acb
->aiocb_type
);
1644 error_report("add_aio_request is failed");
1645 free_aio_req(s
, aio_req
);
1655 if (!--acb
->nr_pending
) {
1661 static coroutine_fn
int sd_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
1662 int nb_sectors
, QEMUIOVector
*qiov
)
1667 if (bs
->growable
&& sector_num
+ nb_sectors
> bs
->total_sectors
) {
1668 ret
= sd_truncate(bs
, (sector_num
+ nb_sectors
) * SECTOR_SIZE
);
1672 bs
->total_sectors
= sector_num
+ nb_sectors
;
1675 acb
= sd_aio_setup(bs
, qiov
, sector_num
, nb_sectors
);
1676 acb
->aio_done_func
= sd_write_done
;
1677 acb
->aiocb_type
= AIOCB_WRITE_UDATA
;
1679 ret
= sd_co_rw_vector(acb
);
1681 qemu_aio_release(acb
);
1685 qemu_coroutine_yield();
1690 static coroutine_fn
int sd_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
1691 int nb_sectors
, QEMUIOVector
*qiov
)
1696 acb
= sd_aio_setup(bs
, qiov
, sector_num
, nb_sectors
);
1697 acb
->aiocb_type
= AIOCB_READ_UDATA
;
1698 acb
->aio_done_func
= sd_finish_aiocb
;
1700 ret
= sd_co_rw_vector(acb
);
1702 qemu_aio_release(acb
);
1706 qemu_coroutine_yield();
1711 static int coroutine_fn
sd_co_flush_to_disk(BlockDriverState
*bs
)
1713 BDRVSheepdogState
*s
= bs
->opaque
;
1718 if (s
->cache_flags
!= SD_FLAG_CMD_CACHE
) {
1722 acb
= sd_aio_setup(bs
, NULL
, 0, 0);
1723 acb
->aiocb_type
= AIOCB_FLUSH_CACHE
;
1724 acb
->aio_done_func
= sd_finish_aiocb
;
1726 aio_req
= alloc_aio_req(s
, acb
, vid_to_vdi_oid(s
->inode
.vdi_id
),
1728 QLIST_INSERT_HEAD(&s
->inflight_aio_head
, aio_req
, aio_siblings
);
1729 ret
= add_aio_request(s
, aio_req
, NULL
, 0, false, acb
->aiocb_type
);
1731 error_report("add_aio_request is failed");
1732 free_aio_req(s
, aio_req
);
1733 qemu_aio_release(acb
);
1737 qemu_coroutine_yield();
1741 static int sd_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
)
1743 BDRVSheepdogState
*s
= bs
->opaque
;
1746 SheepdogInode
*inode
;
1747 unsigned int datalen
;
1749 dprintf("sn_info: name %s id_str %s s: name %s vm_state_size %" PRId64
" "
1750 "is_snapshot %d\n", sn_info
->name
, sn_info
->id_str
,
1751 s
->name
, sn_info
->vm_state_size
, s
->is_snapshot
);
1753 if (s
->is_snapshot
) {
1754 error_report("You can't create a snapshot of a snapshot VDI, "
1755 "%s (%" PRIu32
").", s
->name
, s
->inode
.vdi_id
);
1760 dprintf("%s %s\n", sn_info
->name
, sn_info
->id_str
);
1762 s
->inode
.vm_state_size
= sn_info
->vm_state_size
;
1763 s
->inode
.vm_clock_nsec
= sn_info
->vm_clock_nsec
;
1764 /* It appears that inode.tag does not require a NUL terminator,
1765 * which means this use of strncpy is ok.
1767 strncpy(s
->inode
.tag
, sn_info
->name
, sizeof(s
->inode
.tag
));
1768 /* we don't need to update entire object */
1769 datalen
= SD_INODE_SIZE
- sizeof(s
->inode
.data_vdi_id
);
1771 /* refresh inode. */
1772 fd
= connect_to_sdog(s
->addr
, s
->port
);
1778 ret
= write_object(fd
, (char *)&s
->inode
, vid_to_vdi_oid(s
->inode
.vdi_id
),
1779 s
->inode
.nr_copies
, datalen
, 0, false, s
->cache_flags
);
1781 error_report("failed to write snapshot's inode.");
1785 ret
= do_sd_create(s
->name
, s
->inode
.vdi_size
, s
->inode
.vdi_id
, &new_vid
, 1,
1788 error_report("failed to create inode for snapshot. %s",
1793 inode
= (SheepdogInode
*)g_malloc(datalen
);
1795 ret
= read_object(fd
, (char *)inode
, vid_to_vdi_oid(new_vid
),
1796 s
->inode
.nr_copies
, datalen
, 0, s
->cache_flags
);
1799 error_report("failed to read new inode info. %s", strerror(errno
));
1803 memcpy(&s
->inode
, inode
, datalen
);
1804 dprintf("s->inode: name %s snap_id %x oid %x\n",
1805 s
->inode
.name
, s
->inode
.snap_id
, s
->inode
.vdi_id
);
1812 static int sd_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
)
1814 BDRVSheepdogState
*s
= bs
->opaque
;
1815 BDRVSheepdogState
*old_s
;
1816 char vdi
[SD_MAX_VDI_LEN
], tag
[SD_MAX_VDI_TAG_LEN
];
1819 uint32_t snapid
= 0;
1822 old_s
= g_malloc(sizeof(BDRVSheepdogState
));
1824 memcpy(old_s
, s
, sizeof(BDRVSheepdogState
));
1826 pstrcpy(vdi
, sizeof(vdi
), s
->name
);
1828 snapid
= strtoul(snapshot_id
, NULL
, 10);
1832 pstrcpy(tag
, sizeof(tag
), s
->name
);
1835 ret
= find_vdi_name(s
, vdi
, snapid
, tag
, &vid
, 1);
1837 error_report("Failed to find_vdi_name");
1841 fd
= connect_to_sdog(s
->addr
, s
->port
);
1843 error_report("failed to connect");
1848 buf
= g_malloc(SD_INODE_SIZE
);
1849 ret
= read_object(fd
, buf
, vid_to_vdi_oid(vid
), s
->inode
.nr_copies
,
1850 SD_INODE_SIZE
, 0, s
->cache_flags
);
1858 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
1860 if (!s
->inode
.vm_state_size
) {
1861 error_report("Invalid snapshot");
1866 s
->is_snapshot
= true;
1873 /* recover bdrv_sd_state */
1874 memcpy(s
, old_s
, sizeof(BDRVSheepdogState
));
1878 error_report("failed to open. recover old bdrv_sd_state.");
1883 static int sd_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1885 /* FIXME: Delete specified snapshot id. */
1889 static int sd_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
)
1891 BDRVSheepdogState
*s
= bs
->opaque
;
1893 int fd
, nr
= 1024, ret
, max
= BITS_TO_LONGS(SD_NR_VDIS
) * sizeof(long);
1894 QEMUSnapshotInfo
*sn_tab
= NULL
;
1895 unsigned wlen
, rlen
;
1897 static SheepdogInode inode
;
1898 unsigned long *vdi_inuse
;
1899 unsigned int start_nr
;
1903 vdi_inuse
= g_malloc(max
);
1905 fd
= connect_to_sdog(s
->addr
, s
->port
);
1914 memset(&req
, 0, sizeof(req
));
1916 req
.opcode
= SD_OP_READ_VDIS
;
1917 req
.data_length
= max
;
1919 ret
= do_req(fd
, (SheepdogReq
*)&req
, vdi_inuse
, &wlen
, &rlen
);
1926 sn_tab
= g_malloc0(nr
* sizeof(*sn_tab
));
1928 /* calculate a vdi id with hash function */
1929 hval
= fnv_64a_buf(s
->name
, strlen(s
->name
), FNV1A_64_INIT
);
1930 start_nr
= hval
& (SD_NR_VDIS
- 1);
1932 fd
= connect_to_sdog(s
->addr
, s
->port
);
1934 error_report("failed to connect");
1939 for (vid
= start_nr
; found
< nr
; vid
= (vid
+ 1) % SD_NR_VDIS
) {
1940 if (!test_bit(vid
, vdi_inuse
)) {
1944 /* we don't need to read entire object */
1945 ret
= read_object(fd
, (char *)&inode
, vid_to_vdi_oid(vid
),
1946 0, SD_INODE_SIZE
- sizeof(inode
.data_vdi_id
), 0,
1953 if (!strcmp(inode
.name
, s
->name
) && is_snapshot(&inode
)) {
1954 sn_tab
[found
].date_sec
= inode
.snap_ctime
>> 32;
1955 sn_tab
[found
].date_nsec
= inode
.snap_ctime
& 0xffffffff;
1956 sn_tab
[found
].vm_state_size
= inode
.vm_state_size
;
1957 sn_tab
[found
].vm_clock_nsec
= inode
.vm_clock_nsec
;
1959 snprintf(sn_tab
[found
].id_str
, sizeof(sn_tab
[found
].id_str
), "%u",
1961 pstrcpy(sn_tab
[found
].name
,
1962 MIN(sizeof(sn_tab
[found
].name
), sizeof(inode
.tag
)),
1981 static int do_load_save_vmstate(BDRVSheepdogState
*s
, uint8_t *data
,
1982 int64_t pos
, int size
, int load
)
1985 int fd
, ret
= 0, remaining
= size
;
1986 unsigned int data_len
;
1987 uint64_t vmstate_oid
;
1991 fd
= connect_to_sdog(s
->addr
, s
->port
);
1997 vdi_index
= pos
/ SD_DATA_OBJ_SIZE
;
1998 offset
= pos
% SD_DATA_OBJ_SIZE
;
2000 data_len
= MIN(remaining
, SD_DATA_OBJ_SIZE
- offset
);
2002 vmstate_oid
= vid_to_vmstate_oid(s
->inode
.vdi_id
, vdi_index
);
2004 create
= (offset
== 0);
2006 ret
= read_object(fd
, (char *)data
, vmstate_oid
,
2007 s
->inode
.nr_copies
, data_len
, offset
,
2010 ret
= write_object(fd
, (char *)data
, vmstate_oid
,
2011 s
->inode
.nr_copies
, data_len
, offset
, create
,
2016 error_report("failed to save vmstate %s", strerror(errno
));
2022 remaining
-= data_len
;
2030 static int sd_save_vmstate(BlockDriverState
*bs
, const uint8_t *data
,
2031 int64_t pos
, int size
)
2033 BDRVSheepdogState
*s
= bs
->opaque
;
2035 return do_load_save_vmstate(s
, (uint8_t *)data
, pos
, size
, 0);
2038 static int sd_load_vmstate(BlockDriverState
*bs
, uint8_t *data
,
2039 int64_t pos
, int size
)
2041 BDRVSheepdogState
*s
= bs
->opaque
;
2043 return do_load_save_vmstate(s
, data
, pos
, size
, 1);
2047 static QEMUOptionParameter sd_create_options
[] = {
2049 .name
= BLOCK_OPT_SIZE
,
2051 .help
= "Virtual disk size"
2054 .name
= BLOCK_OPT_BACKING_FILE
,
2056 .help
= "File name of a base image"
2059 .name
= BLOCK_OPT_PREALLOC
,
2061 .help
= "Preallocation mode (allowed values: off, full)"
2066 BlockDriver bdrv_sheepdog
= {
2067 .format_name
= "sheepdog",
2068 .protocol_name
= "sheepdog",
2069 .instance_size
= sizeof(BDRVSheepdogState
),
2070 .bdrv_file_open
= sd_open
,
2071 .bdrv_close
= sd_close
,
2072 .bdrv_create
= sd_create
,
2073 .bdrv_getlength
= sd_getlength
,
2074 .bdrv_truncate
= sd_truncate
,
2076 .bdrv_co_readv
= sd_co_readv
,
2077 .bdrv_co_writev
= sd_co_writev
,
2078 .bdrv_co_flush_to_disk
= sd_co_flush_to_disk
,
2080 .bdrv_snapshot_create
= sd_snapshot_create
,
2081 .bdrv_snapshot_goto
= sd_snapshot_goto
,
2082 .bdrv_snapshot_delete
= sd_snapshot_delete
,
2083 .bdrv_snapshot_list
= sd_snapshot_list
,
2085 .bdrv_save_vmstate
= sd_save_vmstate
,
2086 .bdrv_load_vmstate
= sd_load_vmstate
,
2088 .create_options
= sd_create_options
,
2091 static void bdrv_sheepdog_init(void)
2093 bdrv_register(&bdrv_sheepdog
);
2095 block_init(bdrv_sheepdog_init
);