vmstate_register_with_alias_id: Take an Error **
[qemu.git] / block / sheepdog.c
blobf757157ceae2392f6d23eafcea960097223b3f8d
1 /*
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/osdep.h"
16 #include "qapi/error.h"
17 #include "qemu/uri.h"
18 #include "qemu/error-report.h"
19 #include "qemu/sockets.h"
20 #include "block/block_int.h"
21 #include "sysemu/block-backend.h"
22 #include "qemu/bitops.h"
23 #include "qemu/cutils.h"
25 #define SD_PROTO_VER 0x01
27 #define SD_DEFAULT_ADDR "localhost"
28 #define SD_DEFAULT_PORT 7000
30 #define SD_OP_CREATE_AND_WRITE_OBJ 0x01
31 #define SD_OP_READ_OBJ 0x02
32 #define SD_OP_WRITE_OBJ 0x03
33 /* 0x04 is used internally by Sheepdog */
35 #define SD_OP_NEW_VDI 0x11
36 #define SD_OP_LOCK_VDI 0x12
37 #define SD_OP_RELEASE_VDI 0x13
38 #define SD_OP_GET_VDI_INFO 0x14
39 #define SD_OP_READ_VDIS 0x15
40 #define SD_OP_FLUSH_VDI 0x16
41 #define SD_OP_DEL_VDI 0x17
42 #define SD_OP_GET_CLUSTER_DEFAULT 0x18
44 #define SD_FLAG_CMD_WRITE 0x01
45 #define SD_FLAG_CMD_COW 0x02
46 #define SD_FLAG_CMD_CACHE 0x04 /* Writeback mode for cache */
47 #define SD_FLAG_CMD_DIRECT 0x08 /* Don't use cache */
49 #define SD_RES_SUCCESS 0x00 /* Success */
50 #define SD_RES_UNKNOWN 0x01 /* Unknown error */
51 #define SD_RES_NO_OBJ 0x02 /* No object found */
52 #define SD_RES_EIO 0x03 /* I/O error */
53 #define SD_RES_VDI_EXIST 0x04 /* Vdi exists already */
54 #define SD_RES_INVALID_PARMS 0x05 /* Invalid parameters */
55 #define SD_RES_SYSTEM_ERROR 0x06 /* System error */
56 #define SD_RES_VDI_LOCKED 0x07 /* Vdi is locked */
57 #define SD_RES_NO_VDI 0x08 /* No vdi found */
58 #define SD_RES_NO_BASE_VDI 0x09 /* No base vdi found */
59 #define SD_RES_VDI_READ 0x0A /* Cannot read requested vdi */
60 #define SD_RES_VDI_WRITE 0x0B /* Cannot write requested vdi */
61 #define SD_RES_BASE_VDI_READ 0x0C /* Cannot read base vdi */
62 #define SD_RES_BASE_VDI_WRITE 0x0D /* Cannot write base vdi */
63 #define SD_RES_NO_TAG 0x0E /* Requested tag is not found */
64 #define SD_RES_STARTUP 0x0F /* Sheepdog is on starting up */
65 #define SD_RES_VDI_NOT_LOCKED 0x10 /* Vdi is not locked */
66 #define SD_RES_SHUTDOWN 0x11 /* Sheepdog is shutting down */
67 #define SD_RES_NO_MEM 0x12 /* Cannot allocate memory */
68 #define SD_RES_FULL_VDI 0x13 /* we already have the maximum vdis */
69 #define SD_RES_VER_MISMATCH 0x14 /* Protocol version mismatch */
70 #define SD_RES_NO_SPACE 0x15 /* Server has no room for new objects */
71 #define SD_RES_WAIT_FOR_FORMAT 0x16 /* Waiting for a format operation */
72 #define SD_RES_WAIT_FOR_JOIN 0x17 /* Waiting for other nodes joining */
73 #define SD_RES_JOIN_FAILED 0x18 /* Target node had failed to join sheepdog */
74 #define SD_RES_HALT 0x19 /* Sheepdog is stopped serving IO request */
75 #define SD_RES_READONLY 0x1A /* Object is read-only */
78 * Object ID rules
80 * 0 - 19 (20 bits): data object space
81 * 20 - 31 (12 bits): reserved data object space
82 * 32 - 55 (24 bits): vdi object space
83 * 56 - 59 ( 4 bits): reserved vdi object space
84 * 60 - 63 ( 4 bits): object type identifier space
87 #define VDI_SPACE_SHIFT 32
88 #define VDI_BIT (UINT64_C(1) << 63)
89 #define VMSTATE_BIT (UINT64_C(1) << 62)
90 #define MAX_DATA_OBJS (UINT64_C(1) << 20)
91 #define MAX_CHILDREN 1024
92 #define SD_MAX_VDI_LEN 256
93 #define SD_MAX_VDI_TAG_LEN 256
94 #define SD_NR_VDIS (1U << 24)
95 #define SD_DATA_OBJ_SIZE (UINT64_C(1) << 22)
96 #define SD_MAX_VDI_SIZE (SD_DATA_OBJ_SIZE * MAX_DATA_OBJS)
97 #define SD_DEFAULT_BLOCK_SIZE_SHIFT 22
99 * For erasure coding, we use at most SD_EC_MAX_STRIP for data strips and
100 * (SD_EC_MAX_STRIP - 1) for parity strips
102 * SD_MAX_COPIES is sum of number of data strips and parity strips.
104 #define SD_EC_MAX_STRIP 16
105 #define SD_MAX_COPIES (SD_EC_MAX_STRIP * 2 - 1)
107 #define SD_INODE_SIZE (sizeof(SheepdogInode))
108 #define CURRENT_VDI_ID 0
110 #define LOCK_TYPE_NORMAL 0
111 #define LOCK_TYPE_SHARED 1 /* for iSCSI multipath */
113 typedef struct SheepdogReq {
114 uint8_t proto_ver;
115 uint8_t opcode;
116 uint16_t flags;
117 uint32_t epoch;
118 uint32_t id;
119 uint32_t data_length;
120 uint32_t opcode_specific[8];
121 } SheepdogReq;
123 typedef struct SheepdogRsp {
124 uint8_t proto_ver;
125 uint8_t opcode;
126 uint16_t flags;
127 uint32_t epoch;
128 uint32_t id;
129 uint32_t data_length;
130 uint32_t result;
131 uint32_t opcode_specific[7];
132 } SheepdogRsp;
134 typedef struct SheepdogObjReq {
135 uint8_t proto_ver;
136 uint8_t opcode;
137 uint16_t flags;
138 uint32_t epoch;
139 uint32_t id;
140 uint32_t data_length;
141 uint64_t oid;
142 uint64_t cow_oid;
143 uint8_t copies;
144 uint8_t copy_policy;
145 uint8_t reserved[6];
146 uint64_t offset;
147 } SheepdogObjReq;
149 typedef struct SheepdogObjRsp {
150 uint8_t proto_ver;
151 uint8_t opcode;
152 uint16_t flags;
153 uint32_t epoch;
154 uint32_t id;
155 uint32_t data_length;
156 uint32_t result;
157 uint8_t copies;
158 uint8_t copy_policy;
159 uint8_t reserved[2];
160 uint32_t pad[6];
161 } SheepdogObjRsp;
163 typedef struct SheepdogVdiReq {
164 uint8_t proto_ver;
165 uint8_t opcode;
166 uint16_t flags;
167 uint32_t epoch;
168 uint32_t id;
169 uint32_t data_length;
170 uint64_t vdi_size;
171 uint32_t base_vdi_id;
172 uint8_t copies;
173 uint8_t copy_policy;
174 uint8_t store_policy;
175 uint8_t block_size_shift;
176 uint32_t snapid;
177 uint32_t type;
178 uint32_t pad[2];
179 } SheepdogVdiReq;
181 typedef struct SheepdogVdiRsp {
182 uint8_t proto_ver;
183 uint8_t opcode;
184 uint16_t flags;
185 uint32_t epoch;
186 uint32_t id;
187 uint32_t data_length;
188 uint32_t result;
189 uint32_t rsvd;
190 uint32_t vdi_id;
191 uint32_t pad[5];
192 } SheepdogVdiRsp;
194 typedef struct SheepdogClusterRsp {
195 uint8_t proto_ver;
196 uint8_t opcode;
197 uint16_t flags;
198 uint32_t epoch;
199 uint32_t id;
200 uint32_t data_length;
201 uint32_t result;
202 uint8_t nr_copies;
203 uint8_t copy_policy;
204 uint8_t block_size_shift;
205 uint8_t __pad1;
206 uint32_t __pad2[6];
207 } SheepdogClusterRsp;
209 typedef struct SheepdogInode {
210 char name[SD_MAX_VDI_LEN];
211 char tag[SD_MAX_VDI_TAG_LEN];
212 uint64_t ctime;
213 uint64_t snap_ctime;
214 uint64_t vm_clock_nsec;
215 uint64_t vdi_size;
216 uint64_t vm_state_size;
217 uint16_t copy_policy;
218 uint8_t nr_copies;
219 uint8_t block_size_shift;
220 uint32_t snap_id;
221 uint32_t vdi_id;
222 uint32_t parent_vdi_id;
223 uint32_t child_vdi_id[MAX_CHILDREN];
224 uint32_t data_vdi_id[MAX_DATA_OBJS];
225 } SheepdogInode;
227 #define SD_INODE_HEADER_SIZE offsetof(SheepdogInode, data_vdi_id)
230 * 64 bit FNV-1a non-zero initial basis
232 #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
235 * 64 bit Fowler/Noll/Vo FNV-1a hash code
237 static inline uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval)
239 unsigned char *bp = buf;
240 unsigned char *be = bp + len;
241 while (bp < be) {
242 hval ^= (uint64_t) *bp++;
243 hval += (hval << 1) + (hval << 4) + (hval << 5) +
244 (hval << 7) + (hval << 8) + (hval << 40);
246 return hval;
249 static inline bool is_data_obj_writable(SheepdogInode *inode, unsigned int idx)
251 return inode->vdi_id == inode->data_vdi_id[idx];
254 static inline bool is_data_obj(uint64_t oid)
256 return !(VDI_BIT & oid);
259 static inline uint64_t data_oid_to_idx(uint64_t oid)
261 return oid & (MAX_DATA_OBJS - 1);
264 static inline uint32_t oid_to_vid(uint64_t oid)
266 return (oid & ~VDI_BIT) >> VDI_SPACE_SHIFT;
269 static inline uint64_t vid_to_vdi_oid(uint32_t vid)
271 return VDI_BIT | ((uint64_t)vid << VDI_SPACE_SHIFT);
274 static inline uint64_t vid_to_vmstate_oid(uint32_t vid, uint32_t idx)
276 return VMSTATE_BIT | ((uint64_t)vid << VDI_SPACE_SHIFT) | idx;
279 static inline uint64_t vid_to_data_oid(uint32_t vid, uint32_t idx)
281 return ((uint64_t)vid << VDI_SPACE_SHIFT) | idx;
284 static inline bool is_snapshot(struct SheepdogInode *inode)
286 return !!inode->snap_ctime;
289 static inline size_t count_data_objs(const struct SheepdogInode *inode)
291 return DIV_ROUND_UP(inode->vdi_size,
292 (1UL << inode->block_size_shift));
295 #undef DPRINTF
296 #ifdef DEBUG_SDOG
297 #define DEBUG_SDOG_PRINT 1
298 #else
299 #define DEBUG_SDOG_PRINT 0
300 #endif
301 #define DPRINTF(fmt, args...) \
302 do { \
303 if (DEBUG_SDOG_PRINT) { \
304 fprintf(stderr, "%s %d: " fmt, __func__, __LINE__, ##args); \
306 } while (0)
308 typedef struct SheepdogAIOCB SheepdogAIOCB;
309 typedef struct BDRVSheepdogState BDRVSheepdogState;
311 typedef struct AIOReq {
312 SheepdogAIOCB *aiocb;
313 unsigned int iov_offset;
315 uint64_t oid;
316 uint64_t base_oid;
317 uint64_t offset;
318 unsigned int data_len;
319 uint8_t flags;
320 uint32_t id;
321 bool create;
323 QLIST_ENTRY(AIOReq) aio_siblings;
324 } AIOReq;
326 enum AIOCBState {
327 AIOCB_WRITE_UDATA,
328 AIOCB_READ_UDATA,
329 AIOCB_FLUSH_CACHE,
330 AIOCB_DISCARD_OBJ,
333 #define AIOCBOverlapping(x, y) \
334 (!(x->max_affect_data_idx < y->min_affect_data_idx \
335 || y->max_affect_data_idx < x->min_affect_data_idx))
337 struct SheepdogAIOCB {
338 BDRVSheepdogState *s;
340 QEMUIOVector *qiov;
342 int64_t sector_num;
343 int nb_sectors;
345 int ret;
346 enum AIOCBState aiocb_type;
348 Coroutine *coroutine;
349 int nr_pending;
351 uint32_t min_affect_data_idx;
352 uint32_t max_affect_data_idx;
355 * The difference between affect_data_idx and dirty_data_idx:
356 * affect_data_idx represents range of index of all request types.
357 * dirty_data_idx represents range of index updated by COW requests.
358 * dirty_data_idx is used for updating an inode object.
360 uint32_t min_dirty_data_idx;
361 uint32_t max_dirty_data_idx;
363 QLIST_ENTRY(SheepdogAIOCB) aiocb_siblings;
366 struct BDRVSheepdogState {
367 BlockDriverState *bs;
368 AioContext *aio_context;
370 SheepdogInode inode;
372 char name[SD_MAX_VDI_LEN];
373 bool is_snapshot;
374 uint32_t cache_flags;
375 bool discard_supported;
377 char *host_spec;
378 bool is_unix;
379 int fd;
381 CoMutex lock;
382 Coroutine *co_send;
383 Coroutine *co_recv;
385 uint32_t aioreq_seq_num;
387 /* Every aio request must be linked to either of these queues. */
388 QLIST_HEAD(inflight_aio_head, AIOReq) inflight_aio_head;
389 QLIST_HEAD(failed_aio_head, AIOReq) failed_aio_head;
391 CoQueue overlapping_queue;
392 QLIST_HEAD(inflight_aiocb_head, SheepdogAIOCB) inflight_aiocb_head;
395 typedef struct BDRVSheepdogReopenState {
396 int fd;
397 int cache_flags;
398 } BDRVSheepdogReopenState;
400 static const char * sd_strerror(int err)
402 int i;
404 static const struct {
405 int err;
406 const char *desc;
407 } errors[] = {
408 {SD_RES_SUCCESS, "Success"},
409 {SD_RES_UNKNOWN, "Unknown error"},
410 {SD_RES_NO_OBJ, "No object found"},
411 {SD_RES_EIO, "I/O error"},
412 {SD_RES_VDI_EXIST, "VDI exists already"},
413 {SD_RES_INVALID_PARMS, "Invalid parameters"},
414 {SD_RES_SYSTEM_ERROR, "System error"},
415 {SD_RES_VDI_LOCKED, "VDI is already locked"},
416 {SD_RES_NO_VDI, "No vdi found"},
417 {SD_RES_NO_BASE_VDI, "No base VDI found"},
418 {SD_RES_VDI_READ, "Failed read the requested VDI"},
419 {SD_RES_VDI_WRITE, "Failed to write the requested VDI"},
420 {SD_RES_BASE_VDI_READ, "Failed to read the base VDI"},
421 {SD_RES_BASE_VDI_WRITE, "Failed to write the base VDI"},
422 {SD_RES_NO_TAG, "Failed to find the requested tag"},
423 {SD_RES_STARTUP, "The system is still booting"},
424 {SD_RES_VDI_NOT_LOCKED, "VDI isn't locked"},
425 {SD_RES_SHUTDOWN, "The system is shutting down"},
426 {SD_RES_NO_MEM, "Out of memory on the server"},
427 {SD_RES_FULL_VDI, "We already have the maximum vdis"},
428 {SD_RES_VER_MISMATCH, "Protocol version mismatch"},
429 {SD_RES_NO_SPACE, "Server has no space for new objects"},
430 {SD_RES_WAIT_FOR_FORMAT, "Sheepdog is waiting for a format operation"},
431 {SD_RES_WAIT_FOR_JOIN, "Sheepdog is waiting for other nodes joining"},
432 {SD_RES_JOIN_FAILED, "Target node had failed to join sheepdog"},
433 {SD_RES_HALT, "Sheepdog is stopped serving IO request"},
434 {SD_RES_READONLY, "Object is read-only"},
437 for (i = 0; i < ARRAY_SIZE(errors); ++i) {
438 if (errors[i].err == err) {
439 return errors[i].desc;
443 return "Invalid error code";
447 * Sheepdog I/O handling:
449 * 1. In sd_co_rw_vector, we send the I/O requests to the server and
450 * link the requests to the inflight_list in the
451 * BDRVSheepdogState. The function yields while waiting for
452 * receiving the response.
454 * 2. We receive the response in aio_read_response, the fd handler to
455 * the sheepdog connection. We switch back to sd_co_readv/sd_writev
456 * after all the requests belonging to the AIOCB are finished. If
457 * needed, sd_co_writev will send another requests for the vdi object.
460 static inline AIOReq *alloc_aio_req(BDRVSheepdogState *s, SheepdogAIOCB *acb,
461 uint64_t oid, unsigned int data_len,
462 uint64_t offset, uint8_t flags, bool create,
463 uint64_t base_oid, unsigned int iov_offset)
465 AIOReq *aio_req;
467 aio_req = g_malloc(sizeof(*aio_req));
468 aio_req->aiocb = acb;
469 aio_req->iov_offset = iov_offset;
470 aio_req->oid = oid;
471 aio_req->base_oid = base_oid;
472 aio_req->offset = offset;
473 aio_req->data_len = data_len;
474 aio_req->flags = flags;
475 aio_req->id = s->aioreq_seq_num++;
476 aio_req->create = create;
478 acb->nr_pending++;
479 return aio_req;
482 static void wait_for_overlapping_aiocb(BDRVSheepdogState *s, SheepdogAIOCB *acb)
484 SheepdogAIOCB *cb;
486 retry:
487 QLIST_FOREACH(cb, &s->inflight_aiocb_head, aiocb_siblings) {
488 if (AIOCBOverlapping(acb, cb)) {
489 qemu_co_queue_wait(&s->overlapping_queue);
490 goto retry;
495 static void sd_aio_setup(SheepdogAIOCB *acb, BDRVSheepdogState *s,
496 QEMUIOVector *qiov, int64_t sector_num, int nb_sectors,
497 int type)
499 uint32_t object_size;
501 object_size = (UINT32_C(1) << s->inode.block_size_shift);
503 acb->s = s;
505 acb->qiov = qiov;
507 acb->sector_num = sector_num;
508 acb->nb_sectors = nb_sectors;
510 acb->coroutine = qemu_coroutine_self();
511 acb->ret = 0;
512 acb->nr_pending = 0;
514 acb->min_affect_data_idx = acb->sector_num * BDRV_SECTOR_SIZE / object_size;
515 acb->max_affect_data_idx = (acb->sector_num * BDRV_SECTOR_SIZE +
516 acb->nb_sectors * BDRV_SECTOR_SIZE) / object_size;
518 acb->min_dirty_data_idx = UINT32_MAX;
519 acb->max_dirty_data_idx = 0;
520 acb->aiocb_type = type;
522 if (type == AIOCB_FLUSH_CACHE) {
523 return;
526 wait_for_overlapping_aiocb(s, acb);
527 QLIST_INSERT_HEAD(&s->inflight_aiocb_head, acb, aiocb_siblings);
530 /* Return -EIO in case of error, file descriptor on success */
531 static int connect_to_sdog(BDRVSheepdogState *s, Error **errp)
533 int fd;
535 if (s->is_unix) {
536 fd = unix_connect(s->host_spec, errp);
537 } else {
538 fd = inet_connect(s->host_spec, errp);
540 if (fd >= 0) {
541 int ret = socket_set_nodelay(fd);
542 if (ret < 0) {
543 error_report("%s", strerror(errno));
548 if (fd >= 0) {
549 qemu_set_nonblock(fd);
550 } else {
551 fd = -EIO;
554 return fd;
557 /* Return 0 on success and -errno in case of error */
558 static coroutine_fn int send_co_req(int sockfd, SheepdogReq *hdr, void *data,
559 unsigned int *wlen)
561 int ret;
563 ret = qemu_co_send(sockfd, hdr, sizeof(*hdr));
564 if (ret != sizeof(*hdr)) {
565 error_report("failed to send a req, %s", strerror(errno));
566 return -errno;
569 ret = qemu_co_send(sockfd, data, *wlen);
570 if (ret != *wlen) {
571 error_report("failed to send a req, %s", strerror(errno));
572 return -errno;
575 return ret;
578 static void restart_co_req(void *opaque)
580 Coroutine *co = opaque;
582 qemu_coroutine_enter(co);
585 typedef struct SheepdogReqCo {
586 int sockfd;
587 BlockDriverState *bs;
588 AioContext *aio_context;
589 SheepdogReq *hdr;
590 void *data;
591 unsigned int *wlen;
592 unsigned int *rlen;
593 int ret;
594 bool finished;
595 } SheepdogReqCo;
597 static coroutine_fn void do_co_req(void *opaque)
599 int ret;
600 Coroutine *co;
601 SheepdogReqCo *srco = opaque;
602 int sockfd = srco->sockfd;
603 SheepdogReq *hdr = srco->hdr;
604 void *data = srco->data;
605 unsigned int *wlen = srco->wlen;
606 unsigned int *rlen = srco->rlen;
608 co = qemu_coroutine_self();
609 aio_set_fd_handler(srco->aio_context, sockfd, false,
610 NULL, restart_co_req, NULL, co);
612 ret = send_co_req(sockfd, hdr, data, wlen);
613 if (ret < 0) {
614 goto out;
617 aio_set_fd_handler(srco->aio_context, sockfd, false,
618 restart_co_req, NULL, NULL, co);
620 ret = qemu_co_recv(sockfd, hdr, sizeof(*hdr));
621 if (ret != sizeof(*hdr)) {
622 error_report("failed to get a rsp, %s", strerror(errno));
623 ret = -errno;
624 goto out;
627 if (*rlen > hdr->data_length) {
628 *rlen = hdr->data_length;
631 if (*rlen) {
632 ret = qemu_co_recv(sockfd, data, *rlen);
633 if (ret != *rlen) {
634 error_report("failed to get the data, %s", strerror(errno));
635 ret = -errno;
636 goto out;
639 ret = 0;
640 out:
641 /* there is at most one request for this sockfd, so it is safe to
642 * set each handler to NULL. */
643 aio_set_fd_handler(srco->aio_context, sockfd, false,
644 NULL, NULL, NULL, NULL);
646 srco->ret = ret;
647 srco->finished = true;
648 if (srco->bs) {
649 bdrv_wakeup(srco->bs);
654 * Send the request to the sheep in a synchronous manner.
656 * Return 0 on success, -errno in case of error.
658 static int do_req(int sockfd, BlockDriverState *bs, SheepdogReq *hdr,
659 void *data, unsigned int *wlen, unsigned int *rlen)
661 Coroutine *co;
662 SheepdogReqCo srco = {
663 .sockfd = sockfd,
664 .aio_context = bs ? bdrv_get_aio_context(bs) : qemu_get_aio_context(),
665 .bs = bs,
666 .hdr = hdr,
667 .data = data,
668 .wlen = wlen,
669 .rlen = rlen,
670 .ret = 0,
671 .finished = false,
674 if (qemu_in_coroutine()) {
675 do_co_req(&srco);
676 } else {
677 co = qemu_coroutine_create(do_co_req, &srco);
678 if (bs) {
679 qemu_coroutine_enter(co);
680 BDRV_POLL_WHILE(bs, !srco.finished);
681 } else {
682 qemu_coroutine_enter(co);
683 while (!srco.finished) {
684 aio_poll(qemu_get_aio_context(), true);
689 return srco.ret;
692 static void coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req,
693 struct iovec *iov, int niov,
694 enum AIOCBState aiocb_type);
695 static void coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req);
696 static int reload_inode(BDRVSheepdogState *s, uint32_t snapid, const char *tag);
697 static int get_sheep_fd(BDRVSheepdogState *s, Error **errp);
698 static void co_write_request(void *opaque);
700 static coroutine_fn void reconnect_to_sdog(void *opaque)
702 BDRVSheepdogState *s = opaque;
703 AIOReq *aio_req, *next;
705 aio_set_fd_handler(s->aio_context, s->fd, false, NULL,
706 NULL, NULL, NULL);
707 close(s->fd);
708 s->fd = -1;
710 /* Wait for outstanding write requests to be completed. */
711 while (s->co_send != NULL) {
712 co_write_request(opaque);
715 /* Try to reconnect the sheepdog server every one second. */
716 while (s->fd < 0) {
717 Error *local_err = NULL;
718 s->fd = get_sheep_fd(s, &local_err);
719 if (s->fd < 0) {
720 DPRINTF("Wait for connection to be established\n");
721 error_report_err(local_err);
722 co_aio_sleep_ns(bdrv_get_aio_context(s->bs), QEMU_CLOCK_REALTIME,
723 1000000000ULL);
728 * Now we have to resend all the request in the inflight queue. However,
729 * resend_aioreq() can yield and newly created requests can be added to the
730 * inflight queue before the coroutine is resumed. To avoid mixing them, we
731 * have to move all the inflight requests to the failed queue before
732 * resend_aioreq() is called.
734 QLIST_FOREACH_SAFE(aio_req, &s->inflight_aio_head, aio_siblings, next) {
735 QLIST_REMOVE(aio_req, aio_siblings);
736 QLIST_INSERT_HEAD(&s->failed_aio_head, aio_req, aio_siblings);
739 /* Resend all the failed aio requests. */
740 while (!QLIST_EMPTY(&s->failed_aio_head)) {
741 aio_req = QLIST_FIRST(&s->failed_aio_head);
742 QLIST_REMOVE(aio_req, aio_siblings);
743 resend_aioreq(s, aio_req);
748 * Receive responses of the I/O requests.
750 * This function is registered as a fd handler, and called from the
751 * main loop when s->fd is ready for reading responses.
753 static void coroutine_fn aio_read_response(void *opaque)
755 SheepdogObjRsp rsp;
756 BDRVSheepdogState *s = opaque;
757 int fd = s->fd;
758 int ret;
759 AIOReq *aio_req = NULL;
760 SheepdogAIOCB *acb;
761 uint64_t idx;
763 /* read a header */
764 ret = qemu_co_recv(fd, &rsp, sizeof(rsp));
765 if (ret != sizeof(rsp)) {
766 error_report("failed to get the header, %s", strerror(errno));
767 goto err;
770 /* find the right aio_req from the inflight aio list */
771 QLIST_FOREACH(aio_req, &s->inflight_aio_head, aio_siblings) {
772 if (aio_req->id == rsp.id) {
773 break;
776 if (!aio_req) {
777 error_report("cannot find aio_req %x", rsp.id);
778 goto err;
781 acb = aio_req->aiocb;
783 switch (acb->aiocb_type) {
784 case AIOCB_WRITE_UDATA:
785 if (!is_data_obj(aio_req->oid)) {
786 break;
788 idx = data_oid_to_idx(aio_req->oid);
790 if (aio_req->create) {
792 * If the object is newly created one, we need to update
793 * the vdi object (metadata object). min_dirty_data_idx
794 * and max_dirty_data_idx are changed to include updated
795 * index between them.
797 if (rsp.result == SD_RES_SUCCESS) {
798 s->inode.data_vdi_id[idx] = s->inode.vdi_id;
799 acb->max_dirty_data_idx = MAX(idx, acb->max_dirty_data_idx);
800 acb->min_dirty_data_idx = MIN(idx, acb->min_dirty_data_idx);
803 break;
804 case AIOCB_READ_UDATA:
805 ret = qemu_co_recvv(fd, acb->qiov->iov, acb->qiov->niov,
806 aio_req->iov_offset, rsp.data_length);
807 if (ret != rsp.data_length) {
808 error_report("failed to get the data, %s", strerror(errno));
809 goto err;
811 break;
812 case AIOCB_FLUSH_CACHE:
813 if (rsp.result == SD_RES_INVALID_PARMS) {
814 DPRINTF("disable cache since the server doesn't support it\n");
815 s->cache_flags = SD_FLAG_CMD_DIRECT;
816 rsp.result = SD_RES_SUCCESS;
818 break;
819 case AIOCB_DISCARD_OBJ:
820 switch (rsp.result) {
821 case SD_RES_INVALID_PARMS:
822 error_report("sheep(%s) doesn't support discard command",
823 s->host_spec);
824 rsp.result = SD_RES_SUCCESS;
825 s->discard_supported = false;
826 break;
827 default:
828 break;
832 /* No more data for this aio_req (reload_inode below uses its own file
833 * descriptor handler which doesn't use co_recv).
835 s->co_recv = NULL;
837 QLIST_REMOVE(aio_req, aio_siblings);
838 switch (rsp.result) {
839 case SD_RES_SUCCESS:
840 break;
841 case SD_RES_READONLY:
842 if (s->inode.vdi_id == oid_to_vid(aio_req->oid)) {
843 ret = reload_inode(s, 0, "");
844 if (ret < 0) {
845 goto err;
848 if (is_data_obj(aio_req->oid)) {
849 aio_req->oid = vid_to_data_oid(s->inode.vdi_id,
850 data_oid_to_idx(aio_req->oid));
851 } else {
852 aio_req->oid = vid_to_vdi_oid(s->inode.vdi_id);
854 resend_aioreq(s, aio_req);
855 return;
856 default:
857 acb->ret = -EIO;
858 error_report("%s", sd_strerror(rsp.result));
859 break;
862 g_free(aio_req);
864 if (!--acb->nr_pending) {
866 * We've finished all requests which belong to the AIOCB, so
867 * we can switch back to sd_co_readv/writev now.
869 qemu_coroutine_enter(acb->coroutine);
872 return;
874 err:
875 reconnect_to_sdog(opaque);
878 static void co_read_response(void *opaque)
880 BDRVSheepdogState *s = opaque;
882 if (!s->co_recv) {
883 s->co_recv = qemu_coroutine_create(aio_read_response, opaque);
886 qemu_coroutine_enter(s->co_recv);
889 static void co_write_request(void *opaque)
891 BDRVSheepdogState *s = opaque;
893 qemu_coroutine_enter(s->co_send);
897 * Return a socket descriptor to read/write objects.
899 * We cannot use this descriptor for other operations because
900 * the block driver may be on waiting response from the server.
902 static int get_sheep_fd(BDRVSheepdogState *s, Error **errp)
904 int fd;
906 fd = connect_to_sdog(s, errp);
907 if (fd < 0) {
908 return fd;
911 aio_set_fd_handler(s->aio_context, fd, false,
912 co_read_response, NULL, NULL, s);
913 return fd;
916 static int sd_parse_uri(BDRVSheepdogState *s, const char *filename,
917 char *vdi, uint32_t *snapid, char *tag)
919 URI *uri;
920 QueryParams *qp = NULL;
921 int ret = 0;
923 uri = uri_parse(filename);
924 if (!uri) {
925 return -EINVAL;
928 /* transport */
929 if (!strcmp(uri->scheme, "sheepdog")) {
930 s->is_unix = false;
931 } else if (!strcmp(uri->scheme, "sheepdog+tcp")) {
932 s->is_unix = false;
933 } else if (!strcmp(uri->scheme, "sheepdog+unix")) {
934 s->is_unix = true;
935 } else {
936 ret = -EINVAL;
937 goto out;
940 if (uri->path == NULL || !strcmp(uri->path, "/")) {
941 ret = -EINVAL;
942 goto out;
944 pstrcpy(vdi, SD_MAX_VDI_LEN, uri->path + 1);
946 qp = query_params_parse(uri->query);
947 if (qp->n > 1 || (s->is_unix && !qp->n) || (!s->is_unix && qp->n)) {
948 ret = -EINVAL;
949 goto out;
952 if (s->is_unix) {
953 /* sheepdog+unix:///vdiname?socket=path */
954 if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) {
955 ret = -EINVAL;
956 goto out;
958 s->host_spec = g_strdup(qp->p[0].value);
959 } else {
960 /* sheepdog[+tcp]://[host:port]/vdiname */
961 s->host_spec = g_strdup_printf("%s:%d", uri->server ?: SD_DEFAULT_ADDR,
962 uri->port ?: SD_DEFAULT_PORT);
965 /* snapshot tag */
966 if (uri->fragment) {
967 *snapid = strtoul(uri->fragment, NULL, 10);
968 if (*snapid == 0) {
969 pstrcpy(tag, SD_MAX_VDI_TAG_LEN, uri->fragment);
971 } else {
972 *snapid = CURRENT_VDI_ID; /* search current vdi */
975 out:
976 if (qp) {
977 query_params_free(qp);
979 uri_free(uri);
980 return ret;
984 * Parse a filename (old syntax)
986 * filename must be one of the following formats:
987 * 1. [vdiname]
988 * 2. [vdiname]:[snapid]
989 * 3. [vdiname]:[tag]
990 * 4. [hostname]:[port]:[vdiname]
991 * 5. [hostname]:[port]:[vdiname]:[snapid]
992 * 6. [hostname]:[port]:[vdiname]:[tag]
994 * You can boot from the snapshot images by specifying `snapid` or
995 * `tag'.
997 * You can run VMs outside the Sheepdog cluster by specifying
998 * `hostname' and `port' (experimental).
1000 static int parse_vdiname(BDRVSheepdogState *s, const char *filename,
1001 char *vdi, uint32_t *snapid, char *tag)
1003 char *p, *q, *uri;
1004 const char *host_spec, *vdi_spec;
1005 int nr_sep, ret;
1007 strstart(filename, "sheepdog:", &filename);
1008 p = q = g_strdup(filename);
1010 /* count the number of separators */
1011 nr_sep = 0;
1012 while (*p) {
1013 if (*p == ':') {
1014 nr_sep++;
1016 p++;
1018 p = q;
1020 /* use the first two tokens as host_spec. */
1021 if (nr_sep >= 2) {
1022 host_spec = p;
1023 p = strchr(p, ':');
1024 p++;
1025 p = strchr(p, ':');
1026 *p++ = '\0';
1027 } else {
1028 host_spec = "";
1031 vdi_spec = p;
1033 p = strchr(vdi_spec, ':');
1034 if (p) {
1035 *p++ = '#';
1038 uri = g_strdup_printf("sheepdog://%s/%s", host_spec, vdi_spec);
1040 ret = sd_parse_uri(s, uri, vdi, snapid, tag);
1042 g_free(q);
1043 g_free(uri);
1045 return ret;
1048 static int find_vdi_name(BDRVSheepdogState *s, const char *filename,
1049 uint32_t snapid, const char *tag, uint32_t *vid,
1050 bool lock, Error **errp)
1052 int ret, fd;
1053 SheepdogVdiReq hdr;
1054 SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr;
1055 unsigned int wlen, rlen = 0;
1056 char buf[SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN];
1058 fd = connect_to_sdog(s, errp);
1059 if (fd < 0) {
1060 return fd;
1063 /* This pair of strncpy calls ensures that the buffer is zero-filled,
1064 * which is desirable since we'll soon be sending those bytes, and
1065 * don't want the send_req to read uninitialized data.
1067 strncpy(buf, filename, SD_MAX_VDI_LEN);
1068 strncpy(buf + SD_MAX_VDI_LEN, tag, SD_MAX_VDI_TAG_LEN);
1070 memset(&hdr, 0, sizeof(hdr));
1071 if (lock) {
1072 hdr.opcode = SD_OP_LOCK_VDI;
1073 hdr.type = LOCK_TYPE_NORMAL;
1074 } else {
1075 hdr.opcode = SD_OP_GET_VDI_INFO;
1077 wlen = SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN;
1078 hdr.proto_ver = SD_PROTO_VER;
1079 hdr.data_length = wlen;
1080 hdr.snapid = snapid;
1081 hdr.flags = SD_FLAG_CMD_WRITE;
1083 ret = do_req(fd, s->bs, (SheepdogReq *)&hdr, buf, &wlen, &rlen);
1084 if (ret) {
1085 error_setg_errno(errp, -ret, "cannot get vdi info");
1086 goto out;
1089 if (rsp->result != SD_RES_SUCCESS) {
1090 error_setg(errp, "cannot get vdi info, %s, %s %" PRIu32 " %s",
1091 sd_strerror(rsp->result), filename, snapid, tag);
1092 if (rsp->result == SD_RES_NO_VDI) {
1093 ret = -ENOENT;
1094 } else if (rsp->result == SD_RES_VDI_LOCKED) {
1095 ret = -EBUSY;
1096 } else {
1097 ret = -EIO;
1099 goto out;
1101 *vid = rsp->vdi_id;
1103 ret = 0;
1104 out:
1105 closesocket(fd);
1106 return ret;
1109 static void coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req,
1110 struct iovec *iov, int niov,
1111 enum AIOCBState aiocb_type)
1113 int nr_copies = s->inode.nr_copies;
1114 SheepdogObjReq hdr;
1115 unsigned int wlen = 0;
1116 int ret;
1117 uint64_t oid = aio_req->oid;
1118 unsigned int datalen = aio_req->data_len;
1119 uint64_t offset = aio_req->offset;
1120 uint8_t flags = aio_req->flags;
1121 uint64_t old_oid = aio_req->base_oid;
1122 bool create = aio_req->create;
1124 QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings);
1126 if (!nr_copies) {
1127 error_report("bug");
1130 memset(&hdr, 0, sizeof(hdr));
1132 switch (aiocb_type) {
1133 case AIOCB_FLUSH_CACHE:
1134 hdr.opcode = SD_OP_FLUSH_VDI;
1135 break;
1136 case AIOCB_READ_UDATA:
1137 hdr.opcode = SD_OP_READ_OBJ;
1138 hdr.flags = flags;
1139 break;
1140 case AIOCB_WRITE_UDATA:
1141 if (create) {
1142 hdr.opcode = SD_OP_CREATE_AND_WRITE_OBJ;
1143 } else {
1144 hdr.opcode = SD_OP_WRITE_OBJ;
1146 wlen = datalen;
1147 hdr.flags = SD_FLAG_CMD_WRITE | flags;
1148 break;
1149 case AIOCB_DISCARD_OBJ:
1150 hdr.opcode = SD_OP_WRITE_OBJ;
1151 hdr.flags = SD_FLAG_CMD_WRITE | flags;
1152 s->inode.data_vdi_id[data_oid_to_idx(oid)] = 0;
1153 offset = offsetof(SheepdogInode,
1154 data_vdi_id[data_oid_to_idx(oid)]);
1155 oid = vid_to_vdi_oid(s->inode.vdi_id);
1156 wlen = datalen = sizeof(uint32_t);
1157 break;
1160 if (s->cache_flags) {
1161 hdr.flags |= s->cache_flags;
1164 hdr.oid = oid;
1165 hdr.cow_oid = old_oid;
1166 hdr.copies = s->inode.nr_copies;
1168 hdr.data_length = datalen;
1169 hdr.offset = offset;
1171 hdr.id = aio_req->id;
1173 qemu_co_mutex_lock(&s->lock);
1174 s->co_send = qemu_coroutine_self();
1175 aio_set_fd_handler(s->aio_context, s->fd, false,
1176 co_read_response, co_write_request, NULL, s);
1177 socket_set_cork(s->fd, 1);
1179 /* send a header */
1180 ret = qemu_co_send(s->fd, &hdr, sizeof(hdr));
1181 if (ret != sizeof(hdr)) {
1182 error_report("failed to send a req, %s", strerror(errno));
1183 goto out;
1186 if (wlen) {
1187 ret = qemu_co_sendv(s->fd, iov, niov, aio_req->iov_offset, wlen);
1188 if (ret != wlen) {
1189 error_report("failed to send a data, %s", strerror(errno));
1192 out:
1193 socket_set_cork(s->fd, 0);
1194 aio_set_fd_handler(s->aio_context, s->fd, false,
1195 co_read_response, NULL, NULL, s);
1196 s->co_send = NULL;
1197 qemu_co_mutex_unlock(&s->lock);
1200 static int read_write_object(int fd, BlockDriverState *bs, char *buf,
1201 uint64_t oid, uint8_t copies,
1202 unsigned int datalen, uint64_t offset,
1203 bool write, bool create, uint32_t cache_flags)
1205 SheepdogObjReq hdr;
1206 SheepdogObjRsp *rsp = (SheepdogObjRsp *)&hdr;
1207 unsigned int wlen, rlen;
1208 int ret;
1210 memset(&hdr, 0, sizeof(hdr));
1212 if (write) {
1213 wlen = datalen;
1214 rlen = 0;
1215 hdr.flags = SD_FLAG_CMD_WRITE;
1216 if (create) {
1217 hdr.opcode = SD_OP_CREATE_AND_WRITE_OBJ;
1218 } else {
1219 hdr.opcode = SD_OP_WRITE_OBJ;
1221 } else {
1222 wlen = 0;
1223 rlen = datalen;
1224 hdr.opcode = SD_OP_READ_OBJ;
1227 hdr.flags |= cache_flags;
1229 hdr.oid = oid;
1230 hdr.data_length = datalen;
1231 hdr.offset = offset;
1232 hdr.copies = copies;
1234 ret = do_req(fd, bs, (SheepdogReq *)&hdr, buf, &wlen, &rlen);
1235 if (ret) {
1236 error_report("failed to send a request to the sheep");
1237 return ret;
1240 switch (rsp->result) {
1241 case SD_RES_SUCCESS:
1242 return 0;
1243 default:
1244 error_report("%s", sd_strerror(rsp->result));
1245 return -EIO;
1249 static int read_object(int fd, BlockDriverState *bs, char *buf,
1250 uint64_t oid, uint8_t copies,
1251 unsigned int datalen, uint64_t offset,
1252 uint32_t cache_flags)
1254 return read_write_object(fd, bs, buf, oid, copies,
1255 datalen, offset, false,
1256 false, cache_flags);
1259 static int write_object(int fd, BlockDriverState *bs, char *buf,
1260 uint64_t oid, uint8_t copies,
1261 unsigned int datalen, uint64_t offset, bool create,
1262 uint32_t cache_flags)
1264 return read_write_object(fd, bs, buf, oid, copies,
1265 datalen, offset, true,
1266 create, cache_flags);
1269 /* update inode with the latest state */
1270 static int reload_inode(BDRVSheepdogState *s, uint32_t snapid, const char *tag)
1272 Error *local_err = NULL;
1273 SheepdogInode *inode;
1274 int ret = 0, fd;
1275 uint32_t vid = 0;
1277 fd = connect_to_sdog(s, &local_err);
1278 if (fd < 0) {
1279 error_report_err(local_err);
1280 return -EIO;
1283 inode = g_malloc(SD_INODE_HEADER_SIZE);
1285 ret = find_vdi_name(s, s->name, snapid, tag, &vid, false, &local_err);
1286 if (ret) {
1287 error_report_err(local_err);
1288 goto out;
1291 ret = read_object(fd, s->bs, (char *)inode, vid_to_vdi_oid(vid),
1292 s->inode.nr_copies, SD_INODE_HEADER_SIZE, 0,
1293 s->cache_flags);
1294 if (ret < 0) {
1295 goto out;
1298 if (inode->vdi_id != s->inode.vdi_id) {
1299 memcpy(&s->inode, inode, SD_INODE_HEADER_SIZE);
1302 out:
1303 g_free(inode);
1304 closesocket(fd);
1306 return ret;
1309 static void coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req)
1311 SheepdogAIOCB *acb = aio_req->aiocb;
1313 aio_req->create = false;
1315 /* check whether this request becomes a CoW one */
1316 if (acb->aiocb_type == AIOCB_WRITE_UDATA && is_data_obj(aio_req->oid)) {
1317 int idx = data_oid_to_idx(aio_req->oid);
1319 if (is_data_obj_writable(&s->inode, idx)) {
1320 goto out;
1323 if (s->inode.data_vdi_id[idx]) {
1324 aio_req->base_oid = vid_to_data_oid(s->inode.data_vdi_id[idx], idx);
1325 aio_req->flags |= SD_FLAG_CMD_COW;
1327 aio_req->create = true;
1329 out:
1330 if (is_data_obj(aio_req->oid)) {
1331 add_aio_request(s, aio_req, acb->qiov->iov, acb->qiov->niov,
1332 acb->aiocb_type);
1333 } else {
1334 struct iovec iov;
1335 iov.iov_base = &s->inode;
1336 iov.iov_len = sizeof(s->inode);
1337 add_aio_request(s, aio_req, &iov, 1, AIOCB_WRITE_UDATA);
1341 static void sd_detach_aio_context(BlockDriverState *bs)
1343 BDRVSheepdogState *s = bs->opaque;
1345 aio_set_fd_handler(s->aio_context, s->fd, false, NULL,
1346 NULL, NULL, NULL);
1349 static void sd_attach_aio_context(BlockDriverState *bs,
1350 AioContext *new_context)
1352 BDRVSheepdogState *s = bs->opaque;
1354 s->aio_context = new_context;
1355 aio_set_fd_handler(new_context, s->fd, false,
1356 co_read_response, NULL, NULL, s);
1359 /* TODO Convert to fine grained options */
1360 static QemuOptsList runtime_opts = {
1361 .name = "sheepdog",
1362 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
1363 .desc = {
1365 .name = "filename",
1366 .type = QEMU_OPT_STRING,
1367 .help = "URL to the sheepdog image",
1369 { /* end of list */ }
1373 static int sd_open(BlockDriverState *bs, QDict *options, int flags,
1374 Error **errp)
1376 int ret, fd;
1377 uint32_t vid = 0;
1378 BDRVSheepdogState *s = bs->opaque;
1379 char vdi[SD_MAX_VDI_LEN], tag[SD_MAX_VDI_TAG_LEN];
1380 uint32_t snapid;
1381 char *buf = NULL;
1382 QemuOpts *opts;
1383 Error *local_err = NULL;
1384 const char *filename;
1386 s->bs = bs;
1387 s->aio_context = bdrv_get_aio_context(bs);
1389 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
1390 qemu_opts_absorb_qdict(opts, options, &local_err);
1391 if (local_err) {
1392 error_propagate(errp, local_err);
1393 ret = -EINVAL;
1394 goto out;
1397 filename = qemu_opt_get(opts, "filename");
1399 QLIST_INIT(&s->inflight_aio_head);
1400 QLIST_INIT(&s->failed_aio_head);
1401 QLIST_INIT(&s->inflight_aiocb_head);
1402 s->fd = -1;
1404 memset(vdi, 0, sizeof(vdi));
1405 memset(tag, 0, sizeof(tag));
1407 if (strstr(filename, "://")) {
1408 ret = sd_parse_uri(s, filename, vdi, &snapid, tag);
1409 } else {
1410 ret = parse_vdiname(s, filename, vdi, &snapid, tag);
1412 if (ret < 0) {
1413 error_setg(errp, "Can't parse filename");
1414 goto out;
1416 s->fd = get_sheep_fd(s, errp);
1417 if (s->fd < 0) {
1418 ret = s->fd;
1419 goto out;
1422 ret = find_vdi_name(s, vdi, snapid, tag, &vid, true, errp);
1423 if (ret) {
1424 goto out;
1428 * QEMU block layer emulates writethrough cache as 'writeback + flush', so
1429 * we always set SD_FLAG_CMD_CACHE (writeback cache) as default.
1431 s->cache_flags = SD_FLAG_CMD_CACHE;
1432 if (flags & BDRV_O_NOCACHE) {
1433 s->cache_flags = SD_FLAG_CMD_DIRECT;
1435 s->discard_supported = true;
1437 if (snapid || tag[0] != '\0') {
1438 DPRINTF("%" PRIx32 " snapshot inode was open.\n", vid);
1439 s->is_snapshot = true;
1442 fd = connect_to_sdog(s, errp);
1443 if (fd < 0) {
1444 ret = fd;
1445 goto out;
1448 buf = g_malloc(SD_INODE_SIZE);
1449 ret = read_object(fd, s->bs, buf, vid_to_vdi_oid(vid),
1450 0, SD_INODE_SIZE, 0, s->cache_flags);
1452 closesocket(fd);
1454 if (ret) {
1455 error_setg(errp, "Can't read snapshot inode");
1456 goto out;
1459 memcpy(&s->inode, buf, sizeof(s->inode));
1461 bs->total_sectors = s->inode.vdi_size / BDRV_SECTOR_SIZE;
1462 pstrcpy(s->name, sizeof(s->name), vdi);
1463 qemu_co_mutex_init(&s->lock);
1464 qemu_co_queue_init(&s->overlapping_queue);
1465 qemu_opts_del(opts);
1466 g_free(buf);
1467 return 0;
1468 out:
1469 aio_set_fd_handler(bdrv_get_aio_context(bs), s->fd,
1470 false, NULL, NULL, NULL, NULL);
1471 if (s->fd >= 0) {
1472 closesocket(s->fd);
1474 qemu_opts_del(opts);
1475 g_free(buf);
1476 return ret;
1479 static int sd_reopen_prepare(BDRVReopenState *state, BlockReopenQueue *queue,
1480 Error **errp)
1482 BDRVSheepdogState *s = state->bs->opaque;
1483 BDRVSheepdogReopenState *re_s;
1484 int ret = 0;
1486 re_s = state->opaque = g_new0(BDRVSheepdogReopenState, 1);
1488 re_s->cache_flags = SD_FLAG_CMD_CACHE;
1489 if (state->flags & BDRV_O_NOCACHE) {
1490 re_s->cache_flags = SD_FLAG_CMD_DIRECT;
1493 re_s->fd = get_sheep_fd(s, errp);
1494 if (re_s->fd < 0) {
1495 ret = re_s->fd;
1496 return ret;
1499 return ret;
1502 static void sd_reopen_commit(BDRVReopenState *state)
1504 BDRVSheepdogReopenState *re_s = state->opaque;
1505 BDRVSheepdogState *s = state->bs->opaque;
1507 if (s->fd) {
1508 aio_set_fd_handler(s->aio_context, s->fd, false,
1509 NULL, NULL, NULL, NULL);
1510 closesocket(s->fd);
1513 s->fd = re_s->fd;
1514 s->cache_flags = re_s->cache_flags;
1516 g_free(state->opaque);
1517 state->opaque = NULL;
1519 return;
1522 static void sd_reopen_abort(BDRVReopenState *state)
1524 BDRVSheepdogReopenState *re_s = state->opaque;
1525 BDRVSheepdogState *s = state->bs->opaque;
1527 if (re_s == NULL) {
1528 return;
1531 if (re_s->fd) {
1532 aio_set_fd_handler(s->aio_context, re_s->fd, false,
1533 NULL, NULL, NULL, NULL);
1534 closesocket(re_s->fd);
1537 g_free(state->opaque);
1538 state->opaque = NULL;
1540 return;
1543 static int do_sd_create(BDRVSheepdogState *s, uint32_t *vdi_id, int snapshot,
1544 Error **errp)
1546 SheepdogVdiReq hdr;
1547 SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr;
1548 int fd, ret;
1549 unsigned int wlen, rlen = 0;
1550 char buf[SD_MAX_VDI_LEN];
1552 fd = connect_to_sdog(s, errp);
1553 if (fd < 0) {
1554 return fd;
1557 /* FIXME: would it be better to fail (e.g., return -EIO) when filename
1558 * does not fit in buf? For now, just truncate and avoid buffer overrun.
1560 memset(buf, 0, sizeof(buf));
1561 pstrcpy(buf, sizeof(buf), s->name);
1563 memset(&hdr, 0, sizeof(hdr));
1564 hdr.opcode = SD_OP_NEW_VDI;
1565 hdr.base_vdi_id = s->inode.vdi_id;
1567 wlen = SD_MAX_VDI_LEN;
1569 hdr.flags = SD_FLAG_CMD_WRITE;
1570 hdr.snapid = snapshot;
1572 hdr.data_length = wlen;
1573 hdr.vdi_size = s->inode.vdi_size;
1574 hdr.copy_policy = s->inode.copy_policy;
1575 hdr.copies = s->inode.nr_copies;
1576 hdr.block_size_shift = s->inode.block_size_shift;
1578 ret = do_req(fd, NULL, (SheepdogReq *)&hdr, buf, &wlen, &rlen);
1580 closesocket(fd);
1582 if (ret) {
1583 error_setg_errno(errp, -ret, "create failed");
1584 return ret;
1587 if (rsp->result != SD_RES_SUCCESS) {
1588 error_setg(errp, "%s, %s", sd_strerror(rsp->result), s->inode.name);
1589 return -EIO;
1592 if (vdi_id) {
1593 *vdi_id = rsp->vdi_id;
1596 return 0;
1599 static int sd_prealloc(const char *filename, Error **errp)
1601 BlockBackend *blk = NULL;
1602 BDRVSheepdogState *base = NULL;
1603 unsigned long buf_size;
1604 uint32_t idx, max_idx;
1605 uint32_t object_size;
1606 int64_t vdi_size;
1607 void *buf = NULL;
1608 int ret;
1610 blk = blk_new_open(filename, NULL, NULL,
1611 BDRV_O_RDWR | BDRV_O_PROTOCOL, errp);
1612 if (blk == NULL) {
1613 ret = -EIO;
1614 goto out_with_err_set;
1617 blk_set_allow_write_beyond_eof(blk, true);
1619 vdi_size = blk_getlength(blk);
1620 if (vdi_size < 0) {
1621 ret = vdi_size;
1622 goto out;
1625 base = blk_bs(blk)->opaque;
1626 object_size = (UINT32_C(1) << base->inode.block_size_shift);
1627 buf_size = MIN(object_size, SD_DATA_OBJ_SIZE);
1628 buf = g_malloc0(buf_size);
1630 max_idx = DIV_ROUND_UP(vdi_size, buf_size);
1632 for (idx = 0; idx < max_idx; idx++) {
1634 * The created image can be a cloned image, so we need to read
1635 * a data from the source image.
1637 ret = blk_pread(blk, idx * buf_size, buf, buf_size);
1638 if (ret < 0) {
1639 goto out;
1641 ret = blk_pwrite(blk, idx * buf_size, buf, buf_size, 0);
1642 if (ret < 0) {
1643 goto out;
1647 ret = 0;
1648 out:
1649 if (ret < 0) {
1650 error_setg_errno(errp, -ret, "Can't pre-allocate");
1652 out_with_err_set:
1653 if (blk) {
1654 blk_unref(blk);
1656 g_free(buf);
1658 return ret;
1662 * Sheepdog support two kinds of redundancy, full replication and erasure
1663 * coding.
1665 * # create a fully replicated vdi with x copies
1666 * -o redundancy=x (1 <= x <= SD_MAX_COPIES)
1668 * # create a erasure coded vdi with x data strips and y parity strips
1669 * -o redundancy=x:y (x must be one of {2,4,8,16} and 1 <= y < SD_EC_MAX_STRIP)
1671 static int parse_redundancy(BDRVSheepdogState *s, const char *opt)
1673 struct SheepdogInode *inode = &s->inode;
1674 const char *n1, *n2;
1675 long copy, parity;
1676 char p[10];
1678 pstrcpy(p, sizeof(p), opt);
1679 n1 = strtok(p, ":");
1680 n2 = strtok(NULL, ":");
1682 if (!n1) {
1683 return -EINVAL;
1686 copy = strtol(n1, NULL, 10);
1687 if (copy > SD_MAX_COPIES || copy < 1) {
1688 return -EINVAL;
1690 if (!n2) {
1691 inode->copy_policy = 0;
1692 inode->nr_copies = copy;
1693 return 0;
1696 if (copy != 2 && copy != 4 && copy != 8 && copy != 16) {
1697 return -EINVAL;
1700 parity = strtol(n2, NULL, 10);
1701 if (parity >= SD_EC_MAX_STRIP || parity < 1) {
1702 return -EINVAL;
1706 * 4 bits for parity and 4 bits for data.
1707 * We have to compress upper data bits because it can't represent 16
1709 inode->copy_policy = ((copy / 2) << 4) + parity;
1710 inode->nr_copies = copy + parity;
1712 return 0;
1715 static int parse_block_size_shift(BDRVSheepdogState *s, QemuOpts *opt)
1717 struct SheepdogInode *inode = &s->inode;
1718 uint64_t object_size;
1719 int obj_order;
1721 object_size = qemu_opt_get_size_del(opt, BLOCK_OPT_OBJECT_SIZE, 0);
1722 if (object_size) {
1723 if ((object_size - 1) & object_size) { /* not a power of 2? */
1724 return -EINVAL;
1726 obj_order = ctz32(object_size);
1727 if (obj_order < 20 || obj_order > 31) {
1728 return -EINVAL;
1730 inode->block_size_shift = (uint8_t)obj_order;
1733 return 0;
1736 static int sd_create(const char *filename, QemuOpts *opts,
1737 Error **errp)
1739 int ret = 0;
1740 uint32_t vid = 0;
1741 char *backing_file = NULL;
1742 char *buf = NULL;
1743 BDRVSheepdogState *s;
1744 char tag[SD_MAX_VDI_TAG_LEN];
1745 uint32_t snapid;
1746 uint64_t max_vdi_size;
1747 bool prealloc = false;
1749 s = g_new0(BDRVSheepdogState, 1);
1751 memset(tag, 0, sizeof(tag));
1752 if (strstr(filename, "://")) {
1753 ret = sd_parse_uri(s, filename, s->name, &snapid, tag);
1754 } else {
1755 ret = parse_vdiname(s, filename, s->name, &snapid, tag);
1757 if (ret < 0) {
1758 error_setg(errp, "Can't parse filename");
1759 goto out;
1762 s->inode.vdi_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
1763 BDRV_SECTOR_SIZE);
1764 backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
1765 buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
1766 if (!buf || !strcmp(buf, "off")) {
1767 prealloc = false;
1768 } else if (!strcmp(buf, "full")) {
1769 prealloc = true;
1770 } else {
1771 error_setg(errp, "Invalid preallocation mode: '%s'", buf);
1772 ret = -EINVAL;
1773 goto out;
1776 g_free(buf);
1777 buf = qemu_opt_get_del(opts, BLOCK_OPT_REDUNDANCY);
1778 if (buf) {
1779 ret = parse_redundancy(s, buf);
1780 if (ret < 0) {
1781 error_setg(errp, "Invalid redundancy mode: '%s'", buf);
1782 goto out;
1785 ret = parse_block_size_shift(s, opts);
1786 if (ret < 0) {
1787 error_setg(errp, "Invalid object_size."
1788 " obect_size needs to be power of 2"
1789 " and be limited from 2^20 to 2^31");
1790 goto out;
1793 if (backing_file) {
1794 BlockBackend *blk;
1795 BDRVSheepdogState *base;
1796 BlockDriver *drv;
1798 /* Currently, only Sheepdog backing image is supported. */
1799 drv = bdrv_find_protocol(backing_file, true, NULL);
1800 if (!drv || strcmp(drv->protocol_name, "sheepdog") != 0) {
1801 error_setg(errp, "backing_file must be a sheepdog image");
1802 ret = -EINVAL;
1803 goto out;
1806 blk = blk_new_open(backing_file, NULL, NULL,
1807 BDRV_O_PROTOCOL, errp);
1808 if (blk == NULL) {
1809 ret = -EIO;
1810 goto out;
1813 base = blk_bs(blk)->opaque;
1815 if (!is_snapshot(&base->inode)) {
1816 error_setg(errp, "cannot clone from a non snapshot vdi");
1817 blk_unref(blk);
1818 ret = -EINVAL;
1819 goto out;
1821 s->inode.vdi_id = base->inode.vdi_id;
1822 blk_unref(blk);
1825 s->aio_context = qemu_get_aio_context();
1827 /* if block_size_shift is not specified, get cluster default value */
1828 if (s->inode.block_size_shift == 0) {
1829 SheepdogVdiReq hdr;
1830 SheepdogClusterRsp *rsp = (SheepdogClusterRsp *)&hdr;
1831 Error *local_err = NULL;
1832 int fd;
1833 unsigned int wlen = 0, rlen = 0;
1835 fd = connect_to_sdog(s, &local_err);
1836 if (fd < 0) {
1837 error_report_err(local_err);
1838 ret = -EIO;
1839 goto out;
1842 memset(&hdr, 0, sizeof(hdr));
1843 hdr.opcode = SD_OP_GET_CLUSTER_DEFAULT;
1844 hdr.proto_ver = SD_PROTO_VER;
1846 ret = do_req(fd, NULL, (SheepdogReq *)&hdr,
1847 NULL, &wlen, &rlen);
1848 closesocket(fd);
1849 if (ret) {
1850 error_setg_errno(errp, -ret, "failed to get cluster default");
1851 goto out;
1853 if (rsp->result == SD_RES_SUCCESS) {
1854 s->inode.block_size_shift = rsp->block_size_shift;
1855 } else {
1856 s->inode.block_size_shift = SD_DEFAULT_BLOCK_SIZE_SHIFT;
1860 max_vdi_size = (UINT64_C(1) << s->inode.block_size_shift) * MAX_DATA_OBJS;
1862 if (s->inode.vdi_size > max_vdi_size) {
1863 error_setg(errp, "An image is too large."
1864 " The maximum image size is %"PRIu64 "GB",
1865 max_vdi_size / 1024 / 1024 / 1024);
1866 ret = -EINVAL;
1867 goto out;
1870 ret = do_sd_create(s, &vid, 0, errp);
1871 if (ret) {
1872 goto out;
1875 if (prealloc) {
1876 ret = sd_prealloc(filename, errp);
1878 out:
1879 g_free(backing_file);
1880 g_free(buf);
1881 g_free(s);
1882 return ret;
1885 static void sd_close(BlockDriverState *bs)
1887 Error *local_err = NULL;
1888 BDRVSheepdogState *s = bs->opaque;
1889 SheepdogVdiReq hdr;
1890 SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr;
1891 unsigned int wlen, rlen = 0;
1892 int fd, ret;
1894 DPRINTF("%s\n", s->name);
1896 fd = connect_to_sdog(s, &local_err);
1897 if (fd < 0) {
1898 error_report_err(local_err);
1899 return;
1902 memset(&hdr, 0, sizeof(hdr));
1904 hdr.opcode = SD_OP_RELEASE_VDI;
1905 hdr.type = LOCK_TYPE_NORMAL;
1906 hdr.base_vdi_id = s->inode.vdi_id;
1907 wlen = strlen(s->name) + 1;
1908 hdr.data_length = wlen;
1909 hdr.flags = SD_FLAG_CMD_WRITE;
1911 ret = do_req(fd, s->bs, (SheepdogReq *)&hdr,
1912 s->name, &wlen, &rlen);
1914 closesocket(fd);
1916 if (!ret && rsp->result != SD_RES_SUCCESS &&
1917 rsp->result != SD_RES_VDI_NOT_LOCKED) {
1918 error_report("%s, %s", sd_strerror(rsp->result), s->name);
1921 aio_set_fd_handler(bdrv_get_aio_context(bs), s->fd,
1922 false, NULL, NULL, NULL, NULL);
1923 closesocket(s->fd);
1924 g_free(s->host_spec);
1927 static int64_t sd_getlength(BlockDriverState *bs)
1929 BDRVSheepdogState *s = bs->opaque;
1931 return s->inode.vdi_size;
1934 static int sd_truncate(BlockDriverState *bs, int64_t offset)
1936 Error *local_err = NULL;
1937 BDRVSheepdogState *s = bs->opaque;
1938 int ret, fd;
1939 unsigned int datalen;
1940 uint64_t max_vdi_size;
1942 max_vdi_size = (UINT64_C(1) << s->inode.block_size_shift) * MAX_DATA_OBJS;
1943 if (offset < s->inode.vdi_size) {
1944 error_report("shrinking is not supported");
1945 return -EINVAL;
1946 } else if (offset > max_vdi_size) {
1947 error_report("too big image size");
1948 return -EINVAL;
1951 fd = connect_to_sdog(s, &local_err);
1952 if (fd < 0) {
1953 error_report_err(local_err);
1954 return fd;
1957 /* we don't need to update entire object */
1958 datalen = SD_INODE_SIZE - sizeof(s->inode.data_vdi_id);
1959 s->inode.vdi_size = offset;
1960 ret = write_object(fd, s->bs, (char *)&s->inode,
1961 vid_to_vdi_oid(s->inode.vdi_id), s->inode.nr_copies,
1962 datalen, 0, false, s->cache_flags);
1963 close(fd);
1965 if (ret < 0) {
1966 error_report("failed to update an inode.");
1969 return ret;
1973 * This function is called after writing data objects. If we need to
1974 * update metadata, this sends a write request to the vdi object.
1976 static void coroutine_fn sd_write_done(SheepdogAIOCB *acb)
1978 BDRVSheepdogState *s = acb->s;
1979 struct iovec iov;
1980 AIOReq *aio_req;
1981 uint32_t offset, data_len, mn, mx;
1983 mn = acb->min_dirty_data_idx;
1984 mx = acb->max_dirty_data_idx;
1985 if (mn <= mx) {
1986 /* we need to update the vdi object. */
1987 ++acb->nr_pending;
1988 offset = sizeof(s->inode) - sizeof(s->inode.data_vdi_id) +
1989 mn * sizeof(s->inode.data_vdi_id[0]);
1990 data_len = (mx - mn + 1) * sizeof(s->inode.data_vdi_id[0]);
1992 acb->min_dirty_data_idx = UINT32_MAX;
1993 acb->max_dirty_data_idx = 0;
1995 iov.iov_base = &s->inode;
1996 iov.iov_len = sizeof(s->inode);
1997 aio_req = alloc_aio_req(s, acb, vid_to_vdi_oid(s->inode.vdi_id),
1998 data_len, offset, 0, false, 0, offset);
1999 add_aio_request(s, aio_req, &iov, 1, AIOCB_WRITE_UDATA);
2000 if (--acb->nr_pending) {
2001 qemu_coroutine_yield();
2006 /* Delete current working VDI on the snapshot chain */
2007 static bool sd_delete(BDRVSheepdogState *s)
2009 Error *local_err = NULL;
2010 unsigned int wlen = SD_MAX_VDI_LEN, rlen = 0;
2011 SheepdogVdiReq hdr = {
2012 .opcode = SD_OP_DEL_VDI,
2013 .base_vdi_id = s->inode.vdi_id,
2014 .data_length = wlen,
2015 .flags = SD_FLAG_CMD_WRITE,
2017 SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr;
2018 int fd, ret;
2020 fd = connect_to_sdog(s, &local_err);
2021 if (fd < 0) {
2022 error_report_err(local_err);
2023 return false;
2026 ret = do_req(fd, s->bs, (SheepdogReq *)&hdr,
2027 s->name, &wlen, &rlen);
2028 closesocket(fd);
2029 if (ret) {
2030 return false;
2032 switch (rsp->result) {
2033 case SD_RES_NO_VDI:
2034 error_report("%s was already deleted", s->name);
2035 /* fall through */
2036 case SD_RES_SUCCESS:
2037 break;
2038 default:
2039 error_report("%s, %s", sd_strerror(rsp->result), s->name);
2040 return false;
2043 return true;
2047 * Create a writable VDI from a snapshot
2049 static int sd_create_branch(BDRVSheepdogState *s)
2051 Error *local_err = NULL;
2052 int ret, fd;
2053 uint32_t vid;
2054 char *buf;
2055 bool deleted;
2057 DPRINTF("%" PRIx32 " is snapshot.\n", s->inode.vdi_id);
2059 buf = g_malloc(SD_INODE_SIZE);
2062 * Even If deletion fails, we will just create extra snapshot based on
2063 * the working VDI which was supposed to be deleted. So no need to
2064 * false bail out.
2066 deleted = sd_delete(s);
2067 ret = do_sd_create(s, &vid, !deleted, &local_err);
2068 if (ret) {
2069 error_report_err(local_err);
2070 goto out;
2073 DPRINTF("%" PRIx32 " is created.\n", vid);
2075 fd = connect_to_sdog(s, &local_err);
2076 if (fd < 0) {
2077 error_report_err(local_err);
2078 ret = fd;
2079 goto out;
2082 ret = read_object(fd, s->bs, buf, vid_to_vdi_oid(vid),
2083 s->inode.nr_copies, SD_INODE_SIZE, 0, s->cache_flags);
2085 closesocket(fd);
2087 if (ret < 0) {
2088 goto out;
2091 memcpy(&s->inode, buf, sizeof(s->inode));
2093 s->is_snapshot = false;
2094 ret = 0;
2095 DPRINTF("%" PRIx32 " was newly created.\n", s->inode.vdi_id);
2097 out:
2098 g_free(buf);
2100 return ret;
2104 * Send I/O requests to the server.
2106 * This function sends requests to the server, links the requests to
2107 * the inflight_list in BDRVSheepdogState, and exits without
2108 * waiting the response. The responses are received in the
2109 * `aio_read_response' function which is called from the main loop as
2110 * a fd handler.
2112 * Returns 1 when we need to wait a response, 0 when there is no sent
2113 * request and -errno in error cases.
2115 static void coroutine_fn sd_co_rw_vector(SheepdogAIOCB *acb)
2117 int ret = 0;
2118 unsigned long len, done = 0, total = acb->nb_sectors * BDRV_SECTOR_SIZE;
2119 unsigned long idx;
2120 uint32_t object_size;
2121 uint64_t oid;
2122 uint64_t offset;
2123 BDRVSheepdogState *s = acb->s;
2124 SheepdogInode *inode = &s->inode;
2125 AIOReq *aio_req;
2127 if (acb->aiocb_type == AIOCB_WRITE_UDATA && s->is_snapshot) {
2129 * In the case we open the snapshot VDI, Sheepdog creates the
2130 * writable VDI when we do a write operation first.
2132 ret = sd_create_branch(s);
2133 if (ret) {
2134 acb->ret = -EIO;
2135 return;
2139 object_size = (UINT32_C(1) << inode->block_size_shift);
2140 idx = acb->sector_num * BDRV_SECTOR_SIZE / object_size;
2141 offset = (acb->sector_num * BDRV_SECTOR_SIZE) % object_size;
2144 * Make sure we don't free the aiocb before we are done with all requests.
2145 * This additional reference is dropped at the end of this function.
2147 acb->nr_pending++;
2149 while (done != total) {
2150 uint8_t flags = 0;
2151 uint64_t old_oid = 0;
2152 bool create = false;
2154 oid = vid_to_data_oid(inode->data_vdi_id[idx], idx);
2156 len = MIN(total - done, object_size - offset);
2158 switch (acb->aiocb_type) {
2159 case AIOCB_READ_UDATA:
2160 if (!inode->data_vdi_id[idx]) {
2161 qemu_iovec_memset(acb->qiov, done, 0, len);
2162 goto done;
2164 break;
2165 case AIOCB_WRITE_UDATA:
2166 if (!inode->data_vdi_id[idx]) {
2167 create = true;
2168 } else if (!is_data_obj_writable(inode, idx)) {
2169 /* Copy-On-Write */
2170 create = true;
2171 old_oid = oid;
2172 flags = SD_FLAG_CMD_COW;
2174 break;
2175 case AIOCB_DISCARD_OBJ:
2177 * We discard the object only when the whole object is
2178 * 1) allocated 2) trimmed. Otherwise, simply skip it.
2180 if (len != object_size || inode->data_vdi_id[idx] == 0) {
2181 goto done;
2183 break;
2184 default:
2185 break;
2188 if (create) {
2189 DPRINTF("update ino (%" PRIu32 ") %" PRIu64 " %" PRIu64 " %ld\n",
2190 inode->vdi_id, oid,
2191 vid_to_data_oid(inode->data_vdi_id[idx], idx), idx);
2192 oid = vid_to_data_oid(inode->vdi_id, idx);
2193 DPRINTF("new oid %" PRIx64 "\n", oid);
2196 aio_req = alloc_aio_req(s, acb, oid, len, offset, flags, create,
2197 old_oid,
2198 acb->aiocb_type == AIOCB_DISCARD_OBJ ?
2199 0 : done);
2200 add_aio_request(s, aio_req, acb->qiov->iov, acb->qiov->niov,
2201 acb->aiocb_type);
2202 done:
2203 offset = 0;
2204 idx++;
2205 done += len;
2207 if (--acb->nr_pending) {
2208 qemu_coroutine_yield();
2212 static void sd_aio_complete(SheepdogAIOCB *acb)
2214 if (acb->aiocb_type == AIOCB_FLUSH_CACHE) {
2215 return;
2218 QLIST_REMOVE(acb, aiocb_siblings);
2219 qemu_co_queue_restart_all(&acb->s->overlapping_queue);
2222 static coroutine_fn int sd_co_writev(BlockDriverState *bs, int64_t sector_num,
2223 int nb_sectors, QEMUIOVector *qiov)
2225 SheepdogAIOCB acb;
2226 int ret;
2227 int64_t offset = (sector_num + nb_sectors) * BDRV_SECTOR_SIZE;
2228 BDRVSheepdogState *s = bs->opaque;
2230 if (offset > s->inode.vdi_size) {
2231 ret = sd_truncate(bs, offset);
2232 if (ret < 0) {
2233 return ret;
2237 sd_aio_setup(&acb, s, qiov, sector_num, nb_sectors, AIOCB_WRITE_UDATA);
2238 sd_co_rw_vector(&acb);
2239 sd_write_done(&acb);
2240 sd_aio_complete(&acb);
2242 return acb.ret;
2245 static coroutine_fn int sd_co_readv(BlockDriverState *bs, int64_t sector_num,
2246 int nb_sectors, QEMUIOVector *qiov)
2248 SheepdogAIOCB acb;
2249 BDRVSheepdogState *s = bs->opaque;
2251 sd_aio_setup(&acb, s, qiov, sector_num, nb_sectors, AIOCB_READ_UDATA);
2252 sd_co_rw_vector(&acb);
2253 sd_aio_complete(&acb);
2255 return acb.ret;
2258 static int coroutine_fn sd_co_flush_to_disk(BlockDriverState *bs)
2260 BDRVSheepdogState *s = bs->opaque;
2261 SheepdogAIOCB acb;
2262 AIOReq *aio_req;
2264 if (s->cache_flags != SD_FLAG_CMD_CACHE) {
2265 return 0;
2268 sd_aio_setup(&acb, s, NULL, 0, 0, AIOCB_FLUSH_CACHE);
2270 acb.nr_pending++;
2271 aio_req = alloc_aio_req(s, &acb, vid_to_vdi_oid(s->inode.vdi_id),
2272 0, 0, 0, false, 0, 0);
2273 add_aio_request(s, aio_req, NULL, 0, acb.aiocb_type);
2275 if (--acb.nr_pending) {
2276 qemu_coroutine_yield();
2279 sd_aio_complete(&acb);
2280 return acb.ret;
2283 static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
2285 Error *local_err = NULL;
2286 BDRVSheepdogState *s = bs->opaque;
2287 int ret, fd;
2288 uint32_t new_vid;
2289 SheepdogInode *inode;
2290 unsigned int datalen;
2292 DPRINTF("sn_info: name %s id_str %s s: name %s vm_state_size %" PRId64 " "
2293 "is_snapshot %d\n", sn_info->name, sn_info->id_str,
2294 s->name, sn_info->vm_state_size, s->is_snapshot);
2296 if (s->is_snapshot) {
2297 error_report("You can't create a snapshot of a snapshot VDI, "
2298 "%s (%" PRIu32 ").", s->name, s->inode.vdi_id);
2300 return -EINVAL;
2303 DPRINTF("%s %s\n", sn_info->name, sn_info->id_str);
2305 s->inode.vm_state_size = sn_info->vm_state_size;
2306 s->inode.vm_clock_nsec = sn_info->vm_clock_nsec;
2307 /* It appears that inode.tag does not require a NUL terminator,
2308 * which means this use of strncpy is ok.
2310 strncpy(s->inode.tag, sn_info->name, sizeof(s->inode.tag));
2311 /* we don't need to update entire object */
2312 datalen = SD_INODE_SIZE - sizeof(s->inode.data_vdi_id);
2313 inode = g_malloc(datalen);
2315 /* refresh inode. */
2316 fd = connect_to_sdog(s, &local_err);
2317 if (fd < 0) {
2318 error_report_err(local_err);
2319 ret = fd;
2320 goto cleanup;
2323 ret = write_object(fd, s->bs, (char *)&s->inode,
2324 vid_to_vdi_oid(s->inode.vdi_id), s->inode.nr_copies,
2325 datalen, 0, false, s->cache_flags);
2326 if (ret < 0) {
2327 error_report("failed to write snapshot's inode.");
2328 goto cleanup;
2331 ret = do_sd_create(s, &new_vid, 1, &local_err);
2332 if (ret < 0) {
2333 error_reportf_err(local_err,
2334 "failed to create inode for snapshot: ");
2335 goto cleanup;
2338 ret = read_object(fd, s->bs, (char *)inode,
2339 vid_to_vdi_oid(new_vid), s->inode.nr_copies, datalen, 0,
2340 s->cache_flags);
2342 if (ret < 0) {
2343 error_report("failed to read new inode info. %s", strerror(errno));
2344 goto cleanup;
2347 memcpy(&s->inode, inode, datalen);
2348 DPRINTF("s->inode: name %s snap_id %x oid %x\n",
2349 s->inode.name, s->inode.snap_id, s->inode.vdi_id);
2351 cleanup:
2352 g_free(inode);
2353 closesocket(fd);
2354 return ret;
2358 * We implement rollback(loadvm) operation to the specified snapshot by
2359 * 1) switch to the snapshot
2360 * 2) rely on sd_create_branch to delete working VDI and
2361 * 3) create a new working VDI based on the specified snapshot
2363 static int sd_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
2365 BDRVSheepdogState *s = bs->opaque;
2366 BDRVSheepdogState *old_s;
2367 char tag[SD_MAX_VDI_TAG_LEN];
2368 uint32_t snapid = 0;
2369 int ret = 0;
2371 old_s = g_new(BDRVSheepdogState, 1);
2373 memcpy(old_s, s, sizeof(BDRVSheepdogState));
2375 snapid = strtoul(snapshot_id, NULL, 10);
2376 if (snapid) {
2377 tag[0] = 0;
2378 } else {
2379 pstrcpy(tag, sizeof(tag), snapshot_id);
2382 ret = reload_inode(s, snapid, tag);
2383 if (ret) {
2384 goto out;
2387 ret = sd_create_branch(s);
2388 if (ret) {
2389 goto out;
2392 g_free(old_s);
2394 return 0;
2395 out:
2396 /* recover bdrv_sd_state */
2397 memcpy(s, old_s, sizeof(BDRVSheepdogState));
2398 g_free(old_s);
2400 error_report("failed to open. recover old bdrv_sd_state.");
2402 return ret;
2405 #define NR_BATCHED_DISCARD 128
2407 static bool remove_objects(BDRVSheepdogState *s)
2409 int fd, i = 0, nr_objs = 0;
2410 Error *local_err = NULL;
2411 int ret = 0;
2412 bool result = true;
2413 SheepdogInode *inode = &s->inode;
2415 fd = connect_to_sdog(s, &local_err);
2416 if (fd < 0) {
2417 error_report_err(local_err);
2418 return false;
2421 nr_objs = count_data_objs(inode);
2422 while (i < nr_objs) {
2423 int start_idx, nr_filled_idx;
2425 while (i < nr_objs && !inode->data_vdi_id[i]) {
2426 i++;
2428 start_idx = i;
2430 nr_filled_idx = 0;
2431 while (i < nr_objs && nr_filled_idx < NR_BATCHED_DISCARD) {
2432 if (inode->data_vdi_id[i]) {
2433 inode->data_vdi_id[i] = 0;
2434 nr_filled_idx++;
2437 i++;
2440 ret = write_object(fd, s->bs,
2441 (char *)&inode->data_vdi_id[start_idx],
2442 vid_to_vdi_oid(s->inode.vdi_id), inode->nr_copies,
2443 (i - start_idx) * sizeof(uint32_t),
2444 offsetof(struct SheepdogInode,
2445 data_vdi_id[start_idx]),
2446 false, s->cache_flags);
2447 if (ret < 0) {
2448 error_report("failed to discard snapshot inode.");
2449 result = false;
2450 goto out;
2454 out:
2455 closesocket(fd);
2456 return result;
2459 static int sd_snapshot_delete(BlockDriverState *bs,
2460 const char *snapshot_id,
2461 const char *name,
2462 Error **errp)
2464 unsigned long snap_id = 0;
2465 char snap_tag[SD_MAX_VDI_TAG_LEN];
2466 Error *local_err = NULL;
2467 int fd, ret;
2468 char buf[SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN];
2469 BDRVSheepdogState *s = bs->opaque;
2470 unsigned int wlen = SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN, rlen = 0;
2471 uint32_t vid;
2472 SheepdogVdiReq hdr = {
2473 .opcode = SD_OP_DEL_VDI,
2474 .data_length = wlen,
2475 .flags = SD_FLAG_CMD_WRITE,
2477 SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr;
2479 if (!remove_objects(s)) {
2480 return -1;
2483 memset(buf, 0, sizeof(buf));
2484 memset(snap_tag, 0, sizeof(snap_tag));
2485 pstrcpy(buf, SD_MAX_VDI_LEN, s->name);
2486 ret = qemu_strtoul(snapshot_id, NULL, 10, &snap_id);
2487 if (ret || snap_id > UINT32_MAX) {
2488 error_setg(errp, "Invalid snapshot ID: %s",
2489 snapshot_id ? snapshot_id : "<null>");
2490 return -EINVAL;
2493 if (snap_id) {
2494 hdr.snapid = (uint32_t) snap_id;
2495 } else {
2496 pstrcpy(snap_tag, sizeof(snap_tag), snapshot_id);
2497 pstrcpy(buf + SD_MAX_VDI_LEN, SD_MAX_VDI_TAG_LEN, snap_tag);
2500 ret = find_vdi_name(s, s->name, snap_id, snap_tag, &vid, true,
2501 &local_err);
2502 if (ret) {
2503 return ret;
2506 fd = connect_to_sdog(s, &local_err);
2507 if (fd < 0) {
2508 error_report_err(local_err);
2509 return -1;
2512 ret = do_req(fd, s->bs, (SheepdogReq *)&hdr,
2513 buf, &wlen, &rlen);
2514 closesocket(fd);
2515 if (ret) {
2516 return ret;
2519 switch (rsp->result) {
2520 case SD_RES_NO_VDI:
2521 error_report("%s was already deleted", s->name);
2522 case SD_RES_SUCCESS:
2523 break;
2524 default:
2525 error_report("%s, %s", sd_strerror(rsp->result), s->name);
2526 return -1;
2529 return ret;
2532 static int sd_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
2534 Error *local_err = NULL;
2535 BDRVSheepdogState *s = bs->opaque;
2536 SheepdogReq req;
2537 int fd, nr = 1024, ret, max = BITS_TO_LONGS(SD_NR_VDIS) * sizeof(long);
2538 QEMUSnapshotInfo *sn_tab = NULL;
2539 unsigned wlen, rlen;
2540 int found = 0;
2541 static SheepdogInode inode;
2542 unsigned long *vdi_inuse;
2543 unsigned int start_nr;
2544 uint64_t hval;
2545 uint32_t vid;
2547 vdi_inuse = g_malloc(max);
2549 fd = connect_to_sdog(s, &local_err);
2550 if (fd < 0) {
2551 error_report_err(local_err);
2552 ret = fd;
2553 goto out;
2556 rlen = max;
2557 wlen = 0;
2559 memset(&req, 0, sizeof(req));
2561 req.opcode = SD_OP_READ_VDIS;
2562 req.data_length = max;
2564 ret = do_req(fd, s->bs, &req, vdi_inuse, &wlen, &rlen);
2566 closesocket(fd);
2567 if (ret) {
2568 goto out;
2571 sn_tab = g_new0(QEMUSnapshotInfo, nr);
2573 /* calculate a vdi id with hash function */
2574 hval = fnv_64a_buf(s->name, strlen(s->name), FNV1A_64_INIT);
2575 start_nr = hval & (SD_NR_VDIS - 1);
2577 fd = connect_to_sdog(s, &local_err);
2578 if (fd < 0) {
2579 error_report_err(local_err);
2580 ret = fd;
2581 goto out;
2584 for (vid = start_nr; found < nr; vid = (vid + 1) % SD_NR_VDIS) {
2585 if (!test_bit(vid, vdi_inuse)) {
2586 break;
2589 /* we don't need to read entire object */
2590 ret = read_object(fd, s->bs, (char *)&inode,
2591 vid_to_vdi_oid(vid),
2592 0, SD_INODE_SIZE - sizeof(inode.data_vdi_id), 0,
2593 s->cache_flags);
2595 if (ret) {
2596 continue;
2599 if (!strcmp(inode.name, s->name) && is_snapshot(&inode)) {
2600 sn_tab[found].date_sec = inode.snap_ctime >> 32;
2601 sn_tab[found].date_nsec = inode.snap_ctime & 0xffffffff;
2602 sn_tab[found].vm_state_size = inode.vm_state_size;
2603 sn_tab[found].vm_clock_nsec = inode.vm_clock_nsec;
2605 snprintf(sn_tab[found].id_str, sizeof(sn_tab[found].id_str),
2606 "%" PRIu32, inode.snap_id);
2607 pstrcpy(sn_tab[found].name,
2608 MIN(sizeof(sn_tab[found].name), sizeof(inode.tag)),
2609 inode.tag);
2610 found++;
2614 closesocket(fd);
2615 out:
2616 *psn_tab = sn_tab;
2618 g_free(vdi_inuse);
2620 if (ret < 0) {
2621 return ret;
2624 return found;
2627 static int do_load_save_vmstate(BDRVSheepdogState *s, uint8_t *data,
2628 int64_t pos, int size, int load)
2630 Error *local_err = NULL;
2631 bool create;
2632 int fd, ret = 0, remaining = size;
2633 unsigned int data_len;
2634 uint64_t vmstate_oid;
2635 uint64_t offset;
2636 uint32_t vdi_index;
2637 uint32_t vdi_id = load ? s->inode.parent_vdi_id : s->inode.vdi_id;
2638 uint32_t object_size = (UINT32_C(1) << s->inode.block_size_shift);
2640 fd = connect_to_sdog(s, &local_err);
2641 if (fd < 0) {
2642 error_report_err(local_err);
2643 return fd;
2646 while (remaining) {
2647 vdi_index = pos / object_size;
2648 offset = pos % object_size;
2650 data_len = MIN(remaining, object_size - offset);
2652 vmstate_oid = vid_to_vmstate_oid(vdi_id, vdi_index);
2654 create = (offset == 0);
2655 if (load) {
2656 ret = read_object(fd, s->bs, (char *)data, vmstate_oid,
2657 s->inode.nr_copies, data_len, offset,
2658 s->cache_flags);
2659 } else {
2660 ret = write_object(fd, s->bs, (char *)data, vmstate_oid,
2661 s->inode.nr_copies, data_len, offset, create,
2662 s->cache_flags);
2665 if (ret < 0) {
2666 error_report("failed to save vmstate %s", strerror(errno));
2667 goto cleanup;
2670 pos += data_len;
2671 data += data_len;
2672 remaining -= data_len;
2674 ret = size;
2675 cleanup:
2676 closesocket(fd);
2677 return ret;
2680 static int sd_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
2681 int64_t pos)
2683 BDRVSheepdogState *s = bs->opaque;
2684 void *buf;
2685 int ret;
2687 buf = qemu_blockalign(bs, qiov->size);
2688 qemu_iovec_to_buf(qiov, 0, buf, qiov->size);
2689 ret = do_load_save_vmstate(s, (uint8_t *) buf, pos, qiov->size, 0);
2690 qemu_vfree(buf);
2692 return ret;
2695 static int sd_load_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
2696 int64_t pos)
2698 BDRVSheepdogState *s = bs->opaque;
2699 void *buf;
2700 int ret;
2702 buf = qemu_blockalign(bs, qiov->size);
2703 ret = do_load_save_vmstate(s, buf, pos, qiov->size, 1);
2704 qemu_iovec_from_buf(qiov, 0, buf, qiov->size);
2705 qemu_vfree(buf);
2707 return ret;
2711 static coroutine_fn int sd_co_pdiscard(BlockDriverState *bs, int64_t offset,
2712 int count)
2714 SheepdogAIOCB acb;
2715 BDRVSheepdogState *s = bs->opaque;
2716 QEMUIOVector discard_iov;
2717 struct iovec iov;
2718 uint32_t zero = 0;
2720 if (!s->discard_supported) {
2721 return 0;
2724 memset(&discard_iov, 0, sizeof(discard_iov));
2725 memset(&iov, 0, sizeof(iov));
2726 iov.iov_base = &zero;
2727 iov.iov_len = sizeof(zero);
2728 discard_iov.iov = &iov;
2729 discard_iov.niov = 1;
2730 if (!QEMU_IS_ALIGNED(offset | count, BDRV_SECTOR_SIZE)) {
2731 return -ENOTSUP;
2733 sd_aio_setup(&acb, s, &discard_iov, offset >> BDRV_SECTOR_BITS,
2734 count >> BDRV_SECTOR_BITS, AIOCB_DISCARD_OBJ);
2735 sd_co_rw_vector(&acb);
2736 sd_aio_complete(&acb);
2738 return acb.ret;
2741 static coroutine_fn int64_t
2742 sd_co_get_block_status(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
2743 int *pnum, BlockDriverState **file)
2745 BDRVSheepdogState *s = bs->opaque;
2746 SheepdogInode *inode = &s->inode;
2747 uint32_t object_size = (UINT32_C(1) << inode->block_size_shift);
2748 uint64_t offset = sector_num * BDRV_SECTOR_SIZE;
2749 unsigned long start = offset / object_size,
2750 end = DIV_ROUND_UP((sector_num + nb_sectors) *
2751 BDRV_SECTOR_SIZE, object_size);
2752 unsigned long idx;
2753 int64_t ret = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | offset;
2755 for (idx = start; idx < end; idx++) {
2756 if (inode->data_vdi_id[idx] == 0) {
2757 break;
2760 if (idx == start) {
2761 /* Get the longest length of unallocated sectors */
2762 ret = 0;
2763 for (idx = start + 1; idx < end; idx++) {
2764 if (inode->data_vdi_id[idx] != 0) {
2765 break;
2770 *pnum = (idx - start) * object_size / BDRV_SECTOR_SIZE;
2771 if (*pnum > nb_sectors) {
2772 *pnum = nb_sectors;
2774 if (ret > 0 && ret & BDRV_BLOCK_OFFSET_VALID) {
2775 *file = bs;
2777 return ret;
2780 static int64_t sd_get_allocated_file_size(BlockDriverState *bs)
2782 BDRVSheepdogState *s = bs->opaque;
2783 SheepdogInode *inode = &s->inode;
2784 uint32_t object_size = (UINT32_C(1) << inode->block_size_shift);
2785 unsigned long i, last = DIV_ROUND_UP(inode->vdi_size, object_size);
2786 uint64_t size = 0;
2788 for (i = 0; i < last; i++) {
2789 if (inode->data_vdi_id[i] == 0) {
2790 continue;
2792 size += object_size;
2794 return size;
2797 static QemuOptsList sd_create_opts = {
2798 .name = "sheepdog-create-opts",
2799 .head = QTAILQ_HEAD_INITIALIZER(sd_create_opts.head),
2800 .desc = {
2802 .name = BLOCK_OPT_SIZE,
2803 .type = QEMU_OPT_SIZE,
2804 .help = "Virtual disk size"
2807 .name = BLOCK_OPT_BACKING_FILE,
2808 .type = QEMU_OPT_STRING,
2809 .help = "File name of a base image"
2812 .name = BLOCK_OPT_PREALLOC,
2813 .type = QEMU_OPT_STRING,
2814 .help = "Preallocation mode (allowed values: off, full)"
2817 .name = BLOCK_OPT_REDUNDANCY,
2818 .type = QEMU_OPT_STRING,
2819 .help = "Redundancy of the image"
2822 .name = BLOCK_OPT_OBJECT_SIZE,
2823 .type = QEMU_OPT_SIZE,
2824 .help = "Object size of the image"
2826 { /* end of list */ }
2830 static BlockDriver bdrv_sheepdog = {
2831 .format_name = "sheepdog",
2832 .protocol_name = "sheepdog",
2833 .instance_size = sizeof(BDRVSheepdogState),
2834 .bdrv_needs_filename = true,
2835 .bdrv_file_open = sd_open,
2836 .bdrv_reopen_prepare = sd_reopen_prepare,
2837 .bdrv_reopen_commit = sd_reopen_commit,
2838 .bdrv_reopen_abort = sd_reopen_abort,
2839 .bdrv_close = sd_close,
2840 .bdrv_create = sd_create,
2841 .bdrv_has_zero_init = bdrv_has_zero_init_1,
2842 .bdrv_getlength = sd_getlength,
2843 .bdrv_get_allocated_file_size = sd_get_allocated_file_size,
2844 .bdrv_truncate = sd_truncate,
2846 .bdrv_co_readv = sd_co_readv,
2847 .bdrv_co_writev = sd_co_writev,
2848 .bdrv_co_flush_to_disk = sd_co_flush_to_disk,
2849 .bdrv_co_pdiscard = sd_co_pdiscard,
2850 .bdrv_co_get_block_status = sd_co_get_block_status,
2852 .bdrv_snapshot_create = sd_snapshot_create,
2853 .bdrv_snapshot_goto = sd_snapshot_goto,
2854 .bdrv_snapshot_delete = sd_snapshot_delete,
2855 .bdrv_snapshot_list = sd_snapshot_list,
2857 .bdrv_save_vmstate = sd_save_vmstate,
2858 .bdrv_load_vmstate = sd_load_vmstate,
2860 .bdrv_detach_aio_context = sd_detach_aio_context,
2861 .bdrv_attach_aio_context = sd_attach_aio_context,
2863 .create_opts = &sd_create_opts,
2866 static BlockDriver bdrv_sheepdog_tcp = {
2867 .format_name = "sheepdog",
2868 .protocol_name = "sheepdog+tcp",
2869 .instance_size = sizeof(BDRVSheepdogState),
2870 .bdrv_needs_filename = true,
2871 .bdrv_file_open = sd_open,
2872 .bdrv_reopen_prepare = sd_reopen_prepare,
2873 .bdrv_reopen_commit = sd_reopen_commit,
2874 .bdrv_reopen_abort = sd_reopen_abort,
2875 .bdrv_close = sd_close,
2876 .bdrv_create = sd_create,
2877 .bdrv_has_zero_init = bdrv_has_zero_init_1,
2878 .bdrv_getlength = sd_getlength,
2879 .bdrv_get_allocated_file_size = sd_get_allocated_file_size,
2880 .bdrv_truncate = sd_truncate,
2882 .bdrv_co_readv = sd_co_readv,
2883 .bdrv_co_writev = sd_co_writev,
2884 .bdrv_co_flush_to_disk = sd_co_flush_to_disk,
2885 .bdrv_co_pdiscard = sd_co_pdiscard,
2886 .bdrv_co_get_block_status = sd_co_get_block_status,
2888 .bdrv_snapshot_create = sd_snapshot_create,
2889 .bdrv_snapshot_goto = sd_snapshot_goto,
2890 .bdrv_snapshot_delete = sd_snapshot_delete,
2891 .bdrv_snapshot_list = sd_snapshot_list,
2893 .bdrv_save_vmstate = sd_save_vmstate,
2894 .bdrv_load_vmstate = sd_load_vmstate,
2896 .bdrv_detach_aio_context = sd_detach_aio_context,
2897 .bdrv_attach_aio_context = sd_attach_aio_context,
2899 .create_opts = &sd_create_opts,
2902 static BlockDriver bdrv_sheepdog_unix = {
2903 .format_name = "sheepdog",
2904 .protocol_name = "sheepdog+unix",
2905 .instance_size = sizeof(BDRVSheepdogState),
2906 .bdrv_needs_filename = true,
2907 .bdrv_file_open = sd_open,
2908 .bdrv_reopen_prepare = sd_reopen_prepare,
2909 .bdrv_reopen_commit = sd_reopen_commit,
2910 .bdrv_reopen_abort = sd_reopen_abort,
2911 .bdrv_close = sd_close,
2912 .bdrv_create = sd_create,
2913 .bdrv_has_zero_init = bdrv_has_zero_init_1,
2914 .bdrv_getlength = sd_getlength,
2915 .bdrv_get_allocated_file_size = sd_get_allocated_file_size,
2916 .bdrv_truncate = sd_truncate,
2918 .bdrv_co_readv = sd_co_readv,
2919 .bdrv_co_writev = sd_co_writev,
2920 .bdrv_co_flush_to_disk = sd_co_flush_to_disk,
2921 .bdrv_co_pdiscard = sd_co_pdiscard,
2922 .bdrv_co_get_block_status = sd_co_get_block_status,
2924 .bdrv_snapshot_create = sd_snapshot_create,
2925 .bdrv_snapshot_goto = sd_snapshot_goto,
2926 .bdrv_snapshot_delete = sd_snapshot_delete,
2927 .bdrv_snapshot_list = sd_snapshot_list,
2929 .bdrv_save_vmstate = sd_save_vmstate,
2930 .bdrv_load_vmstate = sd_load_vmstate,
2932 .bdrv_detach_aio_context = sd_detach_aio_context,
2933 .bdrv_attach_aio_context = sd_attach_aio_context,
2935 .create_opts = &sd_create_opts,
2938 static void bdrv_sheepdog_init(void)
2940 bdrv_register(&bdrv_sheepdog);
2941 bdrv_register(&bdrv_sheepdog_tcp);
2942 bdrv_register(&bdrv_sheepdog_unix);
2944 block_init(bdrv_sheepdog_init);