2 * Copyright (C) 2009-2010 Nippon Telegraph and Telephone Corporation.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License version
6 * 2 as published by the Free Software Foundation.
8 * You should have received a copy of the GNU General Public License
9 * along with this program. If not, see <http://www.gnu.org/licenses/>.
12 #include "qemu-common.h"
13 #include "qemu-error.h"
14 #include "qemu_socket.h"
15 #include "block_int.h"
18 #define SD_PROTO_VER 0x01
20 #define SD_DEFAULT_ADDR "localhost"
21 #define SD_DEFAULT_PORT "7000"
23 #define SD_OP_CREATE_AND_WRITE_OBJ 0x01
24 #define SD_OP_READ_OBJ 0x02
25 #define SD_OP_WRITE_OBJ 0x03
27 #define SD_OP_NEW_VDI 0x11
28 #define SD_OP_LOCK_VDI 0x12
29 #define SD_OP_RELEASE_VDI 0x13
30 #define SD_OP_GET_VDI_INFO 0x14
31 #define SD_OP_READ_VDIS 0x15
33 #define SD_FLAG_CMD_WRITE 0x01
34 #define SD_FLAG_CMD_COW 0x02
36 #define SD_RES_SUCCESS 0x00 /* Success */
37 #define SD_RES_UNKNOWN 0x01 /* Unknown error */
38 #define SD_RES_NO_OBJ 0x02 /* No object found */
39 #define SD_RES_EIO 0x03 /* I/O error */
40 #define SD_RES_VDI_EXIST 0x04 /* Vdi exists already */
41 #define SD_RES_INVALID_PARMS 0x05 /* Invalid parameters */
42 #define SD_RES_SYSTEM_ERROR 0x06 /* System error */
43 #define SD_RES_VDI_LOCKED 0x07 /* Vdi is locked */
44 #define SD_RES_NO_VDI 0x08 /* No vdi found */
45 #define SD_RES_NO_BASE_VDI 0x09 /* No base vdi found */
46 #define SD_RES_VDI_READ 0x0A /* Cannot read requested vdi */
47 #define SD_RES_VDI_WRITE 0x0B /* Cannot write requested vdi */
48 #define SD_RES_BASE_VDI_READ 0x0C /* Cannot read base vdi */
49 #define SD_RES_BASE_VDI_WRITE 0x0D /* Cannot write base vdi */
50 #define SD_RES_NO_TAG 0x0E /* Requested tag is not found */
51 #define SD_RES_STARTUP 0x0F /* Sheepdog is on starting up */
52 #define SD_RES_VDI_NOT_LOCKED 0x10 /* Vdi is not locked */
53 #define SD_RES_SHUTDOWN 0x11 /* Sheepdog is shutting down */
54 #define SD_RES_NO_MEM 0x12 /* Cannot allocate memory */
55 #define SD_RES_FULL_VDI 0x13 /* we already have the maximum vdis */
56 #define SD_RES_VER_MISMATCH 0x14 /* Protocol version mismatch */
57 #define SD_RES_NO_SPACE 0x15 /* Server has no room for new objects */
58 #define SD_RES_WAIT_FOR_FORMAT 0x16 /* Waiting for a format operation */
59 #define SD_RES_WAIT_FOR_JOIN 0x17 /* Waiting for other nodes joining */
60 #define SD_RES_JOIN_FAILED 0x18 /* Target node had failed to join sheepdog */
65 * 0 - 19 (20 bits): data object space
66 * 20 - 31 (12 bits): reserved data object space
67 * 32 - 55 (24 bits): vdi object space
68 * 56 - 59 ( 4 bits): reserved vdi object space
69 * 60 - 63 ( 4 bits): object type identifier space
72 #define VDI_SPACE_SHIFT 32
73 #define VDI_BIT (UINT64_C(1) << 63)
74 #define VMSTATE_BIT (UINT64_C(1) << 62)
75 #define MAX_DATA_OBJS (UINT64_C(1) << 20)
76 #define MAX_CHILDREN 1024
77 #define SD_MAX_VDI_LEN 256
78 #define SD_MAX_VDI_TAG_LEN 256
79 #define SD_NR_VDIS (1U << 24)
80 #define SD_DATA_OBJ_SIZE (UINT64_C(1) << 22)
81 #define SD_MAX_VDI_SIZE (SD_DATA_OBJ_SIZE * MAX_DATA_OBJS)
82 #define SECTOR_SIZE 512
84 #define SD_INODE_SIZE (sizeof(SheepdogInode))
85 #define CURRENT_VDI_ID 0
87 typedef struct SheepdogReq
{
94 uint32_t opcode_specific
[8];
97 typedef struct SheepdogRsp
{
103 uint32_t data_length
;
105 uint32_t opcode_specific
[7];
108 typedef struct SheepdogObjReq
{
114 uint32_t data_length
;
122 typedef struct SheepdogObjRsp
{
128 uint32_t data_length
;
134 typedef struct SheepdogVdiReq
{
140 uint32_t data_length
;
142 uint32_t base_vdi_id
;
148 typedef struct SheepdogVdiRsp
{
154 uint32_t data_length
;
161 typedef struct SheepdogInode
{
162 char name
[SD_MAX_VDI_LEN
];
163 char tag
[SD_MAX_VDI_TAG_LEN
];
166 uint64_t vm_clock_nsec
;
168 uint64_t vm_state_size
;
169 uint16_t copy_policy
;
171 uint8_t block_size_shift
;
174 uint32_t parent_vdi_id
;
175 uint32_t child_vdi_id
[MAX_CHILDREN
];
176 uint32_t data_vdi_id
[MAX_DATA_OBJS
];
180 * 64 bit FNV-1a non-zero initial basis
182 #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
185 * 64 bit Fowler/Noll/Vo FNV-1a hash code
187 static inline uint64_t fnv_64a_buf(void *buf
, size_t len
, uint64_t hval
)
189 unsigned char *bp
= buf
;
190 unsigned char *be
= bp
+ len
;
192 hval
^= (uint64_t) *bp
++;
193 hval
+= (hval
<< 1) + (hval
<< 4) + (hval
<< 5) +
194 (hval
<< 7) + (hval
<< 8) + (hval
<< 40);
199 static inline int is_data_obj_writable(SheepdogInode
*inode
, unsigned int idx
)
201 return inode
->vdi_id
== inode
->data_vdi_id
[idx
];
204 static inline int is_data_obj(uint64_t oid
)
206 return !(VDI_BIT
& oid
);
209 static inline uint64_t data_oid_to_idx(uint64_t oid
)
211 return oid
& (MAX_DATA_OBJS
- 1);
214 static inline uint64_t vid_to_vdi_oid(uint32_t vid
)
216 return VDI_BIT
| ((uint64_t)vid
<< VDI_SPACE_SHIFT
);
219 static inline uint64_t vid_to_vmstate_oid(uint32_t vid
, uint32_t idx
)
221 return VMSTATE_BIT
| ((uint64_t)vid
<< VDI_SPACE_SHIFT
) | idx
;
224 static inline uint64_t vid_to_data_oid(uint32_t vid
, uint32_t idx
)
226 return ((uint64_t)vid
<< VDI_SPACE_SHIFT
) | idx
;
229 static inline int is_snapshot(struct SheepdogInode
*inode
)
231 return !!inode
->snap_ctime
;
236 #define dprintf(fmt, args...) \
238 fprintf(stdout, "%s %d: " fmt, __func__, __LINE__, ##args); \
241 #define dprintf(fmt, args...)
244 typedef struct SheepdogAIOCB SheepdogAIOCB
;
246 typedef struct AIOReq
{
247 SheepdogAIOCB
*aiocb
;
248 unsigned int iov_offset
;
253 unsigned int data_len
;
257 QLIST_ENTRY(AIOReq
) outstanding_aio_siblings
;
258 QLIST_ENTRY(AIOReq
) aioreq_siblings
;
266 struct SheepdogAIOCB
{
267 BlockDriverAIOCB common
;
275 enum AIOCBState aiocb_type
;
277 Coroutine
*coroutine
;
278 void (*aio_done_func
)(SheepdogAIOCB
*);
282 QLIST_HEAD(aioreq_head
, AIOReq
) aioreq_head
;
285 typedef struct BDRVSheepdogState
{
288 uint32_t min_dirty_data_idx
;
289 uint32_t max_dirty_data_idx
;
291 char name
[SD_MAX_VDI_LEN
];
302 uint32_t aioreq_seq_num
;
303 QLIST_HEAD(outstanding_aio_head
, AIOReq
) outstanding_aio_head
;
306 static const char * sd_strerror(int err
)
310 static const struct {
314 {SD_RES_SUCCESS
, "Success"},
315 {SD_RES_UNKNOWN
, "Unknown error"},
316 {SD_RES_NO_OBJ
, "No object found"},
317 {SD_RES_EIO
, "I/O error"},
318 {SD_RES_VDI_EXIST
, "VDI exists already"},
319 {SD_RES_INVALID_PARMS
, "Invalid parameters"},
320 {SD_RES_SYSTEM_ERROR
, "System error"},
321 {SD_RES_VDI_LOCKED
, "VDI is already locked"},
322 {SD_RES_NO_VDI
, "No vdi found"},
323 {SD_RES_NO_BASE_VDI
, "No base VDI found"},
324 {SD_RES_VDI_READ
, "Failed read the requested VDI"},
325 {SD_RES_VDI_WRITE
, "Failed to write the requested VDI"},
326 {SD_RES_BASE_VDI_READ
, "Failed to read the base VDI"},
327 {SD_RES_BASE_VDI_WRITE
, "Failed to write the base VDI"},
328 {SD_RES_NO_TAG
, "Failed to find the requested tag"},
329 {SD_RES_STARTUP
, "The system is still booting"},
330 {SD_RES_VDI_NOT_LOCKED
, "VDI isn't locked"},
331 {SD_RES_SHUTDOWN
, "The system is shutting down"},
332 {SD_RES_NO_MEM
, "Out of memory on the server"},
333 {SD_RES_FULL_VDI
, "We already have the maximum vdis"},
334 {SD_RES_VER_MISMATCH
, "Protocol version mismatch"},
335 {SD_RES_NO_SPACE
, "Server has no space for new objects"},
336 {SD_RES_WAIT_FOR_FORMAT
, "Sheepdog is waiting for a format operation"},
337 {SD_RES_WAIT_FOR_JOIN
, "Sheepdog is waiting for other nodes joining"},
338 {SD_RES_JOIN_FAILED
, "Target node had failed to join sheepdog"},
341 for (i
= 0; i
< ARRAY_SIZE(errors
); ++i
) {
342 if (errors
[i
].err
== err
) {
343 return errors
[i
].desc
;
347 return "Invalid error code";
351 * Sheepdog I/O handling:
353 * 1. In sd_co_rw_vector, we send the I/O requests to the server and
354 * link the requests to the outstanding_list in the
355 * BDRVSheepdogState. The function exits without waiting for
356 * receiving the response.
358 * 2. We receive the response in aio_read_response, the fd handler to
359 * the sheepdog connection. If metadata update is needed, we send
360 * the write request to the vdi object in sd_write_done, the write
361 * completion function. We switch back to sd_co_readv/writev after
362 * all the requests belonging to the AIOCB are finished.
365 static inline AIOReq
*alloc_aio_req(BDRVSheepdogState
*s
, SheepdogAIOCB
*acb
,
366 uint64_t oid
, unsigned int data_len
,
367 uint64_t offset
, uint8_t flags
,
368 uint64_t base_oid
, unsigned int iov_offset
)
372 aio_req
= g_malloc(sizeof(*aio_req
));
373 aio_req
->aiocb
= acb
;
374 aio_req
->iov_offset
= iov_offset
;
376 aio_req
->base_oid
= base_oid
;
377 aio_req
->offset
= offset
;
378 aio_req
->data_len
= data_len
;
379 aio_req
->flags
= flags
;
380 aio_req
->id
= s
->aioreq_seq_num
++;
382 QLIST_INSERT_HEAD(&s
->outstanding_aio_head
, aio_req
,
383 outstanding_aio_siblings
);
384 QLIST_INSERT_HEAD(&acb
->aioreq_head
, aio_req
, aioreq_siblings
);
389 static inline int free_aio_req(BDRVSheepdogState
*s
, AIOReq
*aio_req
)
391 SheepdogAIOCB
*acb
= aio_req
->aiocb
;
392 QLIST_REMOVE(aio_req
, outstanding_aio_siblings
);
393 QLIST_REMOVE(aio_req
, aioreq_siblings
);
396 return !QLIST_EMPTY(&acb
->aioreq_head
);
399 static void coroutine_fn
sd_finish_aiocb(SheepdogAIOCB
*acb
)
401 if (!acb
->canceled
) {
402 qemu_coroutine_enter(acb
->coroutine
, NULL
);
404 qemu_aio_release(acb
);
407 static void sd_aio_cancel(BlockDriverAIOCB
*blockacb
)
409 SheepdogAIOCB
*acb
= (SheepdogAIOCB
*)blockacb
;
412 * Sheepdog cannot cancel the requests which are already sent to
413 * the servers, so we just complete the request with -EIO here.
416 qemu_coroutine_enter(acb
->coroutine
, NULL
);
420 static AIOPool sd_aio_pool
= {
421 .aiocb_size
= sizeof(SheepdogAIOCB
),
422 .cancel
= sd_aio_cancel
,
425 static SheepdogAIOCB
*sd_aio_setup(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
426 int64_t sector_num
, int nb_sectors
,
427 BlockDriverCompletionFunc
*cb
, void *opaque
)
431 acb
= qemu_aio_get(&sd_aio_pool
, bs
, cb
, opaque
);
435 acb
->sector_num
= sector_num
;
436 acb
->nb_sectors
= nb_sectors
;
438 acb
->aio_done_func
= NULL
;
440 acb
->coroutine
= qemu_coroutine_self();
442 QLIST_INIT(&acb
->aioreq_head
);
446 static int connect_to_sdog(const char *addr
, const char *port
)
448 char hbuf
[NI_MAXHOST
], sbuf
[NI_MAXSERV
];
450 struct addrinfo hints
, *res
, *res0
;
453 addr
= SD_DEFAULT_ADDR
;
454 port
= SD_DEFAULT_PORT
;
457 memset(&hints
, 0, sizeof(hints
));
458 hints
.ai_socktype
= SOCK_STREAM
;
460 ret
= getaddrinfo(addr
, port
, &hints
, &res0
);
462 error_report("unable to get address info %s, %s",
463 addr
, strerror(errno
));
467 for (res
= res0
; res
; res
= res
->ai_next
) {
468 ret
= getnameinfo(res
->ai_addr
, res
->ai_addrlen
, hbuf
, sizeof(hbuf
),
469 sbuf
, sizeof(sbuf
), NI_NUMERICHOST
| NI_NUMERICSERV
);
474 fd
= socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
480 ret
= connect(fd
, res
->ai_addr
, res
->ai_addrlen
);
482 if (errno
== EINTR
) {
488 dprintf("connected to %s:%s\n", addr
, port
);
492 error_report("failed connect to %s:%s", addr
, port
);
498 static int send_req(int sockfd
, SheepdogReq
*hdr
, void *data
,
503 ret
= qemu_send_full(sockfd
, hdr
, sizeof(*hdr
), 0);
504 if (ret
< sizeof(*hdr
)) {
505 error_report("failed to send a req, %s", strerror(errno
));
508 ret
= qemu_send_full(sockfd
, data
, *wlen
, 0);
510 error_report("failed to send a req, %s", strerror(errno
));
516 static int do_req(int sockfd
, SheepdogReq
*hdr
, void *data
,
517 unsigned int *wlen
, unsigned int *rlen
)
521 socket_set_block(sockfd
);
522 ret
= send_req(sockfd
, hdr
, data
, wlen
);
527 ret
= qemu_recv_full(sockfd
, hdr
, sizeof(*hdr
), 0);
528 if (ret
< sizeof(*hdr
)) {
529 error_report("failed to get a rsp, %s", strerror(errno
));
533 if (*rlen
> hdr
->data_length
) {
534 *rlen
= hdr
->data_length
;
538 ret
= qemu_recv_full(sockfd
, data
, *rlen
, 0);
540 error_report("failed to get the data, %s", strerror(errno
));
546 socket_set_nonblock(sockfd
);
550 static int coroutine_fn
add_aio_request(BDRVSheepdogState
*s
, AIOReq
*aio_req
,
551 struct iovec
*iov
, int niov
, int create
,
552 enum AIOCBState aiocb_type
);
555 * This function searchs pending requests to the object `oid', and
558 static void coroutine_fn
send_pending_req(BDRVSheepdogState
*s
, uint64_t oid
, uint32_t id
)
560 AIOReq
*aio_req
, *next
;
564 QLIST_FOREACH_SAFE(aio_req
, &s
->outstanding_aio_head
,
565 outstanding_aio_siblings
, next
) {
566 if (id
== aio_req
->id
) {
569 if (aio_req
->oid
!= oid
) {
573 acb
= aio_req
->aiocb
;
574 ret
= add_aio_request(s
, aio_req
, acb
->qiov
->iov
,
575 acb
->qiov
->niov
, 0, acb
->aiocb_type
);
577 error_report("add_aio_request is failed");
578 free_aio_req(s
, aio_req
);
579 if (QLIST_EMPTY(&acb
->aioreq_head
)) {
580 sd_finish_aiocb(acb
);
587 * Receive responses of the I/O requests.
589 * This function is registered as a fd handler, and called from the
590 * main loop when s->fd is ready for reading responses.
592 static void coroutine_fn
aio_read_response(void *opaque
)
595 BDRVSheepdogState
*s
= opaque
;
598 AIOReq
*aio_req
= NULL
;
603 if (QLIST_EMPTY(&s
->outstanding_aio_head
)) {
608 ret
= qemu_co_recv(fd
, &rsp
, sizeof(rsp
));
610 error_report("failed to get the header, %s", strerror(errno
));
614 /* find the right aio_req from the outstanding_aio list */
615 QLIST_FOREACH(aio_req
, &s
->outstanding_aio_head
, outstanding_aio_siblings
) {
616 if (aio_req
->id
== rsp
.id
) {
621 error_report("cannot find aio_req %x", rsp
.id
);
625 acb
= aio_req
->aiocb
;
627 switch (acb
->aiocb_type
) {
628 case AIOCB_WRITE_UDATA
:
629 if (!is_data_obj(aio_req
->oid
)) {
632 idx
= data_oid_to_idx(aio_req
->oid
);
634 if (s
->inode
.data_vdi_id
[idx
] != s
->inode
.vdi_id
) {
636 * If the object is newly created one, we need to update
637 * the vdi object (metadata object). min_dirty_data_idx
638 * and max_dirty_data_idx are changed to include updated
639 * index between them.
641 s
->inode
.data_vdi_id
[idx
] = s
->inode
.vdi_id
;
642 s
->max_dirty_data_idx
= MAX(idx
, s
->max_dirty_data_idx
);
643 s
->min_dirty_data_idx
= MIN(idx
, s
->min_dirty_data_idx
);
646 * Some requests may be blocked because simultaneous
647 * create requests are not allowed, so we search the
648 * pending requests here.
650 send_pending_req(s
, vid_to_data_oid(s
->inode
.vdi_id
, idx
), rsp
.id
);
653 case AIOCB_READ_UDATA
:
654 ret
= qemu_co_recvv(fd
, acb
->qiov
->iov
, rsp
.data_length
,
655 aio_req
->iov_offset
);
657 error_report("failed to get the data, %s", strerror(errno
));
663 if (rsp
.result
!= SD_RES_SUCCESS
) {
665 error_report("%s", sd_strerror(rsp
.result
));
668 rest
= free_aio_req(s
, aio_req
);
671 * We've finished all requests which belong to the AIOCB, so
672 * we can switch back to sd_co_readv/writev now.
674 acb
->aio_done_func(acb
);
680 static void co_read_response(void *opaque
)
682 BDRVSheepdogState
*s
= opaque
;
685 s
->co_recv
= qemu_coroutine_create(aio_read_response
);
688 qemu_coroutine_enter(s
->co_recv
, opaque
);
691 static void co_write_request(void *opaque
)
693 BDRVSheepdogState
*s
= opaque
;
695 qemu_coroutine_enter(s
->co_send
, NULL
);
698 static int aio_flush_request(void *opaque
)
700 BDRVSheepdogState
*s
= opaque
;
702 return !QLIST_EMPTY(&s
->outstanding_aio_head
);
705 static int set_nodelay(int fd
)
710 ret
= setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&opt
, sizeof(opt
));
715 * Return a socket discriptor to read/write objects.
717 * We cannot use this discriptor for other operations because
718 * the block driver may be on waiting response from the server.
720 static int get_sheep_fd(BDRVSheepdogState
*s
)
724 fd
= connect_to_sdog(s
->addr
, s
->port
);
726 error_report("%s", strerror(errno
));
730 socket_set_nonblock(fd
);
732 ret
= set_nodelay(fd
);
734 error_report("%s", strerror(errno
));
739 qemu_aio_set_fd_handler(fd
, co_read_response
, NULL
, aio_flush_request
,
747 * filename must be one of the following formats:
749 * 2. [vdiname]:[snapid]
751 * 4. [hostname]:[port]:[vdiname]
752 * 5. [hostname]:[port]:[vdiname]:[snapid]
753 * 6. [hostname]:[port]:[vdiname]:[tag]
755 * You can boot from the snapshot images by specifying `snapid` or
758 * You can run VMs outside the Sheepdog cluster by specifying
759 * `hostname' and `port' (experimental).
761 static int parse_vdiname(BDRVSheepdogState
*s
, const char *filename
,
762 char *vdi
, uint32_t *snapid
, char *tag
)
767 p
= q
= g_strdup(filename
);
769 /* count the number of separators */
779 /* use the first two tokens as hostname and port number. */
793 strncpy(vdi
, p
, SD_MAX_VDI_LEN
);
795 p
= strchr(vdi
, ':');
798 *snapid
= strtoul(p
, NULL
, 10);
800 strncpy(tag
, p
, SD_MAX_VDI_TAG_LEN
);
803 *snapid
= CURRENT_VDI_ID
; /* search current vdi */
806 if (s
->addr
== NULL
) {
813 static int find_vdi_name(BDRVSheepdogState
*s
, char *filename
, uint32_t snapid
,
814 char *tag
, uint32_t *vid
, int for_snapshot
)
818 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
819 unsigned int wlen
, rlen
= 0;
820 char buf
[SD_MAX_VDI_LEN
+ SD_MAX_VDI_TAG_LEN
];
822 fd
= connect_to_sdog(s
->addr
, s
->port
);
827 memset(buf
, 0, sizeof(buf
));
828 strncpy(buf
, filename
, SD_MAX_VDI_LEN
);
829 strncpy(buf
+ SD_MAX_VDI_LEN
, tag
, SD_MAX_VDI_TAG_LEN
);
831 memset(&hdr
, 0, sizeof(hdr
));
833 hdr
.opcode
= SD_OP_GET_VDI_INFO
;
835 hdr
.opcode
= SD_OP_LOCK_VDI
;
837 wlen
= SD_MAX_VDI_LEN
+ SD_MAX_VDI_TAG_LEN
;
838 hdr
.proto_ver
= SD_PROTO_VER
;
839 hdr
.data_length
= wlen
;
841 hdr
.flags
= SD_FLAG_CMD_WRITE
;
843 ret
= do_req(fd
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
849 if (rsp
->result
!= SD_RES_SUCCESS
) {
850 error_report("cannot get vdi info, %s, %s %d %s",
851 sd_strerror(rsp
->result
), filename
, snapid
, tag
);
863 static int coroutine_fn
add_aio_request(BDRVSheepdogState
*s
, AIOReq
*aio_req
,
864 struct iovec
*iov
, int niov
, int create
,
865 enum AIOCBState aiocb_type
)
867 int nr_copies
= s
->inode
.nr_copies
;
871 uint64_t oid
= aio_req
->oid
;
872 unsigned int datalen
= aio_req
->data_len
;
873 uint64_t offset
= aio_req
->offset
;
874 uint8_t flags
= aio_req
->flags
;
875 uint64_t old_oid
= aio_req
->base_oid
;
881 memset(&hdr
, 0, sizeof(hdr
));
883 if (aiocb_type
== AIOCB_READ_UDATA
) {
885 hdr
.opcode
= SD_OP_READ_OBJ
;
889 hdr
.opcode
= SD_OP_CREATE_AND_WRITE_OBJ
;
890 hdr
.flags
= SD_FLAG_CMD_WRITE
| flags
;
893 hdr
.opcode
= SD_OP_WRITE_OBJ
;
894 hdr
.flags
= SD_FLAG_CMD_WRITE
| flags
;
898 hdr
.cow_oid
= old_oid
;
899 hdr
.copies
= s
->inode
.nr_copies
;
901 hdr
.data_length
= datalen
;
904 hdr
.id
= aio_req
->id
;
906 qemu_co_mutex_lock(&s
->lock
);
907 s
->co_send
= qemu_coroutine_self();
908 qemu_aio_set_fd_handler(s
->fd
, co_read_response
, co_write_request
,
909 aio_flush_request
, NULL
, s
);
910 socket_set_cork(s
->fd
, 1);
913 ret
= qemu_co_send(s
->fd
, &hdr
, sizeof(hdr
));
915 qemu_co_mutex_unlock(&s
->lock
);
916 error_report("failed to send a req, %s", strerror(errno
));
921 ret
= qemu_co_sendv(s
->fd
, iov
, wlen
, aio_req
->iov_offset
);
923 qemu_co_mutex_unlock(&s
->lock
);
924 error_report("failed to send a data, %s", strerror(errno
));
929 socket_set_cork(s
->fd
, 0);
930 qemu_aio_set_fd_handler(s
->fd
, co_read_response
, NULL
,
931 aio_flush_request
, NULL
, s
);
932 qemu_co_mutex_unlock(&s
->lock
);
937 static int read_write_object(int fd
, char *buf
, uint64_t oid
, int copies
,
938 unsigned int datalen
, uint64_t offset
,
939 int write
, int create
)
942 SheepdogObjRsp
*rsp
= (SheepdogObjRsp
*)&hdr
;
943 unsigned int wlen
, rlen
;
946 memset(&hdr
, 0, sizeof(hdr
));
951 hdr
.flags
= SD_FLAG_CMD_WRITE
;
953 hdr
.opcode
= SD_OP_CREATE_AND_WRITE_OBJ
;
955 hdr
.opcode
= SD_OP_WRITE_OBJ
;
960 hdr
.opcode
= SD_OP_READ_OBJ
;
963 hdr
.data_length
= datalen
;
967 ret
= do_req(fd
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
969 error_report("failed to send a request to the sheep");
973 switch (rsp
->result
) {
977 error_report("%s", sd_strerror(rsp
->result
));
982 static int read_object(int fd
, char *buf
, uint64_t oid
, int copies
,
983 unsigned int datalen
, uint64_t offset
)
985 return read_write_object(fd
, buf
, oid
, copies
, datalen
, offset
, 0, 0);
988 static int write_object(int fd
, char *buf
, uint64_t oid
, int copies
,
989 unsigned int datalen
, uint64_t offset
, int create
)
991 return read_write_object(fd
, buf
, oid
, copies
, datalen
, offset
, 1, create
);
994 static int sd_open(BlockDriverState
*bs
, const char *filename
, int flags
)
998 BDRVSheepdogState
*s
= bs
->opaque
;
999 char vdi
[SD_MAX_VDI_LEN
], tag
[SD_MAX_VDI_TAG_LEN
];
1003 strstart(filename
, "sheepdog:", (const char **)&filename
);
1005 QLIST_INIT(&s
->outstanding_aio_head
);
1008 memset(vdi
, 0, sizeof(vdi
));
1009 memset(tag
, 0, sizeof(tag
));
1010 if (parse_vdiname(s
, filename
, vdi
, &snapid
, tag
) < 0) {
1013 s
->fd
= get_sheep_fd(s
);
1018 ret
= find_vdi_name(s
, vdi
, snapid
, tag
, &vid
, 0);
1024 dprintf("%" PRIx32
" snapshot inode was open.\n", vid
);
1028 fd
= connect_to_sdog(s
->addr
, s
->port
);
1030 error_report("failed to connect");
1034 buf
= g_malloc(SD_INODE_SIZE
);
1035 ret
= read_object(fd
, buf
, vid_to_vdi_oid(vid
), 0, SD_INODE_SIZE
, 0);
1043 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
1044 s
->min_dirty_data_idx
= UINT32_MAX
;
1045 s
->max_dirty_data_idx
= 0;
1047 bs
->total_sectors
= s
->inode
.vdi_size
/ SECTOR_SIZE
;
1048 strncpy(s
->name
, vdi
, sizeof(s
->name
));
1049 qemu_co_mutex_init(&s
->lock
);
1053 qemu_aio_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
, NULL
, NULL
);
1061 static int do_sd_create(char *filename
, int64_t vdi_size
,
1062 uint32_t base_vid
, uint32_t *vdi_id
, int snapshot
,
1063 const char *addr
, const char *port
)
1066 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1068 unsigned int wlen
, rlen
= 0;
1069 char buf
[SD_MAX_VDI_LEN
];
1071 fd
= connect_to_sdog(addr
, port
);
1076 memset(buf
, 0, sizeof(buf
));
1077 strncpy(buf
, filename
, SD_MAX_VDI_LEN
);
1079 memset(&hdr
, 0, sizeof(hdr
));
1080 hdr
.opcode
= SD_OP_NEW_VDI
;
1081 hdr
.base_vdi_id
= base_vid
;
1083 wlen
= SD_MAX_VDI_LEN
;
1085 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1086 hdr
.snapid
= snapshot
;
1088 hdr
.data_length
= wlen
;
1089 hdr
.vdi_size
= vdi_size
;
1091 ret
= do_req(fd
, (SheepdogReq
*)&hdr
, buf
, &wlen
, &rlen
);
1099 if (rsp
->result
!= SD_RES_SUCCESS
) {
1100 error_report("%s, %s", sd_strerror(rsp
->result
), filename
);
1105 *vdi_id
= rsp
->vdi_id
;
1111 static int sd_prealloc(const char *filename
)
1113 BlockDriverState
*bs
= NULL
;
1114 uint32_t idx
, max_idx
;
1116 void *buf
= g_malloc0(SD_DATA_OBJ_SIZE
);
1119 ret
= bdrv_file_open(&bs
, filename
, BDRV_O_RDWR
);
1124 vdi_size
= bdrv_getlength(bs
);
1129 max_idx
= DIV_ROUND_UP(vdi_size
, SD_DATA_OBJ_SIZE
);
1131 for (idx
= 0; idx
< max_idx
; idx
++) {
1133 * The created image can be a cloned image, so we need to read
1134 * a data from the source image.
1136 ret
= bdrv_pread(bs
, idx
* SD_DATA_OBJ_SIZE
, buf
, SD_DATA_OBJ_SIZE
);
1140 ret
= bdrv_pwrite(bs
, idx
* SD_DATA_OBJ_SIZE
, buf
, SD_DATA_OBJ_SIZE
);
1154 static int sd_create(const char *filename
, QEMUOptionParameter
*options
)
1157 uint32_t vid
= 0, base_vid
= 0;
1158 int64_t vdi_size
= 0;
1159 char *backing_file
= NULL
;
1160 BDRVSheepdogState s
;
1161 char vdi
[SD_MAX_VDI_LEN
], tag
[SD_MAX_VDI_TAG_LEN
];
1164 const char *vdiname
;
1166 strstart(filename
, "sheepdog:", &vdiname
);
1168 memset(&s
, 0, sizeof(s
));
1169 memset(vdi
, 0, sizeof(vdi
));
1170 memset(tag
, 0, sizeof(tag
));
1171 if (parse_vdiname(&s
, vdiname
, vdi
, &snapid
, tag
) < 0) {
1172 error_report("invalid filename");
1176 while (options
&& options
->name
) {
1177 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
1178 vdi_size
= options
->value
.n
;
1179 } else if (!strcmp(options
->name
, BLOCK_OPT_BACKING_FILE
)) {
1180 backing_file
= options
->value
.s
;
1181 } else if (!strcmp(options
->name
, BLOCK_OPT_PREALLOC
)) {
1182 if (!options
->value
.s
|| !strcmp(options
->value
.s
, "off")) {
1184 } else if (!strcmp(options
->value
.s
, "full")) {
1187 error_report("Invalid preallocation mode: '%s'",
1195 if (vdi_size
> SD_MAX_VDI_SIZE
) {
1196 error_report("too big image size");
1201 BlockDriverState
*bs
;
1202 BDRVSheepdogState
*s
;
1205 /* Currently, only Sheepdog backing image is supported. */
1206 drv
= bdrv_find_protocol(backing_file
);
1207 if (!drv
|| strcmp(drv
->protocol_name
, "sheepdog") != 0) {
1208 error_report("backing_file must be a sheepdog image");
1212 ret
= bdrv_file_open(&bs
, backing_file
, 0);
1218 if (!is_snapshot(&s
->inode
)) {
1219 error_report("cannot clone from a non snapshot vdi");
1224 base_vid
= s
->inode
.vdi_id
;
1228 ret
= do_sd_create(vdi
, vdi_size
, base_vid
, &vid
, 0, s
.addr
, s
.port
);
1229 if (!prealloc
|| ret
) {
1233 return sd_prealloc(filename
);
1236 static void sd_close(BlockDriverState
*bs
)
1238 BDRVSheepdogState
*s
= bs
->opaque
;
1240 SheepdogVdiRsp
*rsp
= (SheepdogVdiRsp
*)&hdr
;
1241 unsigned int wlen
, rlen
= 0;
1244 dprintf("%s\n", s
->name
);
1246 fd
= connect_to_sdog(s
->addr
, s
->port
);
1251 memset(&hdr
, 0, sizeof(hdr
));
1253 hdr
.opcode
= SD_OP_RELEASE_VDI
;
1254 wlen
= strlen(s
->name
) + 1;
1255 hdr
.data_length
= wlen
;
1256 hdr
.flags
= SD_FLAG_CMD_WRITE
;
1258 ret
= do_req(fd
, (SheepdogReq
*)&hdr
, s
->name
, &wlen
, &rlen
);
1262 if (!ret
&& rsp
->result
!= SD_RES_SUCCESS
&&
1263 rsp
->result
!= SD_RES_VDI_NOT_LOCKED
) {
1264 error_report("%s, %s", sd_strerror(rsp
->result
), s
->name
);
1267 qemu_aio_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
, NULL
, NULL
);
1272 static int64_t sd_getlength(BlockDriverState
*bs
)
1274 BDRVSheepdogState
*s
= bs
->opaque
;
1276 return s
->inode
.vdi_size
;
1279 static int sd_truncate(BlockDriverState
*bs
, int64_t offset
)
1281 BDRVSheepdogState
*s
= bs
->opaque
;
1283 unsigned int datalen
;
1285 if (offset
< s
->inode
.vdi_size
) {
1286 error_report("shrinking is not supported");
1288 } else if (offset
> SD_MAX_VDI_SIZE
) {
1289 error_report("too big image size");
1293 fd
= connect_to_sdog(s
->addr
, s
->port
);
1298 /* we don't need to update entire object */
1299 datalen
= SD_INODE_SIZE
- sizeof(s
->inode
.data_vdi_id
);
1300 s
->inode
.vdi_size
= offset
;
1301 ret
= write_object(fd
, (char *)&s
->inode
, vid_to_vdi_oid(s
->inode
.vdi_id
),
1302 s
->inode
.nr_copies
, datalen
, 0, 0);
1306 error_report("failed to update an inode.");
1314 * This function is called after writing data objects. If we need to
1315 * update metadata, this sends a write request to the vdi object.
1316 * Otherwise, this switches back to sd_co_readv/writev.
1318 static void coroutine_fn
sd_write_done(SheepdogAIOCB
*acb
)
1321 BDRVSheepdogState
*s
= acb
->common
.bs
->opaque
;
1324 uint32_t offset
, data_len
, mn
, mx
;
1326 mn
= s
->min_dirty_data_idx
;
1327 mx
= s
->max_dirty_data_idx
;
1329 /* we need to update the vdi object. */
1330 offset
= sizeof(s
->inode
) - sizeof(s
->inode
.data_vdi_id
) +
1331 mn
* sizeof(s
->inode
.data_vdi_id
[0]);
1332 data_len
= (mx
- mn
+ 1) * sizeof(s
->inode
.data_vdi_id
[0]);
1334 s
->min_dirty_data_idx
= UINT32_MAX
;
1335 s
->max_dirty_data_idx
= 0;
1337 iov
.iov_base
= &s
->inode
;
1338 iov
.iov_len
= sizeof(s
->inode
);
1339 aio_req
= alloc_aio_req(s
, acb
, vid_to_vdi_oid(s
->inode
.vdi_id
),
1340 data_len
, offset
, 0, 0, offset
);
1341 ret
= add_aio_request(s
, aio_req
, &iov
, 1, 0, AIOCB_WRITE_UDATA
);
1343 free_aio_req(s
, aio_req
);
1348 acb
->aio_done_func
= sd_finish_aiocb
;
1349 acb
->aiocb_type
= AIOCB_WRITE_UDATA
;
1353 sd_finish_aiocb(acb
);
1357 * Create a writable VDI from a snapshot
1359 static int sd_create_branch(BDRVSheepdogState
*s
)
1365 dprintf("%" PRIx32
" is snapshot.\n", s
->inode
.vdi_id
);
1367 buf
= g_malloc(SD_INODE_SIZE
);
1369 ret
= do_sd_create(s
->name
, s
->inode
.vdi_size
, s
->inode
.vdi_id
, &vid
, 1,
1375 dprintf("%" PRIx32
" is created.\n", vid
);
1377 fd
= connect_to_sdog(s
->addr
, s
->port
);
1379 error_report("failed to connect");
1383 ret
= read_object(fd
, buf
, vid_to_vdi_oid(vid
), s
->inode
.nr_copies
,
1392 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
1396 dprintf("%" PRIx32
" was newly created.\n", s
->inode
.vdi_id
);
1405 * Send I/O requests to the server.
1407 * This function sends requests to the server, links the requests to
1408 * the outstanding_list in BDRVSheepdogState, and exits without
1409 * waiting the response. The responses are received in the
1410 * `aio_read_response' function which is called from the main loop as
1413 * Returns 1 when we need to wait a response, 0 when there is no sent
1414 * request and -errno in error cases.
1416 static int coroutine_fn
sd_co_rw_vector(void *p
)
1418 SheepdogAIOCB
*acb
= p
;
1420 unsigned long len
, done
= 0, total
= acb
->nb_sectors
* SECTOR_SIZE
;
1421 unsigned long idx
= acb
->sector_num
* SECTOR_SIZE
/ SD_DATA_OBJ_SIZE
;
1423 uint64_t offset
= (acb
->sector_num
* SECTOR_SIZE
) % SD_DATA_OBJ_SIZE
;
1424 BDRVSheepdogState
*s
= acb
->common
.bs
->opaque
;
1425 SheepdogInode
*inode
= &s
->inode
;
1428 if (acb
->aiocb_type
== AIOCB_WRITE_UDATA
&& s
->is_snapshot
) {
1430 * In the case we open the snapshot VDI, Sheepdog creates the
1431 * writable VDI when we do a write operation first.
1433 ret
= sd_create_branch(s
);
1440 while (done
!= total
) {
1442 uint64_t old_oid
= 0;
1445 oid
= vid_to_data_oid(inode
->data_vdi_id
[idx
], idx
);
1447 len
= MIN(total
- done
, SD_DATA_OBJ_SIZE
- offset
);
1449 if (!inode
->data_vdi_id
[idx
]) {
1450 if (acb
->aiocb_type
== AIOCB_READ_UDATA
) {
1455 } else if (acb
->aiocb_type
== AIOCB_WRITE_UDATA
1456 && !is_data_obj_writable(inode
, idx
)) {
1460 flags
= SD_FLAG_CMD_COW
;
1464 dprintf("update ino (%" PRIu32
") %" PRIu64
" %" PRIu64
1465 " %" PRIu64
"\n", inode
->vdi_id
, oid
,
1466 vid_to_data_oid(inode
->data_vdi_id
[idx
], idx
), idx
);
1467 oid
= vid_to_data_oid(inode
->vdi_id
, idx
);
1468 dprintf("new oid %lx\n", oid
);
1471 aio_req
= alloc_aio_req(s
, acb
, oid
, len
, offset
, flags
, old_oid
, done
);
1475 QLIST_FOREACH(areq
, &s
->outstanding_aio_head
,
1476 outstanding_aio_siblings
) {
1477 if (areq
== aio_req
) {
1480 if (areq
->oid
== oid
) {
1482 * Sheepdog cannot handle simultaneous create
1483 * requests to the same object. So we cannot send
1484 * the request until the previous request
1488 aio_req
->base_oid
= 0;
1494 ret
= add_aio_request(s
, aio_req
, acb
->qiov
->iov
, acb
->qiov
->niov
,
1495 create
, acb
->aiocb_type
);
1497 error_report("add_aio_request is failed");
1498 free_aio_req(s
, aio_req
);
1508 if (QLIST_EMPTY(&acb
->aioreq_head
)) {
1514 static coroutine_fn
int sd_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
1515 int nb_sectors
, QEMUIOVector
*qiov
)
1520 if (bs
->growable
&& sector_num
+ nb_sectors
> bs
->total_sectors
) {
1521 /* TODO: shouldn't block here */
1522 if (sd_truncate(bs
, (sector_num
+ nb_sectors
) * SECTOR_SIZE
) < 0) {
1525 bs
->total_sectors
= sector_num
+ nb_sectors
;
1528 acb
= sd_aio_setup(bs
, qiov
, sector_num
, nb_sectors
, NULL
, NULL
);
1529 acb
->aio_done_func
= sd_write_done
;
1530 acb
->aiocb_type
= AIOCB_WRITE_UDATA
;
1532 ret
= sd_co_rw_vector(acb
);
1534 qemu_aio_release(acb
);
1538 qemu_coroutine_yield();
1543 static coroutine_fn
int sd_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
1544 int nb_sectors
, QEMUIOVector
*qiov
)
1549 acb
= sd_aio_setup(bs
, qiov
, sector_num
, nb_sectors
, NULL
, NULL
);
1550 acb
->aiocb_type
= AIOCB_READ_UDATA
;
1551 acb
->aio_done_func
= sd_finish_aiocb
;
1554 * TODO: we can do better; we don't need to initialize
1557 for (i
= 0; i
< qiov
->niov
; i
++) {
1558 memset(qiov
->iov
[i
].iov_base
, 0, qiov
->iov
[i
].iov_len
);
1561 ret
= sd_co_rw_vector(acb
);
1563 qemu_aio_release(acb
);
1567 qemu_coroutine_yield();
1572 static int sd_snapshot_create(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
)
1574 BDRVSheepdogState
*s
= bs
->opaque
;
1577 SheepdogInode
*inode
;
1578 unsigned int datalen
;
1580 dprintf("sn_info: name %s id_str %s s: name %s vm_state_size %d "
1581 "is_snapshot %d\n", sn_info
->name
, sn_info
->id_str
,
1582 s
->name
, sn_info
->vm_state_size
, s
->is_snapshot
);
1584 if (s
->is_snapshot
) {
1585 error_report("You can't create a snapshot of a snapshot VDI, "
1586 "%s (%" PRIu32
").", s
->name
, s
->inode
.vdi_id
);
1591 dprintf("%s %s\n", sn_info
->name
, sn_info
->id_str
);
1593 s
->inode
.vm_state_size
= sn_info
->vm_state_size
;
1594 s
->inode
.vm_clock_nsec
= sn_info
->vm_clock_nsec
;
1595 strncpy(s
->inode
.tag
, sn_info
->name
, sizeof(s
->inode
.tag
));
1596 /* we don't need to update entire object */
1597 datalen
= SD_INODE_SIZE
- sizeof(s
->inode
.data_vdi_id
);
1599 /* refresh inode. */
1600 fd
= connect_to_sdog(s
->addr
, s
->port
);
1606 ret
= write_object(fd
, (char *)&s
->inode
, vid_to_vdi_oid(s
->inode
.vdi_id
),
1607 s
->inode
.nr_copies
, datalen
, 0, 0);
1609 error_report("failed to write snapshot's inode.");
1614 ret
= do_sd_create(s
->name
, s
->inode
.vdi_size
, s
->inode
.vdi_id
, &new_vid
, 1,
1617 error_report("failed to create inode for snapshot. %s",
1623 inode
= (SheepdogInode
*)g_malloc(datalen
);
1625 ret
= read_object(fd
, (char *)inode
, vid_to_vdi_oid(new_vid
),
1626 s
->inode
.nr_copies
, datalen
, 0);
1629 error_report("failed to read new inode info. %s", strerror(errno
));
1634 memcpy(&s
->inode
, inode
, datalen
);
1635 dprintf("s->inode: name %s snap_id %x oid %x\n",
1636 s
->inode
.name
, s
->inode
.snap_id
, s
->inode
.vdi_id
);
1643 static int sd_snapshot_goto(BlockDriverState
*bs
, const char *snapshot_id
)
1645 BDRVSheepdogState
*s
= bs
->opaque
;
1646 BDRVSheepdogState
*old_s
;
1647 char vdi
[SD_MAX_VDI_LEN
], tag
[SD_MAX_VDI_TAG_LEN
];
1650 uint32_t snapid
= 0;
1651 int ret
= -ENOENT
, fd
;
1653 old_s
= g_malloc(sizeof(BDRVSheepdogState
));
1655 memcpy(old_s
, s
, sizeof(BDRVSheepdogState
));
1657 memset(vdi
, 0, sizeof(vdi
));
1658 strncpy(vdi
, s
->name
, sizeof(vdi
));
1660 memset(tag
, 0, sizeof(tag
));
1661 snapid
= strtoul(snapshot_id
, NULL
, 10);
1663 strncpy(tag
, s
->name
, sizeof(tag
));
1666 ret
= find_vdi_name(s
, vdi
, snapid
, tag
, &vid
, 1);
1668 error_report("Failed to find_vdi_name");
1673 fd
= connect_to_sdog(s
->addr
, s
->port
);
1675 error_report("failed to connect");
1679 buf
= g_malloc(SD_INODE_SIZE
);
1680 ret
= read_object(fd
, buf
, vid_to_vdi_oid(vid
), s
->inode
.nr_copies
,
1690 memcpy(&s
->inode
, buf
, sizeof(s
->inode
));
1692 if (!s
->inode
.vm_state_size
) {
1693 error_report("Invalid snapshot");
1705 /* recover bdrv_sd_state */
1706 memcpy(s
, old_s
, sizeof(BDRVSheepdogState
));
1710 error_report("failed to open. recover old bdrv_sd_state.");
1715 static int sd_snapshot_delete(BlockDriverState
*bs
, const char *snapshot_id
)
1717 /* FIXME: Delete specified snapshot id. */
1721 static int sd_snapshot_list(BlockDriverState
*bs
, QEMUSnapshotInfo
**psn_tab
)
1723 BDRVSheepdogState
*s
= bs
->opaque
;
1725 int fd
, nr
= 1024, ret
, max
= BITS_TO_LONGS(SD_NR_VDIS
) * sizeof(long);
1726 QEMUSnapshotInfo
*sn_tab
= NULL
;
1727 unsigned wlen
, rlen
;
1729 static SheepdogInode inode
;
1730 unsigned long *vdi_inuse
;
1731 unsigned int start_nr
;
1735 vdi_inuse
= g_malloc(max
);
1737 fd
= connect_to_sdog(s
->addr
, s
->port
);
1745 memset(&req
, 0, sizeof(req
));
1747 req
.opcode
= SD_OP_READ_VDIS
;
1748 req
.data_length
= max
;
1750 ret
= do_req(fd
, (SheepdogReq
*)&req
, vdi_inuse
, &wlen
, &rlen
);
1757 sn_tab
= g_malloc0(nr
* sizeof(*sn_tab
));
1759 /* calculate a vdi id with hash function */
1760 hval
= fnv_64a_buf(s
->name
, strlen(s
->name
), FNV1A_64_INIT
);
1761 start_nr
= hval
& (SD_NR_VDIS
- 1);
1763 fd
= connect_to_sdog(s
->addr
, s
->port
);
1765 error_report("failed to connect");
1769 for (vid
= start_nr
; found
< nr
; vid
= (vid
+ 1) % SD_NR_VDIS
) {
1770 if (!test_bit(vid
, vdi_inuse
)) {
1774 /* we don't need to read entire object */
1775 ret
= read_object(fd
, (char *)&inode
, vid_to_vdi_oid(vid
),
1776 0, SD_INODE_SIZE
- sizeof(inode
.data_vdi_id
), 0);
1782 if (!strcmp(inode
.name
, s
->name
) && is_snapshot(&inode
)) {
1783 sn_tab
[found
].date_sec
= inode
.snap_ctime
>> 32;
1784 sn_tab
[found
].date_nsec
= inode
.snap_ctime
& 0xffffffff;
1785 sn_tab
[found
].vm_state_size
= inode
.vm_state_size
;
1786 sn_tab
[found
].vm_clock_nsec
= inode
.vm_clock_nsec
;
1788 snprintf(sn_tab
[found
].id_str
, sizeof(sn_tab
[found
].id_str
), "%u",
1790 strncpy(sn_tab
[found
].name
, inode
.tag
,
1791 MIN(sizeof(sn_tab
[found
].name
), sizeof(inode
.tag
)));
1805 static int do_load_save_vmstate(BDRVSheepdogState
*s
, uint8_t *data
,
1806 int64_t pos
, int size
, int load
)
1810 unsigned int data_len
;
1811 uint64_t vmstate_oid
;
1815 fd
= connect_to_sdog(s
->addr
, s
->port
);
1822 vdi_index
= pos
/ SD_DATA_OBJ_SIZE
;
1823 offset
= pos
% SD_DATA_OBJ_SIZE
;
1825 data_len
= MIN(size
, SD_DATA_OBJ_SIZE
);
1827 vmstate_oid
= vid_to_vmstate_oid(s
->inode
.vdi_id
, vdi_index
);
1829 create
= (offset
== 0);
1831 ret
= read_object(fd
, (char *)data
, vmstate_oid
,
1832 s
->inode
.nr_copies
, data_len
, offset
);
1834 ret
= write_object(fd
, (char *)data
, vmstate_oid
,
1835 s
->inode
.nr_copies
, data_len
, offset
, create
);
1839 error_report("failed to save vmstate %s", strerror(errno
));
1853 static int sd_save_vmstate(BlockDriverState
*bs
, const uint8_t *data
,
1854 int64_t pos
, int size
)
1856 BDRVSheepdogState
*s
= bs
->opaque
;
1858 return do_load_save_vmstate(s
, (uint8_t *)data
, pos
, size
, 0);
1861 static int sd_load_vmstate(BlockDriverState
*bs
, uint8_t *data
,
1862 int64_t pos
, int size
)
1864 BDRVSheepdogState
*s
= bs
->opaque
;
1866 return do_load_save_vmstate(s
, data
, pos
, size
, 1);
1870 static QEMUOptionParameter sd_create_options
[] = {
1872 .name
= BLOCK_OPT_SIZE
,
1874 .help
= "Virtual disk size"
1877 .name
= BLOCK_OPT_BACKING_FILE
,
1879 .help
= "File name of a base image"
1882 .name
= BLOCK_OPT_PREALLOC
,
1884 .help
= "Preallocation mode (allowed values: off, full)"
1889 BlockDriver bdrv_sheepdog
= {
1890 .format_name
= "sheepdog",
1891 .protocol_name
= "sheepdog",
1892 .instance_size
= sizeof(BDRVSheepdogState
),
1893 .bdrv_file_open
= sd_open
,
1894 .bdrv_close
= sd_close
,
1895 .bdrv_create
= sd_create
,
1896 .bdrv_getlength
= sd_getlength
,
1897 .bdrv_truncate
= sd_truncate
,
1899 .bdrv_co_readv
= sd_co_readv
,
1900 .bdrv_co_writev
= sd_co_writev
,
1902 .bdrv_snapshot_create
= sd_snapshot_create
,
1903 .bdrv_snapshot_goto
= sd_snapshot_goto
,
1904 .bdrv_snapshot_delete
= sd_snapshot_delete
,
1905 .bdrv_snapshot_list
= sd_snapshot_list
,
1907 .bdrv_save_vmstate
= sd_save_vmstate
,
1908 .bdrv_load_vmstate
= sd_load_vmstate
,
1910 .create_options
= sd_create_options
,
1913 static void bdrv_sheepdog_init(void)
1915 bdrv_register(&bdrv_sheepdog
);
1917 block_init(bdrv_sheepdog_init
);