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