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
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
41 #define SD_RES_SUCCESS 0x00 /* Success */
42 #define SD_RES_UNKNOWN 0x01 /* Unknown error */
43 #define SD_RES_NO_OBJ 0x02 /* No object found */
44 #define SD_RES_EIO 0x03 /* I/O error */
45 #define SD_RES_VDI_EXIST 0x04 /* Vdi exists already */
46 #define SD_RES_INVALID_PARMS 0x05 /* Invalid parameters */
47 #define SD_RES_SYSTEM_ERROR 0x06 /* System error */
48 #define SD_RES_VDI_LOCKED 0x07 /* Vdi is locked */
49 #define SD_RES_NO_VDI 0x08 /* No vdi found */
50 #define SD_RES_NO_BASE_VDI 0x09 /* No base vdi found */
51 #define SD_RES_VDI_READ 0x0A /* Cannot read requested vdi */
52 #define SD_RES_VDI_WRITE 0x0B /* Cannot write requested vdi */
53 #define SD_RES_BASE_VDI_READ 0x0C /* Cannot read base vdi */
54 #define SD_RES_BASE_VDI_WRITE 0x0D /* Cannot write base vdi */
55 #define SD_RES_NO_TAG 0x0E /* Requested tag is not found */
56 #define SD_RES_STARTUP 0x0F /* Sheepdog is on starting up */
57 #define SD_RES_VDI_NOT_LOCKED 0x10 /* Vdi is not locked */
58 #define SD_RES_SHUTDOWN 0x11 /* Sheepdog is shutting down */
59 #define SD_RES_NO_MEM 0x12 /* Cannot allocate memory */
60 #define SD_RES_FULL_VDI 0x13 /* we already have the maximum vdis */
61 #define SD_RES_VER_MISMATCH 0x14 /* Protocol version mismatch */
62 #define SD_RES_NO_SPACE 0x15 /* Server has no room for new objects */
63 #define SD_RES_WAIT_FOR_FORMAT 0x16 /* Waiting for a format operation */
64 #define SD_RES_WAIT_FOR_JOIN 0x17 /* Waiting for other nodes joining */
65 #define SD_RES_JOIN_FAILED 0x18 /* Target node had failed to join sheepdog */
70 * 0 - 19 (20 bits): data object space
71 * 20 - 31 (12 bits): reserved data object space
72 * 32 - 55 (24 bits): vdi object space
73 * 56 - 59 ( 4 bits): reserved vdi object space
74 * 60 - 63 ( 4 bits): object type identifier space
77 #define VDI_SPACE_SHIFT 32
78 #define VDI_BIT (UINT64_C(1) << 63)
79 #define VMSTATE_BIT (UINT64_C(1) << 62)
80 #define MAX_DATA_OBJS (UINT64_C(1) << 20)
81 #define MAX_CHILDREN 1024
82 #define SD_MAX_VDI_LEN 256
83 #define SD_MAX_VDI_TAG_LEN 256
84 #define SD_NR_VDIS (1U << 24)
85 #define SD_DATA_OBJ_SIZE (UINT64_C(1) << 22)
86 #define SD_MAX_VDI_SIZE (SD_DATA_OBJ_SIZE * MAX_DATA_OBJS)
87 #define SECTOR_SIZE 512
89 #define SD_INODE_SIZE (sizeof(SheepdogInode))
90 #define CURRENT_VDI_ID 0
92 typedef struct SheepdogReq
{
99 uint32_t opcode_specific
[8];
102 typedef struct SheepdogRsp
{
108 uint32_t data_length
;
110 uint32_t opcode_specific
[7];
113 typedef struct SheepdogObjReq
{
119 uint32_t data_length
;
127 typedef struct SheepdogObjRsp
{
133 uint32_t data_length
;
139 typedef struct SheepdogVdiReq
{
145 uint32_t data_length
;
147 uint32_t base_vdi_id
;
153 typedef struct SheepdogVdiRsp
{
159 uint32_t data_length
;
166 typedef struct SheepdogInode
{
167 char name
[SD_MAX_VDI_LEN
];
168 char tag
[SD_MAX_VDI_TAG_LEN
];
171 uint64_t vm_clock_nsec
;
173 uint64_t vm_state_size
;
174 uint16_t copy_policy
;
176 uint8_t block_size_shift
;
179 uint32_t parent_vdi_id
;
180 uint32_t child_vdi_id
[MAX_CHILDREN
];
181 uint32_t data_vdi_id
[MAX_DATA_OBJS
];
185 * 64 bit FNV-1a non-zero initial basis
187 #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
190 * 64 bit Fowler/Noll/Vo FNV-1a hash code
192 static inline uint64_t fnv_64a_buf(void *buf
, size_t len
, uint64_t hval
)
194 unsigned char *bp
= buf
;
195 unsigned char *be
= bp
+ len
;
197 hval
^= (uint64_t) *bp
++;
198 hval
+= (hval
<< 1) + (hval
<< 4) + (hval
<< 5) +
199 (hval
<< 7) + (hval
<< 8) + (hval
<< 40);
204 static inline int is_data_obj_writable(SheepdogInode
*inode
, unsigned int idx
)
206 return inode
->vdi_id
== inode
->data_vdi_id
[idx
];
209 static inline int is_data_obj(uint64_t oid
)
211 return !(VDI_BIT
& oid
);
214 static inline uint64_t data_oid_to_idx(uint64_t oid
)
216 return oid
& (MAX_DATA_OBJS
- 1);
219 static inline uint64_t vid_to_vdi_oid(uint32_t vid
)
221 return VDI_BIT
| ((uint64_t)vid
<< VDI_SPACE_SHIFT
);
224 static inline uint64_t vid_to_vmstate_oid(uint32_t vid
, uint32_t idx
)
226 return VMSTATE_BIT
| ((uint64_t)vid
<< VDI_SPACE_SHIFT
) | idx
;
229 static inline uint64_t vid_to_data_oid(uint32_t vid
, uint32_t idx
)
231 return ((uint64_t)vid
<< VDI_SPACE_SHIFT
) | idx
;
234 static inline int is_snapshot(struct SheepdogInode
*inode
)
236 return !!inode
->snap_ctime
;
241 #define dprintf(fmt, args...) \
243 fprintf(stdout, "%s %d: " fmt, __func__, __LINE__, ##args); \
246 #define dprintf(fmt, args...)
249 typedef struct SheepdogAIOCB SheepdogAIOCB
;
251 typedef struct AIOReq
{
252 SheepdogAIOCB
*aiocb
;
253 unsigned int iov_offset
;
258 unsigned int data_len
;
262 QLIST_ENTRY(AIOReq
) outstanding_aio_siblings
;
263 QLIST_ENTRY(AIOReq
) aioreq_siblings
;
271 struct SheepdogAIOCB
{
272 BlockDriverAIOCB common
;
280 enum AIOCBState aiocb_type
;
282 Coroutine
*coroutine
;
283 void (*aio_done_func
)(SheepdogAIOCB
*);
287 QLIST_HEAD(aioreq_head
, AIOReq
) aioreq_head
;
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 uint8_t cache_enabled
;
309 uint32_t aioreq_seq_num
;
310 QLIST_HEAD(outstanding_aio_head
, AIOReq
) outstanding_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 outstanding_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
++;
389 QLIST_INSERT_HEAD(&s
->outstanding_aio_head
, aio_req
,
390 outstanding_aio_siblings
);
391 QLIST_INSERT_HEAD(&acb
->aioreq_head
, aio_req
, aioreq_siblings
);
396 static inline int free_aio_req(BDRVSheepdogState
*s
, AIOReq
*aio_req
)
398 SheepdogAIOCB
*acb
= aio_req
->aiocb
;
399 QLIST_REMOVE(aio_req
, outstanding_aio_siblings
);
400 QLIST_REMOVE(aio_req
, aioreq_siblings
);
403 return !QLIST_EMPTY(&acb
->aioreq_head
);
406 static void coroutine_fn
sd_finish_aiocb(SheepdogAIOCB
*acb
)
408 if (!acb
->canceled
) {
409 qemu_coroutine_enter(acb
->coroutine
, NULL
);
411 qemu_aio_release(acb
);
414 static void sd_aio_cancel(BlockDriverAIOCB
*blockacb
)
416 SheepdogAIOCB
*acb
= (SheepdogAIOCB
*)blockacb
;
419 * Sheepdog cannot cancel the requests which are already sent to
420 * the servers, so we just complete the request with -EIO here.
423 qemu_coroutine_enter(acb
->coroutine
, NULL
);
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
;
447 acb
->coroutine
= qemu_coroutine_self();
449 QLIST_INIT(&acb
->aioreq_head
);
453 static int connect_to_sdog(const char *addr
, const char *port
)
455 char hbuf
[NI_MAXHOST
], sbuf
[NI_MAXSERV
];
457 struct addrinfo hints
, *res
, *res0
;
460 addr
= SD_DEFAULT_ADDR
;
461 port
= SD_DEFAULT_PORT
;
464 memset(&hints
, 0, sizeof(hints
));
465 hints
.ai_socktype
= SOCK_STREAM
;
467 ret
= getaddrinfo(addr
, port
, &hints
, &res0
);
469 error_report("unable to get address info %s, %s",
470 addr
, strerror(errno
));
474 for (res
= res0
; res
; res
= res
->ai_next
) {
475 ret
= getnameinfo(res
->ai_addr
, res
->ai_addrlen
, hbuf
, sizeof(hbuf
),
476 sbuf
, sizeof(sbuf
), NI_NUMERICHOST
| NI_NUMERICSERV
);
481 fd
= socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
487 ret
= connect(fd
, res
->ai_addr
, res
->ai_addrlen
);
489 if (errno
== EINTR
) {
495 dprintf("connected to %s:%s\n", addr
, port
);
499 error_report("failed connect to %s:%s", addr
, port
);
505 static int send_req(int sockfd
, SheepdogReq
*hdr
, void *data
,
510 ret
= qemu_send_full(sockfd
, hdr
, sizeof(*hdr
), 0);
511 if (ret
< sizeof(*hdr
)) {
512 error_report("failed to send a req, %s", strerror(errno
));
516 ret
= qemu_send_full(sockfd
, data
, *wlen
, 0);
518 error_report("failed to send a req, %s", strerror(errno
));
525 static int send_co_req(int sockfd
, SheepdogReq
*hdr
, void *data
,
530 ret
= qemu_co_send(sockfd
, hdr
, sizeof(*hdr
));
531 if (ret
< sizeof(*hdr
)) {
532 error_report("failed to send a req, %s", strerror(errno
));
536 ret
= qemu_co_send(sockfd
, data
, *wlen
);
538 error_report("failed to send a req, %s", strerror(errno
));
543 static int do_req(int sockfd
, SheepdogReq
*hdr
, void *data
,
544 unsigned int *wlen
, unsigned int *rlen
)
548 socket_set_block(sockfd
);
549 ret
= send_req(sockfd
, hdr
, data
, wlen
);
554 ret
= qemu_recv_full(sockfd
, hdr
, sizeof(*hdr
), 0);
555 if (ret
< sizeof(*hdr
)) {
556 error_report("failed to get a rsp, %s", strerror(errno
));
561 if (*rlen
> hdr
->data_length
) {
562 *rlen
= hdr
->data_length
;
566 ret
= qemu_recv_full(sockfd
, data
, *rlen
, 0);
568 error_report("failed to get the data, %s", strerror(errno
));
575 socket_set_nonblock(sockfd
);
579 static int do_co_req(int sockfd
, SheepdogReq
*hdr
, void *data
,
580 unsigned int *wlen
, unsigned int *rlen
)
584 socket_set_block(sockfd
);
585 ret
= send_co_req(sockfd
, hdr
, data
, wlen
);
590 ret
= qemu_co_recv(sockfd
, hdr
, sizeof(*hdr
));
591 if (ret
< sizeof(*hdr
)) {
592 error_report("failed to get a rsp, %s", strerror(errno
));
597 if (*rlen
> hdr
->data_length
) {
598 *rlen
= hdr
->data_length
;
602 ret
= qemu_co_recv(sockfd
, data
, *rlen
);
604 error_report("failed to get the data, %s", strerror(errno
));
611 socket_set_nonblock(sockfd
);
615 static int coroutine_fn
add_aio_request(BDRVSheepdogState
*s
, AIOReq
*aio_req
,
616 struct iovec
*iov
, int niov
, int create
,
617 enum AIOCBState aiocb_type
);
620 * This function searchs pending requests to the object `oid', and
623 static void coroutine_fn
send_pending_req(BDRVSheepdogState
*s
, uint64_t oid
, uint32_t id
)
625 AIOReq
*aio_req
, *next
;
629 QLIST_FOREACH_SAFE(aio_req
, &s
->outstanding_aio_head
,
630 outstanding_aio_siblings
, next
) {
631 if (id
== aio_req
->id
) {
634 if (aio_req
->oid
!= oid
) {
638 acb
= aio_req
->aiocb
;
639 ret
= add_aio_request(s
, aio_req
, acb
->qiov
->iov
,
640 acb
->qiov
->niov
, 0, acb
->aiocb_type
);
642 error_report("add_aio_request is failed");
643 free_aio_req(s
, aio_req
);
644 if (QLIST_EMPTY(&acb
->aioreq_head
)) {
645 sd_finish_aiocb(acb
);
652 * Receive responses of the I/O requests.
654 * This function is registered as a fd handler, and called from the
655 * main loop when s->fd is ready for reading responses.
657 static void coroutine_fn
aio_read_response(void *opaque
)
660 BDRVSheepdogState
*s
= opaque
;
663 AIOReq
*aio_req
= NULL
;
668 if (QLIST_EMPTY(&s
->outstanding_aio_head
)) {
673 ret
= qemu_co_recv(fd
, &rsp
, sizeof(rsp
));
675 error_report("failed to get the header, %s", strerror(errno
));
679 /* find the right aio_req from the outstanding_aio list */
680 QLIST_FOREACH(aio_req
, &s
->outstanding_aio_head
, outstanding_aio_siblings
) {
681 if (aio_req
->id
== rsp
.id
) {
686 error_report("cannot find aio_req %x", rsp
.id
);
690 acb
= aio_req
->aiocb
;
692 switch (acb
->aiocb_type
) {
693 case AIOCB_WRITE_UDATA
:
694 /* this coroutine context is no longer suitable for co_recv
695 * because we may send data to update vdi objects */
697 if (!is_data_obj(aio_req
->oid
)) {
700 idx
= data_oid_to_idx(aio_req
->oid
);
702 if (s
->inode
.data_vdi_id
[idx
] != s
->inode
.vdi_id
) {
704 * If the object is newly created one, we need to update
705 * the vdi object (metadata object). min_dirty_data_idx
706 * and max_dirty_data_idx are changed to include updated
707 * index between them.
709 s
->inode
.data_vdi_id
[idx
] = s
->inode
.vdi_id
;
710 s
->max_dirty_data_idx
= MAX(idx
, s
->max_dirty_data_idx
);
711 s
->min_dirty_data_idx
= MIN(idx
, s
->min_dirty_data_idx
);
714 * Some requests may be blocked because simultaneous
715 * create requests are not allowed, so we search the
716 * pending requests here.
718 send_pending_req(s
, vid_to_data_oid(s
->inode
.vdi_id
, idx
), rsp
.id
);
721 case AIOCB_READ_UDATA
:
722 ret
= qemu_co_recvv(fd
, acb
->qiov
->iov
, rsp
.data_length
,
723 aio_req
->iov_offset
);
725 error_report("failed to get the data, %s", strerror(errno
));
731 if (rsp
.result
!= SD_RES_SUCCESS
) {
733 error_report("%s", sd_strerror(rsp
.result
));
736 rest
= free_aio_req(s
, aio_req
);
739 * We've finished all requests which belong to the AIOCB, so
740 * we can switch back to sd_co_readv/writev now.
742 acb
->aio_done_func(acb
);
748 static void co_read_response(void *opaque
)
750 BDRVSheepdogState
*s
= opaque
;
753 s
->co_recv
= qemu_coroutine_create(aio_read_response
);
756 qemu_coroutine_enter(s
->co_recv
, opaque
);
759 static void co_write_request(void *opaque
)
761 BDRVSheepdogState
*s
= opaque
;
763 qemu_coroutine_enter(s
->co_send
, NULL
);
766 static int aio_flush_request(void *opaque
)
768 BDRVSheepdogState
*s
= opaque
;
770 return !QLIST_EMPTY(&s
->outstanding_aio_head
);
773 static int set_nodelay(int fd
)
778 ret
= setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&opt
, sizeof(opt
));
783 * Return a socket discriptor to read/write objects.
785 * We cannot use this discriptor for other operations because
786 * the block driver may be on waiting response from the server.
788 static int get_sheep_fd(BDRVSheepdogState
*s
)
792 fd
= connect_to_sdog(s
->addr
, s
->port
);
794 error_report("%s", strerror(errno
));
798 socket_set_nonblock(fd
);
800 ret
= set_nodelay(fd
);
802 error_report("%s", strerror(errno
));
807 qemu_aio_set_fd_handler(fd
, co_read_response
, NULL
, aio_flush_request
, s
);
814 * filename must be one of the following formats:
816 * 2. [vdiname]:[snapid]
818 * 4. [hostname]:[port]:[vdiname]
819 * 5. [hostname]:[port]:[vdiname]:[snapid]
820 * 6. [hostname]:[port]:[vdiname]:[tag]
822 * You can boot from the snapshot images by specifying `snapid` or
825 * You can run VMs outside the Sheepdog cluster by specifying
826 * `hostname' and `port' (experimental).
828 static int parse_vdiname(BDRVSheepdogState
*s
, const char *filename
,
829 char *vdi
, uint32_t *snapid
, char *tag
)
834 p
= q
= g_strdup(filename
);
836 /* count the number of separators */
846 /* use the first two tokens as hostname and port number. */
860 strncpy(vdi
, p
, SD_MAX_VDI_LEN
);
862 p
= strchr(vdi
, ':');
865 *snapid
= strtoul(p
, NULL
, 10);
867 strncpy(tag
, p
, SD_MAX_VDI_TAG_LEN
);
870 *snapid
= CURRENT_VDI_ID
; /* search current vdi */
873 if (s
->addr
== NULL
) {
880 static int find_vdi_name(BDRVSheepdogState
*s
, char *filename
, uint32_t snapid
,
881 char *tag
, uint32_t *vid
, int for_snapshot
)
885 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
886 unsigned int wlen
, rlen
= 0;
887 char buf
[SD_MAX_VDI_LEN
+ SD_MAX_VDI_TAG_LEN
];
889 fd
= connect_to_sdog(s
->addr
, s
->port
);
894 memset(buf
, 0, sizeof(buf
));
895 strncpy(buf
, filename
, SD_MAX_VDI_LEN
);
896 strncpy(buf
+ SD_MAX_VDI_LEN
, tag
, SD_MAX_VDI_TAG_LEN
);
898 memset(&hdr
, 0, sizeof(hdr
));
900 hdr
.opcode
= SD_OP_GET_VDI_INFO
;
902 hdr
.opcode
= SD_OP_LOCK_VDI
;
904 wlen
= SD_MAX_VDI_LEN
+ SD_MAX_VDI_TAG_LEN
;
905 hdr
.proto_ver
= SD_PROTO_VER
;
906 hdr
.data_length
= wlen
;
908 hdr
.flags
= SD_FLAG_CMD_WRITE
;
910 ret
= do_req(fd
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
915 if (rsp
->result
!= SD_RES_SUCCESS
) {
916 error_report("cannot get vdi info, %s, %s %d %s",
917 sd_strerror(rsp
->result
), filename
, snapid
, tag
);
918 if (rsp
->result
== SD_RES_NO_VDI
) {
933 static int coroutine_fn
add_aio_request(BDRVSheepdogState
*s
, AIOReq
*aio_req
,
934 struct iovec
*iov
, int niov
, int create
,
935 enum AIOCBState aiocb_type
)
937 int nr_copies
= s
->inode
.nr_copies
;
941 uint64_t oid
= aio_req
->oid
;
942 unsigned int datalen
= aio_req
->data_len
;
943 uint64_t offset
= aio_req
->offset
;
944 uint8_t flags
= aio_req
->flags
;
945 uint64_t old_oid
= aio_req
->base_oid
;
951 memset(&hdr
, 0, sizeof(hdr
));
953 if (aiocb_type
== AIOCB_READ_UDATA
) {
955 hdr
.opcode
= SD_OP_READ_OBJ
;
959 hdr
.opcode
= SD_OP_CREATE_AND_WRITE_OBJ
;
960 hdr
.flags
= SD_FLAG_CMD_WRITE
| flags
;
963 hdr
.opcode
= SD_OP_WRITE_OBJ
;
964 hdr
.flags
= SD_FLAG_CMD_WRITE
| flags
;
967 if (s
->cache_enabled
) {
968 hdr
.flags
|= SD_FLAG_CMD_CACHE
;
972 hdr
.cow_oid
= old_oid
;
973 hdr
.copies
= s
->inode
.nr_copies
;
975 hdr
.data_length
= datalen
;
978 hdr
.id
= aio_req
->id
;
980 qemu_co_mutex_lock(&s
->lock
);
981 s
->co_send
= qemu_coroutine_self();
982 qemu_aio_set_fd_handler(s
->fd
, co_read_response
, co_write_request
,
983 aio_flush_request
, s
);
984 socket_set_cork(s
->fd
, 1);
987 ret
= qemu_co_send(s
->fd
, &hdr
, sizeof(hdr
));
989 qemu_co_mutex_unlock(&s
->lock
);
990 error_report("failed to send a req, %s", strerror(errno
));
995 ret
= qemu_co_sendv(s
->fd
, iov
, wlen
, aio_req
->iov_offset
);
997 qemu_co_mutex_unlock(&s
->lock
);
998 error_report("failed to send a data, %s", strerror(errno
));
1003 socket_set_cork(s
->fd
, 0);
1004 qemu_aio_set_fd_handler(s
->fd
, co_read_response
, NULL
,
1005 aio_flush_request
, s
);
1006 qemu_co_mutex_unlock(&s
->lock
);
1011 static int read_write_object(int fd
, char *buf
, uint64_t oid
, int copies
,
1012 unsigned int datalen
, uint64_t offset
,
1013 int write
, int create
, uint8_t cache
)
1016 SheepdogObjRsp
*rsp
= (SheepdogObjRsp
*)&hdr
;
1017 unsigned int wlen
, rlen
;
1020 memset(&hdr
, 0, sizeof(hdr
));
1025 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1027 hdr
.opcode
= SD_OP_CREATE_AND_WRITE_OBJ
;
1029 hdr
.opcode
= SD_OP_WRITE_OBJ
;
1034 hdr
.opcode
= SD_OP_READ_OBJ
;
1038 hdr
.flags
|= SD_FLAG_CMD_CACHE
;
1042 hdr
.data_length
= datalen
;
1043 hdr
.offset
= offset
;
1044 hdr
.copies
= copies
;
1046 ret
= do_req(fd
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1048 error_report("failed to send a request to the sheep");
1052 switch (rsp
->result
) {
1053 case SD_RES_SUCCESS
:
1056 error_report("%s", sd_strerror(rsp
->result
));
1061 static int read_object(int fd
, char *buf
, uint64_t oid
, int copies
,
1062 unsigned int datalen
, uint64_t offset
, uint8_t cache
)
1064 return read_write_object(fd
, buf
, oid
, copies
, datalen
, offset
, 0, 0,
1068 static int write_object(int fd
, char *buf
, uint64_t oid
, int copies
,
1069 unsigned int datalen
, uint64_t offset
, int create
,
1072 return read_write_object(fd
, buf
, oid
, copies
, datalen
, offset
, 1, create
,
1076 static int sd_open(BlockDriverState
*bs
, const char *filename
, int flags
)
1080 BDRVSheepdogState
*s
= bs
->opaque
;
1081 char vdi
[SD_MAX_VDI_LEN
], tag
[SD_MAX_VDI_TAG_LEN
];
1085 strstart(filename
, "sheepdog:", (const char **)&filename
);
1087 QLIST_INIT(&s
->outstanding_aio_head
);
1090 memset(vdi
, 0, sizeof(vdi
));
1091 memset(tag
, 0, sizeof(tag
));
1092 if (parse_vdiname(s
, filename
, vdi
, &snapid
, tag
) < 0) {
1096 s
->fd
= get_sheep_fd(s
);
1102 ret
= find_vdi_name(s
, vdi
, snapid
, tag
, &vid
, 0);
1107 if (flags
& BDRV_O_CACHE_WB
) {
1108 s
->cache_enabled
= 1;
1109 s
->flush_fd
= connect_to_sdog(s
->addr
, s
->port
);
1110 if (s
->flush_fd
< 0) {
1111 error_report("failed to connect");
1117 if (snapid
|| tag
[0] != '\0') {
1118 dprintf("%" PRIx32
" snapshot inode was open.\n", vid
);
1122 fd
= connect_to_sdog(s
->addr
, s
->port
);
1124 error_report("failed to connect");
1129 buf
= g_malloc(SD_INODE_SIZE
);
1130 ret
= read_object(fd
, buf
, vid_to_vdi_oid(vid
), 0, SD_INODE_SIZE
, 0,
1139 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
1140 s
->min_dirty_data_idx
= UINT32_MAX
;
1141 s
->max_dirty_data_idx
= 0;
1143 bs
->total_sectors
= s
->inode
.vdi_size
/ SECTOR_SIZE
;
1144 strncpy(s
->name
, vdi
, sizeof(s
->name
));
1145 qemu_co_mutex_init(&s
->lock
);
1149 qemu_aio_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
, NULL
);
1157 static int do_sd_create(char *filename
, int64_t vdi_size
,
1158 uint32_t base_vid
, uint32_t *vdi_id
, int snapshot
,
1159 const char *addr
, const char *port
)
1162 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1164 unsigned int wlen
, rlen
= 0;
1165 char buf
[SD_MAX_VDI_LEN
];
1167 fd
= connect_to_sdog(addr
, port
);
1172 memset(buf
, 0, sizeof(buf
));
1173 strncpy(buf
, filename
, SD_MAX_VDI_LEN
);
1175 memset(&hdr
, 0, sizeof(hdr
));
1176 hdr
.opcode
= SD_OP_NEW_VDI
;
1177 hdr
.base_vdi_id
= base_vid
;
1179 wlen
= SD_MAX_VDI_LEN
;
1181 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1182 hdr
.snapid
= snapshot
;
1184 hdr
.data_length
= wlen
;
1185 hdr
.vdi_size
= vdi_size
;
1187 ret
= do_req(fd
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1195 if (rsp
->result
!= SD_RES_SUCCESS
) {
1196 error_report("%s, %s", sd_strerror(rsp
->result
), filename
);
1201 *vdi_id
= rsp
->vdi_id
;
1207 static int sd_prealloc(const char *filename
)
1209 BlockDriverState
*bs
= NULL
;
1210 uint32_t idx
, max_idx
;
1212 void *buf
= g_malloc0(SD_DATA_OBJ_SIZE
);
1215 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDWR
);
1220 vdi_size
= bdrv_getlength(bs
);
1225 max_idx
= DIV_ROUND_UP(vdi_size
, SD_DATA_OBJ_SIZE
);
1227 for (idx
= 0; idx
< max_idx
; idx
++) {
1229 * The created image can be a cloned image, so we need to read
1230 * a data from the source image.
1232 ret
= bdrv_pread(bs
, idx
* SD_DATA_OBJ_SIZE
, buf
, SD_DATA_OBJ_SIZE
);
1236 ret
= bdrv_pwrite(bs
, idx
* SD_DATA_OBJ_SIZE
, buf
, SD_DATA_OBJ_SIZE
);
1250 static int sd_create(const char *filename
, QEMUOptionParameter
*options
)
1253 uint32_t vid
= 0, base_vid
= 0;
1254 int64_t vdi_size
= 0;
1255 char *backing_file
= NULL
;
1256 BDRVSheepdogState
*s
;
1257 char vdi
[SD_MAX_VDI_LEN
], tag
[SD_MAX_VDI_TAG_LEN
];
1260 const char *vdiname
;
1262 s
= g_malloc0(sizeof(BDRVSheepdogState
));
1264 strstart(filename
, "sheepdog:", &vdiname
);
1266 memset(vdi
, 0, sizeof(vdi
));
1267 memset(tag
, 0, sizeof(tag
));
1268 if (parse_vdiname(s
, vdiname
, vdi
, &snapid
, tag
) < 0) {
1269 error_report("invalid filename");
1274 while (options
&& options
->name
) {
1275 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
1276 vdi_size
= options
->value
.n
;
1277 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
1278 backing_file
= options
->value
.s
;
1279 } else if (!strcmp(options
->name
, BLOCK_OPT_PREALLOC
)) {
1280 if (!options
->value
.s
|| !strcmp(options
->value
.s
, "off")) {
1282 } else if (!strcmp(options
->value
.s
, "full")) {
1285 error_report("Invalid preallocation mode: '%s'",
1294 if (vdi_size
> SD_MAX_VDI_SIZE
) {
1295 error_report("too big image size");
1301 BlockDriverState
*bs
;
1302 BDRVSheepdogState
*s
;
1305 /* Currently, only Sheepdog backing image is supported. */
1306 drv
= bdrv_find_protocol(backing_file
);
1307 if (!drv
|| strcmp(drv
->protocol_name
, "sheepdog") != 0) {
1308 error_report("backing_file must be a sheepdog image");
1313 ret
= bdrv_file_open(&bs
, backing_file
, 0);
1320 if (!is_snapshot(&s
->inode
)) {
1321 error_report("cannot clone from a non snapshot vdi");
1327 base_vid
= s
->inode
.vdi_id
;
1331 ret
= do_sd_create(vdi
, vdi_size
, base_vid
, &vid
, 0, s
->addr
, s
->port
);
1332 if (!prealloc
|| ret
) {
1336 ret
= sd_prealloc(filename
);
1342 static void sd_close(BlockDriverState
*bs
)
1344 BDRVSheepdogState
*s
= bs
->opaque
;
1346 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1347 unsigned int wlen
, rlen
= 0;
1350 dprintf("%s\n", s
->name
);
1352 fd
= connect_to_sdog(s
->addr
, s
->port
);
1357 memset(&hdr
, 0, sizeof(hdr
));
1359 hdr
.opcode
= SD_OP_RELEASE_VDI
;
1360 wlen
= strlen(s
->name
) + 1;
1361 hdr
.data_length
= wlen
;
1362 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1364 ret
= do_req(fd
, (SheepdogReq
*)&hdr
, s
->name
, &wlen
, &rlen
);
1368 if (!ret
&& rsp
->result
!= SD_RES_SUCCESS
&&
1369 rsp
->result
!= SD_RES_VDI_NOT_LOCKED
) {
1370 error_report("%s, %s", sd_strerror(rsp
->result
), s
->name
);
1373 qemu_aio_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
, NULL
);
1375 if (s
->cache_enabled
) {
1376 closesocket(s
->flush_fd
);
1381 static int64_t sd_getlength(BlockDriverState
*bs
)
1383 BDRVSheepdogState
*s
= bs
->opaque
;
1385 return s
->inode
.vdi_size
;
1388 static int sd_truncate(BlockDriverState
*bs
, int64_t offset
)
1390 BDRVSheepdogState
*s
= bs
->opaque
;
1392 unsigned int datalen
;
1394 if (offset
< s
->inode
.vdi_size
) {
1395 error_report("shrinking is not supported");
1397 } else if (offset
> SD_MAX_VDI_SIZE
) {
1398 error_report("too big image size");
1402 fd
= connect_to_sdog(s
->addr
, s
->port
);
1407 /* we don't need to update entire object */
1408 datalen
= SD_INODE_SIZE
- sizeof(s
->inode
.data_vdi_id
);
1409 s
->inode
.vdi_size
= offset
;
1410 ret
= write_object(fd
, (char *)&s
->inode
, vid_to_vdi_oid(s
->inode
.vdi_id
),
1411 s
->inode
.nr_copies
, datalen
, 0, 0, s
->cache_enabled
);
1415 error_report("failed to update an inode.");
1422 * This function is called after writing data objects. If we need to
1423 * update metadata, this sends a write request to the vdi object.
1424 * Otherwise, this switches back to sd_co_readv/writev.
1426 static void coroutine_fn
sd_write_done(SheepdogAIOCB
*acb
)
1429 BDRVSheepdogState
*s
= acb
->common
.bs
->opaque
;
1432 uint32_t offset
, data_len
, mn
, mx
;
1434 mn
= s
->min_dirty_data_idx
;
1435 mx
= s
->max_dirty_data_idx
;
1437 /* we need to update the vdi object. */
1438 offset
= sizeof(s
->inode
) - sizeof(s
->inode
.data_vdi_id
) +
1439 mn
* sizeof(s
->inode
.data_vdi_id
[0]);
1440 data_len
= (mx
- mn
+ 1) * sizeof(s
->inode
.data_vdi_id
[0]);
1442 s
->min_dirty_data_idx
= UINT32_MAX
;
1443 s
->max_dirty_data_idx
= 0;
1445 iov
.iov_base
= &s
->inode
;
1446 iov
.iov_len
= sizeof(s
->inode
);
1447 aio_req
= alloc_aio_req(s
, acb
, vid_to_vdi_oid(s
->inode
.vdi_id
),
1448 data_len
, offset
, 0, 0, offset
);
1449 ret
= add_aio_request(s
, aio_req
, &iov
, 1, 0, AIOCB_WRITE_UDATA
);
1451 free_aio_req(s
, aio_req
);
1456 acb
->aio_done_func
= sd_finish_aiocb
;
1457 acb
->aiocb_type
= AIOCB_WRITE_UDATA
;
1461 sd_finish_aiocb(acb
);
1465 * Create a writable VDI from a snapshot
1467 static int sd_create_branch(BDRVSheepdogState
*s
)
1473 dprintf("%" PRIx32
" is snapshot.\n", s
->inode
.vdi_id
);
1475 buf
= g_malloc(SD_INODE_SIZE
);
1477 ret
= do_sd_create(s
->name
, s
->inode
.vdi_size
, s
->inode
.vdi_id
, &vid
, 1,
1483 dprintf("%" PRIx32
" is created.\n", vid
);
1485 fd
= connect_to_sdog(s
->addr
, s
->port
);
1487 error_report("failed to connect");
1492 ret
= read_object(fd
, buf
, vid_to_vdi_oid(vid
), s
->inode
.nr_copies
,
1493 SD_INODE_SIZE
, 0, s
->cache_enabled
);
1501 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
1505 dprintf("%" PRIx32
" was newly created.\n", s
->inode
.vdi_id
);
1514 * Send I/O requests to the server.
1516 * This function sends requests to the server, links the requests to
1517 * the outstanding_list in BDRVSheepdogState, and exits without
1518 * waiting the response. The responses are received in the
1519 * `aio_read_response' function which is called from the main loop as
1522 * Returns 1 when we need to wait a response, 0 when there is no sent
1523 * request and -errno in error cases.
1525 static int coroutine_fn
sd_co_rw_vector(void *p
)
1527 SheepdogAIOCB
*acb
= p
;
1529 unsigned long len
, done
= 0, total
= acb
->nb_sectors
* SECTOR_SIZE
;
1530 unsigned long idx
= acb
->sector_num
* SECTOR_SIZE
/ SD_DATA_OBJ_SIZE
;
1532 uint64_t offset
= (acb
->sector_num
* SECTOR_SIZE
) % SD_DATA_OBJ_SIZE
;
1533 BDRVSheepdogState
*s
= acb
->common
.bs
->opaque
;
1534 SheepdogInode
*inode
= &s
->inode
;
1537 if (acb
->aiocb_type
== AIOCB_WRITE_UDATA
&& s
->is_snapshot
) {
1539 * In the case we open the snapshot VDI, Sheepdog creates the
1540 * writable VDI when we do a write operation first.
1542 ret
= sd_create_branch(s
);
1549 while (done
!= total
) {
1551 uint64_t old_oid
= 0;
1554 oid
= vid_to_data_oid(inode
->data_vdi_id
[idx
], idx
);
1556 len
= MIN(total
- done
, SD_DATA_OBJ_SIZE
- offset
);
1558 if (!inode
->data_vdi_id
[idx
]) {
1559 if (acb
->aiocb_type
== AIOCB_READ_UDATA
) {
1564 } else if (acb
->aiocb_type
== AIOCB_WRITE_UDATA
1565 && !is_data_obj_writable(inode
, idx
)) {
1569 flags
= SD_FLAG_CMD_COW
;
1573 dprintf("update ino (%" PRIu32
") %" PRIu64
" %" PRIu64
1574 " %" PRIu64
"\n", inode
->vdi_id
, oid
,
1575 vid_to_data_oid(inode
->data_vdi_id
[idx
], idx
), idx
);
1576 oid
= vid_to_data_oid(inode
->vdi_id
, idx
);
1577 dprintf("new oid %lx\n", oid
);
1580 aio_req
= alloc_aio_req(s
, acb
, oid
, len
, offset
, flags
, old_oid
, done
);
1584 QLIST_FOREACH(areq
, &s
->outstanding_aio_head
,
1585 outstanding_aio_siblings
) {
1586 if (areq
== aio_req
) {
1589 if (areq
->oid
== oid
) {
1591 * Sheepdog cannot handle simultaneous create
1592 * requests to the same object. So we cannot send
1593 * the request until the previous request
1597 aio_req
->base_oid
= 0;
1603 ret
= add_aio_request(s
, aio_req
, acb
->qiov
->iov
, acb
->qiov
->niov
,
1604 create
, acb
->aiocb_type
);
1606 error_report("add_aio_request is failed");
1607 free_aio_req(s
, aio_req
);
1617 if (QLIST_EMPTY(&acb
->aioreq_head
)) {
1623 static coroutine_fn
int sd_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
1624 int nb_sectors
, QEMUIOVector
*qiov
)
1629 if (bs
->growable
&& sector_num
+ nb_sectors
> bs
->total_sectors
) {
1630 /* TODO: shouldn't block here */
1631 ret
= sd_truncate(bs
, (sector_num
+ nb_sectors
) * SECTOR_SIZE
);
1635 bs
->total_sectors
= sector_num
+ nb_sectors
;
1638 acb
= sd_aio_setup(bs
, qiov
, sector_num
, nb_sectors
, NULL
, NULL
);
1639 acb
->aio_done_func
= sd_write_done
;
1640 acb
->aiocb_type
= AIOCB_WRITE_UDATA
;
1642 ret
= sd_co_rw_vector(acb
);
1644 qemu_aio_release(acb
);
1648 qemu_coroutine_yield();
1653 static coroutine_fn
int sd_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
1654 int nb_sectors
, QEMUIOVector
*qiov
)
1659 acb
= sd_aio_setup(bs
, qiov
, sector_num
, nb_sectors
, NULL
, NULL
);
1660 acb
->aiocb_type
= AIOCB_READ_UDATA
;
1661 acb
->aio_done_func
= sd_finish_aiocb
;
1664 * TODO: we can do better; we don't need to initialize
1667 for (i
= 0; i
< qiov
->niov
; i
++) {
1668 memset(qiov
->iov
[i
].iov_base
, 0, qiov
->iov
[i
].iov_len
);
1671 ret
= sd_co_rw_vector(acb
);
1673 qemu_aio_release(acb
);
1677 qemu_coroutine_yield();
1682 static int coroutine_fn
sd_co_flush_to_disk(BlockDriverState
*bs
)
1684 BDRVSheepdogState
*s
= bs
->opaque
;
1685 SheepdogObjReq hdr
= { 0 };
1686 SheepdogObjRsp
*rsp
= (SheepdogObjRsp
*)&hdr
;
1687 SheepdogInode
*inode
= &s
->inode
;
1689 unsigned int wlen
= 0, rlen
= 0;
1691 if (!s
->cache_enabled
) {
1695 hdr
.opcode
= SD_OP_FLUSH_VDI
;
1696 hdr
.oid
= vid_to_vdi_oid(inode
->vdi_id
);
1698 ret
= do_co_req(s
->flush_fd
, (SheepdogReq
*)&hdr
, NULL
, &wlen
, &rlen
);
1700 error_report("failed to send a request to the sheep");
1704 if (rsp
->result
== SD_RES_INVALID_PARMS
) {
1705 dprintf("disable write cache since the server doesn't support it\n");
1707 s
->cache_enabled
= 0;
1708 closesocket(s
->flush_fd
);
1712 if (rsp
->result
!= SD_RES_SUCCESS
) {
1713 error_report("%s", sd_strerror(rsp
->result
));
1720 static int sd_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
)
1722 BDRVSheepdogState
*s
= bs
->opaque
;
1725 SheepdogInode
*inode
;
1726 unsigned int datalen
;
1728 dprintf("sn_info: name %s id_str %s s: name %s vm_state_size %d "
1729 "is_snapshot %d\n", sn_info
->name
, sn_info
->id_str
,
1730 s
->name
, sn_info
->vm_state_size
, s
->is_snapshot
);
1732 if (s
->is_snapshot
) {
1733 error_report("You can't create a snapshot of a snapshot VDI, "
1734 "%s (%" PRIu32
").", s
->name
, s
->inode
.vdi_id
);
1739 dprintf("%s %s\n", sn_info
->name
, sn_info
->id_str
);
1741 s
->inode
.vm_state_size
= sn_info
->vm_state_size
;
1742 s
->inode
.vm_clock_nsec
= sn_info
->vm_clock_nsec
;
1743 strncpy(s
->inode
.tag
, sn_info
->name
, sizeof(s
->inode
.tag
));
1744 /* we don't need to update entire object */
1745 datalen
= SD_INODE_SIZE
- sizeof(s
->inode
.data_vdi_id
);
1747 /* refresh inode. */
1748 fd
= connect_to_sdog(s
->addr
, s
->port
);
1754 ret
= write_object(fd
, (char *)&s
->inode
, vid_to_vdi_oid(s
->inode
.vdi_id
),
1755 s
->inode
.nr_copies
, datalen
, 0, 0, s
->cache_enabled
);
1757 error_report("failed to write snapshot's inode.");
1761 ret
= do_sd_create(s
->name
, s
->inode
.vdi_size
, s
->inode
.vdi_id
, &new_vid
, 1,
1764 error_report("failed to create inode for snapshot. %s",
1769 inode
= (SheepdogInode
*)g_malloc(datalen
);
1771 ret
= read_object(fd
, (char *)inode
, vid_to_vdi_oid(new_vid
),
1772 s
->inode
.nr_copies
, datalen
, 0, s
->cache_enabled
);
1775 error_report("failed to read new inode info. %s", strerror(errno
));
1779 memcpy(&s
->inode
, inode
, datalen
);
1780 dprintf("s->inode: name %s snap_id %x oid %x\n",
1781 s
->inode
.name
, s
->inode
.snap_id
, s
->inode
.vdi_id
);
1788 static int sd_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
)
1790 BDRVSheepdogState
*s
= bs
->opaque
;
1791 BDRVSheepdogState
*old_s
;
1792 char vdi
[SD_MAX_VDI_LEN
], tag
[SD_MAX_VDI_TAG_LEN
];
1795 uint32_t snapid
= 0;
1798 old_s
= g_malloc(sizeof(BDRVSheepdogState
));
1800 memcpy(old_s
, s
, sizeof(BDRVSheepdogState
));
1802 memset(vdi
, 0, sizeof(vdi
));
1803 strncpy(vdi
, s
->name
, sizeof(vdi
));
1805 memset(tag
, 0, sizeof(tag
));
1806 snapid
= strtoul(snapshot_id
, NULL
, 10);
1808 strncpy(tag
, s
->name
, sizeof(tag
));
1811 ret
= find_vdi_name(s
, vdi
, snapid
, tag
, &vid
, 1);
1813 error_report("Failed to find_vdi_name");
1817 fd
= connect_to_sdog(s
->addr
, s
->port
);
1819 error_report("failed to connect");
1824 buf
= g_malloc(SD_INODE_SIZE
);
1825 ret
= read_object(fd
, buf
, vid_to_vdi_oid(vid
), s
->inode
.nr_copies
,
1826 SD_INODE_SIZE
, 0, s
->cache_enabled
);
1834 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
1836 if (!s
->inode
.vm_state_size
) {
1837 error_report("Invalid snapshot");
1849 /* recover bdrv_sd_state */
1850 memcpy(s
, old_s
, sizeof(BDRVSheepdogState
));
1854 error_report("failed to open. recover old bdrv_sd_state.");
1859 static int sd_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1861 /* FIXME: Delete specified snapshot id. */
1865 static int sd_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
)
1867 BDRVSheepdogState
*s
= bs
->opaque
;
1869 int fd
, nr
= 1024, ret
, max
= BITS_TO_LONGS(SD_NR_VDIS
) * sizeof(long);
1870 QEMUSnapshotInfo
*sn_tab
= NULL
;
1871 unsigned wlen
, rlen
;
1873 static SheepdogInode inode
;
1874 unsigned long *vdi_inuse
;
1875 unsigned int start_nr
;
1879 vdi_inuse
= g_malloc(max
);
1881 fd
= connect_to_sdog(s
->addr
, s
->port
);
1890 memset(&req
, 0, sizeof(req
));
1892 req
.opcode
= SD_OP_READ_VDIS
;
1893 req
.data_length
= max
;
1895 ret
= do_req(fd
, (SheepdogReq
*)&req
, vdi_inuse
, &wlen
, &rlen
);
1902 sn_tab
= g_malloc0(nr
* sizeof(*sn_tab
));
1904 /* calculate a vdi id with hash function */
1905 hval
= fnv_64a_buf(s
->name
, strlen(s
->name
), FNV1A_64_INIT
);
1906 start_nr
= hval
& (SD_NR_VDIS
- 1);
1908 fd
= connect_to_sdog(s
->addr
, s
->port
);
1910 error_report("failed to connect");
1915 for (vid
= start_nr
; found
< nr
; vid
= (vid
+ 1) % SD_NR_VDIS
) {
1916 if (!test_bit(vid
, vdi_inuse
)) {
1920 /* we don't need to read entire object */
1921 ret
= read_object(fd
, (char *)&inode
, vid_to_vdi_oid(vid
),
1922 0, SD_INODE_SIZE
- sizeof(inode
.data_vdi_id
), 0,
1929 if (!strcmp(inode
.name
, s
->name
) && is_snapshot(&inode
)) {
1930 sn_tab
[found
].date_sec
= inode
.snap_ctime
>> 32;
1931 sn_tab
[found
].date_nsec
= inode
.snap_ctime
& 0xffffffff;
1932 sn_tab
[found
].vm_state_size
= inode
.vm_state_size
;
1933 sn_tab
[found
].vm_clock_nsec
= inode
.vm_clock_nsec
;
1935 snprintf(sn_tab
[found
].id_str
, sizeof(sn_tab
[found
].id_str
), "%u",
1937 strncpy(sn_tab
[found
].name
, inode
.tag
,
1938 MIN(sizeof(sn_tab
[found
].name
), sizeof(inode
.tag
)));
1956 static int do_load_save_vmstate(BDRVSheepdogState
*s
, uint8_t *data
,
1957 int64_t pos
, int size
, int load
)
1960 int ret
= 0, remaining
= size
;
1961 unsigned int data_len
;
1962 uint64_t vmstate_oid
;
1966 fd
= connect_to_sdog(s
->addr
, s
->port
);
1972 vdi_index
= pos
/ SD_DATA_OBJ_SIZE
;
1973 offset
= pos
% SD_DATA_OBJ_SIZE
;
1975 data_len
= MIN(remaining
, SD_DATA_OBJ_SIZE
);
1977 vmstate_oid
= vid_to_vmstate_oid(s
->inode
.vdi_id
, vdi_index
);
1979 create
= (offset
== 0);
1981 ret
= read_object(fd
, (char *)data
, vmstate_oid
,
1982 s
->inode
.nr_copies
, data_len
, offset
,
1985 ret
= write_object(fd
, (char *)data
, vmstate_oid
,
1986 s
->inode
.nr_copies
, data_len
, offset
, create
,
1991 error_report("failed to save vmstate %s", strerror(errno
));
1996 remaining
-= data_len
;
2004 static int sd_save_vmstate(BlockDriverState
*bs
, const uint8_t *data
,
2005 int64_t pos
, int size
)
2007 BDRVSheepdogState
*s
= bs
->opaque
;
2009 return do_load_save_vmstate(s
, (uint8_t *)data
, pos
, size
, 0);
2012 static int sd_load_vmstate(BlockDriverState
*bs
, uint8_t *data
,
2013 int64_t pos
, int size
)
2015 BDRVSheepdogState
*s
= bs
->opaque
;
2017 return do_load_save_vmstate(s
, data
, pos
, size
, 1);
2021 static QEMUOptionParameter sd_create_options
[] = {
2023 .name
= BLOCK_OPT_SIZE
,
2025 .help
= "Virtual disk size"
2028 .name
= BLOCK_OPT_BACKING_FILE
,
2030 .help
= "File name of a base image"
2033 .name
= BLOCK_OPT_PREALLOC
,
2035 .help
= "Preallocation mode (allowed values: off, full)"
2040 BlockDriver bdrv_sheepdog
= {
2041 .format_name
= "sheepdog",
2042 .protocol_name
= "sheepdog",
2043 .instance_size
= sizeof(BDRVSheepdogState
),
2044 .bdrv_file_open
= sd_open
,
2045 .bdrv_close
= sd_close
,
2046 .bdrv_create
= sd_create
,
2047 .bdrv_getlength
= sd_getlength
,
2048 .bdrv_truncate
= sd_truncate
,
2050 .bdrv_co_readv
= sd_co_readv
,
2051 .bdrv_co_writev
= sd_co_writev
,
2052 .bdrv_co_flush_to_disk
= sd_co_flush_to_disk
,
2054 .bdrv_snapshot_create
= sd_snapshot_create
,
2055 .bdrv_snapshot_goto
= sd_snapshot_goto
,
2056 .bdrv_snapshot_delete
= sd_snapshot_delete
,
2057 .bdrv_snapshot_list
= sd_snapshot_list
,
2059 .bdrv_save_vmstate
= sd_save_vmstate
,
2060 .bdrv_load_vmstate
= sd_load_vmstate
,
2062 .create_options
= sd_create_options
,
2065 static void bdrv_sheepdog_init(void)
2067 bdrv_register(&bdrv_sheepdog
);
2069 block_init(bdrv_sheepdog_init
);