]> git.proxmox.com Git - mirror_qemu.git/blame - block/sheepdog.c
coroutine: add co_aio_sleep_ns() to allow sleep in block drivers
[mirror_qemu.git] / block / sheepdog.c
CommitLineData
33b1db1c
MK
1/*
2 * Copyright (C) 2009-2010 Nippon Telegraph and Telephone Corporation.
3 *
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.
7 *
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/>.
6b620ca3
PB
10 *
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.
33b1db1c 13 */
33b1db1c
MK
14
15#include "qemu-common.h"
5d6768e3 16#include "qemu/uri.h"
1de7afc9
PB
17#include "qemu/error-report.h"
18#include "qemu/sockets.h"
737e150e 19#include "block/block_int.h"
1de7afc9 20#include "qemu/bitops.h"
33b1db1c
MK
21
22#define SD_PROTO_VER 0x01
23
24#define SD_DEFAULT_ADDR "localhost"
25af257d 25#define SD_DEFAULT_PORT 7000
33b1db1c
MK
26
27#define SD_OP_CREATE_AND_WRITE_OBJ 0x01
28#define SD_OP_READ_OBJ 0x02
29#define SD_OP_WRITE_OBJ 0x03
cac8f4a6
LY
30/* 0x04 is used internally by Sheepdog */
31#define SD_OP_DISCARD_OBJ 0x05
33b1db1c
MK
32
33#define SD_OP_NEW_VDI 0x11
34#define SD_OP_LOCK_VDI 0x12
35#define SD_OP_RELEASE_VDI 0x13
36#define SD_OP_GET_VDI_INFO 0x14
37#define SD_OP_READ_VDIS 0x15
47622c44 38#define SD_OP_FLUSH_VDI 0x16
859e5553 39#define SD_OP_DEL_VDI 0x17
33b1db1c
MK
40
41#define SD_FLAG_CMD_WRITE 0x01
42#define SD_FLAG_CMD_COW 0x02
0e7106d8
LY
43#define SD_FLAG_CMD_CACHE 0x04 /* Writeback mode for cache */
44#define SD_FLAG_CMD_DIRECT 0x08 /* Don't use cache */
33b1db1c
MK
45
46#define SD_RES_SUCCESS 0x00 /* Success */
47#define SD_RES_UNKNOWN 0x01 /* Unknown error */
48#define SD_RES_NO_OBJ 0x02 /* No object found */
49#define SD_RES_EIO 0x03 /* I/O error */
50#define SD_RES_VDI_EXIST 0x04 /* Vdi exists already */
51#define SD_RES_INVALID_PARMS 0x05 /* Invalid parameters */
52#define SD_RES_SYSTEM_ERROR 0x06 /* System error */
53#define SD_RES_VDI_LOCKED 0x07 /* Vdi is locked */
54#define SD_RES_NO_VDI 0x08 /* No vdi found */
55#define SD_RES_NO_BASE_VDI 0x09 /* No base vdi found */
56#define SD_RES_VDI_READ 0x0A /* Cannot read requested vdi */
57#define SD_RES_VDI_WRITE 0x0B /* Cannot write requested vdi */
58#define SD_RES_BASE_VDI_READ 0x0C /* Cannot read base vdi */
59#define SD_RES_BASE_VDI_WRITE 0x0D /* Cannot write base vdi */
60#define SD_RES_NO_TAG 0x0E /* Requested tag is not found */
61#define SD_RES_STARTUP 0x0F /* Sheepdog is on starting up */
62#define SD_RES_VDI_NOT_LOCKED 0x10 /* Vdi is not locked */
63#define SD_RES_SHUTDOWN 0x11 /* Sheepdog is shutting down */
64#define SD_RES_NO_MEM 0x12 /* Cannot allocate memory */
65#define SD_RES_FULL_VDI 0x13 /* we already have the maximum vdis */
66#define SD_RES_VER_MISMATCH 0x14 /* Protocol version mismatch */
67#define SD_RES_NO_SPACE 0x15 /* Server has no room for new objects */
68#define SD_RES_WAIT_FOR_FORMAT 0x16 /* Waiting for a format operation */
69#define SD_RES_WAIT_FOR_JOIN 0x17 /* Waiting for other nodes joining */
70#define SD_RES_JOIN_FAILED 0x18 /* Target node had failed to join sheepdog */
fca23f0a 71#define SD_RES_HALT 0x19 /* Sheepdog is stopped serving IO request */
6a0b5490 72#define SD_RES_READONLY 0x1A /* Object is read-only */
33b1db1c
MK
73
74/*
75 * Object ID rules
76 *
77 * 0 - 19 (20 bits): data object space
78 * 20 - 31 (12 bits): reserved data object space
79 * 32 - 55 (24 bits): vdi object space
80 * 56 - 59 ( 4 bits): reserved vdi object space
7acae208 81 * 60 - 63 ( 4 bits): object type identifier space
33b1db1c
MK
82 */
83
84#define VDI_SPACE_SHIFT 32
85#define VDI_BIT (UINT64_C(1) << 63)
86#define VMSTATE_BIT (UINT64_C(1) << 62)
87#define MAX_DATA_OBJS (UINT64_C(1) << 20)
88#define MAX_CHILDREN 1024
89#define SD_MAX_VDI_LEN 256
90#define SD_MAX_VDI_TAG_LEN 256
91#define SD_NR_VDIS (1U << 24)
92#define SD_DATA_OBJ_SIZE (UINT64_C(1) << 22)
93#define SD_MAX_VDI_SIZE (SD_DATA_OBJ_SIZE * MAX_DATA_OBJS)
33b1db1c
MK
94
95#define SD_INODE_SIZE (sizeof(SheepdogInode))
96#define CURRENT_VDI_ID 0
97
98typedef struct SheepdogReq {
99 uint8_t proto_ver;
100 uint8_t opcode;
101 uint16_t flags;
102 uint32_t epoch;
103 uint32_t id;
104 uint32_t data_length;
105 uint32_t opcode_specific[8];
106} SheepdogReq;
107
108typedef struct SheepdogRsp {
109 uint8_t proto_ver;
110 uint8_t opcode;
111 uint16_t flags;
112 uint32_t epoch;
113 uint32_t id;
114 uint32_t data_length;
115 uint32_t result;
116 uint32_t opcode_specific[7];
117} SheepdogRsp;
118
119typedef struct SheepdogObjReq {
120 uint8_t proto_ver;
121 uint8_t opcode;
122 uint16_t flags;
123 uint32_t epoch;
124 uint32_t id;
125 uint32_t data_length;
126 uint64_t oid;
127 uint64_t cow_oid;
29a67f7e 128 uint8_t copies;
1841f880
LY
129 uint8_t copy_policy;
130 uint8_t reserved[6];
33b1db1c
MK
131 uint64_t offset;
132} SheepdogObjReq;
133
134typedef struct SheepdogObjRsp {
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 uint32_t result;
29a67f7e 142 uint8_t copies;
1841f880
LY
143 uint8_t copy_policy;
144 uint8_t reserved[2];
33b1db1c
MK
145 uint32_t pad[6];
146} SheepdogObjRsp;
147
148typedef struct SheepdogVdiReq {
149 uint8_t proto_ver;
150 uint8_t opcode;
151 uint16_t flags;
152 uint32_t epoch;
153 uint32_t id;
154 uint32_t data_length;
155 uint64_t vdi_size;
6f74c260 156 uint32_t vdi_id;
29a67f7e 157 uint8_t copies;
1841f880
LY
158 uint8_t copy_policy;
159 uint8_t reserved[2];
33b1db1c
MK
160 uint32_t snapid;
161 uint32_t pad[3];
162} SheepdogVdiReq;
163
164typedef struct SheepdogVdiRsp {
165 uint8_t proto_ver;
166 uint8_t opcode;
167 uint16_t flags;
168 uint32_t epoch;
169 uint32_t id;
170 uint32_t data_length;
171 uint32_t result;
172 uint32_t rsvd;
173 uint32_t vdi_id;
174 uint32_t pad[5];
175} SheepdogVdiRsp;
176
177typedef struct SheepdogInode {
178 char name[SD_MAX_VDI_LEN];
179 char tag[SD_MAX_VDI_TAG_LEN];
180 uint64_t ctime;
181 uint64_t snap_ctime;
182 uint64_t vm_clock_nsec;
183 uint64_t vdi_size;
184 uint64_t vm_state_size;
185 uint16_t copy_policy;
186 uint8_t nr_copies;
187 uint8_t block_size_shift;
188 uint32_t snap_id;
189 uint32_t vdi_id;
190 uint32_t parent_vdi_id;
191 uint32_t child_vdi_id[MAX_CHILDREN];
192 uint32_t data_vdi_id[MAX_DATA_OBJS];
193} SheepdogInode;
194
195/*
196 * 64 bit FNV-1a non-zero initial basis
197 */
198#define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
199
200/*
201 * 64 bit Fowler/Noll/Vo FNV-1a hash code
202 */
203static inline uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval)
204{
205 unsigned char *bp = buf;
206 unsigned char *be = bp + len;
207 while (bp < be) {
208 hval ^= (uint64_t) *bp++;
209 hval += (hval << 1) + (hval << 4) + (hval << 5) +
210 (hval << 7) + (hval << 8) + (hval << 40);
211 }
212 return hval;
213}
214
2f536801 215static inline bool is_data_obj_writable(SheepdogInode *inode, unsigned int idx)
33b1db1c
MK
216{
217 return inode->vdi_id == inode->data_vdi_id[idx];
218}
219
2f536801 220static inline bool is_data_obj(uint64_t oid)
33b1db1c
MK
221{
222 return !(VDI_BIT & oid);
223}
224
225static inline uint64_t data_oid_to_idx(uint64_t oid)
226{
227 return oid & (MAX_DATA_OBJS - 1);
228}
229
72e0996c
MK
230static inline uint32_t oid_to_vid(uint64_t oid)
231{
232 return (oid & ~VDI_BIT) >> VDI_SPACE_SHIFT;
233}
234
33b1db1c
MK
235static inline uint64_t vid_to_vdi_oid(uint32_t vid)
236{
237 return VDI_BIT | ((uint64_t)vid << VDI_SPACE_SHIFT);
238}
239
240static inline uint64_t vid_to_vmstate_oid(uint32_t vid, uint32_t idx)
241{
242 return VMSTATE_BIT | ((uint64_t)vid << VDI_SPACE_SHIFT) | idx;
243}
244
245static inline uint64_t vid_to_data_oid(uint32_t vid, uint32_t idx)
246{
247 return ((uint64_t)vid << VDI_SPACE_SHIFT) | idx;
248}
249
2f536801 250static inline bool is_snapshot(struct SheepdogInode *inode)
33b1db1c
MK
251{
252 return !!inode->snap_ctime;
253}
254
2440a2c3 255#undef DPRINTF
33b1db1c 256#ifdef DEBUG_SDOG
2440a2c3 257#define DPRINTF(fmt, args...) \
33b1db1c
MK
258 do { \
259 fprintf(stdout, "%s %d: " fmt, __func__, __LINE__, ##args); \
260 } while (0)
261#else
2440a2c3 262#define DPRINTF(fmt, args...)
33b1db1c
MK
263#endif
264
265typedef struct SheepdogAIOCB SheepdogAIOCB;
266
267typedef struct AIOReq {
268 SheepdogAIOCB *aiocb;
269 unsigned int iov_offset;
270
271 uint64_t oid;
272 uint64_t base_oid;
273 uint64_t offset;
274 unsigned int data_len;
275 uint8_t flags;
276 uint32_t id;
277
c292ee6a 278 QLIST_ENTRY(AIOReq) aio_siblings;
33b1db1c
MK
279} AIOReq;
280
281enum AIOCBState {
282 AIOCB_WRITE_UDATA,
283 AIOCB_READ_UDATA,
47783072 284 AIOCB_FLUSH_CACHE,
cac8f4a6 285 AIOCB_DISCARD_OBJ,
33b1db1c
MK
286};
287
288struct SheepdogAIOCB {
289 BlockDriverAIOCB common;
290
291 QEMUIOVector *qiov;
292
293 int64_t sector_num;
294 int nb_sectors;
295
296 int ret;
297 enum AIOCBState aiocb_type;
298
2df46246 299 Coroutine *coroutine;
33b1db1c
MK
300 void (*aio_done_func)(SheepdogAIOCB *);
301
2f536801 302 bool canceled;
1d732d7d 303 int nr_pending;
33b1db1c
MK
304};
305
306typedef struct BDRVSheepdogState {
307 SheepdogInode inode;
308
309 uint32_t min_dirty_data_idx;
310 uint32_t max_dirty_data_idx;
311
312 char name[SD_MAX_VDI_LEN];
2f536801 313 bool is_snapshot;
0e7106d8 314 uint32_t cache_flags;
cac8f4a6 315 bool discard_supported;
33b1db1c 316
25af257d 317 char *host_spec;
1b8bbb46 318 bool is_unix;
33b1db1c
MK
319 int fd;
320
2df46246
MK
321 CoMutex lock;
322 Coroutine *co_send;
323 Coroutine *co_recv;
324
33b1db1c 325 uint32_t aioreq_seq_num;
c292ee6a
MK
326 QLIST_HEAD(inflight_aio_head, AIOReq) inflight_aio_head;
327 QLIST_HEAD(pending_aio_head, AIOReq) pending_aio_head;
33b1db1c
MK
328} BDRVSheepdogState;
329
330static const char * sd_strerror(int err)
331{
332 int i;
333
334 static const struct {
335 int err;
336 const char *desc;
337 } errors[] = {
338 {SD_RES_SUCCESS, "Success"},
339 {SD_RES_UNKNOWN, "Unknown error"},
340 {SD_RES_NO_OBJ, "No object found"},
341 {SD_RES_EIO, "I/O error"},
342 {SD_RES_VDI_EXIST, "VDI exists already"},
343 {SD_RES_INVALID_PARMS, "Invalid parameters"},
344 {SD_RES_SYSTEM_ERROR, "System error"},
345 {SD_RES_VDI_LOCKED, "VDI is already locked"},
346 {SD_RES_NO_VDI, "No vdi found"},
347 {SD_RES_NO_BASE_VDI, "No base VDI found"},
348 {SD_RES_VDI_READ, "Failed read the requested VDI"},
349 {SD_RES_VDI_WRITE, "Failed to write the requested VDI"},
350 {SD_RES_BASE_VDI_READ, "Failed to read the base VDI"},
351 {SD_RES_BASE_VDI_WRITE, "Failed to write the base VDI"},
352 {SD_RES_NO_TAG, "Failed to find the requested tag"},
353 {SD_RES_STARTUP, "The system is still booting"},
354 {SD_RES_VDI_NOT_LOCKED, "VDI isn't locked"},
355 {SD_RES_SHUTDOWN, "The system is shutting down"},
356 {SD_RES_NO_MEM, "Out of memory on the server"},
357 {SD_RES_FULL_VDI, "We already have the maximum vdis"},
358 {SD_RES_VER_MISMATCH, "Protocol version mismatch"},
359 {SD_RES_NO_SPACE, "Server has no space for new objects"},
360 {SD_RES_WAIT_FOR_FORMAT, "Sheepdog is waiting for a format operation"},
361 {SD_RES_WAIT_FOR_JOIN, "Sheepdog is waiting for other nodes joining"},
362 {SD_RES_JOIN_FAILED, "Target node had failed to join sheepdog"},
fca23f0a 363 {SD_RES_HALT, "Sheepdog is stopped serving IO request"},
6a0b5490 364 {SD_RES_READONLY, "Object is read-only"},
33b1db1c
MK
365 };
366
367 for (i = 0; i < ARRAY_SIZE(errors); ++i) {
368 if (errors[i].err == err) {
369 return errors[i].desc;
370 }
371 }
372
373 return "Invalid error code";
374}
375
376/*
377 * Sheepdog I/O handling:
378 *
2df46246 379 * 1. In sd_co_rw_vector, we send the I/O requests to the server and
c292ee6a 380 * link the requests to the inflight_list in the
2df46246
MK
381 * BDRVSheepdogState. The function exits without waiting for
382 * receiving the response.
33b1db1c 383 *
2df46246 384 * 2. We receive the response in aio_read_response, the fd handler to
33b1db1c
MK
385 * the sheepdog connection. If metadata update is needed, we send
386 * the write request to the vdi object in sd_write_done, the write
2df46246
MK
387 * completion function. We switch back to sd_co_readv/writev after
388 * all the requests belonging to the AIOCB are finished.
33b1db1c
MK
389 */
390
391static inline AIOReq *alloc_aio_req(BDRVSheepdogState *s, SheepdogAIOCB *acb,
392 uint64_t oid, unsigned int data_len,
393 uint64_t offset, uint8_t flags,
394 uint64_t base_oid, unsigned int iov_offset)
395{
396 AIOReq *aio_req;
397
7267c094 398 aio_req = g_malloc(sizeof(*aio_req));
33b1db1c
MK
399 aio_req->aiocb = acb;
400 aio_req->iov_offset = iov_offset;
401 aio_req->oid = oid;
402 aio_req->base_oid = base_oid;
403 aio_req->offset = offset;
404 aio_req->data_len = data_len;
405 aio_req->flags = flags;
406 aio_req->id = s->aioreq_seq_num++;
407
1d732d7d 408 acb->nr_pending++;
33b1db1c
MK
409 return aio_req;
410}
411
1d732d7d 412static inline void free_aio_req(BDRVSheepdogState *s, AIOReq *aio_req)
33b1db1c
MK
413{
414 SheepdogAIOCB *acb = aio_req->aiocb;
1d732d7d 415
c292ee6a 416 QLIST_REMOVE(aio_req, aio_siblings);
7267c094 417 g_free(aio_req);
33b1db1c 418
1d732d7d 419 acb->nr_pending--;
33b1db1c
MK
420}
421
d8716b41 422static void coroutine_fn sd_finish_aiocb(SheepdogAIOCB *acb)
33b1db1c
MK
423{
424 if (!acb->canceled) {
2df46246 425 qemu_coroutine_enter(acb->coroutine, NULL);
33b1db1c
MK
426 }
427 qemu_aio_release(acb);
428}
429
430static void sd_aio_cancel(BlockDriverAIOCB *blockacb)
431{
432 SheepdogAIOCB *acb = (SheepdogAIOCB *)blockacb;
433
434 /*
435 * Sheepdog cannot cancel the requests which are already sent to
436 * the servers, so we just complete the request with -EIO here.
437 */
2df46246
MK
438 acb->ret = -EIO;
439 qemu_coroutine_enter(acb->coroutine, NULL);
2f536801 440 acb->canceled = true;
33b1db1c
MK
441}
442
d7331bed 443static const AIOCBInfo sd_aiocb_info = {
33b1db1c
MK
444 .aiocb_size = sizeof(SheepdogAIOCB),
445 .cancel = sd_aio_cancel,
446};
447
448static SheepdogAIOCB *sd_aio_setup(BlockDriverState *bs, QEMUIOVector *qiov,
f700f8e3 449 int64_t sector_num, int nb_sectors)
33b1db1c
MK
450{
451 SheepdogAIOCB *acb;
452
f700f8e3 453 acb = qemu_aio_get(&sd_aiocb_info, bs, NULL, NULL);
33b1db1c
MK
454
455 acb->qiov = qiov;
456
457 acb->sector_num = sector_num;
458 acb->nb_sectors = nb_sectors;
459
460 acb->aio_done_func = NULL;
2f536801 461 acb->canceled = false;
2df46246 462 acb->coroutine = qemu_coroutine_self();
33b1db1c 463 acb->ret = 0;
1d732d7d 464 acb->nr_pending = 0;
33b1db1c
MK
465 return acb;
466}
467
25af257d 468static int connect_to_sdog(BDRVSheepdogState *s)
33b1db1c 469{
25af257d
MK
470 int fd;
471 Error *err = NULL;
33b1db1c 472
1b8bbb46
MK
473 if (s->is_unix) {
474 fd = unix_connect(s->host_spec, &err);
475 } else {
476 fd = inet_connect(s->host_spec, &err);
477
478 if (err == NULL) {
479 int ret = socket_set_nodelay(fd);
480 if (ret < 0) {
481 error_report("%s", strerror(errno));
482 }
483 }
484 }
33b1db1c 485
25af257d
MK
486 if (err != NULL) {
487 qerror_report_err(err);
488 error_free(err);
0d6db300 489 } else {
f9e8cacc 490 qemu_set_nonblock(fd);
33b1db1c
MK
491 }
492
33b1db1c
MK
493 return fd;
494}
495
e0d93a89
MK
496static coroutine_fn int send_co_req(int sockfd, SheepdogReq *hdr, void *data,
497 unsigned int *wlen)
47622c44
LY
498{
499 int ret;
500
501 ret = qemu_co_send(sockfd, hdr, sizeof(*hdr));
80731d9d 502 if (ret != sizeof(*hdr)) {
47622c44 503 error_report("failed to send a req, %s", strerror(errno));
eb092180 504 return ret;
47622c44
LY
505 }
506
507 ret = qemu_co_send(sockfd, data, *wlen);
80731d9d 508 if (ret != *wlen) {
47622c44
LY
509 error_report("failed to send a req, %s", strerror(errno));
510 }
511
512 return ret;
513}
e0d93a89 514
2dfcca3b
MK
515static void restart_co_req(void *opaque)
516{
517 Coroutine *co = opaque;
518
519 qemu_coroutine_enter(co, NULL);
520}
521
cddd4ac7
MK
522typedef struct SheepdogReqCo {
523 int sockfd;
524 SheepdogReq *hdr;
525 void *data;
526 unsigned int *wlen;
527 unsigned int *rlen;
528 int ret;
529 bool finished;
530} SheepdogReqCo;
531
532static coroutine_fn void do_co_req(void *opaque)
47622c44
LY
533{
534 int ret;
2dfcca3b 535 Coroutine *co;
cddd4ac7
MK
536 SheepdogReqCo *srco = opaque;
537 int sockfd = srco->sockfd;
538 SheepdogReq *hdr = srco->hdr;
539 void *data = srco->data;
540 unsigned int *wlen = srco->wlen;
541 unsigned int *rlen = srco->rlen;
2dfcca3b
MK
542
543 co = qemu_coroutine_self();
f2e5dca4 544 qemu_aio_set_fd_handler(sockfd, NULL, restart_co_req, co);
47622c44 545
47622c44
LY
546 ret = send_co_req(sockfd, hdr, data, wlen);
547 if (ret < 0) {
548 goto out;
549 }
550
f2e5dca4 551 qemu_aio_set_fd_handler(sockfd, restart_co_req, NULL, co);
2dfcca3b 552
47622c44 553 ret = qemu_co_recv(sockfd, hdr, sizeof(*hdr));
80731d9d 554 if (ret != sizeof(*hdr)) {
47622c44 555 error_report("failed to get a rsp, %s", strerror(errno));
cb595887 556 ret = -errno;
47622c44
LY
557 goto out;
558 }
559
560 if (*rlen > hdr->data_length) {
561 *rlen = hdr->data_length;
562 }
563
564 if (*rlen) {
565 ret = qemu_co_recv(sockfd, data, *rlen);
80731d9d 566 if (ret != *rlen) {
47622c44 567 error_report("failed to get the data, %s", strerror(errno));
cb595887 568 ret = -errno;
47622c44
LY
569 goto out;
570 }
571 }
572 ret = 0;
573out:
ed9ba724
MK
574 /* there is at most one request for this sockfd, so it is safe to
575 * set each handler to NULL. */
f2e5dca4 576 qemu_aio_set_fd_handler(sockfd, NULL, NULL, NULL);
cddd4ac7
MK
577
578 srco->ret = ret;
579 srco->finished = true;
580}
581
582static int do_req(int sockfd, SheepdogReq *hdr, void *data,
583 unsigned int *wlen, unsigned int *rlen)
584{
585 Coroutine *co;
586 SheepdogReqCo srco = {
587 .sockfd = sockfd,
588 .hdr = hdr,
589 .data = data,
590 .wlen = wlen,
591 .rlen = rlen,
592 .ret = 0,
593 .finished = false,
594 };
595
596 if (qemu_in_coroutine()) {
597 do_co_req(&srco);
598 } else {
599 co = qemu_coroutine_create(do_co_req);
600 qemu_coroutine_enter(co, &srco);
601 while (!srco.finished) {
602 qemu_aio_wait();
603 }
604 }
605
606 return srco.ret;
47622c44
LY
607}
608
d8716b41 609static int coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req,
2f536801 610 struct iovec *iov, int niov, bool create,
33b1db1c 611 enum AIOCBState aiocb_type);
13c31de2 612static int coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req);
72e0996c 613static int reload_inode(BDRVSheepdogState *s, uint32_t snapid, const char *tag);
7dc1cde0
MK
614
615static AIOReq *find_pending_req(BDRVSheepdogState *s, uint64_t oid)
616{
617 AIOReq *aio_req;
618
619 QLIST_FOREACH(aio_req, &s->pending_aio_head, aio_siblings) {
620 if (aio_req->oid == oid) {
621 return aio_req;
622 }
623 }
624
625 return NULL;
626}
627
33b1db1c
MK
628/*
629 * This function searchs pending requests to the object `oid', and
630 * sends them.
631 */
c292ee6a 632static void coroutine_fn send_pending_req(BDRVSheepdogState *s, uint64_t oid)
33b1db1c 633{
7dc1cde0 634 AIOReq *aio_req;
33b1db1c
MK
635 SheepdogAIOCB *acb;
636 int ret;
637
7dc1cde0 638 while ((aio_req = find_pending_req(s, oid)) != NULL) {
33b1db1c 639 acb = aio_req->aiocb;
c292ee6a
MK
640 /* move aio_req from pending list to inflight one */
641 QLIST_REMOVE(aio_req, aio_siblings);
642 QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings);
33b1db1c 643 ret = add_aio_request(s, aio_req, acb->qiov->iov,
2f536801 644 acb->qiov->niov, false, acb->aiocb_type);
33b1db1c 645 if (ret < 0) {
6daf194d 646 error_report("add_aio_request is failed");
33b1db1c 647 free_aio_req(s, aio_req);
1d732d7d 648 if (!acb->nr_pending) {
33b1db1c
MK
649 sd_finish_aiocb(acb);
650 }
651 }
652 }
653}
654
655/*
656 * Receive responses of the I/O requests.
657 *
658 * This function is registered as a fd handler, and called from the
659 * main loop when s->fd is ready for reading responses.
660 */
d8716b41 661static void coroutine_fn aio_read_response(void *opaque)
33b1db1c
MK
662{
663 SheepdogObjRsp rsp;
664 BDRVSheepdogState *s = opaque;
665 int fd = s->fd;
666 int ret;
667 AIOReq *aio_req = NULL;
668 SheepdogAIOCB *acb;
cac8f4a6 669 uint64_t idx;
33b1db1c 670
c292ee6a 671 if (QLIST_EMPTY(&s->inflight_aio_head)) {
2df46246 672 goto out;
33b1db1c
MK
673 }
674
675 /* read a header */
8c5135f9 676 ret = qemu_co_recv(fd, &rsp, sizeof(rsp));
80731d9d 677 if (ret != sizeof(rsp)) {
6daf194d 678 error_report("failed to get the header, %s", strerror(errno));
2df46246 679 goto out;
33b1db1c
MK
680 }
681
c292ee6a
MK
682 /* find the right aio_req from the inflight aio list */
683 QLIST_FOREACH(aio_req, &s->inflight_aio_head, aio_siblings) {
33b1db1c
MK
684 if (aio_req->id == rsp.id) {
685 break;
686 }
687 }
688 if (!aio_req) {
6daf194d 689 error_report("cannot find aio_req %x", rsp.id);
2df46246 690 goto out;
33b1db1c
MK
691 }
692
693 acb = aio_req->aiocb;
694
695 switch (acb->aiocb_type) {
696 case AIOCB_WRITE_UDATA:
6d1acda8
MK
697 /* this coroutine context is no longer suitable for co_recv
698 * because we may send data to update vdi objects */
699 s->co_recv = NULL;
33b1db1c
MK
700 if (!is_data_obj(aio_req->oid)) {
701 break;
702 }
703 idx = data_oid_to_idx(aio_req->oid);
704
705 if (s->inode.data_vdi_id[idx] != s->inode.vdi_id) {
706 /*
707 * If the object is newly created one, we need to update
708 * the vdi object (metadata object). min_dirty_data_idx
709 * and max_dirty_data_idx are changed to include updated
710 * index between them.
711 */
bd751f22
LY
712 if (rsp.result == SD_RES_SUCCESS) {
713 s->inode.data_vdi_id[idx] = s->inode.vdi_id;
714 s->max_dirty_data_idx = MAX(idx, s->max_dirty_data_idx);
715 s->min_dirty_data_idx = MIN(idx, s->min_dirty_data_idx);
716 }
33b1db1c
MK
717 /*
718 * Some requests may be blocked because simultaneous
719 * create requests are not allowed, so we search the
720 * pending requests here.
721 */
d6b1ef89 722 send_pending_req(s, aio_req->oid);
33b1db1c
MK
723 }
724 break;
725 case AIOCB_READ_UDATA:
2fc8ae1d
MT
726 ret = qemu_co_recvv(fd, acb->qiov->iov, acb->qiov->niov,
727 aio_req->iov_offset, rsp.data_length);
80731d9d 728 if (ret != rsp.data_length) {
6daf194d 729 error_report("failed to get the data, %s", strerror(errno));
2df46246 730 goto out;
33b1db1c
MK
731 }
732 break;
47783072
LY
733 case AIOCB_FLUSH_CACHE:
734 if (rsp.result == SD_RES_INVALID_PARMS) {
2440a2c3 735 DPRINTF("disable cache since the server doesn't support it\n");
47783072
LY
736 s->cache_flags = SD_FLAG_CMD_DIRECT;
737 rsp.result = SD_RES_SUCCESS;
738 }
739 break;
cac8f4a6
LY
740 case AIOCB_DISCARD_OBJ:
741 switch (rsp.result) {
742 case SD_RES_INVALID_PARMS:
743 error_report("sheep(%s) doesn't support discard command",
744 s->host_spec);
745 rsp.result = SD_RES_SUCCESS;
746 s->discard_supported = false;
747 break;
748 case SD_RES_SUCCESS:
749 idx = data_oid_to_idx(aio_req->oid);
750 s->inode.data_vdi_id[idx] = 0;
751 break;
752 default:
753 break;
754 }
33b1db1c
MK
755 }
756
13c31de2
MK
757 switch (rsp.result) {
758 case SD_RES_SUCCESS:
759 break;
760 case SD_RES_READONLY:
72e0996c
MK
761 if (s->inode.vdi_id == oid_to_vid(aio_req->oid)) {
762 ret = reload_inode(s, 0, "");
763 if (ret < 0) {
764 goto out;
765 }
766 }
767
768 if (is_data_obj(aio_req->oid)) {
769 aio_req->oid = vid_to_data_oid(s->inode.vdi_id,
770 data_oid_to_idx(aio_req->oid));
771 } else {
772 aio_req->oid = vid_to_vdi_oid(s->inode.vdi_id);
773 }
13c31de2
MK
774 ret = resend_aioreq(s, aio_req);
775 if (ret == SD_RES_SUCCESS) {
776 goto out;
777 }
778 /* fall through */
779 default:
33b1db1c 780 acb->ret = -EIO;
6daf194d 781 error_report("%s", sd_strerror(rsp.result));
13c31de2 782 break;
33b1db1c
MK
783 }
784
1d732d7d
MK
785 free_aio_req(s, aio_req);
786 if (!acb->nr_pending) {
33b1db1c
MK
787 /*
788 * We've finished all requests which belong to the AIOCB, so
2df46246 789 * we can switch back to sd_co_readv/writev now.
33b1db1c
MK
790 */
791 acb->aio_done_func(acb);
792 }
2df46246
MK
793out:
794 s->co_recv = NULL;
795}
796
797static void co_read_response(void *opaque)
798{
799 BDRVSheepdogState *s = opaque;
800
801 if (!s->co_recv) {
802 s->co_recv = qemu_coroutine_create(aio_read_response);
803 }
804
805 qemu_coroutine_enter(s->co_recv, opaque);
806}
807
808static void co_write_request(void *opaque)
809{
810 BDRVSheepdogState *s = opaque;
811
812 qemu_coroutine_enter(s->co_send, NULL);
33b1db1c
MK
813}
814
33b1db1c
MK
815/*
816 * Return a socket discriptor to read/write objects.
817 *
818 * We cannot use this discriptor for other operations because
819 * the block driver may be on waiting response from the server.
820 */
821static int get_sheep_fd(BDRVSheepdogState *s)
822{
1b8bbb46 823 int fd;
33b1db1c 824
25af257d 825 fd = connect_to_sdog(s);
33b1db1c 826 if (fd < 0) {
cb595887 827 return fd;
33b1db1c
MK
828 }
829
f2e5dca4 830 qemu_aio_set_fd_handler(fd, co_read_response, NULL, s);
33b1db1c
MK
831 return fd;
832}
833
5d6768e3
MK
834static int sd_parse_uri(BDRVSheepdogState *s, const char *filename,
835 char *vdi, uint32_t *snapid, char *tag)
836{
837 URI *uri;
838 QueryParams *qp = NULL;
839 int ret = 0;
840
841 uri = uri_parse(filename);
842 if (!uri) {
843 return -EINVAL;
844 }
845
1b8bbb46
MK
846 /* transport */
847 if (!strcmp(uri->scheme, "sheepdog")) {
848 s->is_unix = false;
849 } else if (!strcmp(uri->scheme, "sheepdog+tcp")) {
850 s->is_unix = false;
851 } else if (!strcmp(uri->scheme, "sheepdog+unix")) {
852 s->is_unix = true;
853 } else {
854 ret = -EINVAL;
855 goto out;
856 }
857
5d6768e3
MK
858 if (uri->path == NULL || !strcmp(uri->path, "/")) {
859 ret = -EINVAL;
860 goto out;
861 }
862 pstrcpy(vdi, SD_MAX_VDI_LEN, uri->path + 1);
863
1b8bbb46
MK
864 qp = query_params_parse(uri->query);
865 if (qp->n > 1 || (s->is_unix && !qp->n) || (!s->is_unix && qp->n)) {
866 ret = -EINVAL;
867 goto out;
868 }
869
870 if (s->is_unix) {
871 /* sheepdog+unix:///vdiname?socket=path */
872 if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) {
873 ret = -EINVAL;
874 goto out;
875 }
876 s->host_spec = g_strdup(qp->p[0].value);
877 } else {
878 /* sheepdog[+tcp]://[host:port]/vdiname */
879 s->host_spec = g_strdup_printf("%s:%d", uri->server ?: SD_DEFAULT_ADDR,
880 uri->port ?: SD_DEFAULT_PORT);
881 }
5d6768e3
MK
882
883 /* snapshot tag */
884 if (uri->fragment) {
885 *snapid = strtoul(uri->fragment, NULL, 10);
886 if (*snapid == 0) {
887 pstrcpy(tag, SD_MAX_VDI_TAG_LEN, uri->fragment);
888 }
889 } else {
890 *snapid = CURRENT_VDI_ID; /* search current vdi */
891 }
892
893out:
894 if (qp) {
895 query_params_free(qp);
896 }
897 uri_free(uri);
898 return ret;
899}
900
33b1db1c 901/*
5d6768e3 902 * Parse a filename (old syntax)
33b1db1c
MK
903 *
904 * filename must be one of the following formats:
905 * 1. [vdiname]
906 * 2. [vdiname]:[snapid]
907 * 3. [vdiname]:[tag]
908 * 4. [hostname]:[port]:[vdiname]
909 * 5. [hostname]:[port]:[vdiname]:[snapid]
910 * 6. [hostname]:[port]:[vdiname]:[tag]
911 *
912 * You can boot from the snapshot images by specifying `snapid` or
913 * `tag'.
914 *
915 * You can run VMs outside the Sheepdog cluster by specifying
916 * `hostname' and `port' (experimental).
917 */
918static int parse_vdiname(BDRVSheepdogState *s, const char *filename,
919 char *vdi, uint32_t *snapid, char *tag)
920{
5d6768e3
MK
921 char *p, *q, *uri;
922 const char *host_spec, *vdi_spec;
923 int nr_sep, ret;
33b1db1c 924
5d6768e3 925 strstart(filename, "sheepdog:", (const char **)&filename);
7267c094 926 p = q = g_strdup(filename);
33b1db1c
MK
927
928 /* count the number of separators */
929 nr_sep = 0;
930 while (*p) {
931 if (*p == ':') {
932 nr_sep++;
933 }
934 p++;
935 }
936 p = q;
937
5d6768e3 938 /* use the first two tokens as host_spec. */
33b1db1c 939 if (nr_sep >= 2) {
5d6768e3 940 host_spec = p;
33b1db1c 941 p = strchr(p, ':');
5d6768e3 942 p++;
33b1db1c
MK
943 p = strchr(p, ':');
944 *p++ = '\0';
945 } else {
5d6768e3 946 host_spec = "";
33b1db1c
MK
947 }
948
5d6768e3 949 vdi_spec = p;
33b1db1c 950
5d6768e3 951 p = strchr(vdi_spec, ':');
33b1db1c 952 if (p) {
5d6768e3 953 *p++ = '#';
33b1db1c
MK
954 }
955
5d6768e3 956 uri = g_strdup_printf("sheepdog://%s/%s", host_spec, vdi_spec);
33b1db1c 957
5d6768e3
MK
958 ret = sd_parse_uri(s, uri, vdi, snapid, tag);
959
960 g_free(q);
961 g_free(uri);
962
963 return ret;
33b1db1c
MK
964}
965
982dcbf4
MK
966static int find_vdi_name(BDRVSheepdogState *s, const char *filename,
967 uint32_t snapid, const char *tag, uint32_t *vid,
968 bool lock)
33b1db1c
MK
969{
970 int ret, fd;
971 SheepdogVdiReq hdr;
972 SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr;
973 unsigned int wlen, rlen = 0;
974 char buf[SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN];
975
25af257d 976 fd = connect_to_sdog(s);
33b1db1c 977 if (fd < 0) {
cb595887 978 return fd;
33b1db1c
MK
979 }
980
3178e275
JM
981 /* This pair of strncpy calls ensures that the buffer is zero-filled,
982 * which is desirable since we'll soon be sending those bytes, and
983 * don't want the send_req to read uninitialized data.
984 */
33b1db1c
MK
985 strncpy(buf, filename, SD_MAX_VDI_LEN);
986 strncpy(buf + SD_MAX_VDI_LEN, tag, SD_MAX_VDI_TAG_LEN);
987
988 memset(&hdr, 0, sizeof(hdr));
982dcbf4 989 if (lock) {
33b1db1c 990 hdr.opcode = SD_OP_LOCK_VDI;
982dcbf4
MK
991 } else {
992 hdr.opcode = SD_OP_GET_VDI_INFO;
33b1db1c
MK
993 }
994 wlen = SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN;
995 hdr.proto_ver = SD_PROTO_VER;
996 hdr.data_length = wlen;
997 hdr.snapid = snapid;
998 hdr.flags = SD_FLAG_CMD_WRITE;
999
1000 ret = do_req(fd, (SheepdogReq *)&hdr, buf, &wlen, &rlen);
1001 if (ret) {
33b1db1c
MK
1002 goto out;
1003 }
1004
1005 if (rsp->result != SD_RES_SUCCESS) {
6daf194d 1006 error_report("cannot get vdi info, %s, %s %d %s",
33b1db1c 1007 sd_strerror(rsp->result), filename, snapid, tag);
cb595887
MK
1008 if (rsp->result == SD_RES_NO_VDI) {
1009 ret = -ENOENT;
1010 } else {
1011 ret = -EIO;
1012 }
33b1db1c
MK
1013 goto out;
1014 }
1015 *vid = rsp->vdi_id;
1016
1017 ret = 0;
1018out:
1019 closesocket(fd);
1020 return ret;
1021}
1022
d8716b41 1023static int coroutine_fn add_aio_request(BDRVSheepdogState *s, AIOReq *aio_req,
2f536801 1024 struct iovec *iov, int niov, bool create,
33b1db1c
MK
1025 enum AIOCBState aiocb_type)
1026{
1027 int nr_copies = s->inode.nr_copies;
1028 SheepdogObjReq hdr;
47783072 1029 unsigned int wlen = 0;
33b1db1c
MK
1030 int ret;
1031 uint64_t oid = aio_req->oid;
1032 unsigned int datalen = aio_req->data_len;
1033 uint64_t offset = aio_req->offset;
1034 uint8_t flags = aio_req->flags;
1035 uint64_t old_oid = aio_req->base_oid;
1036
1037 if (!nr_copies) {
6daf194d 1038 error_report("bug");
33b1db1c
MK
1039 }
1040
1041 memset(&hdr, 0, sizeof(hdr));
1042
47783072
LY
1043 switch (aiocb_type) {
1044 case AIOCB_FLUSH_CACHE:
1045 hdr.opcode = SD_OP_FLUSH_VDI;
1046 break;
1047 case AIOCB_READ_UDATA:
33b1db1c
MK
1048 hdr.opcode = SD_OP_READ_OBJ;
1049 hdr.flags = flags;
47783072
LY
1050 break;
1051 case AIOCB_WRITE_UDATA:
1052 if (create) {
1053 hdr.opcode = SD_OP_CREATE_AND_WRITE_OBJ;
1054 } else {
1055 hdr.opcode = SD_OP_WRITE_OBJ;
1056 }
33b1db1c 1057 wlen = datalen;
33b1db1c 1058 hdr.flags = SD_FLAG_CMD_WRITE | flags;
47783072 1059 break;
cac8f4a6
LY
1060 case AIOCB_DISCARD_OBJ:
1061 hdr.opcode = SD_OP_DISCARD_OBJ;
1062 break;
33b1db1c
MK
1063 }
1064
0e7106d8
LY
1065 if (s->cache_flags) {
1066 hdr.flags |= s->cache_flags;
47622c44
LY
1067 }
1068
33b1db1c
MK
1069 hdr.oid = oid;
1070 hdr.cow_oid = old_oid;
1071 hdr.copies = s->inode.nr_copies;
1072
1073 hdr.data_length = datalen;
1074 hdr.offset = offset;
1075
1076 hdr.id = aio_req->id;
1077
2df46246
MK
1078 qemu_co_mutex_lock(&s->lock);
1079 s->co_send = qemu_coroutine_self();
f2e5dca4 1080 qemu_aio_set_fd_handler(s->fd, co_read_response, co_write_request, s);
128aa589 1081 socket_set_cork(s->fd, 1);
33b1db1c
MK
1082
1083 /* send a header */
8c5135f9 1084 ret = qemu_co_send(s->fd, &hdr, sizeof(hdr));
80731d9d 1085 if (ret != sizeof(hdr)) {
c3fecea5 1086 qemu_co_mutex_unlock(&s->lock);
6daf194d 1087 error_report("failed to send a req, %s", strerror(errno));
cb595887 1088 return -errno;
33b1db1c
MK
1089 }
1090
1091 if (wlen) {
2fc8ae1d 1092 ret = qemu_co_sendv(s->fd, iov, niov, aio_req->iov_offset, wlen);
80731d9d 1093 if (ret != wlen) {
c3fecea5 1094 qemu_co_mutex_unlock(&s->lock);
6daf194d 1095 error_report("failed to send a data, %s", strerror(errno));
cb595887 1096 return -errno;
33b1db1c
MK
1097 }
1098 }
1099
128aa589 1100 socket_set_cork(s->fd, 0);
f2e5dca4 1101 qemu_aio_set_fd_handler(s->fd, co_read_response, NULL, s);
2df46246 1102 qemu_co_mutex_unlock(&s->lock);
33b1db1c
MK
1103
1104 return 0;
1105}
1106
29a67f7e 1107static int read_write_object(int fd, char *buf, uint64_t oid, uint8_t copies,
33b1db1c 1108 unsigned int datalen, uint64_t offset,
0e7106d8 1109 bool write, bool create, uint32_t cache_flags)
33b1db1c
MK
1110{
1111 SheepdogObjReq hdr;
1112 SheepdogObjRsp *rsp = (SheepdogObjRsp *)&hdr;
1113 unsigned int wlen, rlen;
1114 int ret;
1115
1116 memset(&hdr, 0, sizeof(hdr));
1117
1118 if (write) {
1119 wlen = datalen;
1120 rlen = 0;
1121 hdr.flags = SD_FLAG_CMD_WRITE;
1122 if (create) {
1123 hdr.opcode = SD_OP_CREATE_AND_WRITE_OBJ;
1124 } else {
1125 hdr.opcode = SD_OP_WRITE_OBJ;
1126 }
1127 } else {
1128 wlen = 0;
1129 rlen = datalen;
1130 hdr.opcode = SD_OP_READ_OBJ;
1131 }
47622c44 1132
0e7106d8 1133 hdr.flags |= cache_flags;
47622c44 1134
33b1db1c
MK
1135 hdr.oid = oid;
1136 hdr.data_length = datalen;
1137 hdr.offset = offset;
1138 hdr.copies = copies;
1139
1140 ret = do_req(fd, (SheepdogReq *)&hdr, buf, &wlen, &rlen);
1141 if (ret) {
6daf194d 1142 error_report("failed to send a request to the sheep");
cb595887 1143 return ret;
33b1db1c
MK
1144 }
1145
1146 switch (rsp->result) {
1147 case SD_RES_SUCCESS:
1148 return 0;
1149 default:
6daf194d 1150 error_report("%s", sd_strerror(rsp->result));
cb595887 1151 return -EIO;
33b1db1c
MK
1152 }
1153}
1154
29a67f7e 1155static int read_object(int fd, char *buf, uint64_t oid, uint8_t copies,
0e7106d8
LY
1156 unsigned int datalen, uint64_t offset,
1157 uint32_t cache_flags)
33b1db1c 1158{
2f536801 1159 return read_write_object(fd, buf, oid, copies, datalen, offset, false,
0e7106d8 1160 false, cache_flags);
33b1db1c
MK
1161}
1162
29a67f7e 1163static int write_object(int fd, char *buf, uint64_t oid, uint8_t copies,
2f536801 1164 unsigned int datalen, uint64_t offset, bool create,
0e7106d8 1165 uint32_t cache_flags)
33b1db1c 1166{
2f536801 1167 return read_write_object(fd, buf, oid, copies, datalen, offset, true,
0e7106d8 1168 create, cache_flags);
33b1db1c
MK
1169}
1170
9ff53a0e
MK
1171/* update inode with the latest state */
1172static int reload_inode(BDRVSheepdogState *s, uint32_t snapid, const char *tag)
1173{
1174 SheepdogInode *inode;
1175 int ret = 0, fd;
1176 uint32_t vid = 0;
1177
1178 fd = connect_to_sdog(s);
1179 if (fd < 0) {
1180 return -EIO;
1181 }
1182
1183 inode = g_malloc(sizeof(s->inode));
1184
1185 ret = find_vdi_name(s, s->name, snapid, tag, &vid, false);
1186 if (ret) {
1187 goto out;
1188 }
1189
1190 ret = read_object(fd, (char *)inode, vid_to_vdi_oid(vid),
1191 s->inode.nr_copies, sizeof(*inode), 0, s->cache_flags);
1192 if (ret < 0) {
1193 goto out;
1194 }
1195
1196 if (inode->vdi_id != s->inode.vdi_id) {
1197 memcpy(&s->inode, inode, sizeof(s->inode));
1198 }
1199
1200out:
1201 g_free(inode);
1202 closesocket(fd);
1203
1204 return ret;
1205}
1206
13c31de2
MK
1207static int coroutine_fn resend_aioreq(BDRVSheepdogState *s, AIOReq *aio_req)
1208{
1209 SheepdogAIOCB *acb = aio_req->aiocb;
1210 bool create = false;
13c31de2
MK
1211
1212 /* check whether this request becomes a CoW one */
2412aec7 1213 if (acb->aiocb_type == AIOCB_WRITE_UDATA && is_data_obj(aio_req->oid)) {
13c31de2
MK
1214 int idx = data_oid_to_idx(aio_req->oid);
1215 AIOReq *areq;
1216
1217 if (s->inode.data_vdi_id[idx] == 0) {
1218 create = true;
1219 goto out;
1220 }
1221 if (is_data_obj_writable(&s->inode, idx)) {
1222 goto out;
1223 }
1224
1225 /* link to the pending list if there is another CoW request to
1226 * the same object */
1227 QLIST_FOREACH(areq, &s->inflight_aio_head, aio_siblings) {
1228 if (areq != aio_req && areq->oid == aio_req->oid) {
2440a2c3 1229 DPRINTF("simultaneous CoW to %" PRIx64 "\n", aio_req->oid);
13c31de2
MK
1230 QLIST_REMOVE(aio_req, aio_siblings);
1231 QLIST_INSERT_HEAD(&s->pending_aio_head, aio_req, aio_siblings);
1232 return SD_RES_SUCCESS;
1233 }
1234 }
1235
1236 aio_req->base_oid = vid_to_data_oid(s->inode.data_vdi_id[idx], idx);
1237 aio_req->flags |= SD_FLAG_CMD_COW;
1238 create = true;
1239 }
1240out:
2412aec7
MK
1241 if (is_data_obj(aio_req->oid)) {
1242 return add_aio_request(s, aio_req, acb->qiov->iov, acb->qiov->niov,
1243 create, acb->aiocb_type);
1244 } else {
1245 struct iovec iov;
1246 iov.iov_base = &s->inode;
1247 iov.iov_len = sizeof(s->inode);
1248 return add_aio_request(s, aio_req, &iov, 1, false, AIOCB_WRITE_UDATA);
1249 }
13c31de2
MK
1250}
1251
c8c96350
KW
1252/* TODO Convert to fine grained options */
1253static QemuOptsList runtime_opts = {
1254 .name = "sheepdog",
1255 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
1256 .desc = {
1257 {
1258 .name = "filename",
1259 .type = QEMU_OPT_STRING,
1260 .help = "URL to the sheepdog image",
1261 },
1262 { /* end of list */ }
1263 },
1264};
1265
015a1036
HR
1266static int sd_open(BlockDriverState *bs, QDict *options, int flags,
1267 Error **errp)
33b1db1c
MK
1268{
1269 int ret, fd;
1270 uint32_t vid = 0;
1271 BDRVSheepdogState *s = bs->opaque;
1272 char vdi[SD_MAX_VDI_LEN], tag[SD_MAX_VDI_TAG_LEN];
1273 uint32_t snapid;
1274 char *buf = NULL;
c8c96350
KW
1275 QemuOpts *opts;
1276 Error *local_err = NULL;
1277 const char *filename;
1278
1279 opts = qemu_opts_create_nofail(&runtime_opts);
1280 qemu_opts_absorb_qdict(opts, options, &local_err);
1281 if (error_is_set(&local_err)) {
1282 qerror_report_err(local_err);
1283 error_free(local_err);
1284 ret = -EINVAL;
1285 goto out;
1286 }
1287
1288 filename = qemu_opt_get(opts, "filename");
33b1db1c 1289
c292ee6a
MK
1290 QLIST_INIT(&s->inflight_aio_head);
1291 QLIST_INIT(&s->pending_aio_head);
33b1db1c
MK
1292 s->fd = -1;
1293
1294 memset(vdi, 0, sizeof(vdi));
1295 memset(tag, 0, sizeof(tag));
5d6768e3
MK
1296
1297 if (strstr(filename, "://")) {
1298 ret = sd_parse_uri(s, filename, vdi, &snapid, tag);
1299 } else {
1300 ret = parse_vdiname(s, filename, vdi, &snapid, tag);
1301 }
1302 if (ret < 0) {
33b1db1c
MK
1303 goto out;
1304 }
1305 s->fd = get_sheep_fd(s);
1306 if (s->fd < 0) {
cb595887 1307 ret = s->fd;
33b1db1c
MK
1308 goto out;
1309 }
1310
982dcbf4 1311 ret = find_vdi_name(s, vdi, snapid, tag, &vid, true);
33b1db1c
MK
1312 if (ret) {
1313 goto out;
1314 }
1315
0e7106d8
LY
1316 /*
1317 * QEMU block layer emulates writethrough cache as 'writeback + flush', so
1318 * we always set SD_FLAG_CMD_CACHE (writeback cache) as default.
1319 */
1320 s->cache_flags = SD_FLAG_CMD_CACHE;
1321 if (flags & BDRV_O_NOCACHE) {
1322 s->cache_flags = SD_FLAG_CMD_DIRECT;
1323 }
cac8f4a6 1324 s->discard_supported = true;
0e7106d8 1325
622b6057 1326 if (snapid || tag[0] != '\0') {
2440a2c3 1327 DPRINTF("%" PRIx32 " snapshot inode was open.\n", vid);
2f536801 1328 s->is_snapshot = true;
33b1db1c
MK
1329 }
1330
25af257d 1331 fd = connect_to_sdog(s);
33b1db1c 1332 if (fd < 0) {
cb595887 1333 ret = fd;
33b1db1c
MK
1334 goto out;
1335 }
1336
7267c094 1337 buf = g_malloc(SD_INODE_SIZE);
47622c44 1338 ret = read_object(fd, buf, vid_to_vdi_oid(vid), 0, SD_INODE_SIZE, 0,
0e7106d8 1339 s->cache_flags);
33b1db1c
MK
1340
1341 closesocket(fd);
1342
1343 if (ret) {
1344 goto out;
1345 }
1346
1347 memcpy(&s->inode, buf, sizeof(s->inode));
1348 s->min_dirty_data_idx = UINT32_MAX;
1349 s->max_dirty_data_idx = 0;
1350
e8bfaa2f 1351 bs->total_sectors = s->inode.vdi_size / BDRV_SECTOR_SIZE;
3178e275 1352 pstrcpy(s->name, sizeof(s->name), vdi);
2df46246 1353 qemu_co_mutex_init(&s->lock);
c8c96350 1354 qemu_opts_del(opts);
7267c094 1355 g_free(buf);
33b1db1c
MK
1356 return 0;
1357out:
f2e5dca4 1358 qemu_aio_set_fd_handler(s->fd, NULL, NULL, NULL);
33b1db1c
MK
1359 if (s->fd >= 0) {
1360 closesocket(s->fd);
1361 }
c8c96350 1362 qemu_opts_del(opts);
7267c094 1363 g_free(buf);
cb595887 1364 return ret;
33b1db1c
MK
1365}
1366
25af257d 1367static int do_sd_create(BDRVSheepdogState *s, char *filename, int64_t vdi_size,
1841f880
LY
1368 uint32_t base_vid, uint32_t *vdi_id, int snapshot,
1369 uint8_t copy_policy)
33b1db1c
MK
1370{
1371 SheepdogVdiReq hdr;
1372 SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr;
1373 int fd, ret;
1374 unsigned int wlen, rlen = 0;
1375 char buf[SD_MAX_VDI_LEN];
1376
25af257d 1377 fd = connect_to_sdog(s);
33b1db1c 1378 if (fd < 0) {
cb595887 1379 return fd;
33b1db1c
MK
1380 }
1381
3178e275
JM
1382 /* FIXME: would it be better to fail (e.g., return -EIO) when filename
1383 * does not fit in buf? For now, just truncate and avoid buffer overrun.
1384 */
33b1db1c 1385 memset(buf, 0, sizeof(buf));
3178e275 1386 pstrcpy(buf, sizeof(buf), filename);
33b1db1c
MK
1387
1388 memset(&hdr, 0, sizeof(hdr));
1389 hdr.opcode = SD_OP_NEW_VDI;
6f74c260 1390 hdr.vdi_id = base_vid;
33b1db1c
MK
1391
1392 wlen = SD_MAX_VDI_LEN;
1393
1394 hdr.flags = SD_FLAG_CMD_WRITE;
1395 hdr.snapid = snapshot;
1396
1397 hdr.data_length = wlen;
1398 hdr.vdi_size = vdi_size;
1841f880 1399 hdr.copy_policy = copy_policy;
33b1db1c
MK
1400
1401 ret = do_req(fd, (SheepdogReq *)&hdr, buf, &wlen, &rlen);
1402
1403 closesocket(fd);
1404
1405 if (ret) {
cb595887 1406 return ret;
33b1db1c
MK
1407 }
1408
1409 if (rsp->result != SD_RES_SUCCESS) {
6daf194d 1410 error_report("%s, %s", sd_strerror(rsp->result), filename);
33b1db1c
MK
1411 return -EIO;
1412 }
1413
1414 if (vdi_id) {
1415 *vdi_id = rsp->vdi_id;
1416 }
1417
1418 return 0;
1419}
1420
a8e0fdd7
MK
1421static int sd_prealloc(const char *filename)
1422{
1423 BlockDriverState *bs = NULL;
1424 uint32_t idx, max_idx;
1425 int64_t vdi_size;
7267c094 1426 void *buf = g_malloc0(SD_DATA_OBJ_SIZE);
34b5d2c6 1427 Error *local_err = NULL;
a8e0fdd7
MK
1428 int ret;
1429
34b5d2c6 1430 ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR, &local_err);
a8e0fdd7 1431 if (ret < 0) {
34b5d2c6
HR
1432 qerror_report_err(local_err);
1433 error_free(local_err);
a8e0fdd7
MK
1434 goto out;
1435 }
1436
1437 vdi_size = bdrv_getlength(bs);
1438 if (vdi_size < 0) {
1439 ret = vdi_size;
1440 goto out;
1441 }
1442 max_idx = DIV_ROUND_UP(vdi_size, SD_DATA_OBJ_SIZE);
1443
1444 for (idx = 0; idx < max_idx; idx++) {
1445 /*
1446 * The created image can be a cloned image, so we need to read
1447 * a data from the source image.
1448 */
1449 ret = bdrv_pread(bs, idx * SD_DATA_OBJ_SIZE, buf, SD_DATA_OBJ_SIZE);
1450 if (ret < 0) {
1451 goto out;
1452 }
1453 ret = bdrv_pwrite(bs, idx * SD_DATA_OBJ_SIZE, buf, SD_DATA_OBJ_SIZE);
1454 if (ret < 0) {
1455 goto out;
1456 }
1457 }
1458out:
1459 if (bs) {
4f6fd349 1460 bdrv_unref(bs);
a8e0fdd7 1461 }
7267c094 1462 g_free(buf);
a8e0fdd7
MK
1463
1464 return ret;
1465}
1466
d5124c00
HR
1467static int sd_create(const char *filename, QEMUOptionParameter *options,
1468 Error **errp)
33b1db1c 1469{
b6fc8245 1470 int ret = 0;
b4447363 1471 uint32_t vid = 0, base_vid = 0;
33b1db1c
MK
1472 int64_t vdi_size = 0;
1473 char *backing_file = NULL;
b6fc8245 1474 BDRVSheepdogState *s;
b4447363
MK
1475 char vdi[SD_MAX_VDI_LEN], tag[SD_MAX_VDI_TAG_LEN];
1476 uint32_t snapid;
2f536801 1477 bool prealloc = false;
34b5d2c6 1478 Error *local_err = NULL;
33b1db1c 1479
b6fc8245
MK
1480 s = g_malloc0(sizeof(BDRVSheepdogState));
1481
b4447363
MK
1482 memset(vdi, 0, sizeof(vdi));
1483 memset(tag, 0, sizeof(tag));
5d6768e3
MK
1484 if (strstr(filename, "://")) {
1485 ret = sd_parse_uri(s, filename, vdi, &snapid, tag);
1486 } else {
1487 ret = parse_vdiname(s, filename, vdi, &snapid, tag);
1488 }
1489 if (ret < 0) {
b6fc8245 1490 goto out;
b4447363
MK
1491 }
1492
33b1db1c
MK
1493 while (options && options->name) {
1494 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
1495 vdi_size = options->value.n;
1496 } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
1497 backing_file = options->value.s;
a8e0fdd7
MK
1498 } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) {
1499 if (!options->value.s || !strcmp(options->value.s, "off")) {
2f536801 1500 prealloc = false;
a8e0fdd7 1501 } else if (!strcmp(options->value.s, "full")) {
2f536801 1502 prealloc = true;
a8e0fdd7
MK
1503 } else {
1504 error_report("Invalid preallocation mode: '%s'",
1505 options->value.s);
b6fc8245
MK
1506 ret = -EINVAL;
1507 goto out;
a8e0fdd7 1508 }
33b1db1c
MK
1509 }
1510 options++;
1511 }
1512
1513 if (vdi_size > SD_MAX_VDI_SIZE) {
6daf194d 1514 error_report("too big image size");
b6fc8245
MK
1515 ret = -EINVAL;
1516 goto out;
33b1db1c
MK
1517 }
1518
1519 if (backing_file) {
1520 BlockDriverState *bs;
1521 BDRVSheepdogState *s;
1522 BlockDriver *drv;
1523
1524 /* Currently, only Sheepdog backing image is supported. */
98289620 1525 drv = bdrv_find_protocol(backing_file, true);
33b1db1c 1526 if (!drv || strcmp(drv->protocol_name, "sheepdog") != 0) {
6daf194d 1527 error_report("backing_file must be a sheepdog image");
b6fc8245
MK
1528 ret = -EINVAL;
1529 goto out;
33b1db1c
MK
1530 }
1531
34b5d2c6 1532 ret = bdrv_file_open(&bs, backing_file, NULL, 0, &local_err);
cb595887 1533 if (ret < 0) {
34b5d2c6
HR
1534 qerror_report_err(local_err);
1535 error_free(local_err);
b6fc8245 1536 goto out;
cb595887 1537 }
33b1db1c
MK
1538
1539 s = bs->opaque;
1540
1541 if (!is_snapshot(&s->inode)) {
6daf194d 1542 error_report("cannot clone from a non snapshot vdi");
4f6fd349 1543 bdrv_unref(bs);
b6fc8245
MK
1544 ret = -EINVAL;
1545 goto out;
33b1db1c
MK
1546 }
1547
b4447363 1548 base_vid = s->inode.vdi_id;
4f6fd349 1549 bdrv_unref(bs);
33b1db1c
MK
1550 }
1551
1841f880
LY
1552 /* TODO: allow users to specify copy number */
1553 ret = do_sd_create(s, vdi, vdi_size, base_vid, &vid, 0, 0);
a8e0fdd7 1554 if (!prealloc || ret) {
b6fc8245 1555 goto out;
a8e0fdd7
MK
1556 }
1557
b6fc8245
MK
1558 ret = sd_prealloc(filename);
1559out:
1560 g_free(s);
1561 return ret;
33b1db1c
MK
1562}
1563
1564static void sd_close(BlockDriverState *bs)
1565{
1566 BDRVSheepdogState *s = bs->opaque;
1567 SheepdogVdiReq hdr;
1568 SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr;
1569 unsigned int wlen, rlen = 0;
1570 int fd, ret;
1571
2440a2c3 1572 DPRINTF("%s\n", s->name);
33b1db1c 1573
25af257d 1574 fd = connect_to_sdog(s);
33b1db1c
MK
1575 if (fd < 0) {
1576 return;
1577 }
1578
1579 memset(&hdr, 0, sizeof(hdr));
1580
1581 hdr.opcode = SD_OP_RELEASE_VDI;
6f74c260 1582 hdr.vdi_id = s->inode.vdi_id;
33b1db1c
MK
1583 wlen = strlen(s->name) + 1;
1584 hdr.data_length = wlen;
1585 hdr.flags = SD_FLAG_CMD_WRITE;
1586
1587 ret = do_req(fd, (SheepdogReq *)&hdr, s->name, &wlen, &rlen);
1588
1589 closesocket(fd);
1590
1591 if (!ret && rsp->result != SD_RES_SUCCESS &&
1592 rsp->result != SD_RES_VDI_NOT_LOCKED) {
6daf194d 1593 error_report("%s, %s", sd_strerror(rsp->result), s->name);
33b1db1c
MK
1594 }
1595
f2e5dca4 1596 qemu_aio_set_fd_handler(s->fd, NULL, NULL, NULL);
33b1db1c 1597 closesocket(s->fd);
25af257d 1598 g_free(s->host_spec);
33b1db1c
MK
1599}
1600
1601static int64_t sd_getlength(BlockDriverState *bs)
1602{
1603 BDRVSheepdogState *s = bs->opaque;
1604
1605 return s->inode.vdi_size;
1606}
1607
1608static int sd_truncate(BlockDriverState *bs, int64_t offset)
1609{
1610 BDRVSheepdogState *s = bs->opaque;
1611 int ret, fd;
1612 unsigned int datalen;
1613
1614 if (offset < s->inode.vdi_size) {
6daf194d 1615 error_report("shrinking is not supported");
33b1db1c
MK
1616 return -EINVAL;
1617 } else if (offset > SD_MAX_VDI_SIZE) {
6daf194d 1618 error_report("too big image size");
33b1db1c
MK
1619 return -EINVAL;
1620 }
1621
25af257d 1622 fd = connect_to_sdog(s);
33b1db1c 1623 if (fd < 0) {
cb595887 1624 return fd;
33b1db1c
MK
1625 }
1626
1627 /* we don't need to update entire object */
1628 datalen = SD_INODE_SIZE - sizeof(s->inode.data_vdi_id);
1629 s->inode.vdi_size = offset;
1630 ret = write_object(fd, (char *)&s->inode, vid_to_vdi_oid(s->inode.vdi_id),
0e7106d8 1631 s->inode.nr_copies, datalen, 0, false, s->cache_flags);
33b1db1c
MK
1632 close(fd);
1633
1634 if (ret < 0) {
6daf194d 1635 error_report("failed to update an inode.");
33b1db1c
MK
1636 }
1637
cb595887 1638 return ret;
33b1db1c
MK
1639}
1640
1641/*
1642 * This function is called after writing data objects. If we need to
1643 * update metadata, this sends a write request to the vdi object.
2df46246 1644 * Otherwise, this switches back to sd_co_readv/writev.
33b1db1c 1645 */
d8716b41 1646static void coroutine_fn sd_write_done(SheepdogAIOCB *acb)
33b1db1c
MK
1647{
1648 int ret;
1649 BDRVSheepdogState *s = acb->common.bs->opaque;
1650 struct iovec iov;
1651 AIOReq *aio_req;
1652 uint32_t offset, data_len, mn, mx;
1653
1654 mn = s->min_dirty_data_idx;
1655 mx = s->max_dirty_data_idx;
1656 if (mn <= mx) {
1657 /* we need to update the vdi object. */
1658 offset = sizeof(s->inode) - sizeof(s->inode.data_vdi_id) +
1659 mn * sizeof(s->inode.data_vdi_id[0]);
1660 data_len = (mx - mn + 1) * sizeof(s->inode.data_vdi_id[0]);
1661
1662 s->min_dirty_data_idx = UINT32_MAX;
1663 s->max_dirty_data_idx = 0;
1664
1665 iov.iov_base = &s->inode;
1666 iov.iov_len = sizeof(s->inode);
1667 aio_req = alloc_aio_req(s, acb, vid_to_vdi_oid(s->inode.vdi_id),
1668 data_len, offset, 0, 0, offset);
c292ee6a 1669 QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings);
2f536801 1670 ret = add_aio_request(s, aio_req, &iov, 1, false, AIOCB_WRITE_UDATA);
33b1db1c
MK
1671 if (ret) {
1672 free_aio_req(s, aio_req);
1673 acb->ret = -EIO;
1674 goto out;
1675 }
1676
1677 acb->aio_done_func = sd_finish_aiocb;
1678 acb->aiocb_type = AIOCB_WRITE_UDATA;
1679 return;
1680 }
1681out:
1682 sd_finish_aiocb(acb);
1683}
1684
859e5553
LY
1685/* Delete current working VDI on the snapshot chain */
1686static bool sd_delete(BDRVSheepdogState *s)
1687{
1688 unsigned int wlen = SD_MAX_VDI_LEN, rlen = 0;
1689 SheepdogVdiReq hdr = {
1690 .opcode = SD_OP_DEL_VDI,
1691 .vdi_id = s->inode.vdi_id,
1692 .data_length = wlen,
1693 .flags = SD_FLAG_CMD_WRITE,
1694 };
1695 SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr;
1696 int fd, ret;
1697
1698 fd = connect_to_sdog(s);
1699 if (fd < 0) {
1700 return false;
1701 }
1702
1703 ret = do_req(fd, (SheepdogReq *)&hdr, s->name, &wlen, &rlen);
1704 closesocket(fd);
1705 if (ret) {
1706 return false;
1707 }
1708 switch (rsp->result) {
1709 case SD_RES_NO_VDI:
1710 error_report("%s was already deleted", s->name);
1711 /* fall through */
1712 case SD_RES_SUCCESS:
1713 break;
1714 default:
1715 error_report("%s, %s", sd_strerror(rsp->result), s->name);
1716 return false;
1717 }
1718
1719 return true;
1720}
1721
33b1db1c
MK
1722/*
1723 * Create a writable VDI from a snapshot
1724 */
1725static int sd_create_branch(BDRVSheepdogState *s)
1726{
1727 int ret, fd;
1728 uint32_t vid;
1729 char *buf;
859e5553 1730 bool deleted;
33b1db1c 1731
2440a2c3 1732 DPRINTF("%" PRIx32 " is snapshot.\n", s->inode.vdi_id);
33b1db1c 1733
7267c094 1734 buf = g_malloc(SD_INODE_SIZE);
33b1db1c 1735
859e5553
LY
1736 /*
1737 * Even If deletion fails, we will just create extra snapshot based on
1738 * the workding VDI which was supposed to be deleted. So no need to
1739 * false bail out.
1740 */
1741 deleted = sd_delete(s);
1742 ret = do_sd_create(s, s->name, s->inode.vdi_size, s->inode.vdi_id, &vid,
1841f880 1743 !deleted, s->inode.copy_policy);
33b1db1c
MK
1744 if (ret) {
1745 goto out;
1746 }
1747
2440a2c3 1748 DPRINTF("%" PRIx32 " is created.\n", vid);
33b1db1c 1749
25af257d 1750 fd = connect_to_sdog(s);
33b1db1c 1751 if (fd < 0) {
cb595887 1752 ret = fd;
33b1db1c
MK
1753 goto out;
1754 }
1755
1756 ret = read_object(fd, buf, vid_to_vdi_oid(vid), s->inode.nr_copies,
0e7106d8 1757 SD_INODE_SIZE, 0, s->cache_flags);
33b1db1c
MK
1758
1759 closesocket(fd);
1760
1761 if (ret < 0) {
1762 goto out;
1763 }
1764
1765 memcpy(&s->inode, buf, sizeof(s->inode));
1766
2f536801 1767 s->is_snapshot = false;
33b1db1c 1768 ret = 0;
2440a2c3 1769 DPRINTF("%" PRIx32 " was newly created.\n", s->inode.vdi_id);
33b1db1c
MK
1770
1771out:
7267c094 1772 g_free(buf);
33b1db1c
MK
1773
1774 return ret;
1775}
1776
1777/*
1778 * Send I/O requests to the server.
1779 *
1780 * This function sends requests to the server, links the requests to
c292ee6a 1781 * the inflight_list in BDRVSheepdogState, and exits without
33b1db1c
MK
1782 * waiting the response. The responses are received in the
1783 * `aio_read_response' function which is called from the main loop as
1784 * a fd handler.
2df46246
MK
1785 *
1786 * Returns 1 when we need to wait a response, 0 when there is no sent
1787 * request and -errno in error cases.
33b1db1c 1788 */
d8716b41 1789static int coroutine_fn sd_co_rw_vector(void *p)
33b1db1c
MK
1790{
1791 SheepdogAIOCB *acb = p;
1792 int ret = 0;
e8bfaa2f
LY
1793 unsigned long len, done = 0, total = acb->nb_sectors * BDRV_SECTOR_SIZE;
1794 unsigned long idx = acb->sector_num * BDRV_SECTOR_SIZE / SD_DATA_OBJ_SIZE;
33b1db1c 1795 uint64_t oid;
e8bfaa2f 1796 uint64_t offset = (acb->sector_num * BDRV_SECTOR_SIZE) % SD_DATA_OBJ_SIZE;
33b1db1c
MK
1797 BDRVSheepdogState *s = acb->common.bs->opaque;
1798 SheepdogInode *inode = &s->inode;
1799 AIOReq *aio_req;
1800
33b1db1c
MK
1801 if (acb->aiocb_type == AIOCB_WRITE_UDATA && s->is_snapshot) {
1802 /*
1803 * In the case we open the snapshot VDI, Sheepdog creates the
1804 * writable VDI when we do a write operation first.
1805 */
1806 ret = sd_create_branch(s);
1807 if (ret) {
1808 acb->ret = -EIO;
1809 goto out;
1810 }
1811 }
1812
1d732d7d
MK
1813 /*
1814 * Make sure we don't free the aiocb before we are done with all requests.
1815 * This additional reference is dropped at the end of this function.
1816 */
1817 acb->nr_pending++;
1818
33b1db1c
MK
1819 while (done != total) {
1820 uint8_t flags = 0;
1821 uint64_t old_oid = 0;
2f536801 1822 bool create = false;
33b1db1c
MK
1823
1824 oid = vid_to_data_oid(inode->data_vdi_id[idx], idx);
1825
1826 len = MIN(total - done, SD_DATA_OBJ_SIZE - offset);
1827
19db9b90
CH
1828 switch (acb->aiocb_type) {
1829 case AIOCB_READ_UDATA:
1830 if (!inode->data_vdi_id[idx]) {
1831 qemu_iovec_memset(acb->qiov, done, 0, len);
33b1db1c
MK
1832 goto done;
1833 }
19db9b90
CH
1834 break;
1835 case AIOCB_WRITE_UDATA:
1836 if (!inode->data_vdi_id[idx]) {
2f536801 1837 create = true;
19db9b90
CH
1838 } else if (!is_data_obj_writable(inode, idx)) {
1839 /* Copy-On-Write */
2f536801 1840 create = true;
19db9b90
CH
1841 old_oid = oid;
1842 flags = SD_FLAG_CMD_COW;
1843 }
1844 break;
cac8f4a6
LY
1845 case AIOCB_DISCARD_OBJ:
1846 /*
1847 * We discard the object only when the whole object is
1848 * 1) allocated 2) trimmed. Otherwise, simply skip it.
1849 */
1850 if (len != SD_DATA_OBJ_SIZE || inode->data_vdi_id[idx] == 0) {
1851 goto done;
1852 }
1853 break;
19db9b90
CH
1854 default:
1855 break;
33b1db1c
MK
1856 }
1857
1858 if (create) {
2440a2c3 1859 DPRINTF("update ino (%" PRIu32 ") %" PRIu64 " %" PRIu64 " %ld\n",
1b6ac998 1860 inode->vdi_id, oid,
33b1db1c
MK
1861 vid_to_data_oid(inode->data_vdi_id[idx], idx), idx);
1862 oid = vid_to_data_oid(inode->vdi_id, idx);
2440a2c3 1863 DPRINTF("new oid %" PRIx64 "\n", oid);
33b1db1c
MK
1864 }
1865
1866 aio_req = alloc_aio_req(s, acb, oid, len, offset, flags, old_oid, done);
1867
1868 if (create) {
1869 AIOReq *areq;
c292ee6a 1870 QLIST_FOREACH(areq, &s->inflight_aio_head, aio_siblings) {
33b1db1c
MK
1871 if (areq->oid == oid) {
1872 /*
1873 * Sheepdog cannot handle simultaneous create
1874 * requests to the same object. So we cannot send
1875 * the request until the previous request
1876 * finishes.
1877 */
1878 aio_req->flags = 0;
1879 aio_req->base_oid = 0;
c292ee6a
MK
1880 QLIST_INSERT_HEAD(&s->pending_aio_head, aio_req,
1881 aio_siblings);
33b1db1c
MK
1882 goto done;
1883 }
1884 }
1885 }
1886
c292ee6a 1887 QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings);
33b1db1c
MK
1888 ret = add_aio_request(s, aio_req, acb->qiov->iov, acb->qiov->niov,
1889 create, acb->aiocb_type);
1890 if (ret < 0) {
6daf194d 1891 error_report("add_aio_request is failed");
33b1db1c
MK
1892 free_aio_req(s, aio_req);
1893 acb->ret = -EIO;
1894 goto out;
1895 }
1896 done:
1897 offset = 0;
1898 idx++;
1899 done += len;
1900 }
1901out:
1d732d7d 1902 if (!--acb->nr_pending) {
2df46246 1903 return acb->ret;
33b1db1c 1904 }
2df46246 1905 return 1;
33b1db1c
MK
1906}
1907
a968168c 1908static coroutine_fn int sd_co_writev(BlockDriverState *bs, int64_t sector_num,
2df46246 1909 int nb_sectors, QEMUIOVector *qiov)
33b1db1c
MK
1910{
1911 SheepdogAIOCB *acb;
2df46246 1912 int ret;
33b1db1c
MK
1913
1914 if (bs->growable && sector_num + nb_sectors > bs->total_sectors) {
e8bfaa2f 1915 ret = sd_truncate(bs, (sector_num + nb_sectors) * BDRV_SECTOR_SIZE);
cb595887
MK
1916 if (ret < 0) {
1917 return ret;
33b1db1c
MK
1918 }
1919 bs->total_sectors = sector_num + nb_sectors;
1920 }
1921
f700f8e3 1922 acb = sd_aio_setup(bs, qiov, sector_num, nb_sectors);
33b1db1c
MK
1923 acb->aio_done_func = sd_write_done;
1924 acb->aiocb_type = AIOCB_WRITE_UDATA;
1925
2df46246
MK
1926 ret = sd_co_rw_vector(acb);
1927 if (ret <= 0) {
1928 qemu_aio_release(acb);
1929 return ret;
1930 }
1931
1932 qemu_coroutine_yield();
1933
1934 return acb->ret;
33b1db1c
MK
1935}
1936
a968168c 1937static coroutine_fn int sd_co_readv(BlockDriverState *bs, int64_t sector_num,
2df46246 1938 int nb_sectors, QEMUIOVector *qiov)
33b1db1c
MK
1939{
1940 SheepdogAIOCB *acb;
19db9b90 1941 int ret;
33b1db1c 1942
f700f8e3 1943 acb = sd_aio_setup(bs, qiov, sector_num, nb_sectors);
33b1db1c
MK
1944 acb->aiocb_type = AIOCB_READ_UDATA;
1945 acb->aio_done_func = sd_finish_aiocb;
1946
2df46246
MK
1947 ret = sd_co_rw_vector(acb);
1948 if (ret <= 0) {
1949 qemu_aio_release(acb);
1950 return ret;
1951 }
1952
1953 qemu_coroutine_yield();
1954
1955 return acb->ret;
33b1db1c
MK
1956}
1957
47622c44
LY
1958static int coroutine_fn sd_co_flush_to_disk(BlockDriverState *bs)
1959{
1960 BDRVSheepdogState *s = bs->opaque;
47783072
LY
1961 SheepdogAIOCB *acb;
1962 AIOReq *aio_req;
47622c44 1963 int ret;
47622c44 1964
0e7106d8 1965 if (s->cache_flags != SD_FLAG_CMD_CACHE) {
47622c44
LY
1966 return 0;
1967 }
1968
f700f8e3 1969 acb = sd_aio_setup(bs, NULL, 0, 0);
47783072
LY
1970 acb->aiocb_type = AIOCB_FLUSH_CACHE;
1971 acb->aio_done_func = sd_finish_aiocb;
47622c44 1972
47783072
LY
1973 aio_req = alloc_aio_req(s, acb, vid_to_vdi_oid(s->inode.vdi_id),
1974 0, 0, 0, 0, 0);
1975 QLIST_INSERT_HEAD(&s->inflight_aio_head, aio_req, aio_siblings);
1976 ret = add_aio_request(s, aio_req, NULL, 0, false, acb->aiocb_type);
1977 if (ret < 0) {
1978 error_report("add_aio_request is failed");
1979 free_aio_req(s, aio_req);
1980 qemu_aio_release(acb);
47622c44
LY
1981 return ret;
1982 }
1983
47783072
LY
1984 qemu_coroutine_yield();
1985 return acb->ret;
47622c44
LY
1986}
1987
33b1db1c
MK
1988static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
1989{
1990 BDRVSheepdogState *s = bs->opaque;
1991 int ret, fd;
1992 uint32_t new_vid;
1993 SheepdogInode *inode;
1994 unsigned int datalen;
1995
2440a2c3 1996 DPRINTF("sn_info: name %s id_str %s s: name %s vm_state_size %" PRId64 " "
33b1db1c
MK
1997 "is_snapshot %d\n", sn_info->name, sn_info->id_str,
1998 s->name, sn_info->vm_state_size, s->is_snapshot);
1999
2000 if (s->is_snapshot) {
2001 error_report("You can't create a snapshot of a snapshot VDI, "
6daf194d 2002 "%s (%" PRIu32 ").", s->name, s->inode.vdi_id);
33b1db1c
MK
2003
2004 return -EINVAL;
2005 }
2006
2440a2c3 2007 DPRINTF("%s %s\n", sn_info->name, sn_info->id_str);
33b1db1c
MK
2008
2009 s->inode.vm_state_size = sn_info->vm_state_size;
2010 s->inode.vm_clock_nsec = sn_info->vm_clock_nsec;
3178e275
JM
2011 /* It appears that inode.tag does not require a NUL terminator,
2012 * which means this use of strncpy is ok.
2013 */
33b1db1c
MK
2014 strncpy(s->inode.tag, sn_info->name, sizeof(s->inode.tag));
2015 /* we don't need to update entire object */
2016 datalen = SD_INODE_SIZE - sizeof(s->inode.data_vdi_id);
2017
2018 /* refresh inode. */
25af257d 2019 fd = connect_to_sdog(s);
33b1db1c 2020 if (fd < 0) {
cb595887 2021 ret = fd;
33b1db1c
MK
2022 goto cleanup;
2023 }
2024
2025 ret = write_object(fd, (char *)&s->inode, vid_to_vdi_oid(s->inode.vdi_id),
0e7106d8 2026 s->inode.nr_copies, datalen, 0, false, s->cache_flags);
33b1db1c 2027 if (ret < 0) {
6daf194d 2028 error_report("failed to write snapshot's inode.");
33b1db1c
MK
2029 goto cleanup;
2030 }
2031
25af257d 2032 ret = do_sd_create(s, s->name, s->inode.vdi_size, s->inode.vdi_id, &new_vid,
1841f880 2033 1, s->inode.copy_policy);
33b1db1c 2034 if (ret < 0) {
6daf194d 2035 error_report("failed to create inode for snapshot. %s",
33b1db1c 2036 strerror(errno));
33b1db1c
MK
2037 goto cleanup;
2038 }
2039
7267c094 2040 inode = (SheepdogInode *)g_malloc(datalen);
33b1db1c
MK
2041
2042 ret = read_object(fd, (char *)inode, vid_to_vdi_oid(new_vid),
0e7106d8 2043 s->inode.nr_copies, datalen, 0, s->cache_flags);
33b1db1c
MK
2044
2045 if (ret < 0) {
6daf194d 2046 error_report("failed to read new inode info. %s", strerror(errno));
33b1db1c
MK
2047 goto cleanup;
2048 }
2049
2050 memcpy(&s->inode, inode, datalen);
2440a2c3 2051 DPRINTF("s->inode: name %s snap_id %x oid %x\n",
33b1db1c
MK
2052 s->inode.name, s->inode.snap_id, s->inode.vdi_id);
2053
2054cleanup:
2055 closesocket(fd);
2056 return ret;
2057}
2058
859e5553
LY
2059/*
2060 * We implement rollback(loadvm) operation to the specified snapshot by
2061 * 1) switch to the snapshot
2062 * 2) rely on sd_create_branch to delete working VDI and
2063 * 3) create a new working VDI based on the speicified snapshot
2064 */
33b1db1c
MK
2065static int sd_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
2066{
2067 BDRVSheepdogState *s = bs->opaque;
2068 BDRVSheepdogState *old_s;
9ff53a0e 2069 char tag[SD_MAX_VDI_TAG_LEN];
33b1db1c 2070 uint32_t snapid = 0;
9ff53a0e 2071 int ret = 0;
33b1db1c 2072
7267c094 2073 old_s = g_malloc(sizeof(BDRVSheepdogState));
33b1db1c
MK
2074
2075 memcpy(old_s, s, sizeof(BDRVSheepdogState));
2076
33b1db1c 2077 snapid = strtoul(snapshot_id, NULL, 10);
3178e275
JM
2078 if (snapid) {
2079 tag[0] = 0;
2080 } else {
b579ffb3 2081 pstrcpy(tag, sizeof(tag), snapshot_id);
33b1db1c
MK
2082 }
2083
9ff53a0e 2084 ret = reload_inode(s, snapid, tag);
33b1db1c 2085 if (ret) {
33b1db1c
MK
2086 goto out;
2087 }
2088
cede621f
LY
2089 ret = sd_create_branch(s);
2090 if (ret) {
33b1db1c
MK
2091 goto out;
2092 }
2093
7267c094 2094 g_free(old_s);
33b1db1c
MK
2095
2096 return 0;
2097out:
2098 /* recover bdrv_sd_state */
2099 memcpy(s, old_s, sizeof(BDRVSheepdogState));
7267c094 2100 g_free(old_s);
33b1db1c 2101
6daf194d 2102 error_report("failed to open. recover old bdrv_sd_state.");
33b1db1c
MK
2103
2104 return ret;
2105}
2106
a89d89d3
WX
2107static int sd_snapshot_delete(BlockDriverState *bs,
2108 const char *snapshot_id,
2109 const char *name,
2110 Error **errp)
33b1db1c
MK
2111{
2112 /* FIXME: Delete specified snapshot id. */
2113 return 0;
2114}
2115
33b1db1c
MK
2116static int sd_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
2117{
2118 BDRVSheepdogState *s = bs->opaque;
2119 SheepdogReq req;
2120 int fd, nr = 1024, ret, max = BITS_TO_LONGS(SD_NR_VDIS) * sizeof(long);
2121 QEMUSnapshotInfo *sn_tab = NULL;
2122 unsigned wlen, rlen;
2123 int found = 0;
2124 static SheepdogInode inode;
2125 unsigned long *vdi_inuse;
2126 unsigned int start_nr;
2127 uint64_t hval;
2128 uint32_t vid;
2129
7267c094 2130 vdi_inuse = g_malloc(max);
33b1db1c 2131
25af257d 2132 fd = connect_to_sdog(s);
33b1db1c 2133 if (fd < 0) {
cb595887 2134 ret = fd;
33b1db1c
MK
2135 goto out;
2136 }
2137
2138 rlen = max;
2139 wlen = 0;
2140
2141 memset(&req, 0, sizeof(req));
2142
2143 req.opcode = SD_OP_READ_VDIS;
2144 req.data_length = max;
2145
2146 ret = do_req(fd, (SheepdogReq *)&req, vdi_inuse, &wlen, &rlen);
2147
2148 closesocket(fd);
2149 if (ret) {
2150 goto out;
2151 }
2152
7267c094 2153 sn_tab = g_malloc0(nr * sizeof(*sn_tab));
33b1db1c
MK
2154
2155 /* calculate a vdi id with hash function */
2156 hval = fnv_64a_buf(s->name, strlen(s->name), FNV1A_64_INIT);
2157 start_nr = hval & (SD_NR_VDIS - 1);
2158
25af257d 2159 fd = connect_to_sdog(s);
33b1db1c 2160 if (fd < 0) {
cb595887 2161 ret = fd;
33b1db1c
MK
2162 goto out;
2163 }
2164
2165 for (vid = start_nr; found < nr; vid = (vid + 1) % SD_NR_VDIS) {
2166 if (!test_bit(vid, vdi_inuse)) {
2167 break;
2168 }
2169
2170 /* we don't need to read entire object */
2171 ret = read_object(fd, (char *)&inode, vid_to_vdi_oid(vid),
47622c44 2172 0, SD_INODE_SIZE - sizeof(inode.data_vdi_id), 0,
0e7106d8 2173 s->cache_flags);
33b1db1c
MK
2174
2175 if (ret) {
2176 continue;
2177 }
2178
2179 if (!strcmp(inode.name, s->name) && is_snapshot(&inode)) {
2180 sn_tab[found].date_sec = inode.snap_ctime >> 32;
2181 sn_tab[found].date_nsec = inode.snap_ctime & 0xffffffff;
2182 sn_tab[found].vm_state_size = inode.vm_state_size;
2183 sn_tab[found].vm_clock_nsec = inode.vm_clock_nsec;
2184
2185 snprintf(sn_tab[found].id_str, sizeof(sn_tab[found].id_str), "%u",
2186 inode.snap_id);
3178e275
JM
2187 pstrcpy(sn_tab[found].name,
2188 MIN(sizeof(sn_tab[found].name), sizeof(inode.tag)),
2189 inode.tag);
33b1db1c
MK
2190 found++;
2191 }
2192 }
2193
2194 closesocket(fd);
2195out:
2196 *psn_tab = sn_tab;
2197
7267c094 2198 g_free(vdi_inuse);
33b1db1c 2199
cb595887
MK
2200 if (ret < 0) {
2201 return ret;
2202 }
2203
33b1db1c
MK
2204 return found;
2205}
2206
2207static int do_load_save_vmstate(BDRVSheepdogState *s, uint8_t *data,
2208 int64_t pos, int size, int load)
2209{
2f536801
MK
2210 bool create;
2211 int fd, ret = 0, remaining = size;
33b1db1c
MK
2212 unsigned int data_len;
2213 uint64_t vmstate_oid;
33b1db1c 2214 uint64_t offset;
cede621f
LY
2215 uint32_t vdi_index;
2216 uint32_t vdi_id = load ? s->inode.parent_vdi_id : s->inode.vdi_id;
33b1db1c 2217
25af257d 2218 fd = connect_to_sdog(s);
33b1db1c 2219 if (fd < 0) {
cb595887 2220 return fd;
33b1db1c
MK
2221 }
2222
6f3c714e 2223 while (remaining) {
33b1db1c
MK
2224 vdi_index = pos / SD_DATA_OBJ_SIZE;
2225 offset = pos % SD_DATA_OBJ_SIZE;
2226
1f7a48de 2227 data_len = MIN(remaining, SD_DATA_OBJ_SIZE - offset);
33b1db1c 2228
cede621f 2229 vmstate_oid = vid_to_vmstate_oid(vdi_id, vdi_index);
33b1db1c
MK
2230
2231 create = (offset == 0);
2232 if (load) {
2233 ret = read_object(fd, (char *)data, vmstate_oid,
47622c44 2234 s->inode.nr_copies, data_len, offset,
0e7106d8 2235 s->cache_flags);
33b1db1c
MK
2236 } else {
2237 ret = write_object(fd, (char *)data, vmstate_oid,
47622c44 2238 s->inode.nr_copies, data_len, offset, create,
0e7106d8 2239 s->cache_flags);
33b1db1c
MK
2240 }
2241
2242 if (ret < 0) {
6daf194d 2243 error_report("failed to save vmstate %s", strerror(errno));
33b1db1c
MK
2244 goto cleanup;
2245 }
2246
2247 pos += data_len;
1f7a48de 2248 data += data_len;
6f3c714e 2249 remaining -= data_len;
33b1db1c 2250 }
6f3c714e 2251 ret = size;
33b1db1c
MK
2252cleanup:
2253 closesocket(fd);
2254 return ret;
2255}
2256
cf8074b3
KW
2257static int sd_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
2258 int64_t pos)
33b1db1c
MK
2259{
2260 BDRVSheepdogState *s = bs->opaque;
cf8074b3
KW
2261 void *buf;
2262 int ret;
33b1db1c 2263
cf8074b3
KW
2264 buf = qemu_blockalign(bs, qiov->size);
2265 qemu_iovec_to_buf(qiov, 0, buf, qiov->size);
2266 ret = do_load_save_vmstate(s, (uint8_t *) buf, pos, qiov->size, 0);
2267 qemu_vfree(buf);
2268
2269 return ret;
33b1db1c
MK
2270}
2271
2272static int sd_load_vmstate(BlockDriverState *bs, uint8_t *data,
2273 int64_t pos, int size)
2274{
2275 BDRVSheepdogState *s = bs->opaque;
2276
2277 return do_load_save_vmstate(s, data, pos, size, 1);
2278}
2279
2280
cac8f4a6
LY
2281static coroutine_fn int sd_co_discard(BlockDriverState *bs, int64_t sector_num,
2282 int nb_sectors)
2283{
2284 SheepdogAIOCB *acb;
2285 QEMUIOVector dummy;
2286 BDRVSheepdogState *s = bs->opaque;
2287 int ret;
2288
2289 if (!s->discard_supported) {
2290 return 0;
2291 }
2292
2293 acb = sd_aio_setup(bs, &dummy, sector_num, nb_sectors);
2294 acb->aiocb_type = AIOCB_DISCARD_OBJ;
2295 acb->aio_done_func = sd_finish_aiocb;
2296
2297 ret = sd_co_rw_vector(acb);
2298 if (ret <= 0) {
2299 qemu_aio_release(acb);
2300 return ret;
2301 }
2302
2303 qemu_coroutine_yield();
2304
2305 return acb->ret;
2306}
2307
b6b8a333
PB
2308static coroutine_fn int64_t
2309sd_co_get_block_status(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
2310 int *pnum)
8d71c631
LY
2311{
2312 BDRVSheepdogState *s = bs->opaque;
2313 SheepdogInode *inode = &s->inode;
2314 unsigned long start = sector_num * BDRV_SECTOR_SIZE / SD_DATA_OBJ_SIZE,
2315 end = DIV_ROUND_UP((sector_num + nb_sectors) *
2316 BDRV_SECTOR_SIZE, SD_DATA_OBJ_SIZE);
2317 unsigned long idx;
4bc74be9 2318 int64_t ret = BDRV_BLOCK_DATA;
8d71c631
LY
2319
2320 for (idx = start; idx < end; idx++) {
2321 if (inode->data_vdi_id[idx] == 0) {
2322 break;
2323 }
2324 }
2325 if (idx == start) {
2326 /* Get the longest length of unallocated sectors */
2327 ret = 0;
2328 for (idx = start + 1; idx < end; idx++) {
2329 if (inode->data_vdi_id[idx] != 0) {
2330 break;
2331 }
2332 }
2333 }
2334
2335 *pnum = (idx - start) * SD_DATA_OBJ_SIZE / BDRV_SECTOR_SIZE;
2336 if (*pnum > nb_sectors) {
2337 *pnum = nb_sectors;
2338 }
2339 return ret;
2340}
2341
33b1db1c
MK
2342static QEMUOptionParameter sd_create_options[] = {
2343 {
2344 .name = BLOCK_OPT_SIZE,
2345 .type = OPT_SIZE,
2346 .help = "Virtual disk size"
2347 },
2348 {
2349 .name = BLOCK_OPT_BACKING_FILE,
2350 .type = OPT_STRING,
2351 .help = "File name of a base image"
2352 },
a8e0fdd7
MK
2353 {
2354 .name = BLOCK_OPT_PREALLOC,
2355 .type = OPT_STRING,
2356 .help = "Preallocation mode (allowed values: off, full)"
2357 },
33b1db1c
MK
2358 { NULL }
2359};
2360
5d6768e3 2361static BlockDriver bdrv_sheepdog = {
33b1db1c
MK
2362 .format_name = "sheepdog",
2363 .protocol_name = "sheepdog",
2364 .instance_size = sizeof(BDRVSheepdogState),
030be321 2365 .bdrv_needs_filename = true,
33b1db1c
MK
2366 .bdrv_file_open = sd_open,
2367 .bdrv_close = sd_close,
2368 .bdrv_create = sd_create,
e4f5c1bf 2369 .bdrv_has_zero_init = bdrv_has_zero_init_1,
33b1db1c
MK
2370 .bdrv_getlength = sd_getlength,
2371 .bdrv_truncate = sd_truncate,
2372
2df46246
MK
2373 .bdrv_co_readv = sd_co_readv,
2374 .bdrv_co_writev = sd_co_writev,
47622c44 2375 .bdrv_co_flush_to_disk = sd_co_flush_to_disk,
cac8f4a6 2376 .bdrv_co_discard = sd_co_discard,
b6b8a333 2377 .bdrv_co_get_block_status = sd_co_get_block_status,
33b1db1c
MK
2378
2379 .bdrv_snapshot_create = sd_snapshot_create,
2380 .bdrv_snapshot_goto = sd_snapshot_goto,
2381 .bdrv_snapshot_delete = sd_snapshot_delete,
2382 .bdrv_snapshot_list = sd_snapshot_list,
2383
2384 .bdrv_save_vmstate = sd_save_vmstate,
2385 .bdrv_load_vmstate = sd_load_vmstate,
2386
2387 .create_options = sd_create_options,
2388};
2389
5d6768e3
MK
2390static BlockDriver bdrv_sheepdog_tcp = {
2391 .format_name = "sheepdog",
2392 .protocol_name = "sheepdog+tcp",
2393 .instance_size = sizeof(BDRVSheepdogState),
030be321 2394 .bdrv_needs_filename = true,
5d6768e3
MK
2395 .bdrv_file_open = sd_open,
2396 .bdrv_close = sd_close,
2397 .bdrv_create = sd_create,
e4f5c1bf 2398 .bdrv_has_zero_init = bdrv_has_zero_init_1,
5d6768e3
MK
2399 .bdrv_getlength = sd_getlength,
2400 .bdrv_truncate = sd_truncate,
2401
2402 .bdrv_co_readv = sd_co_readv,
2403 .bdrv_co_writev = sd_co_writev,
2404 .bdrv_co_flush_to_disk = sd_co_flush_to_disk,
cac8f4a6 2405 .bdrv_co_discard = sd_co_discard,
b6b8a333 2406 .bdrv_co_get_block_status = sd_co_get_block_status,
5d6768e3
MK
2407
2408 .bdrv_snapshot_create = sd_snapshot_create,
2409 .bdrv_snapshot_goto = sd_snapshot_goto,
2410 .bdrv_snapshot_delete = sd_snapshot_delete,
2411 .bdrv_snapshot_list = sd_snapshot_list,
2412
2413 .bdrv_save_vmstate = sd_save_vmstate,
2414 .bdrv_load_vmstate = sd_load_vmstate,
2415
2416 .create_options = sd_create_options,
2417};
2418
1b8bbb46
MK
2419static BlockDriver bdrv_sheepdog_unix = {
2420 .format_name = "sheepdog",
2421 .protocol_name = "sheepdog+unix",
2422 .instance_size = sizeof(BDRVSheepdogState),
030be321 2423 .bdrv_needs_filename = true,
1b8bbb46
MK
2424 .bdrv_file_open = sd_open,
2425 .bdrv_close = sd_close,
2426 .bdrv_create = sd_create,
3ac21627 2427 .bdrv_has_zero_init = bdrv_has_zero_init_1,
1b8bbb46
MK
2428 .bdrv_getlength = sd_getlength,
2429 .bdrv_truncate = sd_truncate,
2430
2431 .bdrv_co_readv = sd_co_readv,
2432 .bdrv_co_writev = sd_co_writev,
2433 .bdrv_co_flush_to_disk = sd_co_flush_to_disk,
cac8f4a6 2434 .bdrv_co_discard = sd_co_discard,
b6b8a333 2435 .bdrv_co_get_block_status = sd_co_get_block_status,
1b8bbb46
MK
2436
2437 .bdrv_snapshot_create = sd_snapshot_create,
2438 .bdrv_snapshot_goto = sd_snapshot_goto,
2439 .bdrv_snapshot_delete = sd_snapshot_delete,
2440 .bdrv_snapshot_list = sd_snapshot_list,
2441
2442 .bdrv_save_vmstate = sd_save_vmstate,
2443 .bdrv_load_vmstate = sd_load_vmstate,
2444
2445 .create_options = sd_create_options,
2446};
2447
33b1db1c
MK
2448static void bdrv_sheepdog_init(void)
2449{
2450 bdrv_register(&bdrv_sheepdog);
5d6768e3 2451 bdrv_register(&bdrv_sheepdog_tcp);
1b8bbb46 2452 bdrv_register(&bdrv_sheepdog_unix);
33b1db1c
MK
2453}
2454block_init(bdrv_sheepdog_init);